Day 23: MQTT Climate
Behind the door for today is a battery clip for a 9 V block, with which you can make the entire system moveable. An alternative to a battery is a power bank (a rechargeable battery pack, normally for mobile phones). You can see in today’s layout picture how to connect a 9 V battery correctly.
Climate data should be captured again today. But this time they are published through the MQTT broker and are thus accessible worldwide. The data could thus be called up via a plug-in and stored in a database.
Download today’s programme and program your NanoESP with it. After a short time, the board is connected to the WLAN and the broker and begins to send the measurement data. Now go to the http://www.hivemq.com/try-out/ page and connect to the broker. Subscribe to these topics:
As always, replace MyMQTTiD with your personal ID. You now see in the browser how the data arrive in quick succession. Next subscribe to the following topic:
You ought to receive the message “online.” Now disconnect the board from the power. Logically, you should now no longer receive any new temperature data. However, after approx. 10 seconds, the status changes from “online” to “offline.” How is that possible if the board can’t send any messages at all anymore?
The trick lies in the last will message that was defined upon connection to the broker. This message is sent when the connection is suddenly broken. But it takes a while for the broker to notice the interruption of the connection. How long it takes is likewise defined when the connection is established, with the keep-alive time. If no message is sent from the board to the broker within the time given there, the broker assumes that the connection was broken. This time is defined in the library as standard at 120 seconds. For today’s test, however, it is 10 seconds. So that the connection is not cut off only because the NanoESP currently has nothing to report, there is the stayConnected command. Here, if not stated otherwise, a ping is sent to the broker after half of the keep-alive time as a sign that the connection is still active.