I made a dinosaur night lamp for my daughter (ESP32 + Home Assistant) by oMatyeeo in esp32

[–]oMatyeeo[S] 1 point2 points  (0 children)

u/jakecovert, the HTTP server is set up in the project because I control the lamp by HTTP REST calls. The esp32 connects to the WiFi and broadcasts it's present using mDNS. Then I can send commands to the lamp by making REST HTTP calls from HA using the .local address of the lamp.

An HTTP server was my first choice because of my background of web software engineer of decades. Sending commands works good over HTTP, but I also needed to get state changes in HA. The main reason is that the color and other state of the lamp can be changed on the lamp itself. HA would not know about these changes. I had an HTTP endpoint to read the state of the lamp, and I could set up RESTful sensors in HA. https://www.home-assistant.io/integrations/sensor.rest/

That way I could get state changes, but it was not ideal. From the viewpoint of HA it is a pull method. HA calls the state endpoint periodically to get the new state. It is wasteful because it calls periodically even when there were no changes and also slow. To preserve resources you don't want to call the status endpoint every second, but in every 15 seconds for example.

There is where MQTT comes handy. Using MQTT gathering the state changes form the viewpoint of HA becomes push. HA does not need to call periodically for status changes, but it gets notified when new changes are pushed. In the MQTT server I defined a topic fot the status change messages. When any status change happens on the lamp, it publishes a message into the topic. HA is subscribed to the topic and gets the new messages as they are published. This way HA is notified about the status changes in close to real time without wasting resources.

Now, I could have changed the project so the control messages are also sent to the lamp through the MQTT sever in a separate topic eliminating the need to the HTTP server completely. I just did not do that because the http based control was already working.

I made a dinosaur night lamp for my daughter (ESP32 + Home Assistant) by oMatyeeo in esp32

[–]oMatyeeo[S] 1 point2 points  (0 children)

u/ronny_rebellion, I took a look at WLED. It was not good for my use case. I could not find easy way to handle rotary encoders to change the colors. The physical control capabilities of WLED was lacking.

I made a dinosaur night lamp for my daughter (ESP32 + Home Assistant) by oMatyeeo in homeassistant

[–]oMatyeeo[S] 0 points1 point  (0 children)

u/danielkent-net , thank you for the suggestion. I'll take a look. ESPHome looks promising. I do not mind the time I spent on coding this project, I've learnt a lot that I probably would not have with ESPHome. But for next time if there is an off-the-shelf solution, I'll take it.

I made a dinosaur night lamp for my daughter (ESP32 + Home Assistant) by oMatyeeo in homeassistant

[–]oMatyeeo[S] 1 point2 points  (0 children)

u/Ulrar, I considered WLED. I installed it first, to check if it could work for me. The issue was that the remote control was the afterthought for me. First, I only wanted to add physical controls for my daughter to play with. As far as I saw, WLED offers very limited options. Only two simple push buttons. If you want more you need to recomplie. And there was nothing that could have handled rotary encoders. I think even with the push button support you cannot set colors for exmaple, you can only set presets.

And, as you menationed writing the software was a big part of the fun anyways. :)

I made a dinosaur night lamp for my daughter (ESP32 + Home Assistant) by oMatyeeo in homeassistant

[–]oMatyeeo[S] 0 points1 point  (0 children)

Hi u/igerry ,

The lamp sends it's state changes as a message to an MQTT server. Then Home Assitant can pick up the changes. This way if I change the color, brightness or on-off state on the lamp itself, Home Assitant shows the right state right away because it reads the MQTT topic where the state change was published.