0

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

3
  • Could you please indent the code parts of the question correctly, highlighting the code, thus making it easier to read. Commented Aug 14, 2019 at 2:31
  • Possible duplicate of how to import printed C console to node red Commented Aug 14, 2019 at 6:40
  • Also your function node javascript doesn't do anything, there is no str to run the substring() function on Commented Aug 14, 2019 at 21:21

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

I used the Daemon node as suggested but I can't seem to get the node to work. In the Daemon node under Command i put: /usr/bin/sudo. Under Arguments i put: /home/pi/Desktop/vibe2.py. I also tried under Command: /usr/bin/sudo and Arguments: /usr/bin/python /home/pi/Desktop/vibe2.py.
Have you checked both the node red logs and attached debug nodes to the other outputs of the node to get the errors and return value?
You probably also need to make sure python is not buffering it's output to stdout.
@pi_hobbyist try adding flush=True to your print() calls. e.g. print(sensor, flush=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.