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 sensors, lighting, temperature, and others. If the “smart home” is small, then everything looks simple even with self-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 a lot of 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 very method 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):

    • / myhome / PNP / ESPX-0023CB541 / BUTTON / name — name / description (“FLASH button” - this will be displayed in the web interface)
    • / myhome / PNP / ESPX-0023CB541 / BUTTON / type - type of variable and direction of data flow for it, “O: Switch” means a sensor that provides information, and, for example, “I: Dimmer” - an incoming dimmer channel
    • / myhome / PNP / ESPX-0023CB541 / BUTTON / groups - groups in which this variable will appear by default ("Switches Builtin Buttons")
    • / myhome / PNP / ESPX-0023CB541 / BUTTON / min - the minimum value (for numeric variables, it will be empty for a button)
    • / myhome / PNP / ESPX-0023CB541 / BUTTON / max - maximum value (similarly)

    Script running on the server (by cron or manually, after connecting the device) - downloads the 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 self-made 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 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 the 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:

    • Infrared motion sensors and radio motion sensor Parallax X-band motion detector (up to 3 pieces per module)
    • Temperature / humidity sensor DHT22 or equivalent
    • My Modbus / RS-485 4-channel Dimmer
    • Sonars HC-SR04 and MaxBotix sonars with sequential output
    • Relays on the Sonoff Basic Switch and Sonoff Touch (the latter can be switched both to the relay control mode by a button, and only through OpenHAB / HomeAssistant)
    • Built-in ADC, LED and Flash button (for Sonoff Touch, this is a touch button)
    • A simple thermostat with two relays for controlling a fan (indoor unit) and a compressor (outdoor unit)

    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 and set the network login and password, as well as the broker's IP address. These parameters will remain in the flush, so that the second time you do not need to do this. If it is impossible to connect to the specified network for one minute, it will switch back to 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:

    • setup _ * () - pin configuration
    • loop _ * () - polling the sensor on each program cycle
    • refresh _ * (forceSend) - refreshing sensor status information on MQTT (200 ms by default, and every two seconds - forced refresh)
    • pnp _ * () - sending P & P information
    • subscribe _ * () - a subscription to the necessary incoming topics
    • mqtt _ * () - processing incoming mqtt messages

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

    Also popular now: