Skip to content

Read values from an Arduino via HTTP

In this tutorial I want to expand the Arduino Web Server tutorial to read the values measured by a sensor, so we can just open a page on our browser and see the data.

For example we’re going to measure the temperature using a DHT11 sensor, and we’re going to measure the distance from an object using a proximity sensor.

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 :

if (line.startsWith("GET /on ")){
  digitalWrite(LED_BUILTIN, HIGH);
}
if (line.startsWith("GET /off ")) {
  digitalWrite(LED_BUILTIN, LOW);
}

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 = "";
    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 {
            if (line.startsWith("GET /on ")){
              digitalWrite(LED_BUILTIN, HIGH);
            }
            if (line.startsWith("GET /off ")) {
              digitalWrite(LED_BUILTIN, LOW);
            }

            line = "";
          }
        }
      }
    }

    client.stop();
  }
}

→ Here's my latest YouTube video

→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter

JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list