#include #include //this needs to be first, or it all crashes and burns... #include #include //https://github.com/esp8266/Arduino #include #include // #define BUILTIN_LED D2 #define TRIGGER_PIN D7 #define START_STOP_PIN D6 void reconnect(); void setup_wifi(); void verifyFingerprint(); void callback(char* topic1, byte* payload, unsigned int length); void pulse_pin(uint8_t pin); WiFiClient espClient; PubSubClient client(espClient); #include #include //#include #include //#include #include #include #include const char* ssid = STASSID; const char* password = STAPSK; String hostname = NAME; const char *MyIP = KMYIP; IPAddress ip; IPAddress gateway; IPAddress subnet(255, 255, 255, 0); IPAddress dns; IPAddress secondaryDNS(8, 8, 8, 8); const char* mqtt_fprint = "a3:44:1d:aa:6e:e5:c7:55:02:20:98:ea:9b:df:1a:42:a2:f3:e3:0d"; const char* mqtt_user = "mqtt"; const char* mqtt_pass = "fische"; const unsigned long interval = TINTERVAL * 60000000LU; // Minuten * Mikrosekunden für Sleep Mode const unsigned long intervalLowBatt = TLOWBATT * 60000000LU; // Minuten * Mikrosekunden für Sleep Mode, Akku entladen const unsigned long stoerung = TERROR * 60000000LU; // Minuten * Mikrosekunden für Sleep Mode unsigned long previousMillis = 0; long deviceId; char sID[16]; char clientName[30]; unsigned long startTime; unsigned long endTime; char topic[100]; char topic_1[50]; char topicWert[20]; char msg[20]; long int Feldstaerke; void setup() { startTime = millis(); /* pinMode(BUILTIN_LED, OUTPUT); digitalWrite(BUILTIN_LED, LOW); delay(5000); digitalWrite(BUILTIN_LED, HIGH); */ Serial.begin(74880); while ( !Serial ) delay(100); // wait for native usb WiFi.mode( WIFI_OFF ); WiFi.forceSleepBegin(); Serial.println(); Serial.println(); Serial.println(); Serial.println(); readSystemStatus(); Serial.printf("Systemstatus : 0x%02x \n", SystemStatus); SystemStatus = 0x00; writeSystemStatus(); delay(500); #if (MQTT == 0) init_HTU21(); //Init_BMP280(); Sensor_BME280(); init_MCP9808(); initADS(); #endif if (!F_ADS1115 == true){ AKKU = getBattery(korectur); // ca. 170 ms Serial.println("ALTE UB MESSUNG:"); Serial.println(AKKU); } else{ MessungADS(); AKKU = readChannel(ADS1115_COMP_0_GND); } // ca. 280 ms if (F_HTU_21D == true){ read_HTU21D(); } if (F_ADS1115 == true){ MessungADS(); } /* if (F_BMP280 == true){ read_BMP_280(); }*/ if (F_MCP9808 == true){ valTemp = getTemperature_MCP9808(); } setup_wifi(); // ca. 4,5 s deviceId = ESP.getChipId(); sprintf(sID, "%010ld", deviceId); Serial.print("ID: \t\t"); Serial.println(deviceId); // ca. 5 s //espClient.setFingerprint(mqtt_fprint); client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); Serial.printf("Systemstatus : 0x%02x \n", SystemStatus); //---------- } void loop() { unsigned long Pause = 0; if (!client.connected()) { reconnect(); } int currentMillis = millis(); if (currentMillis - previousMillis >= 10000) { previousMillis = currentMillis; dtostrf(AKKU,8,2,ADSData.Akku); sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/battery" ); client.publish(topic, ADSData.Akku, true); if (F_ADS1115 == true){ sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/Solar" ); client.publish(topic, ADSData.Solar, true); } if (F_HTU_21D) M2M_HTU21D(hostname.c_str()); M2M_BME280(hostname.c_str()); if (F_MCP9808) M2M_Temperatur_MCP9808(hostname.c_str()); sprintf(msg,"%ld", Feldstaerke); sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/RSSI" ); client.publish(topic, msg, true); sprintf(msg,"0x%02x", SystemStatus); sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/SystemStatus" ); client.publish(topic, msg, true); client.loop(); delay(500); //digitalWrite(BUILTIN_LED, HIGH); /* ESP.deepSleep(5e6); delay(100); */ endTime = millis(); if (AKKU < MinimalSpannung){ // --------------------------------- Pause = intervalLowBatt -((endTime - startTime) * 1000); // Pause ca. 60 Minuten Serial.println("AKKU entladen!"); SystemStatus = (SystemStatus | AkkuLeer); Serial.printf("Systemstatus : 0x%04x \n", SystemStatus); writeSystemStatus(); } else{ Pause = interval -((endTime - startTime) * 1000); // Pause ca. 15 Minuten } if(Pause <=0){ Pause = 1; } //ESP.restart(); Serial.print("Ich gehe für ca. "); Serial.print((Pause/1000/1000/60)+1); Serial.println( " Minuten schlafen."); //ESP.deepSleep(Pause, WAKE_RF_DISABLED); // Pause digitalWrite(START_STOP_PIN, !LOW); #if (DEBUG == 1) ESP.deepSleep(10e6); #else writeSystemStatus(); ESP.deepSleep(Pause); #endif delay(100); } } void setup_wifi() { long ErrCount = 0; delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.print(ssid); Serial.print(" "); WiFi.forceSleepWake(); delay( 1 ); WiFi.persistent( false ); WiFi.mode( WIFI_STA ); WiFi.setHostname(hostname.c_str()); //define hostname WiFi.hostname(hostname.c_str()); WiFi.mode( WIFI_STA ); if (!ip.fromString(MyIP)) { // try to parse into the IPAddress Serial.println("UnParsable IP"); } if (!dns.fromString(DNS)) { // try to parse into the IPAddress Serial.println("UnParsable DNS"); } if (!gateway.fromString(GATEWAY)) { // try to parse into the IPAddress Serial.println("UnParsable GATEWAY"); } //WiFi.config( ip, dns, gateway, subnet ); if (!WiFi.config(ip, gateway, subnet, dns, secondaryDNS)) { Serial.println("STA Failed to configure"); } WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); ErrCount ++; if (ErrCount >= MaxErrCount){ endTime = millis(); unsigned long Pause = stoerung -((endTime - startTime) * 1000); // Pause Serial.println(); Serial.println("STÖRUNG WiFi."); Serial.print("Ich gehe für ca. "); Serial.print(Pause/1000/1000/60); Serial.println( " Minuten schlafen."); ESP.deepSleep(Pause, WAKE_NO_RFCAL); // Pause delay(100); } } Serial.println(" WiFi connected"); Serial.print("IP address: \t"); Feldstaerke = WiFi.RSSI(); Serial.print(WiFi.localIP()); Serial.print("\tRESSI: "); Serial.println(Feldstaerke); /* delay(2000); ESP.deepSleep(2e6, WAKE_RF_DEFAULT); delay(100); */ } void callback(char* topic1, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic1); Serial.print("] "); for (unsigned int i = 0; i < length; i++) { msg[i] = (char)payload[i]; } msg[length] = '\0'; Serial.println(msg); if(strcmp(topic1, topic_1)== 0){ Serial.print(msg); Serial.println(); korectur = atof(msg); Serial.print("Korektur:\t");Serial.println(korectur, 8); } } void reconnect() { // Loop until we're reconnected sprintf(clientName, "%s%s", "ESP8266Wetter", sID); while (!client.connected()) { Serial.print("Attempting MQTT connection..."); //verifyFingerprint(); // Attempt to connect if (client.connect(clientName)) { 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); ESP.restart(); } } } void verifyFingerprint() { unsigned long Pause = 0; if(client.connected() || espClient.connected()) return; //Already connected Serial.print("\n\tChecking TLS @ "); Serial.print(mqtt_server); Serial.print("..."); if (!espClient.connect(mqtt_server, mqtt_port)) { //Serial.println("\n\tConnection failed. Rebooting."); Serial.println("\n\tConnection failed."); Serial.flush(); //blink.detach(); //blink.attach(0.05, flip); //delay(5000); endTime = millis(); Pause = stoerung -((endTime - startTime) * 1000); // Pause ca. 15 Minuten Serial.print("\tIch gehe für "); Serial.print((Pause/1000/1000/60)+1); Serial.println( " Minuten schlafen."); ESP.deepSleep(Pause, WAKE_RF_DISABLED); // Pause delay(500); } espClient.stop(); delay(100); } void pulse_pin(uint8_t pin) { digitalWrite(pin, LOW); delay(1); digitalWrite(pin, HIGH); }