/* Use ESP32 to read temperature values of DHT11. Control the on/off cycles of a fan (actuator) by threshold values. */ /* Slowly combining parts of the code. Heating & lighting components, no MQTT. */ #include "DHT.h" #include int light = 15; //LDR readings taken and stored at pin A0. int DHTPIN = 4; //DHT pin to store and take readings at A1. int LEDPIN_1 = 27; //LED pins used to react to light value differences. int LEDPIN_2 = 14; int FAN = 26; //Fan to simulate actuator heat pump, reacts to temperature input. #define DHTTYPE DHT11 // defining DHT type for ESP32 and scope in general. DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); //initiate serial. Serial.print(F("DHT11 Test")); dht.begin(); pinMode(15, INPUT); //setting LDR pin as input. pinMode(LEDPIN_1, OUTPUT); //Setting LEDs as outputs, since they are reacting to incoming light data pinMode(LEDPIN_2, OUTPUT); } void loop() { //DHT int temperature = dht.readTemperature(); // read Temperature in °C Serial.print(temperature); String temperatureString = String(temperature); //Serialprintln(hic); Serial.println(F("°C")); delay (2000); Serial.println("The string equivalent to integer temperature is " + temperatureString); char* TempArr = &temperatureString[0]; //Serial.println(TempArr); //Fan off-on cycle if (temperature > 30) { Serial.println("Its becoming hot in here - The fan is on"); digitalWrite(FAN, HIGH); } else { Serial.println("ITs cold or tolerable; lets save money!"); digitalWrite(FAN, LOW); } //LDR light = analogRead(15); //read light value from LDR in Ohms EVALUATE SHOUÖD LIGHT NOT 15 Serial.println(light); if (light < 1300) { Serial.println("It very bright, lots of natural light"); Serial.println("Both LEDs are off"); digitalWrite(LEDPIN_1, LOW); digitalWrite(LEDPIN_2, LOW); } else if (light > 1300 && light < 2500) { //medium amount of light to show one LED at a time). Serial.println("moderate light quantity, quite cloudy"); digitalWrite(LEDPIN_1, HIGH); digitalWrite(LEDPIN_2, LOW); } else { // both LEDs turn on when the surroundings are dark. Serial.println("Its very dark, extremely overcast or night time"); digitalWrite(LEDPIN_1, HIGH); digitalWrite(LEDPIN_2, HIGH); } delay(750); //750ms delay between sensor readings. }