Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: # Sensor Monitor
- - Version: 003
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2026-03-15 00:40:30
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Read temperature and humidity from DHT22 sensor on */
- /* GPIO4. Display real-time readings on SSD1306 OLED */
- /* with updates every 2 seconds. Show "Temp: XX.X°C" */
- /* and "Humidity: XX.X%" on separate lines */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* [POTA] Include POTA.h and secrets.h. Init POTA in */
- /* setup() with WiFi creds from secrets.h. Call */
- /* pota.loop() in loop(). Use pota.dashboard.setWidge */
- /* tConfigCallback(setupDashboard) and sendUpdates() */
- /* in loop(). */
- /****** SYSTEM REQUIREMENT 3 *****/
- /* [POTA] MANDATORY OTA: call */
- /* pota.checkAndPerformOTA() in setup() after */
- /* begin(). Register pota.onOTAAvailable(cb) where cb */
- /* sets a bool flag. In loop() when flag is true call */
- /* pota.restart(). Without OTA the device cannot */
- /* update remotely. */
- /****** SYSTEM REQUIREMENT 4 *****/
- /* [POTA] Dashboard monitor widget: sensorTemperature */
- /* as TEMPERATURE_CARD. Use pota.dashboard.setValue() */
- /* to send value to dashboard. extra="°C". */
- /****** SYSTEM REQUIREMENT 5 *****/
- /* [POTA] Dashboard monitor widget: sensorHumidity as */
- /* HUMIDITY_CARD. Use pota.dashboard.setValue() to */
- /* send value to dashboard. extra="%". */
- /****** SYSTEM REQUIREMENT 6 *****/
- /* [POTA] Dashboard monitor widget: sensorReady as */
- /* GENERIC_CARD. Use pota.dashboard.setValue() to */
- /* send value to dashboard. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** SYSTEM REQUIREMENTS *****/
- // System Requirement 1: "Read temperature and humidity from DHT22 sensor on GPIO4. Display real-time readings on SSD1306 OLED with updates every 2 seconds. Show "Temp: XX.X°C" and "Humidity: XX.X%" on separate lines"
- // System Requirement 2: "[POTA] Include POTA.h and secrets.h. Init POTA in setup() with WiFi creds from secrets.h. Call pota.loop() in loop(). Use pota.dashboard.setWidgetConfigCallback(setupDashboard) and sendUpdates() in loop()."
- // System Requirement 3: "[POTA] MANDATORY OTA: call pota.checkAndPerformOTA() in setup() after begin(). Register pota.onOTAAvailable(cb) where cb sets a bool flag. In loop() when flag is true call pota.restart(). Without OTA the device cannot update remotely."
- // System Requirement 4: "[POTA] Dashboard monitor widget: sensorTemperature as TEMPERATURE_CARD. Use pota.dashboard.setValue() to send value to dashboard. extra="°C"."
- // System Requirement 5: "[POTA] Dashboard monitor widget: sensorHumidity as HUMIDITY_CARD. Use pota.dashboard.setValue() to send value to dashboard. extra="%"."
- // System Requirement 6: "[POTA] Dashboard monitor widget: sensorReady as GENERIC_CARD. Use pota.dashboard.setValue() to send value to dashboard."
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <Adafruit_SSD1306.h>
- #include <U8g2_for_Adafruit_GFX.h>
- #include <Adafruit_GFX.h>
- #include <DHT.h>
- #include "secrets.h"
- #include "POTA.h"
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initializeDisplay(void);
- void initializeDHT22(void);
- void readDHT22Sensor(void);
- void displaySensorData(void);
- void updateDisplay(void);
- void setupDashboard(void);
- void sendUpdates(void);
- void onOTAAvailable(void);
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21 = 21;
- const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22 = 22;
- const uint8_t myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C;
- /***** DEFINITION OF DHT22 SENSOR PINS *****/
- const uint8_t DHT22_SENSOR_PIN = 4;
- const uint8_t DHT_SENSOR_TYPE = DHT22;
- /***** DEFINITION OF DISPLAY PARAMETERS *****/
- const int SCREEN_WIDTH = 128;
- const int SCREEN_HEIGHT = 64;
- const int OLED_RESET = -1;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Adafruit_SSD1306 display constructor: width, height, I2C address, reset pin
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- // U8g2 font engine for advanced text rendering
- U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
- // DHT22 sensor instance
- DHT dht22Sensor(DHT22_SENSOR_PIN, DHT_SENSOR_TYPE);
- // POTA instance for over-the-air updates and dashboard
- POTA pota;
- /***** DEFINITION OF TIMING VARIABLES *****/
- unsigned long lastDisplayUpdate = 0;
- const unsigned long DISPLAY_UPDATE_INTERVAL = 2000;
- unsigned long lastDashboardUpdate = 0;
- const unsigned long DASHBOARD_UPDATE_INTERVAL = 5000;
- /***** DEFINITION OF SENSOR DATA VARIABLES *****/
- float sensorTemperature = 0.0;
- float sensorHumidity = 0.0;
- boolean sensorReady = false;
- unsigned long dhtReadCounter = 0;
- /***** DEFINITION OF OTA STATE VARIABLES *****/
- volatile boolean otaUpdateAvailable = false;
- /****** setup() - SYSTEM INITIALIZATION *****/
- void setup(void)
- {
- // Initialize Serial communication for debugging
- Serial.begin(115200);
- delay(500);
- Serial.println("\n\nStarting ESP32 DHT22 and SSD1306 OLED Display with POTA Initialization...");
- // Initialize the I2C bus with specified pins
- Wire.begin(myDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21,
- myDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22);
- Serial.println("I2C bus initialized successfully");
- // Initialize the OLED display
- initializeDisplay();
- // Initialize DHT22 sensor
- initializeDHT22();
- // Initialize POTA with WiFi credentials from secrets.h
- Serial.println("Initializing POTA with WiFi credentials...");
- pota.begin(WIFI_SSID, WIFI_PASSWORD, AUTH_TOKEN, SERVER_SECRET, DEVICE_TYPE, FIRMWARE_VERSION);
- Serial.println("POTA begin() completed");
- // Set up the dashboard widget configuration callback
- pota.dashboard.setWidgetConfigCallback(setupDashboard);
- Serial.println("Dashboard configuration callback registered");
- // Register OTA available callback to set flag when update is ready
- pota.onOTAAvailable(onOTAAvailable);
- Serial.println("OTA availability callback registered");
- // Check and perform OTA if available (MANDATORY per system requirements)
- Serial.println("Checking for OTA updates...");
- pota.checkAndPerformOTA();
- Serial.println("OTA check completed");
- // Initialize the update timers
- lastDisplayUpdate = millis();
- lastDashboardUpdate = millis();
- Serial.println("Setup completed successfully");
- }
- /****** loop() - MAIN EXECUTION LOOP *****/
- void loop(void)
- {
- // Call POTA loop to handle WiFi and OTA operations
- pota.loop();
- // Check if OTA update is available and perform restart if needed
- if (otaUpdateAvailable)
- {
- Serial.println("OTA update available, restarting device...");
- pota.restart();
- }
- // Read DHT22 sensor and update display every 2 seconds
- if (millis() - lastDisplayUpdate >= DISPLAY_UPDATE_INTERVAL)
- {
- // Read sensor values from DHT22
- readDHT22Sensor();
- // Update display with real-time sensor readings
- updateDisplay();
- // Update the display timer
- lastDisplayUpdate = millis();
- }
- // Send updates to POTA dashboard every 5 seconds
- if (millis() - lastDashboardUpdate >= DASHBOARD_UPDATE_INTERVAL)
- {
- // Send sensor values to POTA dashboard
- sendUpdates();
- // Update the dashboard timer
- lastDashboardUpdate = millis();
- }
- // Small delay to prevent watchdog timeout
- delay(100);
- }
- /****** initializeDisplay() - INITIALIZE SSD1306 OLED DISPLAY VIA I2C *****/
- void initializeDisplay(void)
- {
- // Initialize display with I2C address 0x3C
- if (!display.begin(SSD1306_SWITCHCAPVCC, myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS))
- {
- Serial.println(F("SSD1306 allocation failed"));
- // Halt execution if display initialization fails
- for (;;)
- {
- delay(100);
- }
- }
- Serial.println("SSD1306 OLED display initialized successfully");
- // Connect U8g2 font engine to Adafruit GFX display
- u8g2_for_adafruit_gfx.begin(display);
- // Clear the display buffer
- display.clearDisplay();
- display.display();
- Serial.println("Display ready for content");
- }
- /****** initializeDHT22() - INITIALIZE DHT22 TEMPERATURE AND HUMIDITY SENSOR *****/
- void initializeDHT22(void)
- {
- // Configure GPIO4 as input for DHT22 data line
- pinMode(DHT22_SENSOR_PIN, INPUT_PULLUP);
- // Initialize DHT22 sensor
- dht22Sensor.begin();
- Serial.println("DHT22 sensor initialized successfully on GPIO4");
- // Delay to allow DHT22 to stabilize
- delay(2000);
- // Perform initial sensor read
- readDHT22Sensor();
- Serial.println("DHT22 sensor ready for readings");
- }
- /****** readDHT22Sensor() - READ TEMPERATURE AND HUMIDITY FROM DHT22 SENSOR *****/
- void readDHT22Sensor(void)
- {
- // Read humidity value from DHT22
- float humidity = dht22Sensor.readHumidity();
- // Read temperature value from DHT22 in Celsius
- float temperature = dht22Sensor.readTemperature();
- // Check if any reads failed and exit early (to try again)
- if (isnan(humidity) || isnan(temperature))
- {
- Serial.println("Failed to read from DHT22 sensor!");
- sensorReady = false;
- return;
- }
- // Update global sensor variables with fresh readings
- sensorTemperature = temperature;
- sensorHumidity = humidity;
- sensorReady = true;
- dhtReadCounter++;
- // Debug output with sensor readings
- Serial.print("DHT22 Read #");
- Serial.print(dhtReadCounter);
- Serial.print(" - Temperature: ");
- Serial.print(sensorTemperature, 1);
- Serial.print("°C, Humidity: ");
- Serial.print(sensorHumidity, 1);
- Serial.println("%");
- }
- /****** displaySensorData() - DISPLAY REAL-TIME SENSOR DATA WITH TEXT FORMATTING *****/
- void displaySensorData(void)
- {
- // Set font mode for U8g2 (transparent mode)
- u8g2_for_adafruit_gfx.setFontMode(1);
- // Set font direction (left to right)
- u8g2_for_adafruit_gfx.setFontDirection(0);
- // Set foreground color (white text)
- u8g2_for_adafruit_gfx.setForegroundColor(SSD1306_WHITE);
- // Display header with larger font
- u8g2_for_adafruit_gfx.setFont(u8g2_font_helvB14_tf);
- u8g2_for_adafruit_gfx.setCursor(0, 18);
- u8g2_for_adafruit_gfx.print(F("DHT22 DATA"));
- // Check if sensor is ready and display status
- if (!sensorReady)
- {
- u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR10_tf);
- u8g2_for_adafruit_gfx.setCursor(0, 35);
- u8g2_for_adafruit_gfx.print(F("Sensor Error"));
- return;
- }
- // Display temperature with formatted text
- u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR12_tf);
- u8g2_for_adafruit_gfx.setCursor(0, 35);
- u8g2_for_adafruit_gfx.print(F("Temp: "));
- u8g2_for_adafruit_gfx.print(sensorTemperature, 1);
- u8g2_for_adafruit_gfx.print(F("\xb0C"));
- // Display humidity with formatted text
- u8g2_for_adafruit_gfx.setCursor(0, 50);
- u8g2_for_adafruit_gfx.print(F("Humidity: "));
- u8g2_for_adafruit_gfx.print(sensorHumidity, 1);
- u8g2_for_adafruit_gfx.print(F("%"));
- // Display read count
- u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR08_tf);
- u8g2_for_adafruit_gfx.setCursor(0, 62);
- u8g2_for_adafruit_gfx.print(F("Reads: "));
- u8g2_for_adafruit_gfx.print(dhtReadCounter);
- }
- /****** updateDisplay() - UPDATE DISPLAY WITH REAL-TIME SENSOR DATA EVERY 2 SECONDS *****/
- void updateDisplay(void)
- {
- // Clear the display buffer to erase previous content
- display.clearDisplay();
- // Display sensor data with formatted text
- displaySensorData();
- // Push the display buffer to the physical display
- display.display();
- // Debug output to Serial
- Serial.print("Display updated with sensor data - Temp: ");
- Serial.print(sensorTemperature, 1);
- Serial.print("°C, Humidity: ");
- Serial.print(sensorHumidity, 1);
- Serial.println("%");
- }
- /****** setupDashboard() - CONFIGURE POTA DASHBOARD WIDGETS *****/
- void setupDashboard(void)
- {
- // This callback is invoked by POTA to configure the dashboard widgets
- // Called when the dashboard needs to be initialized
- Serial.println("Setting up POTA dashboard widgets...");
- // Note: Widget configuration is handled by POTA library
- // The widgets are defined in the POTA backend based on setValue() calls
- // This callback allows for any additional dashboard setup if needed
- Serial.println("Dashboard setup callback completed");
- }
- /****** sendUpdates() - SEND SENSOR DATA TO POTA DASHBOARD *****/
- void sendUpdates(void)
- {
- // Send temperature value to POTA dashboard as TEMPERATURE_CARD widget
- // Widget key "sensorTemperature" with value and extra unit "°C"
- pota.dashboard.setValue("sensorTemperature", sensorTemperature, "°C");
- // Send humidity value to POTA dashboard as HUMIDITY_CARD widget
- // Widget key "sensorHumidity" with value and extra unit "%"
- pota.dashboard.setValue("sensorHumidity", sensorHumidity, "%");
- // Send sensor ready status to POTA dashboard as GENERIC_CARD widget
- // Widget key "sensorReady" with boolean value converted to string
- String readyStatus = sensorReady ? "Ready" : "Error";
- pota.dashboard.setValue("sensorReady", readyStatus);
- // Debug output to Serial
- Serial.print("Dashboard updated - Temp: ");
- Serial.print(sensorTemperature, 1);
- Serial.print("°C, Humidity: ");
- Serial.print(sensorHumidity, 1);
- Serial.print("%, Ready: ");
- Serial.println(readyStatus);
- }
- /****** onOTAAvailable() - CALLBACK WHEN OTA UPDATE IS AVAILABLE *****/
- void onOTAAvailable(void)
- {
- // This callback is invoked by POTA when an OTA update becomes available
- // Set the flag to trigger restart in the main loop
- Serial.println("OTA update is available!");
- otaUpdateAvailable = true;
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment