The best tools to make your project dreams come true

Login or Signup

1/14/2019 | By Maker.io Staff

Automate Your Home With Adafruit IO

In previous articles, we have learned how to use the Adafruit.IO library on the ESP8266. In this project, we will build a generic controller that can be used to automate home devices, as well as control devices via IoT.

Warning: The mains controller device handles high voltages that can be lethal. Only attempt to control mains devices if you are fully competent in house and/or commercial electrical systems. If you are not, you can use the controller to control smaller devices that are powered by battery or a small wall wart.

The Schematic

Automate Your Home With Adafruit IO

You can check out the full Scheme-It schematic for this project here.

BOM

  • 2 x Adafruit Huzzah
  • 2 x 3mm Red LED
  • 1 x 1K resistor
  • 2 x 2.2K resistor
  • 1 x 2N3904
  • 1 x 5V coil 230V AC relay
  • 1 x 1N5817 diode
  • 1 X tactile switch
  • Optional fuse (size depends on the mains device being controlled)
  • Circuit construction material
  • The Project

    The aim of this project is to combine the IoT capabilities of Adafruit IO with Wi-Fi modules and some clever electronics, so we can control mains devices using IoT controllers. In turn, these IoT devices can then be triggered and controlled so that a home can be automated to some degree, which would include automatically turning on taps, turning on heaters, and even opening doors!

    Adafruit IO

    For our project, we will need a dashboard and a feed to store our IoT data. The dashboard itself is not crucial for in-home automation, but it does allow for the control of IoT relays using a phone, computer, or even a tablet. The dashboard can also be used to see how data has changed over the day if temperature, humidity, and pressure sensors are used to gather data.

    Automate Your Home With Adafruit IO

    This project will only use one feed to control one IoT button and IoT relay. The feed is called “buttonState” and is linked to the toggle switch and graph on the dashboard.

    Automate Your Home With Adafruit IO

    An IoT Button

    The IoT button is a device that can be mounted in remote places that has a single button whose function can be tied to just about anything. The button is connected to a digital input on the Adafruit Huzzah that, once pressed, will result in the Huzzah changing the value of a chosen feed to true (buttons should be Boolean variables, as they are either on or off).

    The button circuit uses a single resistor, as the Huzzah does not have internal pull up resistors to provide a high voltage on the absence of a button press. This means that the logic for our button press will be inverted such that a digital read of “0” will indicate the button being pressed and “1” will indicate no button press.

    An LED indicator is also used on the Huzzah to show different situations:

  • Quick flash indicates start up connection to Adafruit IO.
  • Slow brief blinking LED indicates awaiting input.
  • LED held on for a second indicates sending data to Adafruit IO.
  • When the button press is detected, it sends a Boolean value to the “buttonState” feed on Adafruit IO, and the variable that holds the toggle value is also flipped (such that true becomes false and false becomes true).

    Copy Code
    #include <ESP8266WiFi.h>
    #include "AdafruitIO_WiFi.h"
    
    #define WIFI_SSID       "TP-Link_B5AC"
    #define WIFI_PASS       "93080422"
    
    #define IO_USERNAME    "robinmitchell1993"
    #define IO_KEY         "61e79b1ecbf3448d8d5c8bcc13c8f021"
    
    // Connect to Wi-Fi and Adafruit.IO handel 
    AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
    
    // Create a feed object that allows us to send data to
    AdafruitIO_Feed *buttonFeed = io.feed("buttonState");
    
    int counter = 0;
    bool toggle = false;
    
    void setup() 
    {
      // Enable the serial port so we can see updates
      Serial.begin(115200);
    
      pinMode(0, INPUT);
      pinMode(2, OUTPUT);
    
      digitalWrite(2, LOW);
      
      // Connect to Adafruit.IO
      io.connect();
    
      // wait for a connection
      while(io.status() < AIO_CONNECTED) 
      {
        delay(200);
        digitalWrite(2, HIGH);
        delay(200);
        digitalWrite(2, LOW);
      }
    }
    
    void loop() 
    {
      // Always keep this at the top of your main loop
      // While not confirmed, this implies that the Adafruit.IO library is not event driven
      // This means you should refrain from using infinite loops
      io.run();
    
      if(digitalRead(0) == 0)
      {
          buttonFeed->save(toggle);
          toggle = !toggle;
    
          digitalWrite(2, HIGH);
          delay(1000);
          digitalWrite(2, LOW);  
    
          counter = 0;
      }
    
      counter ++;
    
      if(counter > 100)
      {
          digitalWrite(2, HIGH);
          delay(100);
          digitalWrite(2, LOW);   
          counter = 0;   
      }
      
      delay(1);
    }
    

    An IoT Relay

    The IoT relay is a device that contains a relay that can control just about any device, ranging from a smart phone charger all the way to a piece of workshop equipment. The IoT relay (also powered by a Huzzah board) also connects to Adafruit IO and subscribes to the “buttonState” feed, but there is a slight difference; instead of saving data to the feed, it instead receives messages from it. Before we initiate the Adafruit IO code, we create a message handler that will call a special relay function when a new message is received (i.e., when the buttonState feed changes value). The message handler looks at the incoming data and checks to see if the first element in the data (which is a char array) is equal to “1”. If it is, the relay is switched on else the relay is switched off.

    Connecting mains devices to a relay should be done with extreme caution, as high voltages are dangerous! Firstly, only work with mains voltages if you are safe and competent to do so. Secondly, when using switching circuits, it is recommended that you switch the LIVE wire and no others (unless it’s a heater element in a water container; in which case, both LIVE and NEUTRAL should be switched together using a double throw switch). The fuse for the circuit (which protects against short circuits) should only be found on the LIVE wire and no other connection. It is also highly recommended that if you are not a qualified electrician, an RCD plug should be used to provide additional protection.

    Copy Code
    #include <ESP8266WiFi.h>
    #include "AdafruitIO_WiFi.h"
    
    #define WIFI_SSID       "TP-Link_B5AC"
    #define WIFI_PASS       "93080422"
    
    #define IO_USERNAME    "robinmitchell1993"
    #define IO_KEY         "61e79b1ecbf3448d8d5c8bcc13c8f021"
    
    // Connect to Wi-Fi and Adafruit.IO handel 
    AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
    
    // Create a feed object that allows us to send data to
    AdafruitIO_Feed *buttonFeed = io.feed("buttonState");
    
    int counter = 0;
    
    void setup() 
    {
      // Enable the serial port so we can see updates
      Serial.begin(115200);
    
      // Configure IO
      pinMode(2, OUTPUT);
      pinMode(0, OUTPUT);
      digitalWrite(0, LOW);
    
      // Configure Handler
      buttonFeed->onMessage(relayToggle);
          
      // Connect to Adafruit.IO
      io.connect();
    
      // wait for a connection
      while(io.status() < AIO_CONNECTED) 
      {
        delay(200);
        digitalWrite(2, HIGH);
        delay(200);
        digitalWrite(2, LOW);
      }
    }
    
    
    void loop() 
    {
      // Always keep this at the top of your main loop
      // While not confirmed, this implies that the Adafruit.IO library is not event driven
      // This means you should refrain from using infinite loops
      io.run();
    }
    
    
    void relayToggle(AdafruitIO_Data *data)
    {
      Serial.print(data->value()[0]);
      if(data->value()[0] == '1')
      {
        digitalWrite(0, HIGH);  
      }
      else
      {
        digitalWrite(0, LOW);
      }
    }
    

    Home Devices

    Our IoT relay and IoT button are the core to any home automated system, as they allow for wireless control. The button itself may be useful in situations where doors and windows can open automatically, boil the kettle, and even lock the house. But the button is rather limited, so the button could easily be replaced with any digital detector. For example, a laser tripwire system could be used to automatically open doors when you approach them or even sound alarms.

    The IoT relay is incredibly versatile, as it can enable and disable power to just about any device. What exactly you decide to connect the IoT relay to is up to you, but here are some suggestions:

  • Water pump for filling bathtub
  • Lights
  • Door locks
  • Linear actuators for opening doors and windows
  • Fans for when it gets too hot
  • Alarm systems and sirens
  • Computers and IT equipment during office hours