This MQTT broker bridges the data sent from Arduino sensor in the field to the OpenHAB’s MQTT server.
Arduino sensors in the field can send back data in any format to pcDuino. We can use Arduino-style programming on pcDuino to receive the data, and send the data to TCP port 10000. This MQTT broker will listen to port 10000 and converts the data to MQTT protocol. Then send that data to the MQTT server in OpenHAB.
We use Paho MQTT here. https://pypi.python.org/pypi/paho-mqtt.
To install it, type the following:
$sudo apt-get update $sudo apt-get install mosquitto mosquitto-clients python-mosquitto $sudo apt-get install python-pip $sudo pip install paho-mqtt
The mqtt publish python script is as below:
import socket import sys import paho.mqtt.publish as publish # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port server_address = ('localhost', 10000) print >>sys.stderr, 'starting up on %s port %s' % server_address sock.bind(server_address) # Listen for incoming connections sock.listen(1) while True: # Wait for a connection print >>sys.stderr, 'waiting for a connection' connection, client_address = sock.accept() try: print >>sys.stderr, 'connection from', client_address # Receive the data in small chunks and retransmit it while True: data = connection.recv(32) if len(data)==0: connection.close() break #This packet is from garage sensor if data[0:4]=="4032": sendata= data[6:9]; print >>sys.stderr, 'received "%s"' % sendata if len(sendata)!=0 : if float(sendata.strip())<500: print >>sys.stderr, 'publish 25' publish.single("4032", "25",hostname="localhost") else: print >>sys.stderr, 'publish 05' publish.single("4032", "05",hostname="localhost") #This packet is from front door sensor if data[0:4]=="3032": sendata= data[6:9]; print >>sys.stderr, 'received "%s"' % sendata if len(sendata)!=0 : if float(sendata.strip())<1: print >>sys.stderr, 'publish 0' publish.single("3032", "0",hostname="localhost") else: print >>sys.stderr, 'publish 1' publish.single("3032", "1",hostname="localhost") except KeyboardInterrupt: # Clean up the connection connection.close() sys.exit(1) #publish.single("4032", "05",hostname="192.168.1.39")
To run it execute:
$python mqtt_publish.py
The code for Arduio-style receiver that is used to receive the data sent from the Arduino sensor in shield can be found at github: https://github.com/pcduino/c_environment/blob/master/sample/NRF24L01_RX.c
We can clone the project c_enviroment and build the whole project. The executable is located at /c_enviroment/output. The wiring instructions can be found at: http://learn.linksprite.com/pcduino/arduino-ish-program/spi/nrf24l01-for-pcduino/
The Arduino code for the remote node used in garage example can be be downloaded from here ( garage).
Leave a Reply
You must be logged in to post a comment.