If you have any questions or feedback, pleasefill out this form
Table of Contents
This post is translated by ChatGPT and originally written in Mandarin, so there may be some inaccuracies or mistakes.
This article is the fourth in a series:
- Introduction to Sensors - DHT11 and MH-Z14A
- Data Communication - UART (Implementation is using UART, so only UART will be discussed)
- Arduino Pitfalls
- WiFi Section: To save time on debugging, I purchased an ESP32 development board, which already includes WiFi and Bluetooth functionality.
- (Upcoming) MQTT Section: To transmit data to other devices, we will use MQTT, a lightweight communication protocol.
- (Upcoming) Grafana / Web Section: Of course, data stored in a database should be displayed in a flashy way! Here, we will use Grafana + Prometheus and Svelte to visualize the data.
Introduction
To implement WiFi functionality on an Arduino, additional extension modules are typically required, with the ESP8266
being the most common chip. However, if you only purchase the ESP8266, you'll need to solder all the pins yourself and thoroughly study the Datasheet. While this is a great exercise for understanding the underlying workings of WiFi, our goal this time is to realize the complete concept, so I opted for the ESP32 development board, which already has built-in WiFi and Bluetooth capabilities.
The ESP32 development board actually offers more than just WiFi functionality; it also includes GPIO pins, serial interfaces, and UART compatibility, making it easy to upload code with the Arduino IDE. Therefore, even without using Arduino, you can achieve all functionalities solely with 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()
{
}
With the WiFi
library, establishing a WiFi connection is quite simple; you just need to call WiFi.mode
and WiFi.begin
directly in the setup
. I haven't implemented a reconnection mechanism here, so if it fails, you might need to restart the Arduino.
Once connected to WiFi, the possibilities are vast! You can run an HTTP server, send API requests to a server, transmit sensor data to a database, monitor data in real-time, and much more.
In this section, we aim to send sensor data (temperature, humidity, and carbon dioxide concentration) to the server via MQTT, leaving the rest of the logic to be handled by the server. We will cover MQTT in detail in the next article!
If you found this article helpful, please consider buying me a coffee ☕ It'll make my ordinary day shine ✨
☕Buy me a coffee