From a9841f849b08c4f8347574751087c1dd90fcf539 Mon Sep 17 00:00:00 2001 From: Andreas Tacke Date: Sun, 29 May 2022 12:53:06 +0200 Subject: [PATCH] Add support for WiFiManager, remove soft AP --- data/html/settings.htm | 9 - platformio_sample.ini | 28 +- src/Displaytemplateminimal.h | 2 +- src/Displaytemplatescale.h | 4 +- src/Displaytemplatestandard.h | 2 +- src/Displaytemplatetemponly.h | 2 +- src/Displaytemplateupright.h | 2 +- src/RancilioServer.h | 1 + src/brewvoid.h | 2 +- src/display.h | 2 +- src/rancilio-pid.cpp | 517 ++++++++++++---------------------- src/userConfig_sample.h | 31 +- 12 files changed, 216 insertions(+), 386 deletions(-) diff --git a/data/html/settings.htm b/data/html/settings.htm index 47ef387a9..667819b03 100644 --- a/data/html/settings.htm +++ b/data/html/settings.htm @@ -8,15 +8,6 @@
WiFi Settings
-
-
AP Mode
-

- These values are hard-coded at the moment and cannot be edited:
- AP name: %VAR_SHOW_AP_WIFI_SSID%
- Password: %VAR_SHOW_AP_WIFI_KEY% - -

-
Client Mode

