Thursday, March 31, 2016

LED On/OFF Server

LED On/Off Control Server
In this demo we will see our first real IoT control application, here we are controlling on board LED of ESP-01 Module using web server. Basically internet of thing is used to monitor and control the devices, this is our basic control application it can be extended to control multiple home appliances and monitor temperatures and various parameters of house. code is kept as simple as possible to make it useful for many applications and understanding.

Step 1: Hardware Setup
1. ESP-01 Module (you can use ESP-12 also)
2. 3.3V Power Supply for ESP-01 Module
3. Programming setup (USB to Serial Converter)

Circuit Connections:

Step 2: Programming

Design your web page as per your requirements.

HTML Webpage Code stored as header file mainPage.h:

Save this as mainPage.h
const char MAIN_page[] PROGMEM = R"=====(
<HTML>
<TITLE>
REMOTE LED ON/OFF CONTROL
</TITLE>
 <BODY>
  <FORM method="post" action="/form">
  <INPUT TYPE=SUBMIT name=submit VALUE="ON">
  <INPUT TYPE=SUBMIT name=submit VALUE="OFF">
  LED Status: @@LED@@
  </FORM>
 </BODY>
</HTML>
)=====";

ESP-01 Code LEDControl.C:

/*
 * Copyright (c) 2015, Circuits4You.com
 * All rights reserved.
/* Create a WiFi access point and provide a web server on it. */

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>

#include "mainPage.h"

/* Set these to your desired credentials. */
const char *ssid = "LEDServer";
const char *password = "password";

ESP8266WebServer server(80);
String LEDStatus;
//=======================================================================
//                    handles main page 192.168.4.1
//=======================================================================
/* Just a little test message.  Go to http://192.168.4.1 in a web browser
 * connected to this access point to see it.
 */
void handleRoot() {
  String s = MAIN_page;    
  s.replace("@@LED@@", LEDStatus);
  server.send(200, "text/html", s);    
}

//=======================================================================
//                    Handle Set Date/Time Settings
//=======================================================================
void handleForm() {
  String t_state = server.arg("submit");
  
//Change LED State as per request
  if(t_state=="ON")
  {
    LEDStatus="ON";    
    digitalWrite(LED_BUILTIN, LOW);       //LED Turned on
  }
  
  if(t_state=="OFF")
  {
    LEDStatus="OFF";    
    digitalWrite(LED_BUILTIN, HIGH);      //LED Turned off  
  }

  server.sendHeader("Location", "/");
  server.send(302, "text/plain", "Updated-- Press Back Button");  //This Line Keeps It on Same Page
   
  delay(500);
}
//=======================================================================
//                    Power on setup
//=======================================================================

void setup() {
  delay(1000);
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  server.on("/", handleRoot);
  server.on("/form", handleForm);
 
  server.begin();
  pinMode(LED_BUILTIN, OUTPUT);
}

//=======================================================================
//                    Main Program Loop
//=======================================================================
void loop() {
  server.handleClient();
}
//=======================================================================

Step 3: Uploading Code
1. To upload code on ESP-01 Press GPIO0 Switch to make it Low during power on or reset. to put it in programming mode.

2. During programming you will see the Blue LED Blinking.

Step 4: Testing
1. Turn on WiFi on phone or Laptop
2. Connect to LEDServer Network that is from ESP-01
3. Open Web Browser enter IP: 192.168.4.1
4. Press On/Off button on webpage you see
that's it.

Conclusion:
Now you have clear concept of IOT possibilities, this code is made such a way that you can have bidirectional communication from browser to ESP-01.

No comments:

Post a Comment