From 684cd1ad2eee0cdab4bc1688cb98d2bb29836c8c Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Mon, 8 Aug 2016 05:57:35 -0600 Subject: [PATCH] Add initial code of simple python logger Capture RF24Network/RF24Mesh data from the new UDP interface and log info to MQTT broker --- examples/addons/mqttLogger.py | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/addons/mqttLogger.py diff --git a/examples/addons/mqttLogger.py b/examples/addons/mqttLogger.py new file mode 100644 index 0000000..afb4223 --- /dev/null +++ b/examples/addons/mqttLogger.py @@ -0,0 +1,43 @@ +# Note: This code requires paho-mqtt +# sudo pip-install paho-mqtt + +# Simple MQTT data logger, listens for UDP output from RF24Gateway & logs to an MQTT topic +# For use with RF24Mesh nodes that are not able to communicate directly via TCP/IP etc +# See RF24Gateway examples: +# RF24NetworkFrame frame = RF24NetworkFrame(header,buf,size); +# gw.sendUDP(mesh.getNodeID(header.from_node),frame); +# + +# -*- coding: utf-8 -*- +import paho.mqtt.client as mqtt +import socket +import sys + +reload(sys) +sys.setdefaultencoding('utf8') + +### Setup the MQTT host IP & topic to publish to +mqttHost = "10.10.2.2" +topic = 'data/RF24' + +### Bind to UDP port 32001 (default port of RF24Gateway UDP output) +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +server_address = ('localhost', 32001) +print >>sys.stderr, 'Waiting for UDP message on %s port %s\n' % server_address +sock.bind(server_address) + +while True: + + data, address = sock.recvfrom(2048) + + print >>sys.stderr, 'received %s bytes from %s' % (len(data), address) + print >>sys.stderr, data + + if data: + ## Just log the entire chunk of data to MQTT for now + ## Todo: Sort, Display and Analyze the data + mqttc = mqtt.Client() + mqttc.connect(mqttHost, 1883) + data = unicode(data, errors='replace') + mqttc.publish(topic, data) + mqttc.loop(2) \ No newline at end of file