Introduction: DIY SONOFF USING ESP8266-01

About: DIY,IoT, Electronics

What is Sonoff?

  • Sonoff Basic is an affordable device that provides users with smart home control. It is a WiFi-based wireless switch that can connect to a wide range of appliances. Sonoff transmits data to a cloud platform through the WiFi router, which enables users to remotely control all the connected appliances, via the mobile application.

What is ESP8266-01?

  • The ESP8266 ESP-01 is a Wi-Fi module that allows microcontrollers access to a Wi-Fi network. This module is a self-contained SOC (System On a Chip) that doesn’t necessarily need a microcontroller to manipulate inputs and outputs as you would normally do with an Arduino ,it can save you some money and space in your projects.

Today we are going to show you how to making your own sonoff using esp8266 01 wifi module and relay. With this you can control your home appliances from anywhere in the world.

Step 1: COMPONENTS REQUIRED

  • PCB
  • Hi-Link -5v
  • Relay -5v
  • ESP8266-01
  • LD33V IC
  • MCT2E
  • BC547
  • IN4007 Diode
  • 1K Resistor
  • LED - 5mm
  • 2 Pin Screw Connectors - 2 Nos

Step 2: TOOLS NEEDED

  • Soldering Iron
  • Soldering Wire
  • Flux

Step 3: PCB

Therefore, case you want your own PCB, you can obtain through this link on PCBWay.com. You can also order good quality PCBS from PCBWay.com at low cost. In addition to the standard pcbs, we could also support advanced Pcbs, FPC/rigid -flex Pcbs, rapid-rpototyping and other related services that could meet your needs to the most extent.

PCB order steps on PCBWay:

  • Visit the website: www.pcbway.com
  • Click PCB Instant QuoteClickquick-order PCB Upload the gerber file by clicking "+Add Gerber File" and wait until the gerber file is finished
  • uploading Set the PCB specifications as you wish, starting from the number of PCBs, the number of layers, thickness, color and others.
  • After that, look at the left, it will display the estimated costprocessing time and delivery service selected
  • Click Save to cart

Step 4: ASSEMBLING COMPONENTS ON PCB

Connect all components to the PCB

ESP-01 works at 3 volts, so LD33V IC is used to convert 5 volts to 3.3volts.

Step 5: CIRCUIT DIAGRAM

Step 6: SET UP ADAFRUIT MQTT

Adafruit IO is an IOT platform built around the Message Queue Telemetry Transport (MQTT) Protocol. MQTT is a lightweight protocol that allows multiple devices to connect to a shared server, called the MQTT Broker, and subscribe or write to user defined topics. When a device is subscribed to a topic, the broker will send it a notification whenever that topic changes. MQTT is best suited for applications with low data rates, strict power constraints, or slow Internet connections.

  • First you to create a free account for you on adafruit.io website.
  • Then by going to Dashboard you can go to ( Action ) and select " create new dashboard ".give it a name and click create.
  • Once you create your dashboard , go to " create new block "
  • Choose the type of block you want to use like toggle , slider
  • Create block

Click here to know more (Video)

Step 7: PROGRAMING ESP-01

I flash esp-01 using the Esp8266 Serial Module Board

  • Insert ESP-01 to USB serial module
  • Connect Esp8266 Serial Module Board to PC
  • Set up Wifi SSID,PASS,AIO USERNAME,AIO KEY & FEED NAME (The username and aio key can be found by clicking on "My Key" in the adafruit account , If you do not understand, watch this video(Click Here))
  • Go To Tools>Board>ESP8266 Boards> Generic ESP8266 Module (Adafruit_MQTT_Library must be installed)
  • Select Correct Port
  • Upload Code Below

Step 8: CODE

/***************************************************
  This is the code for the project DIY WIFI switch using ESP8266
  
  Original code is from
  Adafruit MQTT Library ESP8266 Example

  Code edited by 
  Pro Maker_101
 ****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define relay 0

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "......."
#define WLAN_PASS       ".........."

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "..........."
#define AIO_KEY         ".........................."

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
//Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");

// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Switch");

/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {
  Serial.begin(115200);
  delay(10);

  Serial.println(F("Adafruit MQTT demo"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());

  pinMode(relay,OUTPUT);

  // Setup MQTT subscription for onoff feed.
  mqtt.subscribe(&onoffbutton);
}

uint32_t x=0;

void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  // this is our 'wait for incoming subscription packets' busy subloop
  // try to spend your time here

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(1))) {
    if (subscription == &onoffbutton) {
      Serial.print(F("Got: "));
      Serial.println((char *)onoffbutton.lastread);
      uint16_t state = atoi((char *)onoffbutton.lastread);
      digitalWrite(relay,state);
    }
  }


  // ping the server to keep the mqtt connection alive
  // NOT required if you are publishing once every KEEPALIVE seconds
  /*
  if(! mqtt.ping()) {
    mqtt.disconnect();
  }
  */
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {

Step 9:

After programming, set the ESP-01 module to PCB. With this our PCB work is completed...

Step 10: INSTALL PCB ON CASE

I have made a case here using PVC sheet, Just do the case if you want

  1. First, Insert PCB in to Case
  2. Glue the bottom part using a piece of PVC sheet

Now all our work is completed....

Step 11: CONNECTION

  • Connect 230v AC supply in to "IN" terminal
  • Connect Load in to "OUT" terminal

I use a led bulb for testing...

Step 12: APP

An application called "IoT OnOff®" is used to control the relay..

Open the application and set MQTT Broker

  • Host = io.adafruit.com
  • Port = 1883
  • Username = Your Adafruit IO Username
  • Password = Your Adafruit IO Key

Then add switch & Set

  • Publish = Username /Feeds/Feed name
  • Publish Value = True=1 , False=0

If you do not understand, watch this video(Click Here)

Step 13: WORKING

Watch this video (Click Here)

THANK YOU....