pleasedontcode

# Sensor Monitor rev_03

Mar 14th, 2026
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 13.47 KB | None | 0 0
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: # Sensor Monitor
  13.     - Version: 003
  14.     - Source Code NOT compiled for: ESP32 DevKit V1
  15.     - Source Code created on: 2026-03-15 00:40:30
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /****** SYSTEM REQUIREMENTS *****/
  20. /****** SYSTEM REQUIREMENT 1 *****/
  21.     /* Read temperature and humidity from DHT22 sensor on */
  22.     /* GPIO4. Display real-time readings on SSD1306 OLED */
  23.     /* with updates every 2 seconds. Show "Temp: XX.X°C" */
  24.     /* and "Humidity: XX.X%" on separate lines */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* [POTA] Include POTA.h and secrets.h. Init POTA in */
  27.     /* setup() with WiFi creds from secrets.h. Call */
  28.     /* pota.loop() in loop(). Use pota.dashboard.setWidge */
  29.     /* tConfigCallback(setupDashboard) and sendUpdates() */
  30.     /* in loop(). */
  31. /****** SYSTEM REQUIREMENT 3 *****/
  32.     /* [POTA] MANDATORY OTA: call */
  33.     /* pota.checkAndPerformOTA() in setup() after */
  34.     /* begin(). Register pota.onOTAAvailable(cb) where cb */
  35.     /* sets a bool flag. In loop() when flag is true call */
  36.     /* pota.restart(). Without OTA the device cannot */
  37.     /* update remotely. */
  38. /****** SYSTEM REQUIREMENT 4 *****/
  39.     /* [POTA] Dashboard monitor widget: sensorTemperature */
  40.     /* as TEMPERATURE_CARD. Use pota.dashboard.setValue() */
  41.     /* to send value to dashboard. extra="°C". */
  42. /****** SYSTEM REQUIREMENT 5 *****/
  43.     /* [POTA] Dashboard monitor widget: sensorHumidity as */
  44.     /* HUMIDITY_CARD. Use pota.dashboard.setValue() to */
  45.     /* send value to dashboard. extra="%". */
  46. /****** SYSTEM REQUIREMENT 6 *****/
  47.     /* [POTA] Dashboard monitor widget: sensorReady as */
  48.     /* GENERIC_CARD. Use pota.dashboard.setValue() to */
  49.     /* send value to dashboard. */
  50. /****** END SYSTEM REQUIREMENTS *****/
  51.  
  52.  
  53. /* START CODE */
  54.  
  55. /****** SYSTEM REQUIREMENTS *****/
  56. // 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"
  57. // 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()."
  58. // 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."
  59. // System Requirement 4: "[POTA] Dashboard monitor widget: sensorTemperature as TEMPERATURE_CARD. Use pota.dashboard.setValue() to send value to dashboard. extra="°C"."
  60. // System Requirement 5: "[POTA] Dashboard monitor widget: sensorHumidity as HUMIDITY_CARD. Use pota.dashboard.setValue() to send value to dashboard. extra="%"."
  61. // System Requirement 6: "[POTA] Dashboard monitor widget: sensorReady as GENERIC_CARD. Use pota.dashboard.setValue() to send value to dashboard."
  62.  
  63. /****** DEFINITION OF LIBRARIES *****/
  64. #include <Wire.h>
  65. #include <Adafruit_SSD1306.h>
  66. #include <U8g2_for_Adafruit_GFX.h>
  67. #include <Adafruit_GFX.h>
  68. #include <DHT.h>
  69. #include "secrets.h"
  70. #include "POTA.h"
  71.  
  72. /****** FUNCTION PROTOTYPES *****/
  73. void setup(void);
  74. void loop(void);
  75. void initializeDisplay(void);
  76. void initializeDHT22(void);
  77. void readDHT22Sensor(void);
  78. void displaySensorData(void);
  79. void updateDisplay(void);
  80. void setupDashboard(void);
  81. void sendUpdates(void);
  82. void onOTAAvailable(void);
  83.  
  84. /***** DEFINITION OF I2C PINS *****/
  85. const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21 = 21;
  86. const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22 = 22;
  87. const uint8_t myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C;
  88.  
  89. /***** DEFINITION OF DHT22 SENSOR PINS *****/
  90. const uint8_t DHT22_SENSOR_PIN = 4;
  91. const uint8_t DHT_SENSOR_TYPE = DHT22;
  92.  
  93. /***** DEFINITION OF DISPLAY PARAMETERS *****/
  94. const int SCREEN_WIDTH = 128;
  95. const int SCREEN_HEIGHT = 64;
  96. const int OLED_RESET = -1;
  97.  
  98. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  99. // Adafruit_SSD1306 display constructor: width, height, I2C address, reset pin
  100. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  101.  
  102. // U8g2 font engine for advanced text rendering
  103. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
  104.  
  105. // DHT22 sensor instance
  106. DHT dht22Sensor(DHT22_SENSOR_PIN, DHT_SENSOR_TYPE);
  107.  
  108. // POTA instance for over-the-air updates and dashboard
  109. POTA pota;
  110.  
  111. /***** DEFINITION OF TIMING VARIABLES *****/
  112. unsigned long lastDisplayUpdate = 0;
  113. const unsigned long DISPLAY_UPDATE_INTERVAL = 2000;
  114. unsigned long lastDashboardUpdate = 0;
  115. const unsigned long DASHBOARD_UPDATE_INTERVAL = 5000;
  116.  
  117. /***** DEFINITION OF SENSOR DATA VARIABLES *****/
  118. float sensorTemperature = 0.0;
  119. float sensorHumidity = 0.0;
  120. boolean sensorReady = false;
  121. unsigned long dhtReadCounter = 0;
  122.  
  123. /***** DEFINITION OF OTA STATE VARIABLES *****/
  124. volatile boolean otaUpdateAvailable = false;
  125.  
  126. /****** setup() - SYSTEM INITIALIZATION *****/
  127. void setup(void)
  128. {
  129.     // Initialize Serial communication for debugging
  130.     Serial.begin(115200);
  131.     delay(500);
  132.     Serial.println("\n\nStarting ESP32 DHT22 and SSD1306 OLED Display with POTA Initialization...");
  133.    
  134.     // Initialize the I2C bus with specified pins
  135.     Wire.begin(myDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21,
  136.                myDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22);
  137.     Serial.println("I2C bus initialized successfully");
  138.    
  139.     // Initialize the OLED display
  140.     initializeDisplay();
  141.    
  142.     // Initialize DHT22 sensor
  143.     initializeDHT22();
  144.    
  145.     // Initialize POTA with WiFi credentials from secrets.h
  146.     Serial.println("Initializing POTA with WiFi credentials...");
  147.     pota.begin(WIFI_SSID, WIFI_PASSWORD, AUTH_TOKEN, SERVER_SECRET, DEVICE_TYPE, FIRMWARE_VERSION);
  148.     Serial.println("POTA begin() completed");
  149.    
  150.     // Set up the dashboard widget configuration callback
  151.     pota.dashboard.setWidgetConfigCallback(setupDashboard);
  152.     Serial.println("Dashboard configuration callback registered");
  153.    
  154.     // Register OTA available callback to set flag when update is ready
  155.     pota.onOTAAvailable(onOTAAvailable);
  156.     Serial.println("OTA availability callback registered");
  157.    
  158.     // Check and perform OTA if available (MANDATORY per system requirements)
  159.     Serial.println("Checking for OTA updates...");
  160.     pota.checkAndPerformOTA();
  161.     Serial.println("OTA check completed");
  162.    
  163.     // Initialize the update timers
  164.     lastDisplayUpdate = millis();
  165.     lastDashboardUpdate = millis();
  166.    
  167.     Serial.println("Setup completed successfully");
  168. }
  169.  
  170. /****** loop() - MAIN EXECUTION LOOP *****/
  171. void loop(void)
  172. {
  173.     // Call POTA loop to handle WiFi and OTA operations
  174.     pota.loop();
  175.    
  176.     // Check if OTA update is available and perform restart if needed
  177.     if (otaUpdateAvailable)
  178.     {
  179.         Serial.println("OTA update available, restarting device...");
  180.         pota.restart();
  181.     }
  182.    
  183.     // Read DHT22 sensor and update display every 2 seconds
  184.     if (millis() - lastDisplayUpdate >= DISPLAY_UPDATE_INTERVAL)
  185.     {
  186.         // Read sensor values from DHT22
  187.         readDHT22Sensor();
  188.        
  189.         // Update display with real-time sensor readings
  190.         updateDisplay();
  191.        
  192.         // Update the display timer
  193.         lastDisplayUpdate = millis();
  194.     }
  195.    
  196.     // Send updates to POTA dashboard every 5 seconds
  197.     if (millis() - lastDashboardUpdate >= DASHBOARD_UPDATE_INTERVAL)
  198.     {
  199.         // Send sensor values to POTA dashboard
  200.         sendUpdates();
  201.        
  202.         // Update the dashboard timer
  203.         lastDashboardUpdate = millis();
  204.     }
  205.    
  206.     // Small delay to prevent watchdog timeout
  207.     delay(100);
  208. }
  209.  
  210. /****** initializeDisplay() - INITIALIZE SSD1306 OLED DISPLAY VIA I2C *****/
  211. void initializeDisplay(void)
  212. {
  213.     // Initialize display with I2C address 0x3C
  214.     if (!display.begin(SSD1306_SWITCHCAPVCC, myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS))
  215.     {
  216.         Serial.println(F("SSD1306 allocation failed"));
  217.         // Halt execution if display initialization fails
  218.         for (;;)
  219.         {
  220.             delay(100);
  221.         }
  222.     }
  223.    
  224.     Serial.println("SSD1306 OLED display initialized successfully");
  225.    
  226.     // Connect U8g2 font engine to Adafruit GFX display
  227.     u8g2_for_adafruit_gfx.begin(display);
  228.    
  229.     // Clear the display buffer
  230.     display.clearDisplay();
  231.     display.display();
  232.    
  233.     Serial.println("Display ready for content");
  234. }
  235.  
  236. /****** initializeDHT22() - INITIALIZE DHT22 TEMPERATURE AND HUMIDITY SENSOR *****/
  237. void initializeDHT22(void)
  238. {
  239.     // Configure GPIO4 as input for DHT22 data line
  240.     pinMode(DHT22_SENSOR_PIN, INPUT_PULLUP);
  241.    
  242.     // Initialize DHT22 sensor
  243.     dht22Sensor.begin();
  244.    
  245.     Serial.println("DHT22 sensor initialized successfully on GPIO4");
  246.    
  247.     // Delay to allow DHT22 to stabilize
  248.     delay(2000);
  249.    
  250.     // Perform initial sensor read
  251.     readDHT22Sensor();
  252.    
  253.     Serial.println("DHT22 sensor ready for readings");
  254. }
  255.  
  256. /****** readDHT22Sensor() - READ TEMPERATURE AND HUMIDITY FROM DHT22 SENSOR *****/
  257. void readDHT22Sensor(void)
  258. {
  259.     // Read humidity value from DHT22
  260.     float humidity = dht22Sensor.readHumidity();
  261.    
  262.     // Read temperature value from DHT22 in Celsius
  263.     float temperature = dht22Sensor.readTemperature();
  264.    
  265.     // Check if any reads failed and exit early (to try again)
  266.     if (isnan(humidity) || isnan(temperature))
  267.     {
  268.         Serial.println("Failed to read from DHT22 sensor!");
  269.         sensorReady = false;
  270.         return;
  271.     }
  272.    
  273.     // Update global sensor variables with fresh readings
  274.     sensorTemperature = temperature;
  275.     sensorHumidity = humidity;
  276.     sensorReady = true;
  277.     dhtReadCounter++;
  278.    
  279.     // Debug output with sensor readings
  280.     Serial.print("DHT22 Read #");
  281.     Serial.print(dhtReadCounter);
  282.     Serial.print(" - Temperature: ");
  283.     Serial.print(sensorTemperature, 1);
  284.     Serial.print("°C, Humidity: ");
  285.     Serial.print(sensorHumidity, 1);
  286.     Serial.println("%");
  287. }
  288.  
  289. /****** displaySensorData() - DISPLAY REAL-TIME SENSOR DATA WITH TEXT FORMATTING *****/
  290. void displaySensorData(void)
  291. {
  292.     // Set font mode for U8g2 (transparent mode)
  293.     u8g2_for_adafruit_gfx.setFontMode(1);
  294.    
  295.     // Set font direction (left to right)
  296.     u8g2_for_adafruit_gfx.setFontDirection(0);
  297.    
  298.     // Set foreground color (white text)
  299.     u8g2_for_adafruit_gfx.setForegroundColor(SSD1306_WHITE);
  300.    
  301.     // Display header with larger font
  302.     u8g2_for_adafruit_gfx.setFont(u8g2_font_helvB14_tf);
  303.     u8g2_for_adafruit_gfx.setCursor(0, 18);
  304.     u8g2_for_adafruit_gfx.print(F("DHT22 DATA"));
  305.    
  306.     // Check if sensor is ready and display status
  307.     if (!sensorReady)
  308.     {
  309.         u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR10_tf);
  310.         u8g2_for_adafruit_gfx.setCursor(0, 35);
  311.         u8g2_for_adafruit_gfx.print(F("Sensor Error"));
  312.         return;
  313.     }
  314.    
  315.     // Display temperature with formatted text
  316.     u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR12_tf);
  317.     u8g2_for_adafruit_gfx.setCursor(0, 35);
  318.     u8g2_for_adafruit_gfx.print(F("Temp: "));
  319.     u8g2_for_adafruit_gfx.print(sensorTemperature, 1);
  320.     u8g2_for_adafruit_gfx.print(F("\xb0C"));
  321.    
  322.     // Display humidity with formatted text
  323.     u8g2_for_adafruit_gfx.setCursor(0, 50);
  324.     u8g2_for_adafruit_gfx.print(F("Humidity: "));
  325.     u8g2_for_adafruit_gfx.print(sensorHumidity, 1);
  326.     u8g2_for_adafruit_gfx.print(F("%"));
  327.    
  328.     // Display read count
  329.     u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR08_tf);
  330.     u8g2_for_adafruit_gfx.setCursor(0, 62);
  331.     u8g2_for_adafruit_gfx.print(F("Reads: "));
  332.     u8g2_for_adafruit_gfx.print(dhtReadCounter);
  333. }
  334.  
  335. /****** updateDisplay() - UPDATE DISPLAY WITH REAL-TIME SENSOR DATA EVERY 2 SECONDS *****/
  336. void updateDisplay(void)
  337. {
  338.     // Clear the display buffer to erase previous content
  339.     display.clearDisplay();
  340.    
  341.     // Display sensor data with formatted text
  342.     displaySensorData();
  343.    
  344.     // Push the display buffer to the physical display
  345.     display.display();
  346.    
  347.     // Debug output to Serial
  348.     Serial.print("Display updated with sensor data - Temp: ");
  349.     Serial.print(sensorTemperature, 1);
  350.     Serial.print("°C, Humidity: ");
  351.     Serial.print(sensorHumidity, 1);
  352.     Serial.println("%");
  353. }
  354.  
  355. /****** setupDashboard() - CONFIGURE POTA DASHBOARD WIDGETS *****/
  356. void setupDashboard(void)
  357. {
  358.     // This callback is invoked by POTA to configure the dashboard widgets
  359.     // Called when the dashboard needs to be initialized
  360.    
  361.     Serial.println("Setting up POTA dashboard widgets...");
  362.    
  363.     // Note: Widget configuration is handled by POTA library
  364.     // The widgets are defined in the POTA backend based on setValue() calls
  365.     // This callback allows for any additional dashboard setup if needed
  366.    
  367.     Serial.println("Dashboard setup callback completed");
  368. }
  369.  
  370. /****** sendUpdates() - SEND SENSOR DATA TO POTA DASHBOARD *****/
  371. void sendUpdates(void)
  372. {
  373.     // Send temperature value to POTA dashboard as TEMPERATURE_CARD widget
  374.     // Widget key "sensorTemperature" with value and extra unit "°C"
  375.     pota.dashboard.setValue("sensorTemperature", sensorTemperature, "°C");
  376.    
  377.     // Send humidity value to POTA dashboard as HUMIDITY_CARD widget
  378.     // Widget key "sensorHumidity" with value and extra unit "%"
  379.     pota.dashboard.setValue("sensorHumidity", sensorHumidity, "%");
  380.    
  381.     // Send sensor ready status to POTA dashboard as GENERIC_CARD widget
  382.     // Widget key "sensorReady" with boolean value converted to string
  383.     String readyStatus = sensorReady ? "Ready" : "Error";
  384.     pota.dashboard.setValue("sensorReady", readyStatus);
  385.    
  386.     // Debug output to Serial
  387.     Serial.print("Dashboard updated - Temp: ");
  388.     Serial.print(sensorTemperature, 1);
  389.     Serial.print("°C, Humidity: ");
  390.     Serial.print(sensorHumidity, 1);
  391.     Serial.print("%, Ready: ");
  392.     Serial.println(readyStatus);
  393. }
  394.  
  395. /****** onOTAAvailable() - CALLBACK WHEN OTA UPDATE IS AVAILABLE *****/
  396. void onOTAAvailable(void)
  397. {
  398.     // This callback is invoked by POTA when an OTA update becomes available
  399.     // Set the flag to trigger restart in the main loop
  400.    
  401.     Serial.println("OTA update is available!");
  402.     otaUpdateAvailable = true;
  403. }
  404.  
  405. /* END CODE */
  406.  
Advertisement
Add Comment
Please, Sign In to add comment