Lesson objectives
In this lesson, students will focus on optimizing their IoT system for long-term stability and performance. After integrating and testing the system, it is important to refine the project by addressing potential problems such as performance bottlenecks, sensor accuracy, and network stability. Students will fine-tune various aspects of their system, including sensor readings, update rates, power consumption, and error handling, so the system runs smoothly and efficiently over longer periods.
By the end of this lesson, students will have a stable and optimized IoT system ready for deployment.
Introduction
Now that you have tested your IoT system and confirmed its basic functionality, it is time to optimize it for better performance and stability. Optimization ensures that the system can run for longer periods without crashing or becoming unstable. This includes managing sensor accuracy, processing data efficiently, optimizing update rates, and ensuring that the WiFi connection remains stable over time. In this lesson, we go through several key optimizations that will help improve the reliability and performance of your IoT project.
List of required components:
![]() |
AHT10 high-precision temperature and humidity sensor |
![]() |
TTGO T-Display ESP32 16MB with WiFi, Bluetooth and 1.1" color LCD screen |
![]() |
Dupont cables, 40 pcs |
Step 1: Optimizing sensor readings
Your IoT system collects temperature and humidity data from the AHT10 sensor. To ensure that readings are accurate and do not fluctuate unnecessarily, consider the following strategies:
-
Fine-tuning sensor intervals
-
Avoid excessive sensor polling: Constant polling can cause data fluctuations or inaccurate readings. Adjust the interval between requests to suit your application. For example, if you are monitoring room temperature, a reading every couple of seconds is typically sufficient. In the code, you can adjust the delay parameter:
delay(2000); // Vent 2 sekunder mellem sensoraflæsninger
-
-
Filtering sensor data
-
Apply a moving average (smoothing): Sensor readings may be subject to small fluctuations due to environmental factors. To improve stability, you can implement a simple moving average that reduces noise in the data:
float smoothedTemperature = (previousTemperature + newT) / 2.0; float smoothedHumidity = (previousHumidity + newH) / 2.0;
This produces more stable values by averaging the current reading with the previous one.
-
Step 2: Managing the TFT display update rate
The TFT display updates in real time to show sensor data. However, fast update rates can cause flickering, slow down the system, or increase power consumption.
-
Reduce unnecessary updates
-
Update only on significant changes: Instead of updating the display on every loop, you can update only when sensor data changes significantly. This reduces system load and flickering:
if (abs(nearestT - previousT) > 0.1 || abs(nearestH - previousH) > 0.1) { tft.fillScreen(TFT_BLACK); // Ryd skærmen tft.drawString(String(nearestT, 1), 10, 40, 7); tft.drawString("C", 100, 100, 3); tft.drawString(String(nearestH, 1), 10, 130, 7); tft.drawString("% rH", 100, 100, 3); }
-
-
Managing screen brightness
-
Optimize power consumption: If your system runs on a battery, it can be worth lowering the backlight when the system is idle or in low light. Many displays can control the backlight through code:
analogWrite(TFT_BACKLIGHT_PIN, brightnessLevel); // Justér lysstyrke
-
Step 3: Optimizing WiFi and web server stability
WiFi connectivity is essential for displaying sensor data on a web page, but unstable connections can cause the system to fail.
-
WiFi auto-reconnect
-
Reconnect automatically: The system should automatically attempt to reconnect if the connection drops:
if (WiFi.status() != WL_CONNECTED) { WiFi.begin(ssid, password); Serial.println("Genopretter WiFi-forbindelse..."); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Forsøger igen..."); } Serial.println("WiFi forbundet"); }
-
-
Handling client requests
-
Avoid overload: The ESP32 web server has limited resources. Limit the number of simultaneous clients or implement simple load management:
if (WiFi.softAPgetStationNum() > MAX_CLIENTS) { Serial.println("For mange klienter tilsluttet!"); server.close(); }
-
Step 4: Error handling and stability measures
-
Handling sensor errors
-
Soft restart: If the sensor fails temporarily, the system should not crash but try again:
if (!aht.begin()) { Serial.println("Sensorfejl. Forsøger igen..."); delay(1000); } else { Serial.println("Sensor genoprettet!"); }
-
-
Watchdog timer
-
Automatic reset: Use the ESP32's built-in watchdog (WDT) to restart automatically if the system hangs:
esp_task_wdt_init(5, true); // Timeout på 5 sekunder esp_task_wdt_add(NULL); // Tilføj nuværende tråd // I loop(): esp_task_wdt_reset(); // Nulstil watchdog
-
Step 5: Optimization conclusion
By applying these techniques, you achieve a system that:
-
Runs efficiently with optimized sensor intervals and data smoothing.
-
Updates the display only for relevant changes, without flickering.
-
Handles WiFi connections robustly with automatic reconnection.
-
Avoids overloading the web server.
-
Has built-in error handling and automatic restarts on critical errors.
Closing remarks
Optimizing your IoT system for stability is essential for real-world applications. This lesson has shown you how to fine-tune sensor readings, manage display updates, handle WiFi connectivity, and implement robust error handling. With a stable and optimized system, you are now ready to deploy your IoT project confidently in home networks, environmental monitoring, or more advanced applications.
About the measurements: The station in this series measures temperature and relative humidity using the AHT10. These are indoor climate readings, not direct measurements of CO2, VOCs, or particles. The series name “air quality monitoring” should be understood with this limitation.


