#include #include //this needs to be first, or it all crashes and burns... #include #include #include //https://github.com/esp8266/Arduino #include #include #define BUILTIN_LED D15 #define TRIGGER_PIN D7 #define START_STOP_PIN D6 void reconnect(); void setup_wifi(); void datenSave(int wert); int readDaten(); int saveKorektur(float wert); float readKorectur(); void verifyFingerprint(); void callback(char* topic1, byte* payload, unsigned int length); void pulse_pin(uint8_t pin); WiFiClientSecure espClient; PubSubClient client(espClient); #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]; int SystemStatus; void setup() { pinMode(TRIGGER_PIN, OUTPUT); pinMode(START_STOP_PIN, OUTPUT); digitalWrite(TRIGGER_PIN, HIGH); digitalWrite(START_STOP_PIN, LOW); pulse_pin(TRIGGER_PIN); // ==> 1 Serial.begin(74880); while ( !Serial ) delay(100); // wait for native usb Serial.println(F("BMP280 Sensor event test")); Serial.println("HTU21D-F test"); startTime = millis(); WiFi.mode( WIFI_OFF ); WiFi.forceSleepBegin(); pulse_pin(TRIGGER_PIN); // ==> 1 Serial.println(); //Serial.println("Testpunkt 1"); /* if (!LittleFS.begin()) { Serial.println("LittleFS mount failed"); delay(5000); return; } */ //Serial.println("Testpunkt 2"); /* Dir dir = LittleFS.openDir("/data"); while (dir.next()) { Serial.print(dir.fileName()); if(dir.fileSize()) { File f = dir.openFile("r"); Serial.println(f.size()); } } */ //Serial.println("Testpunkt 3"); Serial.println(); Serial.println(); Serial.println(); //SystemStatus = readDaten(); //korectur = readKorectur(); //Serial.print("Korektur: "); Serial.println(korectur,6); pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output //digitalWrite(BUILTIN_LED, LOW); //digitalWrite(BUILTIN_LED, HIGH); //Serial.print("STATUS (Systemmeldung): "); Serial.println(SystemStatus); #if (MQTT == 0) init_HTU21(); Init_BMP280(); //initADS(); //init_MCP9808(); #endif AKKU = getBattery(); // ca. 170 ms // ca. 280 ms if (F_HTU_21D == true){ read_HTU21D(); } read_BMP_280(); //MessungADS(); setup_wifi(); // ca. 4,5 s // ca. 12ms // --------------------------------- // Status ändern !!! 0 //datenSave(0); // --------------------------------- //digitalWrite(BUILTIN_LED, HIGH); 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); //---------- } void loop() { unsigned long Pause = 0; if (!client.connected()) { reconnect(); } //client.loop(); int currentMillis = millis(); if (currentMillis - previousMillis >= 10000) { previousMillis = currentMillis; read_HTU21D(); read_BMP_280(); //valTemp = getTemperature_MCP9808(); 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(MQTT == 0) M2M_HTU21D(hostname.c_str()); M2M_BMP280(hostname.c_str()); M2M_Temperatur_MCP9808(hostname.c_str()); #endif long int Feldstaerke = WiFi.RSSI(); sprintf(msg,"%ld", Feldstaerke); sprintf(topic, "%s%s%s", "hjk/devices/", hostname.c_str(), "/telemetry/RSSI" ); client.publish(topic, msg, true); client.loop(); delay(500); digitalWrite(BUILTIN_LED, HIGH); /* ESP.deepSleep(5e6); delay(100); */ endTime = millis(); if (AKKU < MinimalSpannung){ // --------------------------------- // Status ändern !!! -5 datenSave(-5); // --------------------------------- Pause = intervalLowBatt -((endTime - startTime) * 1000); // Pause ca. 60 Minuten Serial.println("AKKU entladen!"); } 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 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){ // --------------------------------- // Status ändern !!! -1 datenSave(-1); // --------------------------------- 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"); Serial.print(WiFi.localIP()); Serial.print("\tRESSI: "); Serial.println(WiFi.RSSI()); /* 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); int er = saveKorektur(korectur); if (er != 0){ Serial.println("Daten konnten nicht gespeichert werden."); } float test = readKorectur(); Serial.print("Korektur:\t");Serial.println(test, 8); } } void reconnect() { // Loop until we're reconnected sprintf(clientName, "%s%s", "ESP8266Client", 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 datenSave(int wert){ File k = LittleFS.open("/status.txt", "w"); if(!k){ Serial.println("file open failed"); } k.println(wert); k.close(); } int readDaten() { int Error; File k = LittleFS.open("/status.txt", "r"); if(!k){ Serial.println("file open failed"); Error = -10; }else{ String data = k.readString(); Error = data.toInt(); k.close(); } return Error; } int saveKorektur(float wert){ int Error = 0; File k = LittleFS.open("/Korektur.txt", "w"); if(!k){ Serial.println("file open failed"); Error = -1; }else{ k.println(String(wert,8)); k.close(); } return Error; } float readKorectur(){ float Korektur; File k = LittleFS.open("/Korektur.txt", "r"); if(!k){ Serial.println("file open failed"); Korektur = 1.00; }else{ String data = k.readString(); Korektur = data.toFloat(); k.close(); } return Korektur; } 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); }