diff --git a/zisterne/display.ino b/zisterne/display.ino index e61b595..00eaf30 100644 --- a/zisterne/display.ino +++ b/zisterne/display.ino @@ -2,12 +2,13 @@ //LCD Setup //*********************** void ladeLCDdisplay() { - Wire.begin(12, 13); //SDA-D6, SCL-D7 // LCD Setup (bei beiden LCD Displays gleich) + // Wire.begin(12, 13); //SDA-D6, SCL-D7 // LCD Setup (bei beiden LCD Displays gleich) // *******************Boot mit LAN-Info für 1602 Display************************** if (cfg.data_display ==1) { timer.setInterval(1000); - lcd.begin(); + // lcd.begin(); + lcd.init(); lcd.backlight(); lcd.print(cfg.SSID); lcd.setCursor(0, 1); @@ -27,7 +28,7 @@ // *******************Boot mit LAN-Info für 1604 Display************************** if (cfg.data_display ==2) { timer.setInterval(1000); - lcd.begin(); + lcd.init(); lcd.backlight(); lcd.setCursor(0, 0); lcd.print("Ihre SSID:"); @@ -47,7 +48,8 @@ delay (5000); lcd.noBacklight(); } - } + } + void ladeOLEDdisplay() { //*******************************Initialisierung und DEBUG OLED************************************ @@ -97,101 +99,140 @@ //LCD mit Tastersteuerung für 1602 Display //*********************** void loop1602() { - if ( millis() - displayMicros >= 5000 ) { - displayMicros = millis(); - lcd.clear(); - lcd.setCursor(0,0); - lcd.print(volumen); - lcd.print( " Liter,"); - lcd.setCursor(0,1); - lcd.print(fuellstand); - lcd.print( " % voll"); + if (millis() - displayMicros >= 5000 ) { + displayMicros = millis(); + lcd.clear(); + lcd.setCursor(0,0); + lcd.print(volumen); + lcd.print( " Liter,"); + lcd.setCursor(0,1); + lcd.print(fuellstand); + lcd.print( " % voll"); } - if ((backlstatus == 0) && (digitalRead(taster) == HIGH)) { - lcd.backlight(); - backlstatus = 1; - delay (300); //gegen Tastenprellen - } unsigned long tasterMicros = millis(); - if (tasterMicros - previousMicros >= 90000 ) { - previousMicros = tasterMicros; - lcd.noBacklight(); + if ((tasterMicros - previousMicros >= 90000) && backlstatus) { + lcd.noBacklight(); + backlstatus = false; + return; } - if ((backlstatus == 1) && (digitalRead(taster) == HIGH)) { - lcd.noBacklight(); - backlstatus = 0; - delay (300); //gegen Tastenprellen + + if ((backlstatus == false) && (tasterHigh)) { + resetBacklightTaster(); + lcd.backlight(); + backlstatus = true; + previousMicros = millis(); + return; + } + + if (backlstatus && tasterHigh) { + resetBacklightTaster(); + lcd.noBacklight(); + backlstatus = false; + return; } - } + +} //*********************** //LCD mit Tastersteuerung für 1604 Display //*********************** - void loop1604() { +void loop1604() { if ( millis() - displayMicros >= 5000 ) { - displayMicros = millis(); - lcd.clear(); - lcd.setCursor(0,0); - lcd.print( "F\365llstand:"); // Ueberschrift - lcd.setCursor(0,1); - lcd.print(volumen); - lcd.print( " Liter,"); - lcd.setCursor(0,3); - lcd.print(fuellstand); - lcd.print( " % voll"); + displayMicros = millis(); + lcd.clear(); + lcd.setCursor(0,0); + lcd.print( "F\365llstand:"); // Ueberschrift + lcd.setCursor(0,1); + lcd.print(volumen); + lcd.print( " Liter,"); + lcd.setCursor(0,3); + lcd.print(fuellstand); + lcd.print( " % voll"); } - if ((backlstatus == 0) && (digitalRead(taster) == HIGH)) { - lcd.backlight(); - backlstatus = 1; - delay (300); //gegen Tastenprellen - } - unsigned long tasterMicros = millis(); - if (tasterMicros - previousMicros >= 180000 ) { - previousMicros = tasterMicros; - lcd.noBacklight(); - } - if ((backlstatus == 1) && (digitalRead(taster) == HIGH)) { - lcd.noBacklight(); - backlstatus = 0; - delay (300); //gegen Tastenprellen - } - } + unsigned long tasterMicros = millis(); + if ((tasterMicros - previousMicros >= 90000) && backlstatus) { + lcd.noBacklight(); + backlstatus = false; + return; + } + + if ((backlstatus == false) && (tasterHigh)) { + resetBacklightTaster(); + lcd.backlight(); + backlstatus = true; + previousMicros = millis(); + return; + } + + if (backlstatus && tasterHigh) { + resetBacklightTaster(); + lcd.noBacklight(); + backlstatus = false; + return; + } +} //*********************** // OLED Display //*********************** + void loopOLED() { - if (((backlstatus == 0) && (digitalRead(taster) == HIGH)) || (backlstatus == 1)){ - backlstatus = 1; - delay (300); - if ( millis() - displayMicros >= 5000 ) { - displayMicros = millis(); + + // setze Display-Timout zu 30 Sekunden + // prüfe wenn Display an ist, ob das letzte Einschalten mehr als 30sek in der Vergangenheit liegt + unsigned long oledTimeout = millis(); + if ((oledTimeout - previousMicros >= 30000) && backlstatus ) { oledFill(&ssoled, 0, 1); - char Volumen[10]; - sprintf(Volumen,"%.2f l", volumen); - char Fuellstand[10]; - sprintf(Fuellstand,"%.2f%%", fuellstand); - oledWriteString(&ssoled, 0,0,0,(char *)"Fuellstand:" , FONT_NORMAL, 0, 1); - oledWriteString(&ssoled, 0,60,3,Volumen, FONT_SMALL, 0, 1); - oledWriteString(&ssoled, 0,60,5,Fuellstand, FONT_SMALL, 0, 1); - //*********************Grafik ********************** - rest = map(fuellstand, 0, 100, 62, 13); // Hoehe des Balkens ermitteln - oledRectangle(&ssoled, (1), (13), (45), (62), 1, 0); // aeussere Linie zeichnen - oledRectangle(&ssoled, (1), (rest), (45), (62), 1, 1); // Fuellbalken - oledDumpBuffer(&ssoled, NULL); - } + backlstatus = false; + return; + } - unsigned long tasterMicros = millis(); - if (tasterMicros - previousMicros >= 180000 ) { - previousMicros = tasterMicros; - oledFill(&ssoled, 0, 1); - backlstatus = 0; - } - if ((backlstatus == 1) && (digitalRead(taster) == HIGH)) { + + //if ((backlstatus == 1) && (digitalRead(taster) == HIGH)) { + + // Schalte das Display aus, falls es an ist + // setze das Flag für den GPIO-Pin zurück + if (backlstatus && tasterHigh) { + resetBacklightTaster(); oledFill(&ssoled, 0, 1); - backlstatus = 0; - delay (300); + //delay (300); //gegen Tastenprellen + backlstatus = false; + return; } + + //if (((backlstatus == 0) && (digitalRead(taster) == HIGH)) || (backlstatus == 1)){ + // if ((backlstatus == 0) && (digitalRead(taster) == HIGH)) { + + // schalte Display ein, falls aus + // aktualisiere Display nach 5 sek falls an + // setze Flag für GPIO-Pin zurück und speichere Zeitpunkt des letzten Taster-Drucks in previousMicros + if (!backlstatus && tasterHigh) { + if (tasterHigh) { + previousMicros = millis(); + } + // delay (300); //gegen Tastenprellen + backlstatus = true; + resetBacklightTaster(); + } + + if (backlstatus) { + // aktualisiere Display + if ( millis() - displayMicros >= 5000 ) { + displayMicros = millis(); + oledFill(&ssoled, 0, 1); + char Volumen[10]; + sprintf(Volumen,"%.2f l", volumen); + char Fuellstand[10]; + sprintf(Fuellstand,"%.2f%%", fuellstand); + oledWriteString(&ssoled, 0,0,0,(char *)"Fuellstand:" , FONT_NORMAL, 0, 1); + oledWriteString(&ssoled, 0,60,3,Volumen, FONT_SMALL, 0, 1); + oledWriteString(&ssoled, 0,60,5,Fuellstand, FONT_SMALL, 0, 1); + //*********************Grafik ********************** + rest = map(fuellstand, 0, 100, 62, 13); // Hoehe des Balkens ermitteln + oledRectangle(&ssoled, (1), (13), (45), (62), 1, 0); // aeussere Linie zeichnen + oledRectangle(&ssoled, (1), (rest), (45), (62), 1, 1); // Fuellbalken + oledDumpBuffer(&ssoled, NULL); + } } } diff --git a/zisterne/myTypes.h b/zisterne/myTypes.h index 35d6783..259fb66 100644 --- a/zisterne/myTypes.h +++ b/zisterne/myTypes.h @@ -47,7 +47,8 @@ typedef struct { //Sonstiges int data_dezstellen_anzeige; int data_anzeige_liter_check; - + int data_height_over_NN; + //weitere MQTT char data_mqtt_abstand_topic[51]; int data_mqtt_port; diff --git a/zisterne/ota.ino b/zisterne/ota.ino index f87a7c9..1a90c6e 100644 --- a/zisterne/ota.ino +++ b/zisterne/ota.ino @@ -20,7 +20,7 @@ void checkForUpdates() { Serial.println( fwVersionURL ); HTTPClient httpClient; - httpClient.begin( fwVersionURL ); + // httpClient.begin( fwVersionURL ); int httpCode = httpClient.GET(); if( httpCode == 200 ) { String newFWVersion = httpClient.getString(); @@ -37,7 +37,7 @@ void checkForUpdates() { String fwImageURL = fwURL; fwImageURL.concat( ".bin" ); - t_httpUpdate_return ret = ESPhttpUpdate.update( fwImageURL ); + t_httpUpdate_return ret = ESPhttpUpdate.update(httpClient, fwImageURL ); switch(ret) { case HTTP_UPDATE_FAILED: diff --git a/zisterne/rechnen.ino b/zisterne/rechnen.ino index 32e564b..2945b73 100644 --- a/zisterne/rechnen.ino +++ b/zisterne/rechnen.ino @@ -52,6 +52,17 @@ void looprechnen() { delay(100); } + //**************************************************** + // Druck Sensor ADS1115 + //**************************************************** + if (cfg.data_zisterne_sensor == 4) { + adc0 = ads.getLastConversionResults(); + //Serial.print("AIN0: "); Serial.println(adc0*0.02); + abstand=(cfg.data_abst_sen_boden-(adc0*0.02)); + fuellhoehe=(adc0*0.02); + delay(100); + } + //abstand = 100; //nur testweise wenn kein Sensor dran haengt //Fuellstand prozentual berechnen diff --git a/zisterne/relais.ino b/zisterne/relais.ino index d7d184a..94477cc 100644 --- a/zisterne/relais.ino +++ b/zisterne/relais.ino @@ -1,5 +1,5 @@ //******************************************************** -// Schaltet ein externes Relais an Ausgang D4 / Pin 2 +// Schaltet ein externes Relais an Ausgang D3 / Pin 0 // in Abhaengigkeit von zwei Werten //******************************************************** void relais() { diff --git a/zisterne/relais2.ino b/zisterne/relais2.ino index 5dbe3f8..176c938 100644 --- a/zisterne/relais2.ino +++ b/zisterne/relais2.ino @@ -1,5 +1,5 @@ //*********************************************************** -// Schaltet ein zweites externes Relais an Ausgang D3 / Pin 2 +// Schaltet ein zweites externes Relais an Ausgang D4 / Pin 2 // in Abhaengigkeit von zwei Werten // NOCH NICHT IMPLEMENTIERT //*********************************************************** diff --git a/zisterne/webserver.ino b/zisterne/webserver.ino index 284a2e3..5faa7ab 100644 --- a/zisterne/webserver.ino +++ b/zisterne/webserver.ino @@ -1,14 +1,16 @@ void webHandleRoot() { String inhalt; float vcc = ESP.getVcc() / 1000.0; - inhalt = ""; - inhalt += "" + anwendungsname + ""; - inhalt += "

  "; - inhalt += "" + anwendungsname + " v" + fw_version + "

