Building an Air Quality Monitoring App with Arduino and ESP32 (4) - WiFi
This article is the fourth in the series:
- Sensors Introduction - DHT11 and MH-Z14A
- Data Communication - UART (Implemented with UART, so only UART is covered)
- Arduino Pitfalls and Lessons Learned
- WiFi: To save debugging time, I bought an ESP32 development board, which already comes with built-in WiFi and Bluetooth.
- (Coming Soon) MQTT: Used MQTT, a lightweight communication protocol, to send data to other devices.
- (Coming Soon) Grafana / Web: Once data is in the database, you obviously want to display it in a flashy way! Here, Grafana + Prometheus and Svelte are used to display the data.
Introduction
Typically, implementing WiFi functionality on an Arduino requires an add-on module, with the ESP8266 being a common chip choice. However, if you only buy an ESP8266 chip, you have to solder all the pins yourself and digest the entire datasheet. While that’s great practice if you want to understand how WiFi works under the hood, our goal here is simply to bring the whole project idea to life. Therefore, I bought an ESP32 development board that already has built-in WiFi and Bluetooth.
The ESP32 development board actually offers far more than just WiFi—it includes GPIO pins, serial interfaces, and UART. It is also compatible with the Arduino IDE for uploading code, making development very convenient. In fact, even without an Arduino, you could implement all the features using just the ESP32.
WiFi Connection
#include "WiFi.h"
WiFiClient client;
void setup()
{
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Wifi is connected!");
}
void loop()
{
}
Using the WiFi library makes setting up a WiFi connection quite straightforward. You just need to call WiFi.mode and WiFi.begin directly inside setup. I haven’t implemented a reconnection mechanism here, so if it fails, you might need to restart the board.
Once connected to WiFi, there’s so much you can do! For example, running an HTTP server, making API calls to a backend, sending sensor readings to a database, real-time monitoring, and more.
Here, what we want to do is send the sensor data (temperature, humidity, and CO2 concentration) to a server via MQTT, leaving all the remaining logic for the server to handle. We’ll introduce MQTT in the next article!
Related Posts
- When a Measure Becomes a Target: From the Window Tax to Pull Request Counts I once wrote a script to tally how many PRs I contributed in a quarter, how many reviews I left, and how many tickets I closed, hoping to use numbers to prove my output to my manager. My manager simply remarked that performance isn't just about output. Years later, I finally understood—when a measure becomes a target, it ceases to be a good measure. From the British window tax and the Hanoi rat bounty to evaluating developers by PR counts today, the underlying mechanism is exactly the same.
- Using Cloudflare Images for Image Storage and Transformation Putting an image on a webpage is the simplest task in frontend development. But doing it properly—including resizing, generating multiple formats, and withstanding heavy traffic—is actually an entire end-to-end solution. Eventually, I offloaded everything to Cloudflare Images, keeping only a single original image.
- Stop Using AWS Access Keys Access Keys are an easily overlooked security risk in AWS. By pairing OIDC with IAM Roles, GitHub Actions can securely operate AWS resources without storing any secrets.
- Database Primary Keys: AUTO_INCREMENT, UUID, and UUIDv7 Backend developers often face the choice of primary keys: should you use auto-increment or UUID? What about collisions? How does UUIDv7 compare to created_at + index in performance? Here are the design decisions and benchmark results from testing 20 million rows.