Wemos webserver example

In this example we will create a basic webserver example using our Wemos, it will connect to your Wifi network and then you will navigate to a URL and a basic page will appear. This page will display 2 links , one will switch on an led connected to D5 and the other option will switch the led off.

Wemos D1 ESP8266 based board

Wemos D1 ESP8266 based board

Parts

1 x Wemos D1 or D2
1 x USB cable
1 x LED and resistor or use a module

Code

#include <ESP8266WiFi.h>
 
const char* ssid = "ssid name";
const char* password = "ssid password";
 
int ledPin = D5;
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 : ");
  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;
  }
 
 
 
  // 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 5 ON<br>");
  client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 5 OFF<br>");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");
 
}

Results

Open the serial monitor , all going well and you will see the IP address and messages like the following

wemos ip address

wemos ip address

Using your favourite web browser navigate to the IP above

wemos browser

wemos browser

 

 

Update

I tried this out and it uses a static IP address

//This example will set up a static IP - in this case 192.168.1.99
 
#include <ESP8266WiFi.h>
 
const char* ssid = "ssid name";
const char* password = "ssid password";
 
int ledPin = D5;
WiFiServer server(80);
IPAddress ip(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(ip);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
  WiFi.config(ip, gateway, subnet); 
  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 : ");
  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;
  }
 
 
 
  // 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 5 ON<br>");
  client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 5 OFF<br>");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");
 
}

 

