Kalan's Blog

Kalan 頭像照片,在淡水拍攝,淺藍背景

四零二曜日電子報上線啦!訂閱訂起來

Software Engineer / Taiwanese / Life in Fukuoka
This blog supports RSS feed (all content), you can click RSS icon or setup through third-party service. If there are special styles such as code syntax in the technical article, it is still recommended to browse to the original website for the best experience.

Current Theme light

我會把一些不成文的筆記或是最近的生活雜感放在短筆記,如果有興趣的話可以來看看唷!

Please notice that currenly most of posts are translated by AI automatically and might contain lots of confusion. I'll gradually translate the post ASAP

Creating an Air Quality Monitoring Application with Arduino and ESP32 (4) - WiFi

This article is the fourth in a series:

  1. Introduction to Sensors - DHT11 and MH-Z14A
  2. Data Communication - UART (We only discuss UART since it is used for implementation)
  3. Arduino Pitfalls
  4. WiFi: To save debugging time, I purchased an ESP32 development board, which already includes WiFi and Bluetooth functionality.
  5. (Upcoming) MQTT: To send data to other devices, we use MQTT, a lightweight communication protocol.
  6. (Upcoming) Grafana / Web: To display the data in a flashy way, we use Grafana + Prometheus and Svelte.

Introduction

Typically, implementing WiFi functionality on Arduino requires additional expansion modules, with the ESP8266 chip being the most common. However, if you only purchase the ESP8266, you have to solder all the pins yourself and study the Datasheet on your own to understand the underlying WiFi operation. While it's a great exercise to learn about WiFi at a low level, our goal this time is to implement the entire idea. Therefore, I purchased an ESP32 development board that already has built-in WiFi and Bluetooth functionality.

The ESP32 development board not only has WiFi functionality but also features GPIO pins, a Serial interface, and UART, making it convenient for development. It can also be programmed using the Arduino IDE. So even without using Arduino, you can accomplish all the required functionality 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()
{
}

With the WiFi library, establishing a WiFi connection is quite simple. Just call WiFi.mode and WiFi.begin directly in the setup function. I haven't set up a reconnection mechanism here, so if it fails, you may need to restart the Arduino.

Once connected to WiFi, there are many things you can do! Such as running an HTTP server, sending API requests to a server, sending sensor data to a database, real-time monitoring, and more.

In this case, we want to send sensor data (temperature, humidity, and CO2 concentration) to a server using MQTT, and let the server handle the rest of the logic. We will cover MQTT in the next article!

Prev

隱性肥宅工程師重訓一個月後的心得

Next

React 17 update focus - useEffect's clean up function timing changes

If you found this article helpful, please consider buy me a drink ☕️ It'll make my ordinary day shine✨

Buy me a coffee