123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- #include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
- #include <PubSubClient.h>
- #include <Wire.h>
- #include "LiquidCrystal_I2C.h"
- //#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
- //#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
- //#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
- #define DHTPIN 13
- #define DHTTYPE DHT22
- #define RELAISPIN 16
- #define MINUSBUTTONPIN 14
- #define PLUSBUTTONPIN 12
- #define MODEBUTTONPIN 0
- #define LCDADDR 0x27
- #define LUXSENSORADDR 0x23
- #include "DHT.h"
- DHT dht(DHTPIN, DHTTYPE);
- LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
- float targetTemp;
- float targetTempDay = 23.5;
- float targetTempNight = 19.0;
- float hysteresis = 0.5;
- int measureinterval = 10000;
- byte heatingMode = 1;
- // 0 = off
- // 1 = default/day
- // 2 = night
- bool turnHeatingOn = false;
- float humidity, temperature;
- const char* ssid = "KS61T5SH"; // WiFi SSID
- const char* password = "Gurken651salat"; // WiFi PWD
- const char* mqtt_server = "10.1.1.11";
- const int mqtt_port = 1883;
- const char* mqtt_topic_prefix = "Test/Thermostat/";
- const char* mqtt_topic_temp = "Test/Thermostat/Temp";
- const char* mqtt_topic_hum = "Test/Thermostat/Hum";
- WiFiClient espClient;
- PubSubClient client(espClient);
- //PubSubClient client(server, port, callback, wifiClient);
- long lastMsg = 0;
- char msg[50];
- //int value = 0;
- char topic[50];
- // Variables for printing temp and humidity
- String temp_str; //see last code block below use these to convert the float that you get back from DHT to a string =str
- String hum_str;
- char temp[50];
- char hum[50];
- // LCD
- byte customCharSun[8] = {0b00100, 0b10001, 0b01110, 0b11111, 0b11111, 0b01110, 0b10001, 0b00100};
- byte customCharMoon[8] = {0b11000, 0b01110, 0b00110, 0b00111, 0b00111, 0b00110, 0b01110, 0b11000};
- byte customCharArrowRight[8] = {0b00000, 0b00100, 0b00110, 0b11111, 0b11111, 0b00110, 0b00100, 0b00000};
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- randomSeed(micros());
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- // Switch on the LED if an 1 was received as first character
- if ((char)payload[0] == '1') {
- digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
- // but actually the LED is on; this is because
- // it is acive low on the ESP-01)
- } else {
- digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
- }
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Create a random client ID
- String clientId = "ESP8266Client-";
- clientId += String(random(0xffff), HEX);
- // Attempt to connect
- if (client.connect(clientId.c_str())) {
- Serial.println("connected");
- // Once connected, publish an announcement...
- client.publish("outTopic", "hello world");
- // ... and resubscribe
- client.subscribe("inTopic");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- //WiFiManager wifiManager;
- //wifiManager.autoConnect("Thermostat", "configesp");
- //wifiManager.setConfigPortalTimeout(180);
- Serial.begin(115200);
- dht.begin();
- setup_wifi();
- client.setServer(mqtt_server, mqtt_port);
- client.setCallback(callback);
- lcd.init();
- lcd.backlight();
- lcd.createChar(0, customCharMoon);
- lcd.createChar(1, customCharSun);
- lcd.createChar(2, customCharArrowRight);
- lcd.print("SERVAS!");
- delay(1000);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- long now = millis();
- if (now - lastMsg > measureinterval) {
- lastMsg = now;
- humidity = dht.readHumidity();
- temperature = dht.readTemperature(); // Read temperature as Celsius (the default)
- hum_str = String(humidity); //converting humidity (the float variable above) to a string
- hum_str.toCharArray(hum, hum_str.length() + 1); //packaging up the data to publish to mqtt whoa...
- temp_str = String(temperature); //converting Temperature (the float variable above) to a string
- temp_str.toCharArray(temp, temp_str.length() + 1); //packaging up the data to publish to mqtt whoa...
- char tempstr2[5]; //result string 4 positions + \0 at the end
- //
- // convert float to fprintf type string
- // format 5 positions with 3 decimal places
- //
- dtostrf(temperature, 2, 1, tempstr2 );
- char humstr2[3]; //result string 4 positions + \0 at the end
- //
- // convert float to fprintf type string
- // format 5 positions with 3 decimal places
- //
- dtostrf(humidity, 2, 0, humstr2 );
- //snprintf (msg, 75, "hello world #%ld", value);
- Serial.println("Publish messages temp/hum ");
- client.publish(mqtt_topic_temp, temp);
- client.publish(mqtt_topic_hum, hum);
- if ( heatingMode == 1 ) targetTemp = targetTempDay;
- else if ( heatingMode == 2 ) targetTemp = targetTempNight;
- else targetTemp = 0;
- Serial.print("Target temp: ");
- Serial.println(targetTemp);
- Serial.print("Current temp: ");
- Serial.println(temperature);
- lcd.setCursor(0, 0);
- //lcd.print("Ist:");
- lcd.write(0x3D); // = Zeichen
- // if (temperature < -9) lcd.print(" ");
- // else if (temperature < 0) lcd.print("");
- // else if (temperature < 10) lcd.print(" ");
- // else lcd.print(" ");
- lcd.print(tempstr2);
- lcd.write(0xDF); // Grad-Symbol
- lcd.print(" ");
- lcd.print(humstr2);
- lcd.print("% ");
- if (turnHeatingOn) lcd.print("HEIZT");
- else lcd.print(" ");
- char targettempstr[5]; //result string 4 positions + \0 at the end
- //
- // convert float to fprintf type string
- // format 5 positions with 3 decimal places
- //
- dtostrf(targetTemp, 2, 1, targettempstr );
- lcd.setCursor(0, 1);
- //lcd.print("Soll: ");
- lcd.write((uint8_t)2); // Pfeil rechts
- lcd.print(targettempstr);
- lcd.write(0xDF); // Grad-Symbol
- lcd.print(" ");
- // if ( heatingMode == 1 ) lcd.write((uint8_t)1); // Sonne
- // else if ( heatingMode == 2 ) lcd.write((uint8_t)0); // Mond
- // else lcd.write(0x2D); // -
- if ( heatingMode == 1 ) lcd.print(" Tag");
- else if ( heatingMode == 2 ) lcd.print(" Nacht");
- else lcd.print(" Aus");
- // lcd.print(" ");
- //
- // if(turnHeatingOn) lcd.print("1");
- // else lcd.print("0");
- if ( temperature >= targetTemp ) {
- turnHeatingOn = false;
- Serial.println("heating off");
- }
- else if ( temperature < ( targetTemp - hysteresis) ) {
- turnHeatingOn = true;
- Serial.println("heating on");
- }
- }
- // Check if any reads failed and exit early (to try again).
- //if (isnan(hum) || isnan(temp)) {
- // Serial.println("Failed to read from DHT sensor!");
- // return;
- //}
- /*Serial.print("Humidity: ");
- Serial.print(hum);
- Serial.print(" %\t");
- Serial.print("Temperature: ");
- Serial.print(temp);
- Serial.print(" *C ");
- Serial.println();*/
- }
|