Robert Cudmore    archive    tags    search    software


MQTT

Use MQTT to send little packets of data around. My current thinking is that any sensor/data network should be a mixture of MQTT and Rest. They are different things and both provide some nice features. I like MQTT because:

I will update this post as it evolves.

Install mosquitto mqtt broker on debian server

sudo apt-get install mosquitto mosquitto-clients

Start and stop mosquitto (Debian Jessie)

sudo systemctl start mosquitto
sudo systemctl stop mosquitto

[To Do] Figure out how to run at startup???

Following this post I think it is just …

sudo systemctl enable application.service

But I need to check this.

To push/send and subscribe on an Arduino ESP8266

https://github.com/knolleary/pubsubclient/blob/master/examples/mqtt_esp8266/mqtt_esp8266.ino

Use the pubsubclient library. And get an ESP8266 wifi chip working. The ESP8266 wifi chip is its own revolution. See chips from Sparkfun, Adafruit, and a writeup in Make. For this chip I have taken the plunge and made my first direct from China purchases on Ali Express. The chips at Ali Express are ~ 5x cheaper. I love this stuff.

To push/send and read MQTT topics in python use paho library

pip install paho-mqtt

Python publisher script

This script publishes data (timestamps for now) every second into the loft/motion1 topic on a mqtt broker.

# post to an mqtt topic

import paho.mqtt.client as paho
import datetime, time

mqttc = paho.Client()
 
host = "192.168.1.200" # the address of the mqtt broker
topic= "loft/motion1"
port = 1883
 
def on_connect(mosq, obj, rc):
    print("on_connect() "+str(rc))
 
mqttc.on_connect = on_connect
 
print("Connecting to " + host)
mqttc.connect(host, port, 60)

# if connection to broker is lost and then regained, on_connect() gets called
mqttc.loop_start()

print 'starting infinite loop'
while 1:
    # publish some data at a regular interval
    now = datetime.datetime.now()
    mqttc.publish(topic, now.strftime('%H:%M:%S'))
    time.sleep(1)

Python subscriber script

This script subscribes to the topic loft/motion1 on a mqtt broker. Whenever there is new data in the topic, on_message() gets called.

# subscribe to an mqtt topic

import paho.mqtt.client as paho

mqttc = paho.Client()

host = "192.168.1.200" # the address of the mqtt broker
topic= "loft/motion1"
port = 1883

def on_connect(mosq, obj, rc):
    print('on_connect() rc=' +str(rc))
    print '   subscribing to', topic
    mqttc.subscribe(topic, 0)

def on_subscribe(mosq, obj, mid, granted_qos):
    print("Subscribed OK")

def on_message(mosq, obj, msg):
    print( "on_message() topic: " + msg.topic + " payload: "+str(msg.payload) + "\n");

mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_message = on_message

# Connect and subscribe
print("Connecting to " + host)
mqttc.connect(host, port, 60)

# if connection to broker is lost and then regained, on_connect() get called
mqttc.loop_start()

print 'starting infinite loop'
while 1:
    aaa = 1

Check version of mosquitto

cudmore@debian:/etc/mosquitto$ sudo apt-cache search mosquitto
[sudo] password for cudmore: 
libmosquitto-dev - MQTT version 3.1 client library, development files
libmosquitto1 - MQTT version 3.1 client library
libmosquittopp-dev - MQTT version 3.1 client C++ library, development files
libmosquittopp1 - MQTT version 3.1 client C++ library
mosquitto - MQTT version 3.1/3.1.1 compatible message broker
mosquitto-clients - Mosquitto command line MQTT clients
mosquitto-dbg - debugging symbols for mosquitto binaries
python-mosquitto - MQTT version 3.1 Python client library
python3-mosquitto - MQTT version 3.1 Python 3 client library
cudmore@debian:/etc/mosquitto$ 
Tags: debian, raspberry, arduino

©2020. Robert Cudmore.