#code#mqtt#iot

part of iot - internet of things

write up on this task

codes can be accessed here.

screenshots


code on esp32


running code. subscriber on the left, publisher on the right triggered by esp32

there are videos inside the github repository

note:
because I have no button component available, I used ultrasonic sensor. if the distance is close, I consider it as ‘pressed’. I set the distance threshold to be 10 cm.

troubleshooting

trouble connecting to MQTT broker

keep hitting client state -2 when connecting to MQTT broker. tried multiple broker, from local device, container (using podman), hiveMQ, EMQX. then finally tried hitting public broker. and it works. then tried keeping the broker server empty, and got -2. there’s not much documentation on what this state means. but based on the last result, I figure that it’s because it can’t find the host. I looked into many forums, checking different way to connecting to MQTT broker (TCP, TLS)

then I realize that they run on different device. I got used to running things on my laptop, where I can access stuff inside using localhost. I set the mqtt broker to be localhost all this time.
solution: they need to be connected over local network. I used my phone hotspot, and connect esp32 and laptop to it. Then I check ifconfig for the IP address of my laptop. and it works.

don’t forget to run the server on 0.0.0.0, not on localhost.

podman chown permission denied

googling a bit, I realized I have similiar problem in the past. I checked into my past project and added this line to the docker-compose.yml file

security_opt:
- label:disable

sources