"; - inhalt += "

Startseite

"; - inhalt += "
"; - inhalt += "
"; - + inhalt = + "" + ""+anwendungsname+"" + // "

  " + ""+anwendungsname+" v"+fw_version+"

" + "

Startseite

" + "
" + "
"; + + int breite_anzeige = 0; // inhalt += ""; @@ -217,13 +219,35 @@ void webHandleRoot() { } //Abstand + if (cfg.data_zisterne_sensor < 4) { inhalt += "
"; inhalt += abstand; inhalt += "cm"; inhalt += "

Abstand Wasser

"; + } + if (cfg.data_zisterne_sensor == 4) { + inhalt += "
"; + inhalt += fuellhoehe; + inhalt += "cm"; + inhalt += "

Füllhöhe

"; + } +//-------------------------------------------------------------------- if (cfg.data_temperatur_enable == 1) { //Temperatur + if (cfg.data_temperatur == 1) { + inhalt += "
"; + inhalt += temperature; + inhalt += "°C"; + inhalt += "

Temperatur

"; + + //Luftfeuchte + inhalt += "
"; + inhalt += (int)roundf(humidity); + inhalt += "%"; + inhalt += "

Luftfeuchte

"; + } + if (cfg.data_temperatur == 2) { inhalt += "
"; inhalt += temperature; inhalt += "°C"; @@ -234,6 +258,26 @@ void webHandleRoot() { inhalt += (int)roundf(humidity); inhalt += "%"; inhalt += "

