MQTT
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 docker exec -it home-assistant_mosquitto_1 mosquitto_sub -t 'WEMOS/#' -q 1 -d
Send a message.
docker exec -it mosquitto mosquitto_pub -t 'sensors/unifi/presence' -q 1 -d -m 'Julie arrived' docker exec -it home-assistant_mosquitto_1 mosquitto_pub -t HOME -q 1 -d -m "`date +%H:%M:%S`"
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
Home Assistant and 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.
Sensor (motion)
I have a device (WEMOS arduino) publishing.
All so complicated, then simple. I just ended up using an MQTT Trigger directly in Automations.
I just send via PUB a simple message WEMOS/Motion with a payload of 1. That's it. It also sends a zero when it goes off but I ignore that.
I looked at ESPHome. My first impression of ESPHome is the hood is bolted shut. Yeah. That. It's geared around HASS.io anyway so no show. On to ESPEasy. Same same, make it so I don't need to program. Too late, I already know how. I don't mind programming, I find it entertaining. NodeMCU?? Ugh, Lua. Gag me.
Controller (RGBW lights)
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
It looks like having multiple Android MQTT clients does not work. So from now on I am testing one at a time.
I want one that can display text messages. I'd also like it to make sounds. I still have not found an app that will display a series of text messages. They just display the most recent.
I avoid 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...
- MyMQTT - will display a list of messages from the same topic!! ON TABLET
- Linear MQTT Dashboard - fancy charting, notifications are good; ON PHONE
- MQTT Dash - simple and works. Text display is weak.
- MQTT Snooper - runs in foreground only, contains ads
More...
- MQTT IoT
- MQTT Terminal costs $.99
- MQTT Alert, 10 installs
- Lazy MQTT Free (ad supported) or Pro ($30)
Rejected
- MQTT Chat (Remnum)
- 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 - in Safari
- Hands-on MQTT Programming With Python Gaston Hillar. Overview, not much python here
- Getting Started with Python for the Internet of Things