pleasedontcode

# Sensor Monitor rev_02

Mar 14th, 2026
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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: 002
  14.     - Source Code NOT compiled for: ESP32 DevKit V1
  15.     - Source Code created on: 2026-03-15 00:36:34
  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. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. /****** SYSTEM REQUIREMENTS *****/
  31. // 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"
  32.  
  33. /****** DEFINITION OF LIBRARIES *****/
  34. #include <Wire.h>
  35. #include <Adafruit_SSD1306.h>
  36. #include <U8g2_for_Adafruit_GFX.h>
  37. #include <Adafruit_GFX.h>
  38. #include <DHT.h>
  39.  
  40. /****** FUNCTION PROTOTYPES *****/
  41. void setup(void);
  42. void loop(void);
  43. void initializeDisplay(void);
  44. void initializeDHT22(void);
  45. void readDHT22Sensor(void);
  46. void displaySensorData(void);
  47. void updateDisplay(void);
  48.  
  49. /***** DEFINITION OF I2C PINS *****/
  50. const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21 = 21;
  51. const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22 = 22;
  52. const uint8_t myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C;
  53.  
  54. /***** DEFINITION OF DHT22 SENSOR PINS *****/
  55. const uint8_t DHT22_SENSOR_PIN = 4;
  56. const uint8_t DHT_SENSOR_TYPE = DHT22;
  57.  
  58. /***** DEFINITION OF DISPLAY PARAMETERS *****/
  59. const int SCREEN_WIDTH = 128;
  60. const int SCREEN_HEIGHT = 64;
  61. const int OLED_RESET = -1;
  62.  
  63. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  64. // Adafruit_SSD1306 display constructor: width, height, I2C address, reset pin
  65. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  66.  
  67. // U8g2 font engine for advanced text rendering
  68. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
  69.  
  70. // DHT22 sensor instance
  71. DHT dht22Sensor(DHT22_SENSOR_PIN, DHT_SENSOR_TYPE);
  72.  
  73. /***** DEFINITION OF TIMING VARIABLES *****/
  74. unsigned long lastDisplayUpdate = 0;
  75. const unsigned long DISPLAY_UPDATE_INTERVAL = 2000;
  76.  
  77. /***** DEFINITION OF SENSOR DATA VARIABLES *****/
  78. float sensorTemperature = 0.0;
  79. float sensorHumidity = 0.0;
  80. boolean sensorReady = false;
  81. unsigned long dhtReadCounter = 0;
  82.  
  83. /****** setup() - SYSTEM INITIALIZATION *****/
  84. void setup(void)
  85. {
  86.     // Initialize Serial communication for debugging
  87.     Serial.begin(115200);
  88.     delay(500);
  89.     Serial.println("\n\nStarting ESP32 DHT22 and SSD1306 OLED Display Initialization...");
  90.    
  91.     // Initialize the I2C bus with specified pins
  92.     Wire.begin(myDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21,
  93.                myDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22);
  94.     Serial.println("I2C bus initialized successfully");
  95.    
  96.     // Initialize the OLED display
  97.     initializeDisplay();
  98.    
  99.     // Initialize DHT22 sensor
  100.     initializeDHT22();
  101.    
  102.     // Initialize the update timer
  103.     lastDisplayUpdate = millis();
  104.    
  105.     Serial.println("Setup completed successfully");
  106. }
  107.  
  108. /****** loop() - MAIN EXECUTION LOOP *****/
  109. void loop(void)
  110. {
  111.     // Read DHT22 sensor every 2 seconds
  112.     if (millis() - lastDisplayUpdate >= DISPLAY_UPDATE_INTERVAL)
  113.     {
  114.         // Read sensor values from DHT22
  115.         readDHT22Sensor();
  116.        
  117.         // Update display with real-time sensor readings
  118.         updateDisplay();
  119.        
  120.         // Update the display timer
  121.         lastDisplayUpdate = millis();
  122.     }
  123.    
  124.     // Small delay to prevent watchdog timeout
  125.     delay(100);
  126. }
  127.  
  128. /****** initializeDisplay() - INITIALIZE SSD1306 OLED DISPLAY VIA I2C *****/
  129. void initializeDisplay(void)
  130. {
  131.     // Initialize display with I2C address 0x3C
  132.     if (!display.begin(SSD1306_SWITCHCAPVCC, myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS))
  133.     {
  134.         Serial.println(F("SSD1306 allocation failed"));
  135.         // Halt execution if display initialization fails
  136.         for (;;)
  137.         {
  138.             delay(100);
  139.         }
  140.     }
  141.    
  142.     Serial.println("SSD1306 OLED display initialized successfully");
  143.    
  144.     // Connect U8g2 font engine to Adafruit GFX display
  145.     u8g2_for_adafruit_gfx.begin(display);
  146.    
  147.     // Clear the display buffer
  148.     display.clearDisplay();
  149.     display.display();
  150.    
  151.     Serial.println("Display ready for content");
  152. }
  153.  
  154. /****** initializeDHT22() - INITIALIZE DHT22 TEMPERATURE AND HUMIDITY SENSOR *****/
  155. void initializeDHT22(void)
  156. {
  157.     // Configure GPIO4 as input for DHT22 data line
  158.     pinMode(DHT22_SENSOR_PIN, INPUT_PULLUP);
  159.    
  160.     // Initialize DHT22 sensor
  161.     dht22Sensor.begin();
  162.    
  163.     Serial.println("DHT22 sensor initialized successfully on GPIO4");
  164.    
  165.     // Delay to allow DHT22 to stabilize
  166.     delay(2000);
  167.    
  168.     // Perform initial sensor read
  169.     readDHT22Sensor();
  170.    
  171.     Serial.println("DHT22 sensor ready for readings");
  172. }
  173.  
  174. /****** readDHT22Sensor() - READ TEMPERATURE AND HUMIDITY FROM DHT22 SENSOR *****/
  175. void readDHT22Sensor(void)
  176. {
  177.     // Read humidity value from DHT22
  178.     float humidity = dht22Sensor.readHumidity();
  179.    
  180.     // Read temperature value from DHT22 in Celsius
  181.     float temperature = dht22Sensor.readTemperature();
  182.    
  183.     // Check if any reads failed and exit early (to try again)
  184.     if (isnan(humidity) || isnan(temperature))
  185.     {
  186.         Serial.println("Failed to read from DHT22 sensor!");
  187.         sensorReady = false;
  188.         return;
  189.     }
  190.    
  191.     // Update global sensor variables with fresh readings
  192.     sensorTemperature = temperature;
  193.     sensorHumidity = humidity;
  194.     sensorReady = true;
  195.     dhtReadCounter++;
  196.    
  197.     // Debug output with sensor readings
  198.     Serial.print("DHT22 Read #");
  199.     Serial.print(dhtReadCounter);
  200.     Serial.print(" - Temperature: ");
  201.     Serial.print(sensorTemperature, 1);
  202.     Serial.print("°C, Humidity: ");
  203.     Serial.print(sensorHumidity, 1);
  204.     Serial.println("%");
  205. }
  206.  
  207. /****** displaySensorData() - DISPLAY REAL-TIME SENSOR DATA WITH TEXT FORMATTING *****/
  208. void displaySensorData(void)
  209. {
  210.     // Set font mode for U8g2 (transparent mode)
  211.     u8g2_for_adafruit_gfx.setFontMode(1);
  212.    
  213.     // Set font direction (left to right)
  214.     u8g2_for_adafruit_gfx.setFontDirection(0);
  215.    
  216.     // Set foreground color (white text)
  217.     u8g2_for_adafruit_gfx.setForegroundColor(SSD1306_WHITE);
  218.    
  219.     // Display header with larger font
  220.     u8g2_for_adafruit_gfx.setFont(u8g2_font_helvB14_tf);
  221.     u8g2_for_adafruit_gfx.setCursor(0, 18);
  222.     u8g2_for_adafruit_gfx.print(F("DHT22 DATA"));
  223.    
  224.     // Check if sensor is ready and display status
  225.     if (!sensorReady)
  226.     {
  227.         u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR10_tf);
  228.         u8g2_for_adafruit_gfx.setCursor(0, 35);
  229.         u8g2_for_adafruit_gfx.print(F("Sensor Error"));
  230.         return;
  231.     }
  232.    
  233.     // Display temperature with formatted text
  234.     u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR12_tf);
  235.     u8g2_for_adafruit_gfx.setCursor(0, 35);
  236.     u8g2_for_adafruit_gfx.print(F("Temp: "));
  237.     u8g2_for_adafruit_gfx.print(sensorTemperature, 1);
  238.     u8g2_for_adafruit_gfx.print(F("\xb0C"));
  239.    
  240.     // Display humidity with formatted text
  241.     u8g2_for_adafruit_gfx.setCursor(0, 50);
  242.     u8g2_for_adafruit_gfx.print(F("Humidity: "));
  243.     u8g2_for_adafruit_gfx.print(sensorHumidity, 1);
  244.     u8g2_for_adafruit_gfx.print(F("%"));
  245.    
  246.     // Display read count
  247.     u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR08_tf);
  248.     u8g2_for_adafruit_gfx.setCursor(0, 62);
  249.     u8g2_for_adafruit_gfx.print(F("Reads: "));
  250.     u8g2_for_adafruit_gfx.print(dhtReadCounter);
  251. }
  252.  
  253. /****** updateDisplay() - UPDATE DISPLAY WITH REAL-TIME SENSOR DATA EVERY 2 SECONDS *****/
  254. void updateDisplay(void)
  255. {
  256.     // Clear the display buffer to erase previous content
  257.     display.clearDisplay();
  258.    
  259.     // Display sensor data with formatted text
  260.     displaySensorData();
  261.    
  262.     // Push the display buffer to the physical display
  263.     display.display();
  264.    
  265.     // Debug output to Serial
  266.     Serial.print("Display updated with sensor data - Temp: ");
  267.     Serial.print(sensorTemperature, 1);
  268.     Serial.print("°C, Humidity: ");
  269.     Serial.print(sensorHumidity, 1);
  270.     Serial.println("%");
  271. }
  272.  
  273. /* END CODE */
  274.  
Advertisement
Add Comment
Please, Sign In to add comment