Created
March 26, 2020 16:42
-
-
Save debsahu/d6675a768df71124d91e96a32beb5837 to your computer and use it in GitHub Desktop.
Modify rpi-rf_receive.py to send data to MQTT server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import paho.mqtt.client as mqtt | |
import argparse | |
import signal | |
import sys | |
import time | |
import logging | |
import json | |
from rpi_rf import RFDevice | |
rfdevice = None | |
client = mqtt.Client() | |
client.username_pw_set(username="mqtt_user", password="mqtt_password") | |
client.connect("mqtt_host_ip", 1883, 60) | |
client.loop_start() | |
# pylint: disable=unused-argument | |
def exithandler(signal, frame): | |
print("interrupted!") | |
client.loop_stop() | |
client.disconnect() | |
rfdevice.cleanup() | |
sys.exit(0) | |
logging.basicConfig(level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S', | |
format='%(asctime)-15s - [%(levelname)s] %(module)s: %(message)s', ) | |
parser = argparse.ArgumentParser(description='Receives a decimal code via a 433/315MHz GPIO device') | |
parser.add_argument('-g', dest='gpio', type=int, default=27, | |
help="GPIO pin (Default: 27)") | |
args = parser.parse_args() | |
signal.signal(signal.SIGINT, exithandler) | |
rfdevice = RFDevice(args.gpio) | |
rfdevice.enable_rx() | |
timestamp = None | |
logging.info("Listening for codes on GPIO " + str(args.gpio)) | |
while True: | |
if rfdevice.rx_code_timestamp != timestamp: | |
timestamp = rfdevice.rx_code_timestamp | |
logging.info(str(rfdevice.rx_code) +" [pulselength " + str(rfdevice.rx_pulselength) + ", protocol " + str(rfdevice.rx_proto) + "]") | |
data_send = {} | |
data_send["time"] = timestamp | |
data_send["code"] = rfdevice.rx_code | |
data_send["length"] = rfdevice.rx_pulselength | |
data_send["protocol"] = rfdevice.rx_proto | |
try: | |
print("Sending >> " + json.dumps(data_send)); | |
client.publish("home/rf433/data", json.dumps(data_send), 0, True); | |
except Exception as e: | |
print(e) | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment