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:
#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
:
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:
#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