32 comments to Wemos webserver example

  • GUY

    GUY
    29 05 2016
    Merci pour ce post

  • Paul

    Will this only work over a local network or over the internet?

  • Andrew

    I used this code, but it is backwards for me. When it says OFF it is ON. ?? I am using the built in LED on D4.
    Thoughts?

  • Vladimir

    Thank You, it works well

  • hi

    is there at way to make it with a static address like 192.168.0.205 ???

    i wish to make it run with a StepMotor and NeoPixels but i cant make it work…. only if i dont use the wifi, but i need the wifi.

    • shedboy71

      I’ve added an example which seemed to work for me but basically you need to add the following

      // config static IP
      IPAddress ip(192, 168, 1, xx); // where xx is the desired IP Address
      IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
      Serial.print(F(“Setting static ip to : “));
      Serial.println(ip);
      IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
      WiFi.config(ip, gateway, subnet);

  • Harp

    What chip and what program are you using because the following line will not compile.
    Serial.print(F(“Setting static ip to : “));
    When I remark it out the compiler (Arduino with the basic 8266 chip) works.

  • Tommy LiYY

    Hi, I don’t know why it will automatically disconnected when i trying to turn on/off the led.

    “connected to wifi
    ……………………
    WiFi connected
    Server started
    Use this URL : http://192.168.137.73/
    new client
    GET / HTTP/1.1
    Client disconnected

    new client
    GET /favicon.ico HTTP/1.1
    Client disconnected

  • Jim

    What security does this library understand? I can get this to connect to my iPhone’s hotspot, but not to my access point, It prints “Connecting to SSID” and dots forever. I have moved the Wemos within 10 feet of the AP. Any other reason besides security mismatch this would not work? Cisco 2600 series AP using only the 2.4GHz channels.

    Thanks, Jim

    • shedboy71

      According to the library code in ESP8266WiFiScan.cpp

      case AUTH_OPEN:
      case AUTH_WPA_PSK:
      case AUTH_WPA2_PSK:
      case AUTH_WPA_WPA2_PSK:

      This has always worked out of the box for me, only thing I can suggest is to try some of the other examples that come if you setup support of the ESP8266 in the Arduino IDE. I have read about people having issues and having to disconnect before then trying to connect.

    • Karim

      Mine did that. If fixed as soon as I realized the SSID is case sensitive.

  • Johan

    Where can i find the ZIP files for the ESP8266mDNS and WiFiUdp libraries. Thanks for your help!

  • Alex Lopes

    HI, thanks for the code. I have an issue when I leave the board energized for a while (like 1 or 2 hours) without any connection. It simply stops to answer http requests in the IP address. I can ping the IP so it means it is there but the page never opens. Any idea on what could be happening?
    Thanks

  • Newbie

    Dear reader,

    My serial monitor prints in the first line:

    sd$ÜŸ|là<Œläc|�Ãä›r“#Œc„û ….. etc.

    What could be the problem?

    The code works fine otherwise.

    • shedboy71

      Double check the baud rate settings – bottom right corner of the Serial Monitor Window

      • Newbie

        Thanks for the quick response.

        But I think it’s not the problem.

        – void setup() {
        Serial.begin(115200);

        – Tools → Upload speed: 115200

        – Bottom right corner of the Serial Monitor Window: 115200

        Do you have another suggestion?

  • Newbie

    Another question.

    What those this line do?

    – Serial.print(F(“Setting static ip to : “));

    On the site from Arduino they say:

    You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :

    Serial.print(F(“Hello World”))

    To send a single byte, use Serial.write().

  • Hassan

    Q: I get this, now what to do?

    Connecting to EHDT26

    Exception (2):
    epc1=0x3ffe8450 epc2=0x00000000 epc3=0x00000000 excvaddr=0x3ffe8450 depc=0x00000000

    ctx: cont
    sp: 3ffef820 end: 3ffefce0 offset: 01a0

    >>>stack>>>
    3ffef9c0: feefeffe feefeffe feefeffe feefeffe
    3ffef9d0: feefeffe feefeffe feefeffe feefeffe
    3ffef9e0: feefeffe feefeffe feefeffe feefeffe
    3ffef9f0: feefeffe feefeffe feefeffe feefeffe
    3ffefa00: feefeffe feefeffe feefeffe feefeffe
    3ffefa10: feefeffe 00000004 3ffefa70 3ffefa81
    3ffefa20: 40107020 00000000 3fff11e8 00000484
    3ffefa30: 3ffefc17 feefeffe 000000a5 feefeffe
    3ffefa40: feefeffe 00000000 00000000 3ffef9d0
    3ffefa50: feefeffe feefeffe feefeffe feefeffe
    3ffefa60: 3ffefa80 3ffefa70 00000004 feefeffe
    3ffefa70: feefeffe 3ffe8564 3fff11e8 00000068
    3ffefa80: 000000a5 3fff0d64 feefeffe feefeffe
    3ffefa90: feefeffe feefeffe feefeffe 4010031d
    3ffefaa0: feefeffe 00001028 3ffeec64 40100426
    3ffefab0: feefeffe feefeffe feefeffe 40100537
    3ffefac0: feefeffe 00001000 00000484 401006f5
    3ffefad0: 00000484 00001000 000003fd 4010724c
    3ffefae0: 40004b31 3ffefb10 0000001c 402244cd
    3ffefaf0: 40105ea2 402245b5 3fff0d64 000003ff
    3ffefb00: 000003fd 3ffefc17 3fff0d64 000003fd
    3ffefb10: ffffff00 55aa55aa 00000010 0000001c
    3ffefb20: 0000001c 00000013 00000013 000003ff
    3ffefb30: 402249a4 3fff0d64 3fff0d64 000000ff
    3ffefb40: 00000001 3ffefc37 40224aff 00000008
    3ffefb50: 3fff0d64 000000ff 3ffefc17 00000000
    3ffefb60: 3fff0e24 3ffefc78 00000001 40224b8c
    3ffefb70: 3ffefc17 3fff0d64 00000000 00000004
    3ffefb80: 3ffefc37 3fff709c 3fff0d64 00000000
    3ffefb90: 40224bc8 3ffe8414 3ffe8424 feefeffe
    3ffefba0: 402027ea 3ffe8424 3ffe8414 4020272a
    3ffefbb0: 00000000 feefeffe 00000000 4000050c
    3ffefbc0: 401041bc 00040000 00000000 00000000
    3ffefbd0: 00000000 00000000 0000001f 40105d55
    3ffefbe0: 4000050c 40103196 4010094e 00000030
    3ffefbf0: 40227116 3fff0571 3fff03cc 3fffdad0
    3ffefc00: 40227f3b 00000023 00000001 00000001
    3ffefc10: ffffff00 45ffffff 32544448 feef0036
    3ffefc20: 00000018 feefeffe feefeffe 0001c200
    3ffefc30: 0000001c 68000000 61737361 6965656e
    3ffefc40: 36323632 00000000 3ffe86e5 4020382c
    3ffefc50: 00000000 00000005 3ffe8429 3ffeecb0
    3ffefc60: 3ffe8370 00000006 3ffeec84 40202ed5
    3ffefc70: 3ffe86e4 00fe8368 3ffeec84 40202ed5
    3ffefc80: 3ffeeb40 00000000 00000001 40203904
    3ffefc90: 402010ae 0000000a 3ffeec84 3ffeecb0
    3ffefca0: 3ffe8370 3ffeeb40 3ffeec84 40202461
    3ffefcb0: feefeffe feefeffe feefeffe feefeffe
    3ffefcc0: 3fffdad0 00000000 3ffeeca9 40202fcc
    3ffefcd0: feefeffe feefeffe 3ffeecc0 40100958
    <<<stack<<<

    ets Jan 8 2013,rst cause:2, boot mode:(1,6)

    ets Jan 8 2013,rst cause:4, boot mode:(1,6)

    wdt reset

    • shedboy71

      I’ve seen this researching the issue on the wemos forum

      As a solution I put:
      WiFi.persistent(false);
      in the beginning of setup() and lo-behold!! Crashes no more!

      Try that

  • Steve

    First class Many thanks

  • Liketoplay

    Thank you so much. My first wifi program!
    Do you know WHY I have to change the baud rate to 9600? Otherwise I get rubbish (B�L ��#�#��).
    I use Arduino IDE 1.8.0. The “Get board info” says BN: Unknown board, VID: 1A86, PID: 7523. How can I solve this?
    I miss a lot of information for the D1 I just bought in november (seems to be a R1). On http://www.wemos.cc there is almost no information. I tried to post a message to the forum on wemos.cc, but you have to wait 24 hours to post your first message. I think the wemos products won’t be a great success if they do it like that.. So thanks again for your great support.

  • […] des Boards findet man hier), WLAN ist an Bord, außerdem ein einfach zu bedienender Webserver. Die erste Demo (Steuerung einer LED per WLAN) war mit guter Anleitung in unter 15 Minuten auf dem Schreibtisch […]

  • ABDALLAH

    thank you but can i use more pins mean d3,d2,d1

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

*