A first IoT project: from a sensor to a phone

Lesson 7 of 9 · Resources

How do you get from the theory of the Internet of Things to a device that measures the temperature in a classroom and shows it on a phone? This project guide connects the ESP32 lessons on this site into one plan – from choosing the parts to showing the data on a web page.

Before you start: required knowledge – The Internet of Things, An introduction to the ESP32.
How to read it: the page is an overview for students and teachers; at the end there is a short quiz, and the links lead to lessons on this site where each topic is taught in detail.

The project plan

StepWhat you doLesson
1. Ideawhat you measure and what the user sees (for example the temperature and the light level of a classroom on a phone)The Internet of Things
2. PartsESP32, a sensor, resistors, a breadboard, a USB cableAn introduction to the ESP32
3. Preparationinstalling the Arduino IDE and ESP32 supportInstalling the ESP32 (SR)
4. Measuringreading the sensor and printing to the serial monitorMeasuring light (ESP32)
5. Networkconnecting to Wi-Fi and a web server on the ESP32How a web server works
6. Displaya web page with values that refreshesStatic web pages on a server
7. Actionthe device switches something on (a pump, a motor, an LED)Starting a pump (ESP32), a servo motor

How the parts work together

[sensor] --voltage--> [ESP32] --Wi-Fi--> [router] ---> [phone / computer]
                         |                                  (web browser)
                         +--> [actuator: LED, pump, motor]

The ESP32 does three jobs: it measures (reads the sensor), decides (for example "if the soil is dry – switch on the pump") and communicates (sends data over the Wi-Fi network). The phone is only a browser that opens the address of the ESP32 in the local network (for example http://192.168.1.50).

The program skeleton

A program for the ESP32 (Arduino IDE) has the same shape in almost every IoT project: connecting to the network in setup(), and measuring and answering requests in loop(). Complete, tested programs are in the lessons from the table above.

#include <WiFi.h>
#include <WebServer.h>

const char* ssid = "network_name";        // the details of your Wi-Fi network
const char* password = "network_password";
WebServer server(80);                    // a web server on port 80

void homePage() {
    int value = analogRead(34);          // the sensor on pin 34 (0–4095)
    server.send(200, "text/html",
        "<h1>Light level: " + String(value) + "</h1>");
}

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) delay(500);   // wait for the network
    Serial.println(WiFi.localIP());      // open this address on the phone
    server.on("/", homePage);
    server.begin();
}

void loop() {
    server.handleClient();               // answer the browser's requests
}

Safety and good habits

  • Do not publish your Wi-Fi password in code you share (on GitHub, in a paper) – replace it with "network_password".
  • Do not open a device in the local network to the internet without protection (a password, HTTPS) – do that only when you know how.
  • Pumps and motors are powered by a separate supply and switched through a relay or a driver, never directly from an ESP32 pin.
  • For 230 V (mains electricity) – only with an adult, qualified person.

Ideas for the next project

ProjectWhat you learn
a weather station (temperature, humidity, pressure)several sensors, a chart in the browser
automatic plant wateringa soil moisture sensor, a relay, a pump
a people counter at the dooran ultrasonic sensor, counting events
a smart lightan LDR sensor, PWM, control from a phone
an open window alarma magnetic switch, notifications
Common mistakes and tips
  • Before connecting, check the voltage of the sensor: the ESP32 works at 3.3 V, and some sensors give 5 V.
  • If the ESP32 cannot connect to Wi-Fi, check that the network is on 2.4 GHz (the ESP32 does not work on 5 GHz).
  • Test each step separately (first the sensor, then Wi-Fi, then the web page) – that makes a mistake easy to find.

Check your knowledge

Open the quiz

A first IoT project

1. What does the ESP32 do in an IoT project?




2. Which Wi-Fi frequency band does the ESP32 work on?




3. How do you switch on a pump that has its own power supply?