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

In this tutorial I want to expand the Arduino Web Server example to allow us to send commands to the Arduino via the browser.

Let’s do this: we light up the built-in LED on the Arduino by reaching out to the /on URL, and we turn it off by opening the /off URL. Anything else does nothing.

This is the code from the other tutorial:

#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");  // the connection will be closed after completion of the response
            client.println();
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("test");
            client.println("</html>");
            break;
          } else {
            line = "";
          }
        }
      }
    }

    client.stop();
  }
}

In the last else you see, we have a full line, so we can check its content before clearing it. In this case we can check for GET /on and GET /off, and detect the command we’re asked to perform:

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:

if (command == "on") {
  digitalWrite(LED_BUILTIN, HIGH);
} else if (command == "off") {
  digitalWrite(LED_BUILTIN, LOW);
}

We can also send a response confirmation back with

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

That’s it! Now load the code on the Arduino and call the /on URL, or the /off URL.

I reserved a static IP to the Arduino using my local network router, and I named it arduino.local in my /etc/hosts file, so reaching out to http://arduino.local/on turns the LED on, and to http://arduino.local/off turns the LED off.

Here’s the complete program:

#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();
  }
}
👀 Looking for a job? look at DEVJOBS (BETA) 🔥