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.
int status = WL_IDLE_STATUS; while (status != WL_CONNECTED) { Serial.print("Connecting to "); Serial.println(ssid); status = WiFi.begin(ssid, pass); delay(5000); }
voidloop(){ WiFiClient client = server.available(); if (client) { String line = ""; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c);
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.
int status = WL_IDLE_STATUS; while (status != WL_CONNECTED) { Serial.print("Connecting to "); Serial.println(ssid); status = WiFi.begin(ssid, pass); delay(5000); }
voidloop(){ WiFiClient client = server.available(); if (client) { String line = ""; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c);