//filename: ESP32 WiFi and alarmerer test(20221021)
//filename: ESP32 temperature and humidity monitor (20221028) v2
#include <DHT.h>
#include "WiFi.h"
#define DHTTYPE DHT22 //Ser DHT 22 (AM2302) to D22
#define EnablePIN 2 //Set Enable flag to pin 2 (D2)
#define DHTPIN 4 //Set DHT sensor to pin 4 (D4)
#define AlarmPin 12 //Set alarm sensor to pin 12
//Setup WiFi
const char ssid[]="張俊隆 的 iPhone"; //修改為你家的WiFi網路名稱
const char pwd[]="0922909128"; //修改為你家的WiFi密碼
//dht DHT;
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
int HumidityTh = 80; //Set Humidity threshold
int HighTemperatureTh = 36; //Set high temperature threshold
int LowTemperatureTh = 12; //Set low temperature threshold
void setup() {
// put your setup code here, to run once:
//Show ESP32 connected
Serial.begin(115200);
Serial.println("Hello, Vincent's ESP32!");
WiFi.mode(WIFI_STA); //設置WiFi模式
WiFi.begin(ssid,pwd);
/*
//當WiFi連線時會回傳WL_CONNECTED,因此跳出迴圈時代表已成功連線
while(WiFi.status()!= WL_CONNECTED){
Serial.print(".");
delay(1000);
}
}
*/
pinMode(EnablePIN, INPUT); //set alert switch input
pinMode(DHTPIN, INPUT); //set DHT22, i.e, tempature, humidity sensoe input
//pinMode(DigicLogicPin, INPUT); //set DHT22, i.e, tempature, humidity sensoe input
pinMode(22, OUTPUT); //set buzzer output when alert
//pinMode(13, OUTPUT); //set LED output
Serial.begin(19200);
Serial.println("DHT22_test.ino");
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
//int DigicLogicPinON = digitalRead(DigicLogicPin);
//Serial.println("DigicLogic PIN ON: ", DigicLogicPinON);
//Serial.println(DigicLogicPinON);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
int UncomfotableFlag = ( (h > HumidityTh ) || ( t > HighTemperatureTh) )
|| ( t < LowTemperatureTh);
int EnablePinFlag = digitalRead(EnablePIN);
if ( ( EnablePinFlag == 1 ) && (UncomfotableFlag == 1) ) {
Serial.println("Uncomfotable enviroment!");
tone(AlarmPin, 300, 250); // Plays 300Hz tone for 0.250 seconds
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}