pi_hobbyist
pi_hobbyist

Reputation: 11

Running a Python script with exec node then parsing to javascript with function node in Node red

I have a python script that controls a vibration sensor connected to my raspberry pi. I have that python script in the exec node. I need to parse the python code into javascript using the function node. (I'm doing this in node-red on the raspberry pi).

The JavaScript code I'm running does not return the value I need from the python code. I need the variable "sensor" to return in the javascript code.

python code:

import time 
import RPi.GPIO as GPIO

vibe = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(vibe, GPIO.IN)

def callback(Vibe):
    if GPIO.input(vibe):
        sensor = "vibration"
        print(sensor)

    else:
        print("error")

GPIO.add_event_detect(vibe, GPIO.BOTH, bouncetime=300)
GPIO.add_event_callback(vibe, callback)

while True:
    time.sleep(1)

GPIO.cleanup()

javascript code:

var vibrationSensor = msg.payload;

var sensor = str.substr(1,15);

msg.payload = {

    "vibration": sensor,

};

return msg;

What it looks like in node-red:

img

Upvotes: 0

Views: 1398

Answers (1)

hardillb
hardillb

Reputation: 59608

Since the python code will never exit, you need to use the Daemon node rather than the exec node

Upvotes: 1

Related Questions