Skip to content

Read values from an Arduino via HTTP

New Course Coming Soon:

Get Really Good at Git

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();
  }
}
Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!

Here is how can I help you: