TechGuru
Published

How to Control LED from Simple Web Page Wirelessly

In this IoT project, an LED light can be controlled wirelessly from a simple html page. This project is for all students, doers or makers.

BeginnerFull instructions provided3 hours10,530
How to Control LED from Simple Web Page Wirelessly

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
LED (generic)
LED (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Board pin mapping

Code

Code

Arduino
Make sure wifi board is connected to correct port.
 //This example will set up a static IP - in this case 192.168.1.99
 
#include <ESP8266WiFi.h>

//////////////////////
// WiFi Definitions //
//////////////////////
const char WiFiAPPSK[] = "12345678";
 
int ledPin = D3;
const int ANALOG_PIN = A0; // The only analog pin on the Thing
const int DIGITAL_PIN = D3; // Digital pin to be read -> Just for testing

WiFiServer server(80);

IPAddress apIP(192, 168, 1, 99); // where xx is the desired IP Address
//IPAddress gateway(192, 168, 1, 1); // set gateway to match your network

void setup() {
  Serial.begin(115200);
  delay(10);
 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
 
  Serial.print(F("Setting static ip to : "));
  Serial.println(apIP);

  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));   // subnet FF FF FF 00
  WiFi.softAP("espHotSpot", WiFiAPPSK); //- See more at: http://www.esp8266.com/viewtopic.php?f=29&t=12124#sthash.bE1emnEw.dpuf
  
  // Connect to WiFi network
  Serial.println();
 
//  // Start the server
  server.begin();
  Serial.println("Server started");
}
 
void loop() {
 
 // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();

  // Match the request
  int val = -1; // We'll use 'val' to keep track of both the
                // request type (read/set) and value if set.

  Serial.print("For 0: ");
  Serial.println(req.indexOf("/0"));
  Serial.print("For 1: ");
  Serial.println(req.indexOf("/1"));
                
  if (req.indexOf("/status1=0") != -1 || req.indexOf("/gpio/0") != -1)
    val = 1; // Will write LED High
  else if (req.indexOf("/status1=1") != -1 || req.indexOf("/gpio/1") != -1)
    val = 0; // Will write LED Low
  else if (req.indexOf("/read") != -1)
    val = -2; // Will print pin reads
  // Otherwise request will be invalid. We'll say as much in HTML

  // Set GPIO5 according to the request
  if (val >= 0)
    digitalWrite(ledPin, val);

  

  // Prepare the response. Start with the common header:
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  // If we're setting the LED, print out a message saying we did
  if (val >= 0)
  {
    s += "LED is now ";
    s += (val)?"on":"off";
  }
  else if (val == -2)
  { // If we're reading pins, print out those values:
    s += "Analog Pin = ";
    s += String(analogRead(ANALOG_PIN));
    s += "<br>"; // Go to the next line.
    s += "Digital Pin 12 = ";
    s += String(digitalRead(DIGITAL_PIN));
  }
  else
  {
    s += "Invalid Request.<br> Try /led/1, /led/0, or /read.";
  }
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(100);
  Serial.println("Client disonnected");
client.flush();
  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
 
}

Web Page and scripts

HTML
Make sure index.html, images folder and jquery.min.js are in same folder extracted.
No preview (download only).

Credits

TechGuru

TechGuru

8 projects • 13 followers
We provide Science,School and Technology projects(Working Real Time Models) for your requirements. YOU HAVE A BUDGET, WE HAVE A PROJECT !

Comments