wifiwebserver

ESP8266 ESP01 blink  WifiLedOnWebsiteESP8266
Shopping list
– 1 ESP8266 (for example version ESP01)
– 1 led (I use a red one)
– 1 USB to TTL (I use FT232RL)
– 2 push buttons
– 10 wires
– 1 breadboard
– a 3.3V power source (I use an battery in this example, power from your USB TTL will most likely might NOT work)

Hardware
The setup is the same as in the simple example blink
TTL to ESP01 blink upload and reset button

USB TTL ESP8266 ESP-01
—————————
GND — GND
TX — RX
RX — TX

———————–
+3.3V — CH_PD
pushbutton — GPI0
resetbutton — RST
battery – — GND
battery + — VCC
led,longleg — GPIO2
led,shortleg — GND

Coding

Copy this code. Change ssid into your wifi accesspoint, and change the password into yours.
and compile.

ledPin = 2 means that we are using GPIO2.

 

[cpp]
#include <ESP8266WiFi.h>

const char* ssid = "your-ssid";
const char* password = "your-password";

int ledPin = 2; // GPIO2
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("");

}
[/cpp]

Download source here: WifiWebServer

Uploading
Press reset button while holding flash button pressed. In other words. 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.
Press upload in the Arduino IDE, arduino will compile and upload. After the sketch is uploaded, the led will blink for one second on and off.

Testing
Open the serial monitor from the menu tools. You will see the URL

Serial Monitor ESP8266 WifiServer

The strange characters on the first line after a reset is normal.
Copy the URL from the serial monitor into your browser.

WifiLedOnWebsiteESP8266

Try to click on and the light will go on (or off)

 

When you have uploaded the sketch, you may remove all the wires required for uploading.
This is what is left.

ESP8266 ESP01 blink

 

32 thoughts on “wifiwebserver

    1. I got compiler errors when I pasted this code. I eliminated them by retyping lines 65 and 69, but I’m still left with these errors:

      Assembler messages:
      Fatal error: can’t create core_esp8266_si2c.c.o: Permission denied

      Error compiling.
      C:\Users\Dick\AppData\Roaming\Arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\1.20.0-26-gb404fb9\bin\xtensa-lx106-elf-ar: core_esp8266_si2c.c.o: No such file or directory
      Error compiling.

      I suggest you provide a link to the code to avoid the pasting problem (always the best way to eliminate errors).

      Don’t know what to do about the remaining errors.

      By the way, I’m using version 1.6.5 of the Arduino IDE on a PC, and I have the generic ESP8266 module selected.

      1. >I suggest you provide a link to the code to avoid the pasting problem
        Good suggestion. I have added a link to the source code.
        [Next to that I slightly changed the source code It was not compiling anymore under 1.6.5.]

    2. I have your program running on a nodemcu dev board which uses a esp8266-12.

      I would like to make the webserver operate several outputs, not just the one in the existing sketch.
      could you point me in the right direction on how to accomplish this?
      any help would be appreciated.

    3. Hi, I’m trying to learn how to use esp8266. I have a question for you, there is necessity of an html page to run this sketch ? Thank you fot attention, and sorry for my english.

    4. Hey, thanks for this amazing tutorial!
      got it all working as required however I am wanting to tack on a physical push button that is in sync with web server. Would this be something you’d be able to do (as I’m not the best at coding) or would you have any suggestions?
      Thanks!

  1. The LED is ON and then OFF

    I change code from line 62 from

    // Match the request
    int value = LOW;
    if (request.indexOf(“/LED=ON”) != -1) {
    value = HIGH;
    }

    // Set ledPin according to the request
    digitalWrite(ledPin, value);

    to

    // Match the request
    int value;
    if (request.indexOf(“/LED=ON”) != -1) {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
    } else if(request.indexOf(“/LED=OFF”) != -1){
    digitalWrite(ledPin, LOW);
    value = LOW;
    }

    // Set ledPin according to the request
    //digitalWrite(ledPin, value);

    1. Hello,
      I am getting an error while uploading the program.
      Error is : stray ‘\’ in program

      if (request.indexOf(“/LED=ON”) != -1)
      {
      digitalWrite(ledPin, HIGH);
      value = HIGH;
      }
      else (request.indexOf(“/LED=OFF”) != -1)
      {
      digitalWrite(ledPin, LOW);
      value = LOW;
      }

      in this part of code error occurs,
      Please help…

  2. hI, after several minutes of normal operation , the Web server is no longer accessible even if the ESP is connected to the access point ( see it on the list of active devices ) .
    If the restart king begins to function properly.
    What can be ?

  3. I followed each n every step. While uploading is done..I start the serial monitor. It appears blank. When I press reset (reset pin of ESP is connected to ground for a sec and released.)then some ghost text appears.. But no IP address..

    no IP address is returned in Serial. Please help.

  4. my project is working fine… when i open web link i am able to ON and OFF Led also….
    but font size on that web link is very small…..
    how to increase the size of font ?
    if i want to access through any android application…. it possible ?

Leave a Reply