So, from my previous post, I continue my project adding DHT22 module. As far, it works fine. I want to modify the program which make the buzzer on if the relative humidity reachs threshold value. But the code turn to be error, and it said "event. not declared". Can anybody fix my program? I'm sure there's something wrong, I'm still a newbie. Also I want to know how to declare the variable that we get from the module?
Note: I'm using active buzzer 5V which I connect to pin 16 of ESP32 series with resistor 220 ohm.
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Adafruit_Sensor.h>
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire, -1);
DHT_Unified dht(4, DHT22); //DHT connected to pin 4 of ESP32
unsigned long lastdisplay = 0;
**float event.relative_humidity;** // i'm sure this is wrong, but I want to find a way to declare the **event.relative.humidity**
**int buzzerpin = 16;** //buzzer connected to pin 4 of ESP32
void setup(){
Serial.begin(115200);
**pinMode(16,OUTPUT);**
rtc.begin();
//rtc.adjust(DateTime(__DATE__, __TIME__));
dht.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Processing...");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
display.print("Processing...");
display.display();
delay(3000);
lcd.clear(); //clear the lcd before shows the parameter
}
void loop(){
unsigned long currentMillis = millis();
DateTime now = rtc.now();
if (currentMillis - lastdisplay >= 1000) {
lastdisplay = currentMillis;
Serial.printf("Time: %02d:%02d:%02d\n", now.hour(), now.minute(), now.second()); //to display 2 digits for second
lcd.setCursor(11,0); // Move cursor to correct position
lcd.printf("%02d:%02d:%02d", now.hour(), now.minute(), now.second()); //to display 2 digits for second
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0);
display.printf("%02d:%02d:%02d\n", now.hour(), now.minute(), now.second()); //to display 2 digits for second
display.display();
//Serial print and display DHT Temperature on LCD
sensors_event_t event;
dht.temperature().getEvent(&event);
Serial.print("Temp: "); Serial.print(event.temperature);Serial.println("°C");
lcd.setCursor(10,2);lcd.print("T :");lcd.print(event.temperature);lcd.print(" C");
//Serial print and display DHT Relative Humidity on LCD
dht.humidity().getEvent(&event);
Serial.print("RH:");Serial.print(event.relative_humidity);Serial.println("%");
Serial.println("");
lcd.setCursor(10,3);lcd.print("RH:");lcd.print(event.relative_humidity);lcd.print(" %");
}
//Buzzer on if RH = 86.70%
**if (event.relative_humidity <= 86.70) {**
** digitalWrite(buzzerpin,HIGH);**
** }**
** else{**
** digitalWrite(buzzerpin,LOW);**
** }**
**}**
The program shows error when i add the bold code