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();
  }
}


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

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();
  }
}


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-08-25T05:00:00+00:00) Read values from an Arduino via HTTP. Retrieved from https://www.scien.cx/2021/08/25/read-values-from-an-arduino-via-http-2/

MLA
" » Read values from an Arduino via HTTP." flaviocopes.com | Sciencx - Wednesday August 25, 2021, https://www.scien.cx/2021/08/25/read-values-from-an-arduino-via-http-2/
HARVARD
flaviocopes.com | Sciencx Wednesday August 25, 2021 » Read values from an Arduino via HTTP., viewed ,<https://www.scien.cx/2021/08/25/read-values-from-an-arduino-via-http-2/>
VANCOUVER
flaviocopes.com | Sciencx - » Read values from an Arduino via HTTP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/25/read-values-from-an-arduino-via-http-2/
CHICAGO
" » Read values from an Arduino via HTTP." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/08/25/read-values-from-an-arduino-via-http-2/
IEEE
" » Read values from an Arduino via HTTP." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/08/25/read-values-from-an-arduino-via-http-2/. [Accessed: ]
rf:citation
» Read values from an Arduino via HTTP | flaviocopes.com | Sciencx | https://www.scien.cx/2021/08/25/read-values-from-an-arduino-via-http-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.