0

This is the first time I program a Raspberry PI and the first time I'm using Python, JavaScript or Node-Red.

I'm trying to lift a gate by operating a servomotor in Node-RED. Basically, what I'm doing is take a picture of a license plate with my pi camera, process it, send it to OpenALPR, and get the license plate back as a msg.payload.

Now, what I want to do is activate the servomotor if the msg.paylaod.results[0].plate matches the plate numbers I need. I'm trying to create a global variable to use in the subflow that activates the servomotor. This is the code:

if(msg.payload.results[0].plate=="SB06KBE" || 
msg.payload.results[0].plate=="AR18AUG")
{
    console.log(msg.payload.results[0].plate);
    context.global.cond=1;
}
else console.log("not recognized");

This node enters the motor subflow:

motor

variable node:

var mycond=context.global.cond;
return mycond;

motor pwm node:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7,GPIO.OUT)
p=GPIO.PWM(7,50)
p.start(7)

if mycond==1:
  try:
    while True:
      p.ChangeDutyCycle(7)
      time.sleep(1)
      p.ChangeDutyCycle(3)
      time.sleep(10)
  except KeyboardInterrupt:
      p.stop()
      GPIO.cleanup()

For some reason, the motor won't start running.

I tried using the switch node and removed the variable node in the subflow and the if condition in the motor node but the switch would not go to any output. This is the switch node I tried using:

switch

Also, if I remove the variable node in the subflow the motor would run even if the license plate is not recognized.

LE: I also tried to connect the first two outputs of the switch node to two JavaScript nodes that would print a message but they never run even though the conditions are met.

Thanks in advance!

Iulian Leon
  • 21
  • 1
  • 8
  • What makes you think the Node-RED context variables will be availble in you python code that is driving the motor? Also all nodes should return a message object not a raw value as your function node seams to. – hardillb May 20 '17 at 13:19
  • I thought they would be available. That means that the only way to set conditions to the motor is through the switch node, but I can't get it to work. – Iulian Leon May 20 '17 at 13:24
  • Also the loop in your python code will never exit – hardillb May 20 '17 at 13:30
  • I intend to use an if condition instead of the while. I mainly use that python code for testing until I can get it to run when the license plate is recognized. – Iulian Leon May 20 '17 at 13:32
  • I searched the web a bit, and stumbled upon a question you answered years ago. It appears that I should use the exec node in order to execute the python function that controls the servomotor. Is that true? If so, could you tell me how exactly do I use the exec node and how could I set conditions for it? Thanks in advance! – Iulian Leon May 20 '17 at 14:32

0 Answers0