// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++ Importing Library ++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++ Wi-Fi Library +++++++++++++++++++++++++++++++++++++++++++++++
#include <WiFi.h> // ESP 32 Wi-Fi Libray
#include <WiFiClient.h> // Create Web-Server Client
#include <WebServer.h> // Web-Server libray for HTTP communication
#include <uri/UriBraces.h> // URL handling
#define WIFI_SSID "Wokwi-GUEST" // Define SSID of the network
#define WIFI_PASSWORD "" // Define The Password
WebServer server(80); // Server on port 80 is set
// +++++++++++++++++++++++++++++++++++++++ DHT22 Humidity seensor Library +++++++++++++++++++++++++++++++++++++++
#include <DHT.h> // Library for Humidity sensor
#define DHTPIN 14 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT Type is set to DHT22
DHT dht(DHTPIN, DHTTYPE); // dht object is created, pin and type are selected
// +++++++++++++++++++++++++++++++++++++++ Oled Display SSD1306 Library +++++++++++++++++++++++++++++++++++++++
#include <Wire.h> // Wire library for I2C sensor
#include <Adafruit_GFX.h> // Adafruit GFX graphic library
#include <Adafruit_SSD1306.h> // Library for controlling SSD1306 Display
#include <Adafruit_Sensor.h> // Another Library for sensor
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Instance for screen is set as display. Widht, Heihgt and connection set for display - reset pin
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Declaring global variable for humidity and temperature
float humidity;
float temperature;
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++ Main Program +++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void setup() {
// Entry point for program, it will run at the start
Serial.begin(115200); // Initiate serial communication at baud rate of 115200 bits per second
dht.begin(); // Initialize the sensor for communication
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); // Connect to the pre-established network
// Check if the sensors are working correctly and force the program to alt with a infinite while loop.
if (!sensorcheck()) {
Serial.println("Error with sensor/display. Halting execution.");
while (true) { // Infinite loop that halt the execution of the system
delay(1000);
}
}
// ++++++++++ Wi-Fi Conenction Process ++++++++++
// Display the SSID that the ESP32 try to connect
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// While waiting for connection it print dot (.) in loop
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!"); // Connection successful
// IP addrress of ESP32
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Setup route handlers
server.on("/", []() { // lambda function for HTTP GET it wait for a response
httpCode(); // Function that will use global variables for temperature and humidity
});
server.begin(); // Start the web server
Serial.println("HTTP server started"); // Server started successful
// Console Log for debugging and terminal visualization
Serial.println("");
Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++");
Serial.println("+++++ DHT11 Humidity & temperature Sensor +++++");
Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++");
splash(); // Start the splash function
}
void loop() {
// Loop that manage the reading from DHT22 sensor and start different functions
// Humidity and Temperature are associated with the readHumidity and readTemperature function of DHT22 sensor
humidity = dht.readHumidity();
temperature = dht.readTemperature();
server.handleClient(); // The server continuously listens for and responds to incoming requests
console_stament(); // Function for displaying output on console
Temp_and_Humidity(); // Function for displaing output on display
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++ Main Function +++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void Temp_and_Humidity(){
// Temperature and humidity read on loop function are called into this function and displayed on the OLED screen
display.clearDisplay();
// ++ Temperature Setting ++
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(dht.readTemperature());
display.print(" ");
display.setTextSize(1);
display.cp437(true); // Enable the use of Asci symbol
display.write(167); //Prints the degree symbol (°) from the ascii dataset
display.setTextSize(2);
display.print("C");
// ++ Humidity Setting ++
display.setTextSize(1);
display.setCursor(0, 35);
display.print("Humidity: ");
display.setTextSize(2);
display.setCursor(0, 45);
display.print(dht.readHumidity());
display.print(" %");
// ++ Display Everyting on Oled ++
display.display();
}
void console_stament(){
// Print statements for humidity and temperature on console
Serial.print("Current humidity = ");
Serial.print(humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht.readTemperature());
Serial.println("C ");
delay(2000); // 2 second pause from each reading
}
void httpCode() {
// Prepare the HTML content with placeholders for temperature and humidity
String response = R"(
<!DOCTYPE html><html>
<head>
<! –– Refresh the page every 5 second ––>
<meta http-equiv="refresh" content="5">
<! –– Page Name ––>
<title>Humidity Sensor System</title>
<! –– Styling options ––>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html { font-family: sans-serif; text-align: center; }
body { display: inline-flex; flex-direction: column; }
h1 { margin-bottom: 1.2em; }
h2 { margin: 0; }
div { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto; grid-auto-flow: column; grid-gap: 1em; }
</style>
</head>
<body>
<! –– Page Content ––>
<h1>ESP32 Web Server By Mauro</h1>
<div>
<h2>Temperature</h2>
<p>TEMP_VALUE °C</p>
<h2>Humidity</h2>
<p>HUMID_VALUE %</p>
</div>
</body>
</html>
)";
// Convert Humidity and Temperature value from float to string using 2 decimal places
String tempStr = String(temperature, 2);
String humidStr = String(humidity, 2);
// Replace placeholders in the HTML content with actual sensor readings
response.replace("TEMP_VALUE", String(temperature, 2));
response.replace("HUMID_VALUE", String(humidity, 2));
// Send the response to the client
server.send(200, "text/html", response);
}
void splash(){
// Splash screen for display oled
display.clearDisplay(); // Clear Eventual Output on display
// ++ Print Temp & Humidity ++
display.setTextSize(1); // Set a font size for the text
display.setTextColor(WHITE); // Set a color size for the text
display.setCursor(0, 10); // Position the cusror to a specific point
display.print("Temp & Humidity "); // Pint on display
// ++ Print Mauro DC (Student Name) ++
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 40);
display.println("Mauro DC");
display.display(); // The output is set on display
delay(500); // Delay time to ensure sensor start
}
bool sensorcheck() {
bool isDHTSensorOk = true;
bool isDisplayOk = true;
// Check if the DHT22 sensor is correctly attached to the board
if (isnan(dht.readTemperature()) || isnan(dht.readHumidity())) {
Serial.println("Failed to read from DHT sensor!");
isDHTSensorOk = false;
}
// Check if the oled display is correctly attached to the board
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
isDisplayOk = false;
}
return isDHTSensorOk && isDisplayOk; // Returns true only if both checks pass
}