MQTT: Difference between revisions
Brian Wilson (talk | contribs) |
Brian Wilson (talk | contribs) |
||
Line 88: | Line 88: | ||
== Resources == | == Resources == | ||
Books - There are | Books - There are good books in Safari including | ||
'''Hands-on MQTT Programming With Python''' Gaston Hillar. | |||
https://www.baldengineer.com/mqtt-tutorial.html | https://www.baldengineer.com/mqtt-tutorial.html | ||
[[Category: IoT]] | [[Category: IoT]] |
Revision as of 16:33, 24 June 2020
I was originally looking at MQTT right now in the context of Presence Detection. It turned out that Home Assistant already had a good integration with UniFi so I stopped working on that. Now I am building an ESP8266 Arduino (WeMos D1) light strip controller.
My Mosquitto MQTT Server is in a docker container on Bellman. I can log into that container and run commands to learn more.
Listen for presence messages from anyone. QOS is set to 'at least once'. -d is for debug. -t defines what I listen to, where the "+" is a wildcard.
docker exec -it mosquitto mosquitto_sub -t 'sensors/+/presence' -q 1 -d
Send a message.
docker exec -it mosquitto mosquitto_pub -t 'sensors/unifi/presence' -q 1 -d -m 'Julie arrived'
How should my messages look?
- PUB Home Assistant will publish desired LED settings.
- SUB The Arduino will subscribe and adjust the LEDs accordingly.
- PUB After making changes the Arduino will send current settings. On power up the Arduino will restore the last state of the strip and send status.
- SUB Home Assistant will subscribe and display the LED settings.
I should be able to mock both sides with a bit of Python.
Python for testing
conda create --name mqtt conda activate mqtt conda install -c conda-force paho-mqtt
Here's my very first program. It sends a 1 or a 0 to whatever wants to hear it.
import paho.mqtt.publish as publish import time HOST = "192.168.123.2" state = True target = "blinker" while(1): print(state) publish.single(target, "1" if state else "0", hostname=HOST) time.sleep(1) state = not state
Arduino
Now I need to make the WeMos D1 listen and respond. First I tried the Adafruit library and their sample code. It works, but the MQTT server is out there in the cloud and interfaces with a web page at Adafruit. I want to keep a lid on things.
Per the article in the Resources section I am trying PubSubClient which can be installed from the Library Manager in the Arduino IDE.
I wrote my own little MQTT program that just communicated with RGBW numbers, like this: 0100 0100 0300 0400. It worked great and I was able to send commands from the MQTT console in the Developer section of Home Assistant. Then I found this: https://github.com/corbanmailloux/esp-mqtt-rgb-led
I used the name mqtt-hallway and topics state: home/rgbw and command: home/rgbw/set.
Android MQTT clients
I want one that can display text messages. I'd also like it to make sounds.
I ignore any that "contain ads" and most of the ones that cost $.
There are specialized clients, for example one to watch a doorbell and one to arm/disarm security systems. There is one for OBD2 called "Car IoT". Some free clients require using a cloud-based broker.
In evaluation...
- Linear MQTT Dashboard - fancy charting, notifications are good; working pretty nicely for me so far
- MQTT Dash - simple and works. Text display is weak.
More...
- MQTT IoT
- MQTT Terminal costs $.99
- MQTT Alert, 10 installs
- Lazy MQTT Free (ad supported) or Pro ($30)
Rejected
- MQTT Push Client (Helios) - uses a push notification services
- Wallpanel -- Google text-to-speech, face detection (-1), complex
- MQTIZER - chat style display; but appears to do nothing
- MQTT Notification - just shows a popup, does nothing else
Resources
Books - There are good books in Safari including Hands-on MQTT Programming With Python Gaston Hillar.