Internet of Things messaging MQTT 2: Install Python Clients on PC, Carambola, RPI,…
Installing MQTT clients
Linux
apt-get update apt-get install openssl libssl-dev python
cd /tmp/ wget http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.python.git/snapshot/org.eclipse.paho.mqtt.python-1.1.tar.gz tar xzf org.eclipse.paho.mqtt.python-1.1.tar.gz cd org.eclipse.paho.mqtt.python-1.1 python setup.py install
Windows
- Install Python
- Download http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.python.git/snapshot/org.eclipse.paho.mqtt.python-1.1.tar.gz (please not, that there might be newer version available)
- Unzip, go to folder where setup.py is located and type
python setup.py install
Carambola2
Carambola from 8devices has huge advantage when used as smart sensor or local sensor concentrator – it has low power consumption, integrated high power WiFi and LAN connections. Also features some GPIO pins and lot of digital sensors can be used directly.
opkg install python python-openssl pyserial
cd /tmp/ wget http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.python.git/snapshot/org.eclipse.paho.mqtt.python-1.1.tar.gz tar xzf org.eclipse.paho.mqtt.python-1.1.tar.gz cd org.eclipse.paho.mqtt.python-1.1 python setup.py install
Carambola can be used not only as paho client but also whole mosquitto broker can be installed. It should be enough for house or small office. For OpenWrt mosquitto sources see on mosquitto web page.
Raspberry Pi
I have tested only Ubuntu RPi package, so installation process is the same like Linux. It has some advantage over Carambola, when bigger processing power and storage is needed. It should be better used as MQTT broker. Local broker running on Raspberry Pi can be very handy and compact IoT gateway machine, especially when fitted aluminium case I designed some time ago.
Some usage examples
Copy ca.crt certificate from mosquitto server installation to fresh directory or add a path to ca.crt file.
1. Elementary subscribe code snippet. This code snippet will subscribe topic test and will messages to screen. Easy!
import paho.mqtt.client as paho def on_message(clnt, userdata, msg): print(msg.topic+" "+str(msg.payload)) mqttc = paho.Client() mqttc.on_message = on_message mqttc.tls_set("ca.crt") mqttc.connect("127.0.0.1", 8883) mqttc.subscribe("test/#") mqttc.loop_forever()
2. Complementary example for publishing message on topic
import paho.mqtt.client as paho mqttc = paho.Client() mqttc.tls_set("ca.crt") mqttc.connect("127.0.0.1", 8883) mqttc.publish("test/temperature", '22.5', 0)