ESP8266 Server

The following example slightly modified from the example that comes with the Arduino package, sets up a simple web server the ESP8266 that accepts commands to turn on and off an LED attached to the board.

Assumes that you have followed the ESP8266 Getting Started Guide and can successfully upload the Blink program.


/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://xxx.xxx.xxx.xxx/gpio/0 will set the GPIO2 low,
* http://xxx.xxx.xxx.xxx/gpio/1 will set the GPIO2 high
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*
* Example from ESP8266 core for Arduino

*https://github.com/esp8266/Arduino
* Modified by: Benn Thomsen, 2 Dec 2015
*/

#include <ESP8266WiFi.h>

//#define PIN 0 // Corresponds to GPIO0 labelled pin D3 on NodeMCU board
//#define PIN 12 // Corresponds to GPIO2 labelled pin D4 on NodeMCU board this pin is also connected to the LED cathode on the module
#define PIN 16 // Corresponds to GPIO16 labelled pin D0 on NodeMCU board this pin is also connected to the LED cathode on the NodeMCU board

const char* ssid = "Your-SSID";
const char* password = "Password";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
Serial.begin(115200);
delay(10);

// prepare GPIO PIN
pinMode(PIN, OUTPUT);
digitalWrite(PIN, 0);

// 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.println(WiFi.localIP());
}

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 req = client.readStringUntil('\r');
Serial.println(req);
client.flush();

// Match the request
int val;
if (req.indexOf("/gpio/0") != -1)
val = 0;
else if (req.indexOf("/gpio/1") != -1)
val = 1;
else {
Serial.println("invalid request");
client.stop();
return;
}

// Set GPIO2 according to the request
digitalWrite(PIN, val);

client.flush();

// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO ";
s += PIN;
s += " is now ";
s += (val)?"high":"low";
s += "</html>\n";

// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disconnected");

// The client will actually be disconnected
// when the function returns and 'client' object is destroyed
}

 

Then click Upload

If you get the following warning and the code fails to upload.

warning: espcomm_sync failed
error: espcomm_open failed

You will need to put the board into flash mode manually. To do this press and hold the Flash button on the NodeMCU board then press and release the Reset button, finally release the Flash button. You should then be able to successfully upload the code.

If you then open the serial monitor you should see the NodeMCU trying to connect to your wireless router. When connected it will print the IP address that it has been assigned by the router. If you put this IP address into the xxx.xxx.xxx.xxx placeholders in the browser addresses that appear in the comments at the top of the code, also shown below

http://xxx.xxx.xxx.xxx/gpio/0 will set the GPIO2 low
http://xxx.xxx.xxx.xxx/gpio/1 will set the GPIO2 high

You will be able to turn the red LED on the NodeMCU board on and off. Note the anode on the red LED is pulled up to Vcc so setting pin16 high will actually run the LED off and conversely setting pin16 low will turn it on. See note below about the LEDs on the NodeMCU board.

NodeMCU on board LEDs

The NodeMCU board and the ESP8266MOD modules from AI-THINKER both have LEDs on them. The blue LED near pin 22 on the ESP8266MOD-12E module is connected between Vcc and GPIO2 (TXD1) labelled D4 on the NodeMCU 1.0 Board. The Blue (or Red) LED on the NodeMCU Board is connected between Vcc and GPIO16 labelled D0 on the NodeMCU 1.0 Board as shown on the schematic for the NodeMCU 1.0 Board

NodeMCU pin functions

nodemcu_pinout_700-2

NodeMCU 1.0 board pin out. Credit Alex Bloggt