Difference between revisions of "MQTT"

From Review or Discard at Will
Jump to: navigation, search
(Added build dates)
(cmnd: global tasmotas)
 
(13 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
*[http://mqtt.org/ MQTT]
 
*[http://mqtt.org/ MQTT]
 
*[http://mosquitto.org/ Mosquitto] An Open Source MQTT v3.1/v3.1.1 Broker
 
*[http://mosquitto.org/ Mosquitto] An Open Source MQTT v3.1/v3.1.1 Broker
 +
*[http://vasters.com/clemensv/2014/06/02/MQTT+An+Implementers+Perspective.aspx MQTT. An Implementer’s Perspective] by Clemens Vasters
 +
TLS
 +
*[https://www.feistyduck.com/books/openssl-cookbook/ OpenSSL Cookbook] A short book that covers the most frequently used OpenSSL features and commands, by Ivan Ristić
 +
*[http://rockingdlabs.dunmire.org/exercises-experiments/ssl-client-certs-to-secure-mqtt SSL/TLS Client Certs to Secure MQTT] JERRY DUNMIRE
 +
*[http://jpmens.net/2013/02/25/lots-of-messages-mqtt-pub-sub-and-the-mosquitto-broker/ Lots of messages: MQTT, Pub/Sub, and the Mosquitto broker] Jan-Piet Mens
 +
*[https://answers.launchpad.net/mosquitto/+question/204639 Unable to connect to mosquitto server using server CA] Useful thread found on mosquitto launchpad
 +
*[https://github.com/owntracks/tools/blob/master/TLS/generate-CA.sh tools/TLS/generate-CA.sh] GitHub owntracks / tools
 +
 
==Build History==
 
==Build History==
 
===Carnage of Assumptions===
 
===Carnage of Assumptions===
Line 11: Line 19:
 
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).
 
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 for 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.
+
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===
 
===Working prototype===
Line 19: Line 27:
  
 
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.
 
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.
 +
 +
[[File:R18s0.gif|500px]]
 +
I made an animated .gif of the HomeAmation.Mqtt app in action.
 +
 +
===TLS===
 +
====Resources====
 +
*[http://rockingdlabs.dunmire.org/exercises-experiments/ssl-client-certs-to-secure-mqtt SSL/TLS Client Certs to Secure MQTT]
 +
*[https://github.com/owntracks/tools/blob/master/TLS/generate-CA.sh generate-CA.sh] from @jpmens
 +
====Mosquitto_[pub|sub]====
 +
<source lang="text">
 +
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
 +
</source>
 +
 +
=====cmnd=====
 +
<source text=bash>
 +
root@mqtt0:/etc/mosquitto/conf.d# mosquitto_pub -h debbie -t cmnd/220B4C/STATUS -m ''
 +
root@mqtt0:/etc/mosquito/conf.d#
 +
# The following returns the current configuration of MQTT on the tasmota device with the id of 2200B4C
 +
root@mqtt0:/etc/mosquito/conf.d# mosquitto_pub -h debbie -t cmnd/220B4C/status -m '6'
 +
root@mqtt0:/etc/mosquito/conf.d#
 +
# returns the current mqtt broker
 +
root@mqtt0:/etc/mosquitto/conf.d# mosquitto_pub -h debbie -t cmnd/220B4C/MqttHost -n
 +
root@mqtt0:/etc/mosquito/conf.d#
 +
Client (null) received PUBLISH (d0, q0, r0, m0, 'cmnd/220B4C/MqttHost', ... (0 bytes))
 +
Client (null) received PUBLISH (d0, q0, r0, m0, 'stat/220B4C/RESULT', ... (28 bytes))
 +
{"MqttHost":"192.168.1.225"}
 +
# use 'tasmotas' for all tasmota devices
 +
root@mqtt0:/etc/mosquitto/conf.d# mosquitto_pub -h debbie -t cmnd/tasmotas/status -m '6'
 +
root@mqtt0:/etc/mosquito/conf.d#
 +
</source>
 +
 +
====Python====
 +
<source lang="python">
 +
'''
 +
Created on Dec 3, 2015
 +
 +
@author: jeffa
 +
'''
 +
 +
import paho.mqtt.client as mqtt
 +
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()
 +
</source>
 +
[[Category:HomeAmation]]
 +
[[Category:IoT]]

Latest revision as of 14:09, 16 August 2023

MQTT

Resources

TLS

Build History

Carnage of Assumptions

September 9, 2015

TheCarnageOfAssumptions.jpg

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

MqttBbbSubscribeWin8ClientLinuxLog.jpg

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.

R18s0.gif I made an animated .gif of the HomeAmation.Mqtt app in action.

TLS

Resources

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
cmnd
root@mqtt0:/etc/mosquitto/conf.d# mosquitto_pub -h debbie -t cmnd/220B4C/STATUS -m ''
root@mqtt0:/etc/mosquito/conf.d#
# The following returns the current configuration of MQTT on the tasmota device with the id of 2200B4C
root@mqtt0:/etc/mosquito/conf.d# mosquitto_pub -h debbie -t cmnd/220B4C/status -m '6'
root@mqtt0:/etc/mosquito/conf.d#
# returns the current mqtt broker
root@mqtt0:/etc/mosquitto/conf.d# mosquitto_pub -h debbie -t cmnd/220B4C/MqttHost -n
root@mqtt0:/etc/mosquito/conf.d#
Client (null) received PUBLISH (d0, q0, r0, m0, 'cmnd/220B4C/MqttHost', ... (0 bytes))
Client (null) received PUBLISH (d0, q0, r0, m0, 'stat/220B4C/RESULT', ... (28 bytes))
{"MqttHost":"192.168.1.225"}
# use 'tasmotas' for all tasmota devices
root@mqtt0:/etc/mosquitto/conf.d# mosquitto_pub -h debbie -t cmnd/tasmotas/status -m '6'
root@mqtt0:/etc/mosquito/conf.d#

Python

'''
Created on Dec 3, 2015

@author: jeffa
'''

import paho.mqtt.client as mqtt
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()