Controlling LED with Web-server using NodeMCU/ESP8266/Arduino MRK100

Here the main objective is to control an LED light with NodeMCU/ESP8266 or Arduino MKR1000 using WiFi. ESP8266 or any other wireless connected board will act as a web server. If you are using NodeMCU you can use the inbuilt LED to test this no other hardware is required for this setup.

In this blog, I’m using NodeMCU developer board. We will connect the NodeMCU to the home WiFi router using SSID and password. NodeMCU will be acting as a web server when it is connected to WiFi. To get the IP address of the server we need to look at the serial monitor of the Arduino IDE.

Here I have connected LED to D7 of NodeMCU which is referred as GPIO13 on the board. We have mapped to pin 13 in Arduino program.

 

After uploading the code to NodeMCU to get the IP address look at the serial monitor. If everything goes fine you will get an IP address as shown in the figure below.

 

Note: Please change the SSID and Password.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include ESP8266WiFi.h;
const char* ssid = "YOUR WIFI SSID";
const char* password = "YOUR WIFI PASSWORD";
int ledPin = 13; // GPIO13
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("");
client.println(""); client.print("Led pin is now: "); if(value == HIGH) { client.print("On"); } else { client.print("Off"); } client.println(""); client.println("<button>Turn On </button>"); client.println("<button>Turn Off </button>"); client.println("");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}

Products from Amazon.com

Leave a Reply