WiFi Based Distributed IOT Home Automation

Autonomous intelligent IOT nodes connected to the home network, controlled via simple IFTTT style rules, configured via web interface

Similar projects worth following
My aim is to create autonomous home automation IOT devices, that require no installation and minimum configuration. At the same time these devices need to form a connected automation system that offers great flexibility and cater for the needs of a connected home. No central management unit required - just add more devices!

The idea is based on ESP8266 nodes or any other module that has WIFI connectivity and enough processing power and capacity to send UDP broadcast and run a small web server. The aim is every node to broadcast via UDP its sensor values, serve out its own management interface and to be able to 'see' the sensor values from the other nodes. Also nodes that are able to switch things ON and OFF or 'DO' any actual work have a simple IFTTT like rule engine. This type of set up allows for autonomous operation of each node with reporting and configuration functions as well as building a complex home automation system based on simple rules involving different nodes' sensor values.

The first node is a 'smart plug' with ESP8266-12 NodeMCU 1.0, ACS712 30A hall-effect current sensor, 30A relay module and 7-segment display module based on the TM1637 chip.

I am writing the code through the Arduino IDE with the help of the "Arduino core for ESP8266 Wi-Fi chip" project. Currently I have three entity lists that I keep in memory – “Nodes”, “Things” and “Recipes”. They are persisted via ESPFFS to the 3Mb flash memory and loaded at start up. Each “Node” has a set of “Things” that it can either switch ON or OFF, or it can report their current value, or both. The decision to switch a state of a “Thing” is made either by direct command via override property or by processing the “Recipes” that involve the “Thing”. The “Recipes” follow “IF This Then That” format and are executed sequentially (order is important). The management and reporting interface is a single html5 page stored also on the ESPFFS with jquery and jqplot JavaScript libraries. The page is loaded once and the communication with the node is done via Web Sockets. Each “Node” broadcasts its “Things” state at a set interval via UDP and to all its Web Socket clients. The UDP broadcast is used to update the local list of “Nodes” and to process the “Recipes” that involve the “Things” in the broadcast.


This the connection schema for the SmartPlug

Portable Network Graphics (PNG) - 121.01 kB - 08/16/2016 at 18:59


  • 2 × ESP8266 ModeMCU 1.0
  • 1 × TM1637 7-segment display
  • 1 × ACS712 30A hall-effect current sensor
  • 1 × 30A relay module
  • 1 × DHT11 Temperature and humidity sensor

View all 6 components

  • Code on GitHub

    Andrey08/22/2016 at 15:35 0 comments

    I finally have a more or less stable version of the code. It is far from complete as the web interface is missing the recipe management. The 'recipes' can be added manually in the code as per the following example:

        arrRecipes[0].id = 1;
        arrRecipes[0].name = "Max Watts/Hour";
        arrRecipes[0].localThingId = 1;
        arrRecipes[0].localValue = 0;
        arrRecipes[0].sourceNodeId = CHIP_ID;
        arrRecipes[0].sourceThingId = 3;
        arrRecipes[0].sourceValue = 0;
        arrRecipes[0].relation = BIGGER_THAN;
        arrRecipes[0].targetValue = 20;
        serializeRecipes(&arrRecipes, sRecipes, RECIPE_JSON_SIZE*RECIPES_LEN);
      } else {

    This 'recipe' turns off the SmartPlug when the daily consumption reaches 20 watt hours. Please, feel free to experiment with the 'recipes'. The other value that you can use is the 'Clock' (arrThings[3]), which gives you 'seconds from midnight'.

    The code can be found here: ESP_SyncSmartPlug

  • Second Node SmartWeatherStation

    Andrey08/21/2016 at 13:36 0 comments

    My Second node is SmartWeatherStation based on the ESP8266 Weather station V2. The idea behind it is for it to be a portable device that gives you a quick weather update from It also measures and broadcasts via UDP the current temperature and humidity. This broadcast will be picked up by the SmartPlug and used in a 'Recipe' to turn ON and OFF depending on the temperature in the room where the SmartWeatherStation is placed. This is a picture of the current state of the WeatherStation.

  • ESP SmartPlug connection diagram

    Andrey08/17/2016 at 14:13 0 comments

    I added a fritzing diagram of how I have connected the different components to the NodeMCU. The thing worth mentioning is the 10kΩ potentiometer that I used to reduce the 5V output from the ACS712 current sensor. I figured out that the NodeMCU actually reads up to 3.3.V on the analog input pin and not the 1.1V as per the specification of the ESP8266. I encountered another problem with analog input read. When there was no current to read from the ACS712 the readings from the analog pin were too erratic. I solved this in code by calculating the frequency of the current readings and only taking it in account if it was 50Hz. Please, feel free to ask me anything if the diagram is not clear to you.

  • Video of the 'ESP SmartPlug' in action

    Andrey07/28/2016 at 10:48 0 comments

    After couple of iterations of code re-factoring, I decided it is time to post this log showing the 'ESP SmartPlug' in action. I combined video of a heater being switched on and off remotely with a screen capture of the browser window running the remote interface. On the browser window you can see the 'Override' button, being pressed first, which stops the rules processing and allows for manual operation. After the heater is turned on, I change to a higher setting and you can see the real time graph changing. Currently the plug status updates are pushed every 5 seconds to the browser via websockets and also a UDP multicast is sent at the same time. Everything you see is being run purely from the ESP module, which gives me great confidence the my initial is achievable. The completed functionality in the ESP include the following:

    • Persisting of 'nodes', 'things' and 'recipes' to file and loading them on start up;
    • Dynamically adding other 'nodes' that send broadcasts;
    • Processing updates from broadcasts;
    • Processing of 'recipes' and updating state of 'things';
    • Websocket interfaces to update 'things'; add, update and delete 'recipes';

    The javascript code already caters for a multi 'node' scenario and the only thing left is adding the interface for managing the current 'node recipes'.

    Please, feel free to give me some feedback on the project idea and implementation of it so far.

View all 4 project logs

Enjoy this project?



Naveed wrote 12/07/2017 at 02:19 point

HI Andrey, Thanks for sharing this great project.  To keep things simple I took out the core part from things_functions.cpp and try to run it independently. My current sensor is for 5A max. However I noticed cycleCounter never exceed the value of 9. This is the max and at the same time Vrms remain less than 300.  I am using NodeMCU with ESP8266-12E. Can you point me tor right direction? how did you calculate  the cycleCount Range? or Vrms value should be higher than 300?

  Are you sure? yes | no

Nikitha-Kondapalli wrote 06/20/2017 at 10:38 point

Hi, You mentioned that "When there was no current to read from the ACS712 the readings from the analog pin were too erratic", I am facing the same problem(my code is in arduino ide and I am uploading that in nodemcu v1.0 (ESP-12EModule) ) when ever the switch is off I am getting current that too large. I did not use a potentiometer between A0 from node mcu and Vo of ACS712 5A module

  Are you sure? yes | no

Andrey wrote 09/28/2016 at 10:39 point

Hi there, the code that you need to look at, is in the things_functions.cpp. The part of the code that does the cycle count correction is this: if (cycleCounter < 48 || cycleCounter > 52 || vrms < 300) vrms = 0; . You would need to adjust that to test between 58 and 62 cycles. One thing that you need to remember is that you shouldn't use delay function in your loop as it would affect the count. Regards

  Are you sure? yes | no

ekobayuhit wrote 09/24/2016 at 23:45 point

can you show me how to calculating current reading for 60 hz? I have a problem with sampling time. Thank you.

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates