📜 ⬆️ ⬇️

MQTT, smart home, ESP-8266 and Plug & Play

Creating a smart home involves a lot of different sensors that monitor the situation in the house - motion, lighting, temperature and other sensors. If the “smart home” is small, then everything looks simple even with home-made systems based on open solutions (MQTT, OpenHAB, etc.) - they hooked up the sensor, registered it in the OpenHAB or HomeAssistant program and started working. But if there are many sensors, a lot of routine and thankless work appears on prescribing each of them in the control system.

The proposed solution (“crutch”) allows to manage a minimum of manual operations at this stage. The sensor controller connects to the MQTT broker and tells about its sensors and devices in a specific format in special topics, then a small script on the smart home server creates configuration files corresponding to these sensors, then you only need to manually define them in the home-specific groups and start creating management scripts.

Specifically, this firmware works on modules with ESP-8266 (including the Sonoff Basic Switch and Sonoff Touch Wallswitch) with P & P scripts for OpenHAB and HomeAssistant, but the way of issuing plug & play information can be used in other projects.

When a device is connected to the MQTT broker, topic views are created (in this case, for the FLASH button on ESP-8266 with the serial number 0023CB541):
')

Script running on the server (by cron or manually, after connecting the device) - downloads a list of topics from / myhome / PNP, searches for devices missing in OpenHAB and creates configuration files for them. If there is already a configuration file for such a device, then the P & P information is completely ignored, but you can delete this file, if necessary, re-create the file for a device with a changed set of sensors.

So this is a simple and transparent way - we get rid of a rather big headache if you need to connect a large number of network-controlled devices in our homemade smart home.

A little more detail will tell about the actual firmware for ESP-8266. Its source codes (and support scripts for OpenHAB and HomeAssistant) can be found on Github (specifically the support scripts are here ).

It works on standard ESP-8266 modules (as well as Sonoff Basic Switch and Sonoff Touch Wallswitch), supports firmware upgrade on the fly (ArduinoOTA), and also has a simple web server for initial configuration of WiFi network parameters and setting the MQTT IP address -broker.

The firmware structure is modular and you can add your sensors / devices quite easily. The current list includes:


It should be noted that due to the lack of pins on the ESP-8266, the modules may conflict with each other (for example, a dimmer with a sonar can not be connected at the same time, unless you redistribute the pins yourself).

After selecting the necessary devices in esp-sensors.h, compilation and firmware - the module will switch to configuration mode - will raise its WiFi network with a name like “espx-0023cb541” to which you need to connect (you can use any smartphone) with a password from the same file then go to 192.168.4.1 and set the network login and password, as well as the broker's IP address. These parameters will remain in the flush, so that you do not need to do this a second time. If it is impossible to connect to the specified network for one minute, it will switch back to the configuration mode for three minutes and then repeat this cycle until a successful connection.

For those who are going to edit this firmware, I will mention that each sensor / device module is divided into six functions:


These functions need to be defined in the file of your module, then add their call to the appropriate locations of esp-sensors.ino.

Source: https://habr.com/ru/post/432730/


All Articles