Nile Ellis
Nile Ellis

Reputation: 13

How to pass CTRL+C to Raspberry Pi terminal from Node-RED?

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

Upvotes: 0

Views: 1622

Answers (2)

JanVdA
JanVdA

Reputation: 342

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

Upvotes: 0

hardillb
hardillb

Reputation: 59791

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.

Upvotes: 1

Related Questions