Hard to test without the required hardware.
Function throws at least some error's so there's is happening something
Strip your code and see where it's going wrong.
If you installed node-red via the script, it gave you an option to install Pi specific nodes, one of which are the GPIO nodes if I am not mistaken, you can install them separately too. No need for external python scripts if you can do it all inside node-red.
i will try but i don't understand why if i run the script outside node-red i get the needed value, inside node-red i'm not able to get the needed value in the msg objects
I remember that others have had problems using that node, as Dave suggests, you might be better using an exec node to call your script. If you do that make sure you put the full path to the script in the exec node.
global pippo
def test():
global pippo
pippo = 'test'
return pippo
while True:
test()
msg['payload'] = {'pippo':pippo}
return msg
but i don't understand why in node-red i have the msg one time and not repeated.
But my big problem is this:
from pigpio_encoder.rotary import Rotary
global my_counter
my_counter = 0
def sw_short():
pass
def up_callback(counter):
global my_counter
if counter%40==0:
my_counter+=1
print(my_counter)
return my_counter
def down_callback(counter):
global my_counter
if counter%40==0:
my_counter-=1
print(my_counter)
return my_counter
my_rotary = Rotary(clk_gpio=13, dt_gpio=12,sw_gpio = 17)
my_rotary.setup_rotary( min=-10000,
max=10000,
debounce = 1,
up_callback=up_callback,
down_callback=down_callback,)
#rotary_callback=rotary_callback,
my_rotary.setup_switch(sw_short_callback=sw_short)
msg['payload'] = {'my_counter':my_counter}
return msg
Always return
it return my_counter = 0 because he get the value from the beginning but not when the value change
can you tell me where is my mistake
Thanks
I am not at home in python, but aren't variables global by default if they are defined outside a function ? ie. can you remove all the "global" definitions ?
from pigpio_encoder.rotary import Rotary
global my_counter
my_counter = 0
def sw_short():
pass
def up_callback(counter):
global my_counter
if counter%40==0:
my_counter+=1
print(my_counter)
msg['payload'] = {'my_counter':my_counter}
return msg
def down_callback(counter):
global my_counter
if counter%40==0:
my_counter-=1
print(my_counter)
msg['payload'] = {'my_counter':my_counter}
return msg
my_rotary = Rotary(clk_gpio=13, dt_gpio=12,sw_gpio = 17)
my_rotary.setup_rotary( min=-10000,
max=10000,
debounce = 1,
up_callback=up_callback,
down_callback=down_callback,)
#rotary_callback=rotary_callback,
my_rotary.setup_switch(sw_short_callback=sw_short)
msg['payload'] = {'my_counter':my_counter}
return msg
i get only the first msg object with my_counter = 0
then when i move the encoder in the node-red shell i get all the print message :
but i get no return object.
dear bakman2 in node-red if i never set as global the my_counter variable i get:
NameError: name 'my_counter' is not defined
create an instance of Rotary and set a few properties of it
set your msg['payload'] to a value
return this msg
After the return command the script is terminated because there are no more commands. Therefore you get once the value of msg.
The two function (the callback functions for the Roraty object) will never have been called, because the script is finished before the Rotary object can fire any event.