Combining Measurements with 6LoWPAN for an MQTT Broker

To test the sensor and gain experience with it, use the following code. It utilizes the MQTT protocol to transfer data and should be implemented on the Raspberry Pi connected to the sensor. Once the MQTT broker is set up, you can use the publisher and subscriber parts to expand the network as needed. If you haven't already, download the 6LoWPAN project implementation code from the repository using the command provided in the first module IoT-1.


git clone https://github.com/GT-ARC/CC-IoT/

Note that the command mentioned above will create a local copy of all codes required for this batch of training modules (from IOT-1 to IOT-10). If you have already done this earlier, you can skip this step. For the current training module, navigate to the directory "CC+IoT/9-10/" in your local repository.

 
import Adafruit_DHT
import time
import paho.mqtt.client as mqtt

HOST = “fe80::f87c:f7ed:c0f7:1395%lowpan0” #ADD real host IP here

PORT = 1883
topic = "/test/temperature"

client = mqtt.Client()

client.connect(HOST, PORT)

# use the library to talk and interact with the DHT22 sensor.
DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if humidity is not None and temperature is not None:
        print("Temperature={0:0.1f}*C   Humidity={1:0.1f}%".format(temperature, humidity))
    else:
        print("Failed to retrieve data from humidity sensor")

    client.publish(topic, "Temperature={0:0.1f}*C   Humidity={1:0.1f}%".format(temperature, humidity))

    time.sleep(2)

MQTT Broker is ready, but publisher and subscriber parts should be implemented to retrieve data on the receiver Raspberry. Publisher aims to measure the temperature value and send the subscriber. MQTT Publisher and Subscriber are implemented by making use of python paho-mqtt library.


Publisher.py (to be implemented on the sensor Raspberry)



#!/usr/bin/env python
import time

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
   print("Connected with result code"+ str(rc))

#client =  mqtt.Client(client_id="", clean_session=True, userdata=None, protocol=mqtt.MQTTv31)

client = mqtt.Client("zzzzzzzz", protocol=mqtt.MQTTv31)
client.on_connect = on_connect

client.connect("fe80::f87c:f7ed:c0f7:1395%lowpan0",1883,60)
client.loop_start()

while True:
    time.sleep(3)
    client.publish("test/temperature","test")

Subscriber.py (to be implemented on the receiver Raspberry)



#!/usr/bin/env python
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
     print("Connected with result code " + str(rc))
     client.subscribe("test/#")
def on_message(client, userdata, msg):
     print(msg.topic + " " + str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect('fe80::f87c:f7ed:c0f7:1395%lowpan0', 1883, 60)

client.loop_forever()

Installing MQTT.fx

To view the results, first download the MQTT.fx tool from [http://mqttfx.org/] and run it on the receiver Raspberry Pi. Once it is running, the next step is to set up the MQTT broker configuration. In this example, the MQTT broker is running on the Raspberry Pi, so provide the IP address of the Raspberry Pi on which the MQTT broker is installed.