ESP8266 ESP-01 Webserver © LGPL

ESP8266 ESP-01 chip provides a really low cost and effective way to communicate over the internet.

  • 63,995 views
  • 15 comments
  • 97 respects

Components and supplies

Apps and online services

About this project

In this tutorial we use an Esp8266 ESP-01 chip which is really cheap yet really effective while communicating over the internet. Refer my previous tutorial to see how to get started with the ESP-01 Wi-Fi module, configure it, and verify that there is communication established between the module and another device without using a USB to TTL converter . ESP8266 Baisics.

In this tutorial we are using an USB to TTL converter to program the Esp8266 ESP-01 . Here we can use Arduino IDE to develop the web server to control an led remotely.

Hardware Setup

We can either use a usb to ttl converter or use an arduino to program the the Esp8266. Refer the following diagram and set up the hardware accordingly. Here there are three methods you can follow one which suits you the most.

USB to TTL Converter with DTR PIN

If your using an usb to ttl converter which has an DTR pin then uploading will go smoothly. Please be informed that Serial monitor will not work anymore.

USB TTL------ ESP8266 ESP-01

GND-----------GND

TX--------------RX

RX--------------TX

RTS------------RST

DTR-----------GPIO0

USB to TTL Converter Without DTR PIN

For USB TO TTL converter without DTR pin we have to use manual flashing .For this we must use two push buttons. Refer the following diagram

USB TTL---------------- ESP8266 ESP-01

GND-----------------------GND

TX---------------------------RX

RX---------------------------TX

Reset Button------------RST

Flash Button------------GPIO0

While uploading the code press flash button, keep the flash button pushed while you once click on reset. You may now release the flash button. The ESP8266 is now in flash mode. You are able to upload the sketch.

Using Arduino Uno to Flash the code to ESP8266

You can use arduino uno for flashing code to ESP8266 ESP-01 .

Arduino---------------- ESP8266 ESP-01

GND-----------------------GND

TX---------------------------TX

RX---------------------------RX

Reset Button------------RST

Flash Button------------GPIO0

While uploading the code follow the same procedure to keep the flash button pushed while you once click on reset. and release the flash button.

Uploading

Use any of the above suitable method and now open the Arduino IDE and select choose your ESP8266 board from Tools > Board > Generic ESP8266 Module

Now copy the below given code to Arduino IDE and press upload button. Change ssid into your wifi accesspoint, and change the password into yours. and compile.

Open the serial monitor and open the URL shown in your serial monitor

Open the URL and and connect the GPIO 2 of ESP8266 to longer lead of led. Now you can control the Led remotely by using the internet.

Remove all the wires which were required for uploading .Lm117 is used to provide regulated 3.3 output.

Comments and Questions welcome! Thanks for reading.

Code

ESP_web.inoC/C++
#include <ESP8266WiFi.h>
 
const char* ssid = "YOUR_SSID";//type your ssid
const char* password = "YOUR_PASSWORD";//type your password
 
int ledPin = 2; // GPIO2 of ESP8266
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
   
  // 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");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
    
}
 
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 request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
   
  // Match the request
 
  int value = LOW;
  if (request.indexOf("/LED=ON") != -1) {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
  } 
  if (request.indexOf("/LED=OFF") != -1){
    digitalWrite(ledPin, LOW);
    value = LOW;
  }
 
// Set ledPin according to the request
//digitalWrite(ledPin, value);
   
 
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
   
  client.print("Led pin is now: ");
   
  if(value == HIGH) {
    client.print("On");  
  } else {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("Click <a href=\"/LED=ON\">here</a> turn the LED on pin 2 ON<br>");
  client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 2 OFF<br>");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
 
}

Comments

Similar projects you might like

LoRa Gateway for DeviceHive

Project tutorial by DeviceHive IoT team

  • 846 views
  • 2 comments
  • 16 respects

Pac-Man LED Pixel Panel Costume

Project tutorial by Ben Muller

  • 3,987 views
  • 3 comments
  • 69 respects

SmartWay

Project tutorial by Universum

  • 103 views
  • 0 comments
  • 3 respects

Raspberry Pi and Arduino Laptop

Project tutorial by Dante Roumega

  • 17,235 views
  • 6 comments
  • 42 respects

Arduino-Based Automatic Guitar Tuner

Project tutorial by Ben Overzat

  • 2,124 views
  • 0 comments
  • 10 respects

Really Smart Box

Project tutorial by Stephen Harrison

  • 2,665 views
  • 0 comments
  • 8 respects
Add projectSign up / Login