Problems programing switch sensor

60 views
Skip to first unread message

Ian

unread,
Nov 11, 2020, 5:03:16 AM11/11/20
to PrivateEyePi
Hi, 
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:
Fri Oct 30 20:17:40 2020 80 AWAKE----
Fri Oct 30 20:17:40 2020 80 BUTTONON-
Fri Oct 30 20:17:40 2020 80 BUTTONON-
Fri Oct 30 20:17:40 2020 80 BUTTONON-
Fri Oct 30 20:17:42 2020 80 AWAKE----
Fri Oct 30 20:17:42 2020 80 BUTTONOFF
Fri Oct 30 20:17:42 2020 80 BUTTONOFF
Fri Oct 30 20:17:42 2020 80 SLEEPING-
Fri Oct 30 20:17:45 2020 80 AWAKE----

The only other documentation I can find is: 
https://jemrf.github.io/RF-Documentation/python_rflib.htm
So I've been trying to work through that document, but I'm not having any luck.

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.

Cheers,
Ian

Gadjet Nut

unread,
Nov 11, 2020, 10:57:29 AM11/11/20
to Ian, PrivateEyePi
Hi Ian,

There are a number of examples (like rf2blynk.py, rf2mqtt.py, rf2adafuitio.py) in the rf_tools repository you could modify. But we have added another one called rf2X.py that could help you. I've added a comment "Add your code here" for you to place your code or insert a call to your code.

Start by using rf2X.py without changes and test that it works (it will print messages to the screen) and then add your modifications. 

Here is a link to rf2X.py, or refresh your rf_tools directory and it will pull it down.


Hope that helps.

Regards,

Jonathan




--
You received this message because you are subscribed to the Google Groups "PrivateEyePi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to privateeyepi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/privateeyepi/beb85331-af1d-4789-9061-2964f155a078n%40googlegroups.com.

Ian M

unread,
Nov 12, 2020, 5:51:56 AM11/12/20
to Gadjet Nut, PrivateEyePi
Hi Jonathon,
Thanks so much for replying - that works very nicely!
I still have a few challenges ahead to get all my existing code merged into that (using 2 switches and also responding to on screen button presses), but hopefully can figure that out myself!

Cheers,
Ian
Reply all
Reply to author
Forward
0 new messages