Skip to content

Commit

Permalink
Add initial code of simple python logger
Browse files Browse the repository at this point in the history
Capture RF24Network/RF24Mesh data from the new UDP interface and log
info to MQTT broker
  • Loading branch information
TMRh20 committed Aug 8, 2016
1 parent 32c70bf commit 684cd1a
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/addons/mqttLogger.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 684cd1a

Please # to comment.