Introduction: ESP8266 Simple and Easy Way to Control AC Lamp Using Arduino

About: revolutionary device designed to control AC Loads working on 110V/220V allowing to control High voltage AC loads using Microcontrollers Like AVR,PIC,STM32 or Electronics platforms like arduino,Raspberry pi,bea…

In this short tutorial we will see How to control AC lamp through WiFi network using ESP8266 WiFi module.

This module is easy to connect and easy to use, so we will see step by step how to program this module using Arduino IDE.

Let's start with the components and software we need.

Step 1: Hardware and Software Requirements

For Hardware components we need:

  1. Arduino Uno board"or use FTDI breakout to program ESP8266
  2. ESP8266 WiFi Module.
  3. Breakout for ESP8266.
  4. Arduino Board"Arduino Nano i used".
  5. Sugar Device "I used Sugar300".
  6. Power cord C14.
  7. RJ12 Cable.
  8. Resistor 510 ohm *3
  9. Resistor 10K ohm.

Software you need:

Newer Arduino IDE"i used Arduino 1.6.9 version and we will need to install an adds-on for ESP8266.

Step 2: Software Add Library Manager

To add ESP8266 board to your arduino IDE do the following:

  1. Go to File >> Performance and copy this URL into the “Additional Board Manager URLs" box:
    http://arduino.esp8266.com/stable/package_esp8266com_index.json
  2. Go to Sketch >> include library >>Manage library and search for ESP8266 and install it.
  3. You can see now a new Boards installed as in the picture.
  4. We will select Generic ESP8266.

Step 3: Prepare the Hardware

The biggest problem with ESP8266 is how to connect it, you can use Solderless jumper and connect it with the Male ESP8266, or make a female pinheader connection to fit the breadboard.

But I designed my own PCB to connect the ESP8266 Module easily.

In my experience I faced a different connection for this module to program it using arduino board.

First i remove the ATMEGA328P from Uno board, the connection will be like this:

ESP8266 >>> Arduino

Tx >>> D1"Tx"

Rx >>> D0"Rx"

RST >>> RST"with 10K ohm in series"

CH_PD >>> 3.3V

GPIO 0 >>> Pull down Resistor 510 ohm

VCC >>> 3.3V

GND >>> GND


Here is the trick, most tutorial on the internet tell you to connect TX to Rx and vise versa, that's right with FTDI USB module, with arduino UNO works as a programmer tou must connect it as I mentioned above.

The RST pin, in most case you connect it with VCC directly ,from my experience connect it with arduino UNO RST make the programming stable,so it's up to you to choose any way of connection you want.

Arduino UNO here is a programmer only, and because of that we need another arduino Board to take the signal from WiFi module and control Sugar device.

The Arduino Nano used to control Sugar and you can see the connection also in attachment

Step 4: You Love Sugar Device ? We Need Your Help

Sugar is a startup project , designed and Manufactured the Prototypes in Taipei - Taiwan and Now sugar need your help to become a real product in the market and bring a simple tools to control AC voltage.

Sugar comes in 2 different type:

  • Sugar300 : can Handle Up to 300 W
  • Sugar1000 : can Handle Up to 1000 W

and both type support AC output voltage control "For Dimmer applications" Hope you support us and become our backer Now on Indiegogo Campaign you can visit our campaign page https://igg.me/at/sugdev/x .

Keep in touch with us on:

Facebook: https://www.facebook.com/SugarWorld.net/
Twitter: https://twitter.com/Sugar_Device

Instagram: https://www.instagram.com/sugardevice/

Youtube official accounts: https://www.youtube.com/channel/UCDthbcEGR-r0NB49p...

Visit Our website to see all features and tutorial and see the story behind sugar

http://www.sugarworld.net

http://www.sugarworld.net/story-behind-sugar

http://www.sugarworld.net/features

Step 5: Assembly and Code Test

Here some photos for the connection.

the sugar device connection is very easy,just connect the Lamp to sugar output socket,power on the device using C14 power cable,and finally the RJ12 Cable to send the controlling signal from Arduino.

This code for ESP8266 :

<p>/*<br> *  This sketch demonstrates how to set up a simple HTTP-like server.
 *  The server will set a GPIO pin depending on the request
 *    <a href="http://server_ip/gpio/0" rel="nofollow"> http://server_ip/gpio/0 </a> will set the GPIO2 low,
 *    <a href="http://server_ip/gpio/1" rel="nofollow"> http://server_ip/gpio/0 </a> will set the GPIO2 high
 *  server_ip is the IP address of the ESP8266 module, will be 
 *  printed to Serial when the module is connected.
 */</p><p>#include </p><p>const char* ssid = "xxxxxxxxxx";
const char* password = "xxxxxxxxxx";</p><p>// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);</p><p>void setup() {
  Serial.begin(115200);
  delay(10);</p><p>  // prepare GPIO2
  pinMode(2, OUTPUT);
  digitalWrite(2, 0);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  Serial.println("Server started");</p><p>  // Print the IP address
  Serial.println(WiFi.localIP());
}</p><p>void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }</p><p>  // Set GPIO2 according to the request
  digitalWrite(2, val);
  
  client.flush();</p><p>  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n
\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</p><p>\n";</p><p>  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");</p><p>  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}</p>

And this is a code for Arduino Nano:

<p>/*<br>written by Mohannad Rawashdeh (Raw)
*/
const int SugarPin=9;
const int SugarON =8;
const int SugarOFF=7;
const int delayTime=2000;
void setup() 
{
// define arduino PWM as an output
pinMode(SugarPin,OUTPUT);
//define Button as an input with PullUp resistor
pinMode(SugarON ,INPUT_PULLUP);
pinMode(SugarOFF,INPUT_PULLUP);
}</p><p>void loop() {
  // read button 
  boolean ButtonON =digitalRead(SugarON);
  boolean ButtonOFF=digitalRead(SugarOFF);
  //check if the button pushed 
  if(ButtonON==false)
  {
    // turn on the device
    // to turn On the device, send High pulse with 500uSec  
   analogWrite(SugarPin,64);    
   delay(delayTime);   
  }
  if(ButtonOFF==false)
  {
    // turn off the device    
   // to turn off the device, send High pulse with 100uSec  
   analogWrite(SugarPin,12);    
   delay(delayTime);
  }  
  //delay between each reading
  delay(50);
}</p>

All you need to do is :

  • After you connect the ESP8266 , from serial monitor you can obtain IP address.
  • then go to your web browser and print this URL:
    yourIP/gpio/1

yourIP/gpio/0

You can modifiy the code to control any device you want and integrate it with you smartphone application..