Difference between revisions of "MQTT"
(Added TLS resources and .py code.) |
m (→Python) |
||
Line 49: | Line 49: | ||
import ssl | import ssl | ||
− | |||
publishHost = "192.168.1.111" | publishHost = "192.168.1.111" | ||
mqttPort = 8883 | mqttPort = 8883 | ||
Line 71: | Line 70: | ||
certfile=clientCrt, | certfile=clientCrt, | ||
keyfile=clientKey, | keyfile=clientKey, | ||
− | + | tls_version=ssl.PROTOCOL_TLSv1_2) | |
client.on_connect = on_connect | client.on_connect = on_connect |
Revision as of 12:17, 6 December 2015
Contents
MQTT
Resources
- MQTT
- Mosquitto An Open Source MQTT v3.1/v3.1.1 Broker
- MQTT. An Implementer’s Perspective by Clemens Vasters
TLS
- SSL/TLS Client Certs to Secure MQTT JERRY DUNMIRE
- Lots of messages: MQTT, Pub/Sub, and the Mosquitto broker Jan-Piet Mens
- Unable to connect to mosquitto server using server CA Useful thread found on mosquitto launchpad
- tools/TLS/generate-CA.sh GitHub owntracks / tools
Build History
Carnage of Assumptions
September 9, 2015
Pictured a Beagle Bone Black running Debian with a python mqtt subscription to control two relays via the custom shield I built with a couple FET transistors (with room to expand to 8 total).
Have you ever spent way to much time fixing something based on an assumption that you never question again? At least four wasted hours. It was some loading in the relay! bleuch. I replaced FET transistor a couple times and a resistor before realizing there was something funky with the relay drive.
Working prototype
September 13, 2015
Pictured here is a Windows 10 client I built that reads sensors, in this case DS18B20 temperature sensors, and controls for switches. The win10 client is a universal windows app deployed to my desktop, a tablet and hopefully real soon now a windows 10 phone. I used MvvM Light view models and Messenger and utilized the new compiled bindings available now with win10 visual studio 2015.
I made an animated .gif of the HomeAmation.Mqtt app in action.
TLS
Mosquitto_[pub|sub]
mosquitto_sub -t "#" -v --cafile ca.crt --cert client.crt --key client.key -p 8883 -h 192.168.1.111
mosquitto_pub -t test -m "Howdy `date`" -d --cafile ca.crt --cert client.crt --key client.key -p 8883 -h 192.168.1.111
Python
'''
Created on Dec 3, 2015
@author: jeffa
'''
import paho.mqtt.client as mqtt
import json
import HaClasses
import os
import ssl
publishHost = "192.168.1.111"
mqttPort = 8883
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("#")
def on_message(client, userdata, msg):
print(msg.payload)
rootDir = os.path.dirname(__file__)
dataDir = os.path.join(rootDir, 'Data')
caCrt = os.path.join(dataDir, 'ca.crt')
clientCrt = os.path.join(dataDir, 'client.crt')
clientKey = os.path.join(dataDir, 'client.key')
client = mqtt.Client(protocol=mqtt.MQTTv311)
client.tls_set(caCrt,
certfile=clientCrt,
keyfile=clientKey,
tls_version=ssl.PROTOCOL_TLSv1_2)
client.on_connect = on_connect
client.on_message = on_message
client.connect(publishHost, mqttPort, 60)
client.loop_forever()