Luftfeuchte

"; + + //Luftdruck + inhalt += "
"; + inhalt += (int)roundf(pressure); + inhalt += "hPa"; + inhalt += "

Luftdruck (NN)

"; + } + + if (cfg.data_temperatur == 3) { + inhalt += "
"; + inhalt += temperature; + inhalt += "°C"; + inhalt += "

Temperatur

"; + + //Luftdruck + inhalt += "
"; + inhalt += (int)roundf(pressure); + inhalt += "hPa"; + inhalt += "

Luftdruck (NN)

"; + } } //******************************* @@ -258,7 +302,7 @@ void webHandleRoot() { // inhalt += "
"; // inhalt += relais2_status; // inhalt += ""; - // inhalt += "

Schalter an D3

"; + // inhalt += "

Schalter an D4

"; //} if (cfg.data_webapi_check == 1) { @@ -305,6 +349,9 @@ void webHandleRoot() { } if (cfg.data_zisterne_sensor == 3) { inhalt += "VL53L1X"; + } + if (cfg.data_zisterne_sensor == 4) { + inhalt += "ADS1115"; } inhalt += ""; @@ -529,16 +576,22 @@ void webHandleConfig() { inhalt += "'> an GPIO-Pin 14 (D5) mit 10kΩ von Daten gegen VCC"; inhalt += ""; - inhalt += "
BME280 //Pololu-Lib #include "SparkFun_VL53L1X.h" +#include +#include +#include +#include "Adafruit_BMP280.h" + extern "C" { #include "user_interface.h" @@ -39,11 +44,16 @@ VL53L0X l0x_sensor; //VL53L1X SFEVL53L1X l1x_sensor; +//ADS1115 +Adafruit_ADS1115 ads; /* Use this for the 16-bit version */ +//Adafruit_ADS1015 ads; /* Use this for the 12-bit version */ +int16_t adc0; //******************************************************** // Variablen für Abstandsmessung //******************************************************** int abstand; +int fuellhoehe; float fuellstand; float volumen; int volumen_max; @@ -57,14 +67,23 @@ unsigned long startMillisAbstand; //********************* // Temperatursensor //********************* +// DHT22 #define DHTPIN 14 //D5 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); float humidity; float temperature; +float pressure; +float pressureReal; +float altitude; unsigned long startMillisTemp; unsigned long currentMillisTemp; +//BME280 +Adafruit_BME280 bme; // I2C + +//BMP280 +Adafruit_BMP280 bmp; // I2C //******************************************************** // Konfiguration der Displays @@ -78,22 +97,22 @@ SSOLED ssoled; // benoetigt fuer OLED //Taster fuer Display //int sensor = 14; // D5 VCC Sensor int taster = 15; // D8 für Taster -int tasterstatus = 0; -int previousMicros = 0; -int displayMicros = 0; -int backlstatus = 0; +// int tasterstatus = 0; +int previousMicros = 0; //timer +int displayMicros = 0; //timer +bool backlstatus = false; //******************************************************** // Relais an D4 //******************************************************** -const int relaisPIN = 2; //D4 +const int relaisPIN = 2; //D4 HIGH bei Boot unsigned long startMillisRelais; unsigned long currentMillisRelais; //******************************************************** // Relais an D3 //******************************************************** -const int relais2PIN = 2; //D3 +const int relais2PIN = 0; //D3 unsigned long startMillisRelais2; unsigned long currentMillisRelais2; @@ -144,10 +163,12 @@ const long utcOffsetInSeconds = 7200; NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds); //******************************************************** -// Display refresh +// Display variablen //******************************************************** unsigned long startMillisDisplay; unsigned long currentMillisDisplay; +// vgl. https://docs.arduino.cc/language-reference/funktionen/external-interrupts/attachInterrupt/ +volatile bool tasterHigh; //******************************************************** // Grundsätzliche Einstellungen @@ -187,6 +208,21 @@ uint32_t freeheap = system_get_free_heap_size(); bool gesendet_pushover = false; bool gesendet_mail = false; + +//******************************************************** +// ISR +//******************************************************** +// ICACHE_RAM_ATTR void enableBacklight() { (veraltet) +IRAM_ATTR void enableBacklight() { + tasterHigh = true; +} + +void resetBacklightTaster() { + tasterHigh = false; +} + + + //******************************************************** // SETUP //******************************************************** @@ -194,9 +230,9 @@ void setup() { Wire.begin(); Serial.begin(115200); - delay(3000); + delay(1000); Serial.println("Auf gehts..."); - + //Lade Konfiguration aus EEPROM ladekonfig(); @@ -275,7 +311,32 @@ void setup() { } } - + //ADS1115 + if (cfg.data_zisterne_sensor == 4) { + Serial.println("ADS1115 ausgewaehlt"); + // The ADC input range (or gain) can be changed via the following + // functions, but be careful never to exceed VDD +0.3V max, or to + // exceed the upper and lower limits if you adjust the input range! + // Setting these values incorrectly may destroy your ADC! + // ------- ------- + ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default) + // ads.setGain(GAIN_ONE); //1x gain +/- 4.096V 1 bit = 2mV 0.125mV + // ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV + // ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV + // ads.setGain(GAIN_EIGHT); // 8x gaSerial.println("Single-ended readings from AIN0 with >3.0V comparator"); + // Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)"); + // Serial.println("Comparator Threshold: 1000 (3.000V)");in +/- 0.512V 1 bit = 0.25mV 0.015625mV + // ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV + if (!ads.begin()) { + Serial.println("Failed to initialize ADS."); + while (1); + } + + // Setup 3V comparator on channel 0 + ads.startComparator_SingleEnded(0, 1000); + } + +//----------------------------------------------------------------------------------------- //Zeit merken um EigenAPI, WebAPI und OTA zeitgesteuert aufzurufen //auch Intervall zum Refresh von Display und NTP startMillisEigen = millis(); @@ -313,23 +374,57 @@ void setup() { } //Starte Temperaturmessung - dht.begin(); + if (cfg.data_temperatur == 1) { + dht.begin(); //DHT22 //einmal initial messen humidity = dht.readHumidity(); Serial.println(humidity); // Lese Temperatur temperature = dht.readTemperature(); Serial.println(temperature); + } +// -------------------------------- + if (cfg.data_temperatur == 2) { + bme.begin(0x76); //BME280 + temperature = (bme.readTemperature() -1); + Serial.println(temperature); + humidity = bme.readHumidity(); + Serial.println(humidity); + pressureReal = (bme.readPressure() / 100); + Serial.println(pressureReal); + altitude = (cfg.data_height_over_NN); + Serial.println(altitude); + // Berechnung Druck über NN + pressure = pressureReal/pow(1 - altitude/44330.0, 5.255); + Serial.println(pressure); + } +//---------------------------------------- + if (cfg.data_temperatur == 3) { + bmp.begin(0x76); //BMP280 + temperature = (bmp.readTemperature() -1); + Serial.println(temperature); + pressureReal = (bmp.readPressure() / 100); + Serial.println(pressureReal); + altitude = (cfg.data_height_over_NN); + Serial.println(altitude); + // Berechnung Druck über NN + pressure = pressureReal/pow(1 - altitude/44330.0, 5.255); + Serial.println(pressure); + } - //Ausgang D4 + //Ausgang D3 pinMode(relaisPIN, OUTPUT); digitalWrite(relaisPIN,LOW); //Serial.println("LED Test.."); //digitalWrite(relaisPIN,HIGH); - //Ausgang D3 + //Ausgang D4 //pinMode(relais2PIN, OUTPUT); //digitalWrite(relais2PIN,LOW); + + //PIN taster für Backlight als Interupt registrieren + resetBacklightTaster(); + attachInterrupt(digitalPinToInterrupt(taster), enableBacklight, RISING); } @@ -377,9 +472,37 @@ void loop() { Serial.println(temperature); } - //BME Sensor + //BME280 if (cfg.data_temperatur == 2) { - //noch nix zu tun + // Lese Luftfeuchte + humidity = bme.readHumidity(); + Serial.println(humidity); + // Lese Temperatur + temperature = (bme.readTemperature() -1); + Serial.println(temperature); + // Lese Luftdruck + pressureReal = (bme.readPressure() / 100); + Serial.println(pressureReal); + altitude = (cfg.data_height_over_NN); + Serial.println(altitude); + // Berechnung Druck über NN + pressure = pressureReal/pow(1 - altitude/44330.0, 5.255); + Serial.println(pressure); + } + + //BMP280 + if (cfg.data_temperatur == 3) { + // Lese Temperatur + temperature = (bmp.readTemperature() -1); + Serial.println(temperature); + // Lese Luftdruck + pressureReal = (bmp.readPressure() / 100); + Serial.println(pressureReal); + altitude = (cfg.data_height_over_NN); + Serial.println(altitude); + // Berechnung Druck über NN + pressure = pressureReal/pow(1 - altitude/44330.0, 5.255); + Serial.println(pressure); } } }