siosin

thermostat

Apr 1st, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. /*********
  2. Rui Santos
  3. Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-thermostat-web-server/
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files.
  7.  
  8. The above copyright notice and this permission notice shall be included in all
  9. copies or substantial portions of the Software.
  10. *********/
  11.  
  12. #ifdef ESP32
  13. #include <WiFi.h>
  14. #include <AsyncTCP.h>
  15. #else
  16. #include <ESP8266WiFi.h>
  17. #include <ESPAsyncTCP.h>
  18. #endif
  19. #include <ESPAsyncWebServer.h>
  20. #include <Wire.h>
  21. #include <OneWire.h>
  22. #include <DallasTemperature.h>
  23.  
  24. // REPLACE WITH YOUR NETWORK CREDENTIALS
  25. const char* ssid = "REPLACE_WITH_YOUR_SSID";
  26. const char* password = "REPLACE_WITH_YOUR_PASSWORD";
  27.  
  28. // Default Threshold Temperature Value
  29. String inputMessage = "25.0";
  30. String lastTemperature;
  31. String enableArmChecked = "checked";
  32. String inputMessage2 = "true";
  33.  
  34. // HTML web page to handle 2 input fields (threshold_input, enable_arm_input)
  35. const char index_html[] PROGMEM = R"rawliteral(
  36. <!DOCTYPE HTML><html><head>
  37. <title>Temperature Threshold Output Control</title>
  38. <meta name="viewport" content="width=device-width, initial-scale=1">
  39. </head><body>
  40. <h2>DS18B20 Temperature</h2>
  41. <h3>%TEMPERATURE% &deg;C</h3>
  42. <h2>ESP Arm Trigger</h2>
  43. <form action="/get">
  44. Temperature Threshold <input type="number" step="0.1" name="threshold_input" value="%THRESHOLD%" required><br>
  45. Arm Trigger <input type="checkbox" name="enable_arm_input" value="true" %ENABLE_ARM_INPUT%><br><br>
  46. <input type="submit" value="Submit">
  47. </form>
  48. </body></html>)rawliteral";
  49.  
  50. void notFound(AsyncWebServerRequest *request) {
  51. request->send(404, "text/plain", "Not found");
  52. }
  53.  
  54. AsyncWebServer server(80);
  55.  
  56. // Replaces placeholder with DS18B20 values
  57. String processor(const String& var){
  58. //Serial.println(var);
  59. if(var == "TEMPERATURE"){
  60. return lastTemperature;
  61. }
  62. else if(var == "THRESHOLD"){
  63. return inputMessage;
  64. }
  65. else if(var == "ENABLE_ARM_INPUT"){
  66. return enableArmChecked;
  67. }
  68. return String();
  69. }
  70.  
  71. // Flag variable to keep track if triggers was activated or not
  72. bool triggerActive = false;
  73.  
  74. const char* PARAM_INPUT_1 = "threshold_input";
  75. const char* PARAM_INPUT_2 = "enable_arm_input";
  76.  
  77. // Interval between sensor readings. Learn more about ESP32 timers: https://RandomNerdTutorials.com/esp32-pir-motion-sensor-interrupts-timers/
  78. unsigned long previousMillis = 0;
  79. const long interval = 5000;
  80.  
  81. // GPIO where the output is connected to
  82. const int output = 2;
  83.  
  84. // GPIO where the DS18B20 is connected to
  85. const int oneWireBus = 4;
  86. // Setup a oneWire instance to communicate with any OneWire devices
  87. OneWire oneWire(oneWireBus);
  88. // Pass our oneWire reference to Dallas Temperature sensor
  89. DallasTemperature sensors(&oneWire);
  90.  
  91. void setup() {
  92. Serial.begin(115200);
  93. WiFi.mode(WIFI_STA);
  94. WiFi.begin(ssid, password);
  95. if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  96. Serial.println("WiFi Failed!");
  97. return;
  98. }
  99. Serial.println();
  100. Serial.print("ESP IP Address: http://");
  101. Serial.println(WiFi.localIP());
  102.  
  103. pinMode(output, OUTPUT);
  104. digitalWrite(output, LOW);
  105.  
  106. // Start the DS18B20 sensor
  107. sensors.begin();
  108.  
  109. // Send web page to client
  110. server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  111. request->send_P(200, "text/html", index_html, processor);
  112. });
  113.  
  114. // Receive an HTTP GET request at <ESP_IP>/get?threshold_input=<inputMessage>&enable_arm_input=<inputMessage2>
  115. server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
  116. // GET threshold_input value on <ESP_IP>/get?threshold_input=<inputMessage>
  117. if (request->hasParam(PARAM_INPUT_1)) {
  118. inputMessage = request->getParam(PARAM_INPUT_1)->value();
  119. // GET enable_arm_input value on <ESP_IP>/get?enable_arm_input=<inputMessage2>
  120. if (request->hasParam(PARAM_INPUT_2)) {
  121. inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
  122. enableArmChecked = "checked";
  123. }
  124. else {
  125. inputMessage2 = "false";
  126. enableArmChecked = "";
  127. }
  128. }
  129. Serial.println(inputMessage);
  130. Serial.println(inputMessage2);
  131. request->send(200, "text/html", "HTTP GET request sent to your ESP.<br><a href=\"/\">Return to Home Page</a>");
  132. });
  133. server.onNotFound(notFound);
  134. server.begin();
  135. }
  136.  
  137. void loop() {
  138. unsigned long currentMillis = millis();
  139. if (currentMillis - previousMillis >= interval) {
  140. previousMillis = currentMillis;
  141. sensors.requestTemperatures();
  142. // Temperature in Celsius degrees
  143. float temperature = sensors.getTempCByIndex(0);
  144. Serial.print(temperature);
  145. Serial.println(" *C");
  146.  
  147. // Temperature in Fahrenheit degrees
  148. /*float temperature = sensors.getTempFByIndex(0);
  149. Serial.print(temperature);
  150. Serial.println(" *F");*/
  151.  
  152. lastTemperature = String(temperature);
  153.  
  154. // Check if temperature is above threshold and if it needs to trigger output
  155. if(temperature > inputMessage.toFloat() && inputMessage2 == "true" && !triggerActive){
  156. String message = String("Temperature above threshold. Current temperature: ") +
  157. String(temperature) + String("C");
  158. Serial.println(message);
  159. triggerActive = true;
  160. digitalWrite(output, HIGH);
  161. }
  162. // Check if temperature is below threshold and if it needs to trigger output
  163. else if((temperature < inputMessage.toFloat()) && inputMessage2 == "true" && triggerActive) {
  164. String message = String("Temperature below threshold. Current temperature: ") +
  165. String(temperature) + String(" C");
  166. Serial.println(message);
  167. triggerActive = false;
  168. digitalWrite(output, LOW);
  169. }
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment