ESP8266-WiFi-Thermostat.ino 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
  2. #include <PubSubClient.h>
  3. #include <Wire.h>
  4. #include "LiquidCrystal_I2C.h"
  5. //#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
  6. //#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
  7. //#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
  8. #define DHTPIN 13
  9. #define DHTTYPE DHT22
  10. #define RELAISPIN 16
  11. #define MINUSBUTTONPIN 14
  12. #define PLUSBUTTONPIN 12
  13. #define MODEBUTTONPIN 0
  14. #define LCDADDR 0x27
  15. #define LUXSENSORADDR 0x23
  16. #include "DHT.h"
  17. DHT dht(DHTPIN, DHTTYPE);
  18. LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  19. float targetTemp;
  20. float targetTempDay = 23.5;
  21. float targetTempNight = 19.0;
  22. float hysteresis = 0.5;
  23. int measureinterval = 10000;
  24. byte heatingMode = 1;
  25. // 0 = off
  26. // 1 = default/day
  27. // 2 = night
  28. bool turnHeatingOn = false;
  29. float humidity, temperature;
  30. const char* ssid = "KS61T5SH"; // WiFi SSID
  31. const char* password = "Gurken651salat"; // WiFi PWD
  32. const char* mqtt_server = "10.1.1.11";
  33. const int mqtt_port = 1883;
  34. const char* mqtt_topic_prefix = "Test/Thermostat/";
  35. const char* mqtt_topic_temp = "Test/Thermostat/Temp";
  36. const char* mqtt_topic_hum = "Test/Thermostat/Hum";
  37. WiFiClient espClient;
  38. PubSubClient client(espClient);
  39. //PubSubClient client(server, port, callback, wifiClient);
  40. long lastMsg = 0;
  41. char msg[50];
  42. //int value = 0;
  43. char topic[50];
  44. // Variables for printing temp and humidity
  45. String temp_str; //see last code block below use these to convert the float that you get back from DHT to a string =str
  46. String hum_str;
  47. char temp[50];
  48. char hum[50];
  49. // LCD
  50. byte customCharSun[8] = {0b00100, 0b10001, 0b01110, 0b11111, 0b11111, 0b01110, 0b10001, 0b00100};
  51. byte customCharMoon[8] = {0b11000, 0b01110, 0b00110, 0b00111, 0b00111, 0b00110, 0b01110, 0b11000};
  52. byte customCharArrowRight[8] = {0b00000, 0b00100, 0b00110, 0b11111, 0b11111, 0b00110, 0b00100, 0b00000};
  53. void setup_wifi() {
  54. delay(10);
  55. // We start by connecting to a WiFi network
  56. Serial.println();
  57. Serial.print("Connecting to ");
  58. Serial.println(ssid);
  59. WiFi.begin(ssid, password);
  60. while (WiFi.status() != WL_CONNECTED) {
  61. delay(500);
  62. Serial.print(".");
  63. }
  64. randomSeed(micros());
  65. Serial.println("");
  66. Serial.println("WiFi connected");
  67. Serial.println("IP address: ");
  68. Serial.println(WiFi.localIP());
  69. }
  70. void callback(char* topic, byte* payload, unsigned int length) {
  71. Serial.print("Message arrived [");
  72. Serial.print(topic);
  73. Serial.print("] ");
  74. for (int i = 0; i < length; i++) {
  75. Serial.print((char)payload[i]);
  76. }
  77. Serial.println();
  78. // Switch on the LED if an 1 was received as first character
  79. if ((char)payload[0] == '1') {
  80. digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  81. // but actually the LED is on; this is because
  82. // it is acive low on the ESP-01)
  83. } else {
  84. digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
  85. }
  86. }
  87. void reconnect() {
  88. // Loop until we're reconnected
  89. while (!client.connected()) {
  90. Serial.print("Attempting MQTT connection...");
  91. // Create a random client ID
  92. String clientId = "ESP8266Client-";
  93. clientId += String(random(0xffff), HEX);
  94. // Attempt to connect
  95. if (client.connect(clientId.c_str())) {
  96. Serial.println("connected");
  97. // Once connected, publish an announcement...
  98. client.publish("outTopic", "hello world");
  99. // ... and resubscribe
  100. client.subscribe("inTopic");
  101. } else {
  102. Serial.print("failed, rc=");
  103. Serial.print(client.state());
  104. Serial.println(" try again in 5 seconds");
  105. // Wait 5 seconds before retrying
  106. delay(5000);
  107. }
  108. }
  109. }
  110. void setup() {
  111. //WiFiManager wifiManager;
  112. //wifiManager.autoConnect("Thermostat", "configesp");
  113. //wifiManager.setConfigPortalTimeout(180);
  114. Serial.begin(115200);
  115. dht.begin();
  116. setup_wifi();
  117. client.setServer(mqtt_server, mqtt_port);
  118. client.setCallback(callback);
  119. lcd.init();
  120. lcd.backlight();
  121. lcd.createChar(0, customCharMoon);
  122. lcd.createChar(1, customCharSun);
  123. lcd.createChar(2, customCharArrowRight);
  124. lcd.print("SERVAS!");
  125. delay(1000);
  126. }
  127. void loop() {
  128. if (!client.connected()) {
  129. reconnect();
  130. }
  131. client.loop();
  132. long now = millis();
  133. if (now - lastMsg > measureinterval) {
  134. lastMsg = now;
  135. humidity = dht.readHumidity();
  136. temperature = dht.readTemperature(); // Read temperature as Celsius (the default)
  137. hum_str = String(humidity); //converting humidity (the float variable above) to a string
  138. hum_str.toCharArray(hum, hum_str.length() + 1); //packaging up the data to publish to mqtt whoa...
  139. temp_str = String(temperature); //converting Temperature (the float variable above) to a string
  140. temp_str.toCharArray(temp, temp_str.length() + 1); //packaging up the data to publish to mqtt whoa...
  141. char tempstr2[5]; //result string 4 positions + \0 at the end
  142. //
  143. // convert float to fprintf type string
  144. // format 5 positions with 3 decimal places
  145. //
  146. dtostrf(temperature, 2, 1, tempstr2 );
  147. char humstr2[3]; //result string 4 positions + \0 at the end
  148. //
  149. // convert float to fprintf type string
  150. // format 5 positions with 3 decimal places
  151. //
  152. dtostrf(humidity, 2, 0, humstr2 );
  153. //snprintf (msg, 75, "hello world #%ld", value);
  154. Serial.println("Publish messages temp/hum ");
  155. client.publish(mqtt_topic_temp, temp);
  156. client.publish(mqtt_topic_hum, hum);
  157. if ( heatingMode == 1 ) targetTemp = targetTempDay;
  158. else if ( heatingMode == 2 ) targetTemp = targetTempNight;
  159. else targetTemp = 0;
  160. Serial.print("Target temp: ");
  161. Serial.println(targetTemp);
  162. Serial.print("Current temp: ");
  163. Serial.println(temperature);
  164. lcd.setCursor(0, 0);
  165. //lcd.print("Ist:");
  166. lcd.write(0x3D); // = Zeichen
  167. // if (temperature < -9) lcd.print(" ");
  168. // else if (temperature < 0) lcd.print("");
  169. // else if (temperature < 10) lcd.print(" ");
  170. // else lcd.print(" ");
  171. lcd.print(tempstr2);
  172. lcd.write(0xDF); // Grad-Symbol
  173. lcd.print(" ");
  174. lcd.print(humstr2);
  175. lcd.print("% ");
  176. if (turnHeatingOn) lcd.print("HEIZT");
  177. else lcd.print(" ");
  178. char targettempstr[5]; //result string 4 positions + \0 at the end
  179. //
  180. // convert float to fprintf type string
  181. // format 5 positions with 3 decimal places
  182. //
  183. dtostrf(targetTemp, 2, 1, targettempstr );
  184. lcd.setCursor(0, 1);
  185. //lcd.print("Soll: ");
  186. lcd.write((uint8_t)2); // Pfeil rechts
  187. lcd.print(targettempstr);
  188. lcd.write(0xDF); // Grad-Symbol
  189. lcd.print(" ");
  190. // if ( heatingMode == 1 ) lcd.write((uint8_t)1); // Sonne
  191. // else if ( heatingMode == 2 ) lcd.write((uint8_t)0); // Mond
  192. // else lcd.write(0x2D); // -
  193. if ( heatingMode == 1 ) lcd.print(" Tag");
  194. else if ( heatingMode == 2 ) lcd.print(" Nacht");
  195. else lcd.print(" Aus");
  196. // lcd.print(" ");
  197. //
  198. // if(turnHeatingOn) lcd.print("1");
  199. // else lcd.print("0");
  200. if ( temperature >= targetTemp ) {
  201. turnHeatingOn = false;
  202. Serial.println("heating off");
  203. }
  204. else if ( temperature < ( targetTemp - hysteresis) ) {
  205. turnHeatingOn = true;
  206. Serial.println("heating on");
  207. }
  208. }
  209. // Check if any reads failed and exit early (to try again).
  210. //if (isnan(hum) || isnan(temp)) {
  211. // Serial.println("Failed to read from DHT sensor!");
  212. // return;
  213. //}
  214. /*Serial.print("Humidity: ");
  215. Serial.print(hum);
  216. Serial.print(" %\t");
  217. Serial.print("Temperature: ");
  218. Serial.print(temp);
  219. Serial.print(" *C ");
  220. Serial.println();*/
  221. }