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: 002
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2026-03-15 00:36:34
- ********* 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 */
- /****** 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"
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <Adafruit_SSD1306.h>
- #include <U8g2_for_Adafruit_GFX.h>
- #include <Adafruit_GFX.h>
- #include <DHT.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initializeDisplay(void);
- void initializeDHT22(void);
- void readDHT22Sensor(void);
- void displaySensorData(void);
- void updateDisplay(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);
- /***** DEFINITION OF TIMING VARIABLES *****/
- unsigned long lastDisplayUpdate = 0;
- const unsigned long DISPLAY_UPDATE_INTERVAL = 2000;
- /***** DEFINITION OF SENSOR DATA VARIABLES *****/
- float sensorTemperature = 0.0;
- float sensorHumidity = 0.0;
- boolean sensorReady = false;
- unsigned long dhtReadCounter = 0;
- /****** 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 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 the update timer
- lastDisplayUpdate = millis();
- Serial.println("Setup completed successfully");
- }
- /****** loop() - MAIN EXECUTION LOOP *****/
- void loop(void)
- {
- // Read DHT22 sensor 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();
- }
- // 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("%");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment