/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-cooling-system-using-ds18b20-temperature-sensor */ //Modified by Kishore Gaikwad, 23-Apr-2022 #include #include #include #include #include #include #include #include LiquidCrystal_I2C lcd(0x27,16,2); #define SECRET_SSID "MySSID" // replace MySSID with your WiFi network name #define SECRET_PASS "MyPass" // replace MyPass word with your WiFi password #define SECRET_CH_ID 0000000 // replace 0000000 with your channel number #define SECRET_WRITE_APIKEY "XYZ" // replace XYZ with your channel write API Key char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password int keyIndex = 0; // your network key Index number (needed only for WEP) WiFiClient client; unsigned long myChannelNumber = SECRET_CH_ID; const char *myWriteAPIKey = SECRET_WRITE_APIKEY; #define TEMP_UPPER_THRESHOLD 36 // upper temperature threshold //#define TEMP_LOWER_THRESHOLD 34 // lower temperature threshold #define MQ2pin 13 #define SENSOR_PIN 12 // ESP32 pin connected to DS18B20 sensor's DQ pin #define RELAY_FAN_PIN 26 // ESP32 pin connected to relay float sensorValue; //variable to store sensor value OneWire oneWire(SENSOR_PIN); DallasTemperature DS18B20(&oneWire); void setup() { Serial.begin(115200); // initialize serial DS18B20.begin(); // initialize the DS18B20 sensor pinMode(RELAY_FAN_PIN, OUTPUT); pinMode(MQ2pin, INPUT); lcd.begin(); lcd.backlight(); WiFi.mode(WIFI_STA); ThingSpeak.begin(client); // Initialize ThingSpeak } void loop() { // Connect or reconnect to WiFi if (WiFi.status() != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(SECRET_SSID); while (WiFi.status() != WL_CONNECTED) { WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network Serial.print("."); delay(4000); } Serial.println("\nConnected."); } sensorValue = digitalRead(MQ2pin); // read analog input pin 0 Serial.print("Sensor Value: "); Serial.print(sensorValue); Serial.print("\t"); DS18B20.requestTemperatures(); // send the command to get temperatures float temperature = DS18B20.getTempCByIndex(0); // read temperature in Celsius Serial.print(temperature); Serial.println("ºC"); lcd.clear(); lcd.setCursor(0, 0); // set cursor to first column, first row lcd.print(temperature); // print temp on lcd lcd.print((char)223); // display ° character lcd.print("C"); if ((temperature > TEMP_UPPER_THRESHOLD)&& (sensorValue > 200)){ Serial.println("Turn the fan on"); lcd.setCursor(0, 1); lcd.print("Turn the fan on"); digitalWrite(RELAY_FAN_PIN, LOW); // turn on } else if ((temperature < TEMP_UPPER_THRESHOLD)&& (sensorValue < 200)) { Serial.println("Turn the fan off"); lcd.setCursor(0, 1); lcd.print("Turn the fan off"); digitalWrite(RELAY_FAN_PIN, HIGH); // turn off } if ((temperature > TEMP_UPPER_THRESHOLD)|| (sensorValue > 200)){ Serial.println("Turn the fan on"); lcd.setCursor(0, 1); lcd.print("Turn the fan on"); digitalWrite(RELAY_FAN_PIN, LOW); // turn on } else if ((temperature < TEMP_UPPER_THRESHOLD)&& (sensorValue < 200)) { Serial.println("Turn the fan off"); lcd.setCursor(0, 1); lcd.print("Turn the fan off"); digitalWrite(RELAY_FAN_PIN, HIGH); // turn off } delay(500); int x = ThingSpeak.writeField(myChannelNumber, 1,temperature, myWriteAPIKey); //dry atp temp int y = ThingSpeak.writeField(myChannelNumber, 2,sensorValue, myWriteAPIKey); //gas values }