diff --git a/platformio_sample.ini b/platformio_sample.ini index d5a217465..cb5651133 100644 --- a/platformio_sample.ini +++ b/platformio_sample.ini @@ -4,19 +4,21 @@ src_dir = src [env] lib_deps = - lebuni/ZACwire for TSic @ ^2.0.0 - schm1tz1/TSIC @ ^1.1.2 - milesburton/DallasTemperature @ ^3.9.1 - paulstoffregen/OneWire @ ^2.3.5 - adafruit/Adafruit_VL53L0X @ ^1.1.1 - olkal/HX711_ADC @ ^1.2.8 - olikraus/U8g2 @ ^2.28.8 - br3ttb/PID @ ^1.2.1 - knolleary/PubSubClient @ ^2.8 - me-no-dev/AsyncTCP @ ^1.1.1 - me-no-dev/ESP Async WebServer @ ^1.2.3 - bblanchon/ArduinoJson @ ^6.18.5 - tobiasschuerg/ESP8266 Influxdb @ 3.9.0 + lebuni/ZACwire for TSic @ ^2.0.0 + schm1tz1/TSIC @ ^1.1.2 + milesburton/DallasTemperature @ ^3.9.1 + paulstoffregen/OneWire @ ^2.3.5 + adafruit/Adafruit_VL53L0X @ ^1.1.1 + olkal/HX711_ADC @ ^1.2.8 + olikraus/U8g2 @ ^2.28.8 + br3ttb/PID @ ^1.2.1 + knolleary/PubSubClient @ ^2.8 + me-no-dev/AsyncTCP @ ^1.1.1 + bblanchon/ArduinoJson @ ^6.18.5 + tobiasschuerg/ESP8266 Influxdb @ 3.9.0 + git+https://github.com/me-no-dev/ESPAsyncWebServer + git+https://github.com/tzapu/WiFiManager + [env:nodemcuv2_usb] platform = espressif8266 @ ^2.6.3 board = nodemcuv2 diff --git a/src/Displaytemplateminimal.h b/src/Displaytemplateminimal.h index 7f4d183ec..31996ed11 100644 --- a/src/Displaytemplateminimal.h +++ b/src/Displaytemplateminimal.h @@ -103,7 +103,7 @@ void printScreen() { } // Für Statusinfos - if (Offlinemodus == 0) { + if (offlineMode == 0) { getSignalStrength(); if (WiFi.status() != WL_CONNECTED) { diff --git a/src/Displaytemplatescale.h b/src/Displaytemplatescale.h index 21a792e6f..4cbb41f16 100644 --- a/src/Displaytemplatescale.h +++ b/src/Displaytemplatescale.h @@ -102,7 +102,7 @@ void printScreen() { // Für Statusinfos u8g2.drawFrame(32, 0, 128, 12); - if (Offlinemodus == 0) { + if (offlineMode == 0) { getSignalStrength(); if (WiFi.status() == WL_CONNECTED) { @@ -138,7 +138,7 @@ void printScreen() { } } else { u8g2.setCursor(40, 2); - u8g2.print("Offlinemodus"); + u8g2.print("Offline Mode"); } if (TOF == 1) { diff --git a/src/Displaytemplatestandard.h b/src/Displaytemplatestandard.h index 35257dc1e..0a6dc8c1f 100644 --- a/src/Displaytemplatestandard.h +++ b/src/Displaytemplatestandard.h @@ -113,7 +113,7 @@ void printScreen() // Für Statusinfos u8g2.drawFrame(32, 0, 128, 12); - if (Offlinemodus == 0) { + if (offlineMode == 0) { getSignalStrength(); if (WiFi.status() == WL_CONNECTED) { diff --git a/src/Displaytemplatetemponly.h b/src/Displaytemplatetemponly.h index 71dced17c..0cc956a96 100644 --- a/src/Displaytemplatetemponly.h +++ b/src/Displaytemplatetemponly.h @@ -48,7 +48,7 @@ void printScreen() { } // Für Statusinfos - if (Offlinemodus == 0) { + if (offlineMode == 0) { getSignalStrength(); if (WiFi.status() != WL_CONNECTED) { diff --git a/src/Displaytemplateupright.h b/src/Displaytemplateupright.h index 908558008..94244844d 100644 --- a/src/Displaytemplateupright.h +++ b/src/Displaytemplateupright.h @@ -106,7 +106,7 @@ void printScreen() { // Für Statusinfos u8g2.drawFrame(0, 0, 64, 12); - if (Offlinemodus == 0) { + if (offlineMode == 0) { getSignalStrength(); if (WiFi.status() == WL_CONNECTED) { diff --git a/src/RancilioServer.h b/src/RancilioServer.h index 2bbd92a22..2980b259e 100644 --- a/src/RancilioServer.h +++ b/src/RancilioServer.h @@ -17,6 +17,7 @@ #elif defined(ESP8266) #include #include + #define WEBSERVER_H #endif #include diff --git a/src/brewvoid.h b/src/brewvoid.h index 25e2fadff..1f18c7598 100644 --- a/src/brewvoid.h +++ b/src/brewvoid.h @@ -140,7 +140,7 @@ void checkbrewswitch() { void backflush() { if (backflushState != 10 && backflushON == 0) { backflushState = 43; // force reset in case backflushON is reset during backflush! - } else if (Offlinemodus == 1 || brewcounter > 10 || maxflushCycles <= 0 || backflushON == 0) { + } else if (offlineMode == 1 || brewcounter > 10 || maxflushCycles <= 0 || backflushON == 0) { return; } diff --git a/src/display.h b/src/display.h index 0106d5829..0d1f27f8f 100644 --- a/src/display.h +++ b/src/display.h @@ -160,7 +160,7 @@ void Displaymachinestate() { u8g2.clearBuffer(); u8g2.drawFrame(8, 0, 110, 12); - if (Offlinemodus == 0) { + if (offlineMode == 0) { getSignalStrength(); if (WiFi.status() == WL_CONNECTED) { diff --git a/src/rancilio-pid.cpp b/src/rancilio-pid.cpp index ac3ac356b..5f0c9171e 100644 --- a/src/rancilio-pid.cpp +++ b/src/rancilio-pid.cpp @@ -25,6 +25,7 @@ // Libraries #include // for ToF Sensor #include // Library for dallas temp sensor +#include #include #include #include // i2c display @@ -90,7 +91,7 @@ int lastmachinestatepid = -1; // Definitions below must be changed in the userConfig.h file int connectmode = CONNECTMODE; -int Offlinemodus = 0; +int offlineMode = 0; const int OnlyPID = ONLYPID; const int TempSensor = TEMPSENSOR; const int Brewdetection = BREWDETECTION; @@ -98,8 +99,6 @@ const int triggerType = TRIGGERTYPE; const int VoltageSensorType = VOLTAGESENSORTYPE; const boolean ota = OTA; const int grafana = GRAFANA; -const unsigned long wifiConnectionDelay = WIFICINNECTIONDELAY; -const unsigned int maxWifiReconnects = MAXWIFIRECONNECTS; const unsigned long brewswitchDelay = BREWSWITCHDELAY; int BrewMode = BREWMODE; @@ -118,24 +117,15 @@ double distance; double percentage; // WiFi +WiFiManager wm; +const unsigned long wifiConnectionDelay = WIFICINNECTIONDELAY; +const unsigned int maxWifiReconnects = MAXWIFIRECONNECTS; const char *hostname = HOSTNAME; const char *auth = AUTH; -const char *ssid = D_SSID; const char *pass = PASS; unsigned long lastWifiConnectionAttempt = millis(); unsigned int wifiReconnects = 0; // actual number of reconnects -uint8_t softApEnabled = 0; -IPAddress localIp(192, 168, 1, 1); -IPAddress gateway(192, 168, 1, 1); -IPAddress subnet(255, 255, 255, 0); -const char *AP_WIFI_SSID = APWIFISSID; -const char *AP_WIFI_KEY = APWIFIKEY; -const unsigned long checkpowerofftime = 30 * 1000; -boolean checklastpoweroffEnabled = false; -boolean softApEnabledcheck = false; -int softApstate = 0; - // OTA const char *OTAhost = OTAHOST; const char *OTApass = OTAPASS; @@ -402,8 +392,6 @@ std::vector editableVars = { {"PID_BD_TV", "BD D", kDouble, (void *)&aggbTv}, {"PID_BD_TIMER", "PID BD Time (s)", kDouble, (void *)&brewtimersoftware}, {"PID_BD_BREWBOARDER", "PID BD Sensitivity", kDouble, (void *)&brewboarder}, - {"AP_WIFI_SSID", "AP WiFi Name", kCString, (void *)AP_WIFI_SSID}, - {"AP_WIFI_KEY", "AP WiFi Password", kCString, (void *)AP_WIFI_KEY}, {"START_KP", "Start P", kDouble, (void *)&startKp}, {"START_TN", "Start I", kDouble, (void *)&startTn}, {"STEAM_MODE", "Steam Mode", rInteger, (void *)&SteamON}, @@ -419,7 +407,7 @@ unsigned long tempEventInterval = 1000; * @brief Get Wifi signal strength and set bars for display */ void getSignalStrength() { - if (Offlinemodus == 1) return; + if (offlineMode == 1) return; long rssi; @@ -485,67 +473,10 @@ const unsigned long intervalDisplay = 500; #endif #endif -/** - * @brief Create software WiFi AP - */ -void createSoftAp() { - if (softApEnabledcheck == false) { - WiFi.enableAP(true); - WiFi.softAP(AP_WIFI_SSID, AP_WIFI_KEY); - WiFi.softAPConfig(localIp, gateway, subnet); - - softApEnabledcheck = true; - Serial.println("Set softApEnabled: 1, Setup AP MODE\n"); - - uint8_t eepromvalue = 0; - storageSet(STO_ITEM_SOFT_AP_ENABLED_CHECK, eepromvalue, true); - softApstate = 0; - Serial.printf( - "AccessPoint created with SSID %s and KEY %s and OTA Flash via " - "http://%i.%i.%i.%i/\r\n", - AP_WIFI_SSID, AP_WIFI_KEY, WiFi.softAPIP()[0], WiFi.softAPIP()[1], - WiFi.softAPIP()[2], WiFi.softAPIP()[3]); - - #if (OLED_DISPLAY != 0) - displayMessage("Setup-MODE: SSID:", String(AP_WIFI_SSID), "KEY:", String(AP_WIFI_KEY), "IP:", "192.168.1.1"); - #endif - } - - yield(); -} - -void stopSoftAp() { - Serial.println("Closing AccesPoint"); - WiFi.enableAP(false); -} - -void checklastpoweroff() { - storageGet(STO_ITEM_SOFT_AP_ENABLED_CHECK, softApEnabled); - - Serial.printf("softApEnabled: %i\n", softApEnabled); - - if (softApEnabled != 1) { - Serial.printf("Set softApEnabled: 1, was 0\n"); - uint8_t eepromvalue = 1; - storageSet(STO_ITEM_SOFT_AP_ENABLED_CHECK, eepromvalue); - } - - storageCommit(); -} - -void setchecklastpoweroff() { - if (millis() > checkpowerofftime && checklastpoweroffEnabled == false) { - Serial.printf("Set softApEnabled 0 after checkpowerofftime\n"); - uint8_t eepromvalue = 0; - storageSet(STO_ITEM_SOFT_AP_ENABLED_CHECK, eepromvalue, true); - checklastpoweroffEnabled = true; - storageCommit(); - } -} // Blynk define pins and read values BLYNK_CONNECTED() { - if (Offlinemodus == 0 && BLYNK == 1) { + if (offlineMode == 0 && BLYNK == 1) { Blynk.syncAll(); } } @@ -804,7 +735,7 @@ void initOfflineMode() { #endif Serial.println("Start offline mode with eeprom values, no wifi :("); - Offlinemodus = 1; + offlineMode = 1; if (readSysParamsFromStorage() != 0) { #if OLED_DISPLAY != 0 @@ -823,7 +754,7 @@ void initOfflineMode() { * @brief Check if Wifi is connected, if not reconnect abort function if offline, or brew is running */ void checkWifi() { - if (Offlinemodus == 1 || brewcounter > 11) return; + if (offlineMode == 1 || brewcounter > 11) return; /* if kaltstart ist still true when checkWifi() is called, then there was no WIFI connection * at boot -> connect or offlinemode @@ -843,8 +774,8 @@ void checkWifi() { #endif } - WiFi.disconnect(); - WiFi.begin(ssid, pass); // attempt to connect to Wifi network + wm.disconnect(); + wm.autoConnect(); int count = 1; @@ -907,7 +838,7 @@ void sendInflux() { * blynk is also using maxWifiReconnects! */ void checkBlynk() { - if (Offlinemodus == 1 || BLYNK == 0 || brewcounter > 11) return; + if (offlineMode == 1 || BLYNK == 0 || brewcounter > 11) return; if ((millis() - lastBlynkConnectionAttempt >= wifiConnectionDelay) && (blynkReCnctCount <= maxWifiReconnects)) { int statusTemp = Blynk.connected(); @@ -927,7 +858,7 @@ void checkBlynk() { * MQTT is also using maxWifiReconnects! */ void checkMQTT() { - if (Offlinemodus == 1 || brewcounter > 11) return; + if (offlineMode == 1 || brewcounter > 11) return; if ((millis() - lastMQTTConnectionAttempt >= wifiConnectionDelay) && (MQTTReCnctCount <= maxWifiReconnects)) { int statusTemp = mqtt.connected(); @@ -991,7 +922,7 @@ bool mqtt_publish(const char *reading, char *payload) { * @brief Send data to Blynk server */ void sendToBlynkMQTT() { - if (Offlinemodus == 1) return; + if (offlineMode == 1) return; unsigned long currentMillisBlynk = millis(); @@ -1111,7 +1042,7 @@ void brewdetection() { } const unsigned long minBrewDurationForSteamModeQM_ON = 50; - if (brewSteamDetectedQM == 1 && millis()-timePVStoON > minBrewDurationForSteamModeQM_ON) + if (brewSteamDetectedQM == 1 && millis()-timePVStoON > minBrewDurationForSteamModeQM_ON) { if (pvs == VoltageSensorOFF) { @@ -1765,38 +1696,10 @@ void tempLed() { * @brief Set up internal WiFi hardware */ void wiFiSetup() { - unsigned long started = millis(); - - #if defined(ESP8266) - WiFi.hostname(hostname); - #endif - - #if OLED_DISPLAY != 0 - displayLogo(langstring_connectwifi1, ssid); - #endif - - /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default, - * would try to act as both a client and an access-point and could cause - * network-issues with your other WiFi-devices on your WiFi-network. - */ - WiFi.mode(WIFI_STA); - WiFi.persistent(false); // needed, otherwise exceptions are triggered \o.O/ - WiFi.begin(ssid, pass); - - #if defined(ESP32) - WiFi.setHostname(hostname); - #endif - - Serial.printf("Connecting to %s ...\n", ssid); - - // wait up to 20 seconds for connection: - while ((WiFi.status() != WL_CONNECTED) && (millis() - started < 20000)) { - yield(); // Prevent Watchdog trigger - } + wm.setCleanConnect(true); + wm.setBreakAfterConfig(true); - checkWifi(); // try to reconnect - - if (WiFi.status() == WL_CONNECTED) { + if (wm.autoConnect(hostname, pass)) { Serial.printf("WiFi connected - IP = %i.%i.%i.%i\n", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]); @@ -1810,15 +1713,23 @@ void wiFiSetup() { String macaddr5 = number2string(mac[5]); String completemac = macaddr0 + macaddr1 + macaddr2 + macaddr3 + macaddr4 + macaddr5; Serial.printf("MAC-ADRESSE: %s\n", completemac.c_str()); - } else { // No WiFi + + } else { + Serial.println("WiFi connection timed out..."); + #if OLED_DISPLAY != 0 displayLogo(langstring_nowifi[0], langstring_nowifi[1]); #endif - Serial.println("No WIFI"); - WiFi.disconnect(true); + wm.disconnect(); delay(1000); + + offlineMode = 1; } + + #if OLED_DISPLAY != 0 + displayLogo(langstring_connectwifi1, wm.getWiFiSSID(true)); + #endif } /** @@ -1899,261 +1810,196 @@ void setup() { storageSetup(); - // Check AP Mode - checklastpoweroff(); - - if (softApEnabled == 1) { - #if OLED_DISPLAY != 0 - u8g2.setI2CAddress(oled_i2c * 2); - u8g2.begin(); - u8g2_prepare(); - displayLogo(sysVersion, ""); - delay(2000); - #endif - - disableTimer1(); - createSoftAp(); - } else if (softApEnabled == 0) { - // Define trigger type - if (triggerType) { - relayON = HIGH; - relayOFF = LOW; - } else { - relayON = LOW; - relayOFF = HIGH; - } - - if (TRIGGERRELAYTYPE) { - relayETriggerON = HIGH; - relayETriggerOFF = LOW; - } else { - relayETriggerON = LOW; - relayETriggerOFF = HIGH; - } - if (VOLTAGESENSORTYPE) { - VoltageSensorON = HIGH; - VoltageSensorOFF = LOW; - } else { - VoltageSensorON = LOW; - VoltageSensorOFF = HIGH; - } - - // Initialize Pins - pinMode(PINVALVE, OUTPUT); - pinMode(PINPUMP, OUTPUT); - pinMode(PINHEATER, OUTPUT); - pinMode(PINSTEAMSWITCH, INPUT); - digitalWrite(PINVALVE, relayOFF); - digitalWrite(PINPUMP, relayOFF); - digitalWrite(PINHEATER, LOW); - - // IF Etrigger selected - if (ETRIGGER == 1) { - pinMode(PINETRIGGER, OUTPUT); - digitalWrite(PINETRIGGER, relayETriggerOFF); // Set the E-Trigger OFF its, - // important for LOW Trigger Relais - } + // Define trigger type + if (triggerType) { + relayON = HIGH; + relayOFF = LOW; + } else { + relayON = LOW; + relayOFF = HIGH; + } - // IF Voltage sensor selected - if (BREWDETECTION == 3) { - pinMode(PINVOLTAGESENSOR, PINMODEVOLTAGESENSOR); - } + if (TRIGGERRELAYTYPE) { + relayETriggerON = HIGH; + relayETriggerOFF = LOW; + } else { + relayETriggerON = LOW; + relayETriggerOFF = HIGH; + } + if (VOLTAGESENSORTYPE) { + VoltageSensorON = HIGH; + VoltageSensorOFF = LOW; + } else { + VoltageSensorON = LOW; + VoltageSensorOFF = HIGH; + } - // IF PINBREWSWITCH & Steam selected - if (PINBREWSWITCH > 0) { - #if (defined(ESP8266) && PINBREWSWITCH == 16) - pinMode(PINBREWSWITCH, INPUT_PULLDOWN_16); - #endif + // Initialize Pins + pinMode(PINVALVE, OUTPUT); + pinMode(PINPUMP, OUTPUT); + pinMode(PINHEATER, OUTPUT); + pinMode(PINSTEAMSWITCH, INPUT); + digitalWrite(PINVALVE, relayOFF); + digitalWrite(PINPUMP, relayOFF); + digitalWrite(PINHEATER, LOW); - #if (defined(ESP8266) && PINBREWSWITCH == 15) - pinMode(PINBREWSWITCH, INPUT); - #endif + // IF Etrigger selected + if (ETRIGGER == 1) { + pinMode(PINETRIGGER, OUTPUT); + digitalWrite(PINETRIGGER, relayETriggerOFF); // Set the E-Trigger OFF its, + // important for LOW Trigger Relais + } - #if defined(ESP32) - pinMode(PINBREWSWITCH, INPUT_PULLDOWN); - ; - #endif - } + // IF Voltage sensor selected + if (BREWDETECTION == 3) { + pinMode(PINVOLTAGESENSOR, PINMODEVOLTAGESENSOR); + } - #if (defined(ESP8266) && PINSTEAMSWITCH == 16) - pinMode(PINSTEAMSWITCH, INPUT_PULLDOWN_16); + // IF PINBREWSWITCH & Steam selected + if (PINBREWSWITCH > 0) { + #if (defined(ESP8266) && PINBREWSWITCH == 16) + pinMode(PINBREWSWITCH, INPUT_PULLDOWN_16); #endif - #if (defined(ESP8266) && PINSTEAMSWITCH == 15) - pinMode(PINSTEAMSWITCH, INPUT); + #if (defined(ESP8266) && PINBREWSWITCH == 15) + pinMode(PINBREWSWITCH, INPUT); #endif #if defined(ESP32) - pinMode(PINSTEAMSWITCH, INPUT_PULLDOWN); + pinMode(PINBREWSWITCH, INPUT_PULLDOWN); + ; #endif + } - #if OLED_DISPLAY != 0 - u8g2.setI2CAddress(oled_i2c * 2); - u8g2.begin(); - u8g2_prepare(); - displayLogo(sysVersion, ""); - delay(2000); - #endif + #if (defined(ESP8266) && PINSTEAMSWITCH == 16) + pinMode(PINSTEAMSWITCH, INPUT_PULLDOWN_16); + #endif - // Init Scale by BREWMODE 2 or SHOTTIMER 2 - #if (BREWMODE == 2 || ONLYPIDSCALE == 1) - initScale(); - #endif + #if (defined(ESP8266) && PINSTEAMSWITCH == 15) + pinMode(PINSTEAMSWITCH, INPUT); + #endif - // VL530L0x TOF sensor - if (TOF != 0) { - lox.begin(tof_i2c); // initialize TOF sensor at I2C address - lox.setMeasurementTimingBudgetMicroSeconds(2000000); - } + #if defined(ESP32) + pinMode(PINSTEAMSWITCH, INPUT_PULLDOWN); + #endif - // BLYNK & Fallback offline - if (connectmode == 1) { // WiFi Mode - wiFiSetup(); - websiteSetup(); + #if OLED_DISPLAY != 0 + u8g2.setI2CAddress(oled_i2c * 2); + u8g2.begin(); + u8g2_prepare(); + displayLogo(sysVersion, ""); + delay(2000); + #endif - BlynkSetup(); + // Init Scale by BREWMODE 2 or SHOTTIMER 2 + #if (BREWMODE == 2 || ONLYPIDSCALE == 1) + initScale(); + #endif - // OTA Updates - if (ota && WiFi.status() == WL_CONNECTED) { - ArduinoOTA.setHostname(OTAhost); // Device name for OTA - ArduinoOTA.setPassword(OTApass); // Password for OTA - ArduinoOTA.begin(); - } + // VL530L0x TOF sensor + if (TOF != 0) { + lox.begin(tof_i2c); // initialize TOF sensor at I2C address + lox.setMeasurementTimingBudgetMicroSeconds(2000000); + } - if (MQTT == 1) { - snprintf(topic_will, sizeof(topic_will), "%s%s/%s", mqtt_topic_prefix, hostname, "will"); - snprintf(topic_set, sizeof(topic_set), "%s%s/+/%s", mqtt_topic_prefix, hostname, "set"); - mqtt.setServer(mqtt_server_ip, mqtt_server_port); - mqtt.setCallback(mqtt_callback); - checkMQTT(); - } + // BLYNK & Fallback offline + if (connectmode == 1) { // WiFi Mode + wiFiSetup(); + websiteSetup(); - if (INFLUXDB == 1) { - if (INFLUXDB_AUTH_TYPE == 1) { - influxClient.setConnectionParams(INFLUXDB_URL, INFLUXDB_ORG_NAME, INFLUXDB_DB_NAME, INFLUXDB_API_TOKEN); - } - else if (INFLUXDB_AUTH_TYPE == 2 && (strlen(INFLUXDB_USER) > 0) && (strlen(INFLUXDB_PASSWORD) > 0)) { - influxClient.setConnectionParamsV1(INFLUXDB_URL, INFLUXDB_DB_NAME, INFLUXDB_USER, INFLUXDB_PASSWORD); - } - } - } + BlynkSetup(); - // Initialize PID controller - bPID.SetSampleTime(windowSize); - bPID.SetOutputLimits(0, windowSize); - bPID.SetMode(AUTOMATIC); - - // Temp sensor - if (TempSensor == 1) { - sensors.begin(); - sensors.getAddress(sensorDeviceAddress, 0); - sensors.setResolution(sensorDeviceAddress, 10); - sensors.requestTemperatures(); - Input = sensors.getTempCByIndex(0); + // OTA Updates + if (ota && WiFi.status() == WL_CONNECTED) { + ArduinoOTA.setHostname(OTAhost); // Device name for OTA + ArduinoOTA.setPassword(OTApass); // Password for OTA + ArduinoOTA.begin(); } - if (TempSensor == 2) { - temperature = 0; - - #if (ONE_WIRE_BUS == 16 && defined(ESP8266)) - Sensor1.getTemperature(&temperature); - Input = Sensor1.calc_Celsius(&temperature); - #endif - - #if ((ONE_WIRE_BUS != 16 && defined(ESP8266)) || defined(ESP32)) - Input = Sensor2.getTemp(); - #endif + if (MQTT == 1) { + snprintf(topic_will, sizeof(topic_will), "%s%s/%s", mqtt_topic_prefix, hostname, "will"); + snprintf(topic_set, sizeof(topic_set), "%s%s/+/%s", mqtt_topic_prefix, hostname, "set"); + mqtt.setServer(mqtt_server_ip, mqtt_server_port); + mqtt.setCallback(mqtt_callback); + checkMQTT(); } - // moving average ini array - if (Brewdetection == 1) { - for (int thisReading = 0; thisReading < numReadings; thisReading++) { - readingstemp[thisReading] = 0; - readingstime[thisReading] = 0; - readingchangerate[thisReading] = 0; + if (INFLUXDB == 1) { + if (INFLUXDB_AUTH_TYPE == 1) { + influxClient.setConnectionParams(INFLUXDB_URL, INFLUXDB_ORG_NAME, INFLUXDB_DB_NAME, INFLUXDB_API_TOKEN); + } + else if (INFLUXDB_AUTH_TYPE == 2 && (strlen(INFLUXDB_USER) > 0) && (strlen(INFLUXDB_PASSWORD) > 0)) { + influxClient.setConnectionParamsV1(INFLUXDB_URL, INFLUXDB_DB_NAME, INFLUXDB_USER, INFLUXDB_PASSWORD); } } + } - // Initialisation MUST be at the very end of the init(), otherwise the - // time comparision in loop() will have a big offset - unsigned long currentTime = millis(); - previousMillistemp = currentTime; - windowStartTime = currentTime; - previousMillisDisplay = currentTime; - previousMillisBlynk = currentTime; - previousMillisMQTT = currentTime; - previousMillisInflux = currentTime; - previousMillisETrigger = currentTime; - previousMillisVoltagesensorreading = currentTime; - lastMQTTConnectionAttempt = currentTime; + // Initialize PID controller + bPID.SetSampleTime(windowSize); + bPID.SetOutputLimits(0, windowSize); + bPID.SetMode(AUTOMATIC); - #if (BREWMODE == 2) - previousMillisScale = currentTime; + // Temp sensor + if (TempSensor == 1) { + sensors.begin(); + sensors.getAddress(sensorDeviceAddress, 0); + sensors.setResolution(sensorDeviceAddress, 10); + sensors.requestTemperatures(); + Input = sensors.getTempCByIndex(0); + } + + if (TempSensor == 2) { + temperature = 0; + + #if (ONE_WIRE_BUS == 16 && defined(ESP8266)) + Sensor1.getTemperature(&temperature); + Input = Sensor1.calc_Celsius(&temperature); #endif - #if (PRESSURESENSOR == 1) - previousMillisPressure = currentTime; + + #if ((ONE_WIRE_BUS != 16 && defined(ESP8266)) || defined(ESP32)) + Input = Sensor2.getTemp(); #endif + } + + // moving average ini array + if (Brewdetection == 1) { + for (int thisReading = 0; thisReading < numReadings; thisReading++) { + readingstemp[thisReading] = 0; + readingstime[thisReading] = 0; + readingchangerate[thisReading] = 0; + } + } - setupDone = true; + // Initialisation MUST be at the very end of the init(), otherwise the + // time comparision in loop() will have a big offset + unsigned long currentTime = millis(); + previousMillistemp = currentTime; + windowStartTime = currentTime; + previousMillisDisplay = currentTime; + previousMillisBlynk = currentTime; + previousMillisMQTT = currentTime; + previousMillisInflux = currentTime; + previousMillisETrigger = currentTime; + previousMillisVoltagesensorreading = currentTime; + lastMQTTConnectionAttempt = currentTime; + + #if (BREWMODE == 2) + previousMillisScale = currentTime; + #endif + #if (PRESSURESENSOR == 1) + previousMillisPressure = currentTime; + #endif - enableTimer1(); + setupDone = true; - } // else softenable == 1 + enableTimer1(); } void loop() { if (calibration_mode == 1 && TOF == 1) { loopcalibrate(); - } else if (softApEnabled == 0) { + } else { looppid(); - } else if (softApEnabled == 1) { - switch (softApstate) { - case 0: - if (WiFi.softAPgetStationNum() > 0) { - ArduinoOTA.setHostname(OTAhost); // Device name for OTA - ArduinoOTA.setPassword(OTApass); // Password for OTA - ArduinoOTA.begin(); - softApstate = 10; - } - break; - case 10: - ArduinoOTA.handle(); // For OTA - - // Disable interrupt it OTA is starting, otherwise it will not work - ArduinoOTA.onStart([]() { - #if defined(ESP8266) - timer1_disable(); - #endif - - #if defined(ESP32) - timerAlarmDisable(timer); - #endif - - digitalWrite(PINHEATER, LOW); // Stop heating - }); - - ArduinoOTA.onError([](ota_error_t error) { - #if defined(ESP8266) - timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE); - #endif - - #if defined(ESP32) - timerAlarmEnable(timer); - #endif - }); - - // Enable interrupts if OTA is finished - ArduinoOTA.onEnd([]() { - #if defined(ESP8266) - timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE); - #endif - - #if defined(ESP32) - timerAlarmEnable(timer); - #endif - }); - break; - } } } @@ -2195,7 +2041,7 @@ void loopcalibrate() { void looppid() { // Only do Wifi stuff, if Wifi is connected - if (WiFi.status() == WL_CONNECTED && Offlinemodus == 0) { + if (WiFi.status() == WL_CONNECTED && offlineMode == 0) { if (MQTT == 1) { checkMQTT(); writeSysParamsToMQTT(); @@ -2268,7 +2114,6 @@ void looppid() { setEmergencyStopTemp(); sendToBlynkMQTT(); machinestatevoid(); // calc machinestate - setchecklastpoweroff(); // FOR AP MODE tempLed(); if (INFLUXDB == 1) { diff --git a/src/userConfig_sample.h b/src/userConfig_sample.h index 75e09a4c7..3a88ed3a1 100644 --- a/src/userConfig_sample.h +++ b/src/userConfig_sample.h @@ -4,10 +4,6 @@ * @version 3.0.1 Alpha * */ - -/** - * Define area, do not change anything here - */ #ifndef _userConfig_H #define _userConfig_H @@ -45,10 +41,10 @@ enum MACHINE { // Connectivity #define CONNECTMODE 1 // 0 = offline 1 = WIFI-MODE 2 = AP-MODE (not working in the moment) -#define BLYNK 0 // 0 = no Blynk , 1 = Blynk -#define MQTT 0 // 1 = MQTT enabled, 0 = MQTT disabled -#define GRAFANA 0 // 2= custom Grafana 1 = grafana visualisation (access required), 0 = off (default) -#define INFLUXDB 0 // 1 = INFLUX enabled, 0 = INFLUX disabled +#define HOSTNAME "silvia" +#define PASS "CleverCoffee" // default password for WiFiManager +#define MAXWIFIRECONNECTS 5 // maximum number of reconnection attempts, use -1 to deactivate +#define WIFICINNECTIONDELAY 10000 // delay between reconnects in ms // PID & Hardware #define ONLYPID 1 // 1 = Only PID, 0 = PID and preinfusion @@ -75,7 +71,7 @@ enum MACHINE { #define ETRIGGERTIME 600 // seconds, time between trigger signal #define TRIGGERRELAYTYPE HIGH // LOW = low trigger, HIGH = high trigger relay for E-Trigger -//Weight SCALE +// Brew Scale #define SCALE_WEIGHTSETPOINT 30 // In grams #define SCALE_SAMPLES 2 // Load cell sample rate #define SCALE_CALIBRATION_FACTOR 3195.83 // Raw data is divided by this value to convert to readable data @@ -90,23 +86,13 @@ enum MACHINE { #define FULLSCALE 922 // 90% of ADC input @3.3V supply = 922 #define MAXPRESSURE 200 -// Accesspoint -#define APWIFISSID "Rancilio" -#define APWIFIKEY "Rancilio" - -/// Wifi -#define HOSTNAME "wifi-hostname" -#define D_SSID "myssid" -#define PASS "mypass" -#define MAXWIFIRECONNECTS 5 // maximum number of reconnection attempts, use -1 to deactivate -#define WIFICINNECTIONDELAY 10000 // delay between reconnects in ms - // OTA #define OTA true // true = OTA activated, false = OTA deactivated #define OTAHOST "ota_hostname" // Name to be shown in ARUDINO IDE Port #define OTAPASS "otapass" // Password for OTA updtates // MQTT +#define MQTT 0 // 1 = MQTT enabled, 0 = MQTT disabled #define MQTT_USERNAME "mymqttuser" #define MQTT_PASSWORD "mymqttpass" #define MQTT_TOPIC_PREFIX "custom/Küche." // topic will be "/" @@ -114,11 +100,16 @@ enum MACHINE { #define MQTT_SERVER_PORT 1883 // BLYNK +#define BLYNK 0 // 0 = no Blynk , 1 = Blynk #define AUTH "blynk_auth" #define BLYNKADDRESS "blynk.clevercoffee.de" // blynk-server IP-Address #define BLYNKPORT 8080 // blynk-server portver +// Grafana +#define GRAFANA 0 // 2= custom Grafana 1 = grafana visualisation (access required), 0 = off (default) + // INFLUXDB +#define INFLUXDB 0 // 1 = INFLUX enabled, 0 = INFLUX disabled #define INFLUXDB_URL "" // InfluxDB server address #define INFLUXDB_AUTH_TYPE 1 // 1 = API Token , 2 = User/Pass #define INFLUXDB_API_TOKEN ""