/

Read values from an Arduino via HTTP

Read values from an Arduino via HTTP

In this tutorial, we will expand on the Arduino Web Server tutorial to include reading the values measured by a sensor. This will allow us to view the data by simply opening a page in our browser.

For this example, we will measure the temperature using a DHT11 sensor and measure the distance from an object using a proximity sensor.

To control the built-in LED on the Arduino, we can send requests to the /on URL to turn it on and the /off URL to turn it off. Any other URL will not trigger any action.

Here is 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 last else block, we have a complete line, which we can check for its content before clearing it. In this case, we can check for GET /on and GET /off:

1
2
3
4
5
6
if (line.startsWith("GET /on ")) {
digitalWrite(LED_BUILTIN, HIGH);
}
if (line.startsWith("GET /off ")) {
digitalWrite(LED_BUILTIN, LOW);
}

That’s it! Now you can load the code onto the Arduino and call the /on URL or the /off URL to control the LED.

I have assigned a static IP address to the Arduino using my local network router, and I have named it arduino.local in my /etc/hosts file. So, by accessing http://arduino.local/on, the LED turns on, and by accessing http://arduino.local/off, the LED turns 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
#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 {
if (line.startsWith("GET /on ")) {
digitalWrite(LED_BUILTIN, HIGH);
}
if (line.startsWith("GET /off ")) {
digitalWrite(LED_BUILTIN, LOW);
}

line = "";
}
}
}
}

client.stop();
}
}

Tags: Arduino, HTTP, sensors, DHT11, proximity sensor