Level 3Unit B · Session 7
7

Unit B · What your device can sense

Temperature, on your page

What we are making: the temperature of this room, readable on any phone in the building.

One new line at a time

You already have a board that joins the WiFi and serves a page. Nothing about that changes today. All you are doing is putting a real number on a page that already works — and that is the pattern for the next five sessions too.

Notice what has just happened, though. Until now, reading a sensor meant sitting at the laptop it was plugged into. From today the reading travels. That is the entire point of this year.

New words
Web serverA program that hands out a page when a browser asks for it.
handleRoot()The function that builds the page for the address ‘/’.
StringText you can add to with +. How the page gets built up.
IP addressThe number you type into the browser to reach your board.
ESP32USB to the laptop3V3GNDGPIO 4DHT11temperature and humidity+out−3.3VGNDdata3.3 volts, not 5. The ESP32 is a 3.3 volt board and every module here is happy with that.
The temperature sensor. Three wires, exactly as on the Uno — only the pin names changed.
Session 7 — a thermometer anybody on the WiFi can read — 4 slots
SLOT 1
the temperature sensor's library
#include <DHT.h>
SLOT 2
the sensor itself, data wire on GPIO 4
DHT sensor(4, DHT11);
SLOT 3
read it, hand it back as text
float t = sensor.readTemperature();
return String(t, 1) + " C";
SLOT 5
wake the sensor up, once
sensor.begin();

Do it

  1. Wire the sensor. Three wires, and 3.3V rather than 5V.
  2. Open your copy of station.ino. Fill the four slots above, one at a time.
  3. Put your school's network name and password into the two lines near the top.
  4. Upload, then open the Serial Monitor at 115200 — not 9600.
  5. Wait for the dots to stop. An IP address will appear, something like 192.168.1.47.
  6. Type that address into a browser on your phone. Your reading should be there.
💡 Tip
If the dots never stop, it is almost never your code. Check the network name for a typo first, then check it is a 2.4 GHz network — an ESP32 physically cannot see a 5 GHz one. If the school WiFi will not take it, your teacher will switch the class to the board's own hotspot, and everything else in this unit works exactly the same.

Change it

  1. Add the humidity underneath, the way the temperature is done.
  2. Put your team name on the page as a second heading.
  3. Show the temperature to one decimal place instead of two.
What you seeWhyWhat to do
Dots forever, no IP addressWrong name, wrong password, or a 5 GHz network.Check the spelling first. It is the spelling about half the time.
Serial Monitor shows nonsense charactersWrong speed.Set it to 115200. The ESP32 does not use 9600.
Page loads but shows nanThe sensor is not answering.Check the data wire is on GPIO 4 and that power is 3.3V.
Page will not load on my phoneThe phone is on a different network.Both have to be on the same WiFi. Mobile data will not reach it.

What you learned

  • A reading can leave the room it was taken in.
  • The page is built as text, then handed to whoever asks for it.
  • Your IP address is how the world finds your board.
Challenge optional — only if you finish early
  • Show the highest and lowest temperature seen since the board was switched on.
6. Putting Unit A together