Day 21: House automation
In today’s test, we’ll illuminate the characteristics and possibilities of MQTT in more detail by creating a practical scenario. In the example, there is a building with two rooms, each of which has a light switch, a light and an alarm sensor. The light switch switches the light of the respective room on or off. The alarm sensors activate a building-wide alarm.
Normally, seven different devices would be used here, which are all connected to each other through an MQTT broker. But since we probably have only one NanoESP, this will simulate all devices. For this reason, today’s layout looks somewhat complicated. The LED D3 is used today as the second light. Therefore, today it won’t show you whether you’re connected to the WLAN. The tilt sensors are used on the extension cables as alarm sensors. A person could, for example, attach them to doors or windows so that they trigger an alarm if the entry opens. Behind today’s door is the second sensor.
The programme now subscribes to the following topics, where MyMQTTiD is replaced with your ID:
You can now use the buttons to activate the light in the respective room. If you tip a tilt sensor, the alarm is triggered. It is deactivated either after 20 seconds or if you send a 0 to the alarm topic, e.g. through the http://www.hivemq.com/try-out/ webpage. You are also informed of all changes through the serial monitor.