MQTT: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
Brian Wilson (talk | contribs)
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 3: Line 3:
working on that. Now I am building an ESP8266 Arduino (WeMos D1) light strip controller.
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]].
My Mosquitto MQTT Server is in a docker container on [[Violet]]. I can log into that container and run commands to learn more.
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.  
Listen for presence messages from anyone. QOS is set to 'at least once'. -d is for debug.  
Line 50: Line 49:
== Home Assistant and Arduino ==
== Home Assistant and Arduino ==


Now I need to make the WeMos D1 listen and respond.
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
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.
and interfaces with a web page at Adafruit. I want to keep a lid on things.


Line 62: Line 60:
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 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 [https://esphome.io/ ESPHome].
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.
My first impression of ESPHome is the hood is bolted shut. I was wrong. It's cool. Go look [[ESPHome]].
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) ===
=== Controller (RGBW lights) ===
Line 73: Line 69:


I wrote my own little MQTT program that just communicated with RGBW numbers, like this: 0100 0100 0300 0400.
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.
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
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.
I used the name mqtt-hallway and topics state: home/rgbw and command: home/rgbw/set.
Line 118: Line 113:
https://www.baldengineer.com/mqtt-tutorial.html
https://www.baldengineer.com/mqtt-tutorial.html


[[Category: IoT]]
[[Category:IoT]]

Latest revision as of 23:26, 11 June 2024

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 Violet. 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. I was wrong. It's cool. Go look ESPHome.

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...

  1. MyMQTT - will display a list of messages from the same topic!! ON TABLET
  2. Linear MQTT Dashboard - fancy charting, notifications are good; ON PHONE
  3. MQTT Dash - simple and works. Text display is weak.
  4. MQTT Snooper - runs in foreground only, contains ads

More...

  1. MQTT IoT
  2. MQTT Terminal costs $.99
  3. MQTT Alert, 10 installs
  4. Lazy MQTT Free (ad supported) or Pro ($30)

Rejected

  1. MQTT Chat (Remnum)
  2. MQTT Push Client (Helios) - uses a push notification services
  3. Wallpanel -- Google text-to-speech, face detection (-1), complex
  4. MQTIZER - chat style display; but appears to do nothing
  5. 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

https://www.baldengineer.com/mqtt-tutorial.html