/********* Rui Santos Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-thermostat-web-server/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. *********/ #ifdef ESP32 #include #include #else #include #include #endif #include #include #include #include // REPLACE WITH YOUR NETWORK CREDENTIALS const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // Default Threshold Temperature Value String inputMessage = "25.0"; String lastTemperature; String enableArmChecked = "checked"; String inputMessage2 = "true"; // HTML web page to handle 2 input fields (threshold_input, enable_arm_input) const char index_html[] PROGMEM = R"rawliteral( Temperature Threshold Output Control

DS18B20 Temperature

%TEMPERATURE% °C

ESP Arm Trigger

Temperature Threshold
Arm Trigger

)rawliteral"; void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); } AsyncWebServer server(80); // Replaces placeholder with DS18B20 values String processor(const String& var){ //Serial.println(var); if(var == "TEMPERATURE"){ return lastTemperature; } else if(var == "THRESHOLD"){ return inputMessage; } else if(var == "ENABLE_ARM_INPUT"){ return enableArmChecked; } return String(); } // Flag variable to keep track if triggers was activated or not bool triggerActive = false; const char* PARAM_INPUT_1 = "threshold_input"; const char* PARAM_INPUT_2 = "enable_arm_input"; // Interval between sensor readings. Learn more about ESP32 timers: https://RandomNerdTutorials.com/esp32-pir-motion-sensor-interrupts-timers/ unsigned long previousMillis = 0; const long interval = 5000; // GPIO where the output is connected to const int output = 2; // GPIO where the DS18B20 is connected to const int oneWireBus = 4; // Setup a oneWire instance to communicate with any OneWire devices OneWire oneWire(oneWireBus); // Pass our oneWire reference to Dallas Temperature sensor DallasTemperature sensors(&oneWire); void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); if (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("WiFi Failed!"); return; } Serial.println(); Serial.print("ESP IP Address: http://"); Serial.println(WiFi.localIP()); pinMode(output, OUTPUT); digitalWrite(output, LOW); // Start the DS18B20 sensor sensors.begin(); // Send web page to client server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html, processor); }); // Receive an HTTP GET request at /get?threshold_input=&enable_arm_input= server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) { // GET threshold_input value on /get?threshold_input= if (request->hasParam(PARAM_INPUT_1)) { inputMessage = request->getParam(PARAM_INPUT_1)->value(); // GET enable_arm_input value on /get?enable_arm_input= if (request->hasParam(PARAM_INPUT_2)) { inputMessage2 = request->getParam(PARAM_INPUT_2)->value(); enableArmChecked = "checked"; } else { inputMessage2 = "false"; enableArmChecked = ""; } } Serial.println(inputMessage); Serial.println(inputMessage2); request->send(200, "text/html", "HTTP GET request sent to your ESP.
Return to Home Page"); }); server.onNotFound(notFound); server.begin(); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; sensors.requestTemperatures(); // Temperature in Celsius degrees float temperature = sensors.getTempCByIndex(0); Serial.print(temperature); Serial.println(" *C"); // Temperature in Fahrenheit degrees /*float temperature = sensors.getTempFByIndex(0); Serial.print(temperature); Serial.println(" *F");*/ lastTemperature = String(temperature); // Check if temperature is above threshold and if it needs to trigger output if(temperature > inputMessage.toFloat() && inputMessage2 == "true" && !triggerActive){ String message = String("Temperature above threshold. Current temperature: ") + String(temperature) + String("C"); Serial.println(message); triggerActive = true; digitalWrite(output, HIGH); } // Check if temperature is below threshold and if it needs to trigger output else if((temperature < inputMessage.toFloat()) && inputMessage2 == "true" && triggerActive) { String message = String("Temperature below threshold. Current temperature: ") + String(temperature) + String(" C"); Serial.println(message); triggerActive = false; digitalWrite(output, LOW); } } }