0

I am creating a tutorial for visiting high school students.

Using Node-RED I have a Raspberry Pi that runs a script written in python. I am looking to end the python script from node-RED by passing a keyboardInterrupt (CTRL+C) so that the script ends gracefully and the GPIO is cleaned up for the next run.

I can run the script just fine by using the EXEC block in node-RED and passing "sudo python Documents/python/blinker.py" to the terminal. Problem is because I am not working directly in the terminal I cannot just use CTRL+C to exit the while loop. Is there a way to pass CTRL+C to the terminal from node-RED?

I have attached the python code for those interested:

import RPi.GPIO as GPIO
import time

pwmPin = 18
ledPin = 23
butPin = 17

dc = 95

GPIO.setmode(GPIO.BCM)
GPIO.setup(ledPin, GPIO.OUT)
GPIO.setup(pwmPin, GPIO.OUT)
pwm = GPIO.PWM(pwmPin, 50)
GPIO.setup(butPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.output(ledPin, GPIO.LOW)
pwm.start(dc)

print("Press CTRL+C to exit")
try:
        while 1:
                if GPIO.input(butPin):
                        pwm.ChangeDutyCycle(dc)
                        GPIO.output(ledPin, GPIO.LOW)
                else:
                        pwm.ChangeDutyCycle(100-dc)
                        GPIO.output(ledPin, GPIO.HIGH)
                        time.sleep(0.075)
                        GPIO.output(ledPin, GPIO.LOW)
                        time.sleep(0.075)
except keyboardInterrupt:
        pwm.stop()
        GPIO.cleanup()

I have tried using the kill and killall commands and that terminates the application but the GPIO.cleanup() isn't run then and any outputs that were on will remain on.

I have also tried passing "echo $'\cc' | ./blinker.py" but get a premissions error.

/bin/sh: 1: Documents/python/blinker.py: Permission denied echo: write error: Broken pipe

Any help is appreciate

Nile Ellis
  • 13
  • 7
  • Any reason why you are using python to interact with the GPIO pins rather than Node-RED's built in GPIO support? – hardillb May 16 '18 at 18:28
  • Its a tutorial for highschool students so we plan to show them how to run the script they will have made earlier in the day and then show them how to do what you have suggested as well – Nile Ellis May 16 '18 at 19:02

2 Answers2

1

For a long running script you should probably be using the daemon node instead of the exec node.

But also both these nodes will try kill any running instances of the script when a new version of the flow is deployed (causing the nodes to be rebuilt).

You can also explicitly kill the daemon nodes by sending it a msg.kill with the name of the signal to send. From the daemon node docs:

Setting msg.kill to a signal name (e.g. SIGINT, SIGHUP) will stop the process - but if the restart flag is set it will then auto restart.

The fact you are running the script with sudo is why you can't kill the script directly because you trying to kill a process owned by root. if you send the signal to the instance of sudo that's running the process is should kill the child process.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • I agree that the daemon node is the way to go after reading about it but I am having a problem running the python script. I am using "python Documents/python/blinker.py" as the command but it just returns the error "Command not found" despite working with the exec node. – Nile Ellis May 16 '18 at 19:24
  • Try the full path to python - `/usr/bin/python` – hardillb May 16 '18 at 20:20
0

Instead of writing the GPIO logic in a python script, you could write this logic completely in node-red using the raspberry pi GPIO nodes (I think these nodes are installed by default when installing node-red on a raspberry pi).

I admit that as the sleep times in the while loop are very short, that I don't know if node-red on a raspberry pi is able to handle that many updates properly. So it might be good to check with the unix top command that the node-red process is not using close to 100% CPU.

Regarding the configuration of node-red to access the GPIO pins see also section Accessing GPIO on page https://nodered.org/docs/hardware/raspberrypi

JanVdA
  • 338
  • 3
  • 15
  • Thanks for the reply. I was hoping to be able to do both but for now I have done exactly what you described. – Nile Ellis May 17 '18 at 19:28