About Lesson
To add WiFiManager functionality to your previous code, you can integrate the WiFiManager
library. This library allows for easy configuration of Wi-Fi credentials through a web interface. Once connected to Wi-Fi, your ESP32 will display DHT22 data on the OLED display as before.
Steps to Add WiFi:
- Install the
WiFiManager
library via the Arduino Library Manager. - Include WiFiManager in your code and configure the Wi-Fi connection.
Here’s the complete updated code that includes WiFiManager
functionality:
Updated Code with WiFiManager:
C++
#include <WiFiManager.h> // WiFiManager library
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // We don't use reset pin on most OLEDs
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// DHT22 settings
#define DHTPIN 4 // GPIO pin for DHT22
#define DHTTYPE DHT22 // DHT22 sensor type
DHT dht(DHTPIN, DHTTYPE);
WiFiManager wifiManager; // Create WiFiManager instance
void setup() {
// Start the Serial Monitor
Serial.begin(115200);
// Initialize DHT22
dht.begin();
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
// Clear display buffer
display.clearDisplay();
// Start WiFiManager
wifiManager.autoConnect("ESP32_DHT22_OLED"); // SSID for the Access Point
// Once connected to WiFi, display success message
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Connected to Wi-Fi");
display.display(); // Update the display
delay(2000); // Wait for 2 seconds
// Clear the screen for sensor data
display.clearDisplay();
}
void loop() {
// Read temperature and humidity from DHT22
float humidity = dht.readHumidity();
float temperatureF = dht.readTemperature(true); // Fahrenheit
// Check if readings are valid
if (isnan(humidity) || isnan(temperatureF)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Clear the display before showing new data
display.clearDisplay();
// Display temperature in Fahrenheit
display.setCursor(0, 10);
display.setTextSize(2);
display.println("Temp:");
display.setTextSize(2);
display.setCursor(0, 30);
display.print(temperatureF);
display.println(" F");
// Display humidity
display.setCursor(0, 50);
display.setTextSize(2);
display.print("Hum:");
display.setCursor(0, 70);
display.print(humidity);
display.println(" %");
// Update the display with new values
display.display();
// Print the same data to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureF);
Serial.print(" F, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Wait a few seconds between measurements
delay(2000);
}
Key Additions:
- WiFiManager Library: The
WiFiManager
library is included and initialized. - WiFiManager AutoConnect: The
wifiManager.autoConnect("ESP32_DHT22_OLED");
function initiates an Access Point (AP) with SSID “ESP32_DHT22_OLED”. When you connect to it, a captive portal appears allowing you to configure Wi-Fi credentials. - Wi-Fi Status Message: After successfully connecting to Wi-Fi, a success message is displayed on the OLED.
How It Works:
- When the ESP32 boots up, it starts the
WiFiManager
portal. - You connect to the portal using the default SSID (in this case, “ESP32_DHT22_OLED”).
- Once you configure the Wi-Fi settings and the ESP32 connects to Wi-Fi, it will display “Connected to Wi-Fi” on the OLED and then start reading and displaying DHT22 data.