/

Arduino project: Controlling the built-in LED using your browser

Arduino project: Controlling the built-in LED using your browser

In this tutorial, we will build upon the Arduino Web Server example to enable controlling the built-in LED on the Arduino board through commands sent from a web browser.

To achieve this, we will turn on the LED by accessing the /on URL and turn it off by accessing the /off URL. Any other URL will have no effect on the LED.

Let’s start with the code from the previous tutorial:

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
#include <SPI.h>
#include <WiFiNINA.h>

WiFiServer server(80);

void setup() {
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

Serial.begin(9600);
while (!Serial);

int status = WL_IDLE_STATUS;
while (status != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(5000);
}

Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.begin();
}

void loop() {
WiFiClient client = server.available();
if (client) {
String line = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);

if (c != '\n' && c != '\r') {
line += c;
}

if (c == '\n') {
if (line.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("test");
client.println("</html>");
break;
} else {
line = "";
}
}
}
}

client.stop();
}
}

In the else block at the end of the code, we have a complete line that we can check for its content before clearing it. Here, we can check if it starts with GET /on or GET /off to determine the command we are requested to perform:

1
2
3
4
5
6
7
8
9
10
String command = "";

/* ... */

if (line.startsWith("GET /on ")) {
command = "on";
}
if (line.startsWith("GET /off ")) {
command = "off";
}

When we are ready to send the response back, we can check the command and turn the LED on or off accordingly:

1
2
3
4
5
if (command == "on") {
digitalWrite(LED_BUILTIN, HIGH);
} else if (command == "off") {
digitalWrite(LED_BUILTIN, LOW);
}

We can also send a confirmation response back to the client with:

1
client.println("Turned the LED " + command);

That’s it! Now upload the code to the Arduino and access the /on or /off URLs in your browser.

If you want to assign a static IP to the Arduino on your local network, you can do so through your router’s settings. In my case, I have added an entry for arduino.local in my /etc/hosts file, so I can access the Arduino using http://arduino.local/on to turn the LED on, and http://arduino.local/off to turn it off.

Here’s the complete program:

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
69
70
71
72
73
74
#include <SPI.h>
#include <WiFiNINA.h>

WiFiServer server(80);

void setup() {
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

Serial.begin(9600);
while (!Serial);

int status = WL_IDLE_STATUS;
while (status != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(5000);
}

Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.begin();
}

void loop() {
WiFiClient client = server.available();
if (client) {
String line = "";
String command = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);

if (c != '\n' && c != '\r') {
line += c;
}

if (c == '\n') {
if (line.length() == 0) {
if (command == "on") {
digitalWrite(LED_BUILTIN, HIGH);
} else if (command == "off") {
digitalWrite(LED_BUILTIN, LOW);
}

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Turned the LED " + command);
client.println("</html>");
break;
} else {
if (line.startsWith("GET /on ")) {
command = "on";
}
if (line.startsWith("GET /off ")) {
command = "off";
}

line = "";
}
}
}
}

client.stop();
}
}

Tags: Arduino, Web Server, LED control