Hoping someone can point me in the right direction with using the JemRF switch sensors in a python script. I want to use hem to sense when doors are opened or closed and make calls to some functions I've written whenever those events occur.
The only example code on the jemrf website I could find for the wireless switches was the the serial_mon.py and rflib.py scripts in the git repo.
That appears to work correctly, it gives the following response when I close & open the switch with the magnet:
I've written this bit of code to print messages when data is received from the switches:
#!/usr/bin/python
#Copied from serial_mon.py:
import sys
from threading import Thread
from rflib import rf2serial, fetch_messages, request_reply, getMessage
import rflib
from time import sleep
import time
#Taken from
https://jemrf.github.io/RF-Documentation/python_rflib.html :
def main():
#Opens the serial port and initializes variables and queues
rflib.init()
#Call the rf2serial() function
a=Thread(target=rf2serial, args=())
a.start()
message = getMessage()
if message.sensordata <> "":
print(message.devID)
print(message.type)
print(message.data)
print(message.description)
print(message.sensordata)
When I run that, I get the following error:
./test.py
Traceback (most recent call last):
File "./test.py", line 19, in <module>
message = getMessage()
File "/home/pi/Airlock/rf_tools/rflib.py", line 258, in getMessage
return(getMessage_class())
File "/home/pi/Airlock/rf_tools/rflib.py", line 172, in __init__
fetch_messages(1);
File "/home/pi/Airlock/rf_tools/rflib.py", line 82, in fetch_messages
while rf_event.is_set() and not event.is_set():
NameError: global name 'rf_event' is not defined
I have no idea how to fix that error - perhaps it's my lack of python knowledge or I'm just going about this the wrong way, but I can't find any other code examples to point me in the right direction.