diff --git a/Software/ChainLube/platformio.ini b/Software/ChainLube/platformio.ini index e5e5e23..418a80a 100644 --- a/Software/ChainLube/platformio.ini +++ b/Software/ChainLube/platformio.ini @@ -17,16 +17,18 @@ platform = espressif8266 board = d1_mini framework = arduino -upload_port = 10.0.1.90 -upload_protocol = espota -upload_flags = - --auth = ${wifi_cred.admin_pass} +upload_protocol = esptool +;upload_port = 10.0.1.90 +;upload_protocol = espota +;upload_flags = +; --auth=${wifi_cred.admin_pass} build_flags = !python git_rev_macro.py -DWIFI_SSID=${wifi_cred.wifi_ssid} -DWIFI_PASSWORD=${wifi_cred.wifi_password} - -DADMIN_PASS=${wifi_cred.admin_pass} + -DADMIN_PASSWORD=${wifi_cred.admin_password} + -DWIFI_AP_PASSWORD=${wifi_cred.wifi_ap_password} ;-DDEBUG -fexceptions @@ -43,4 +45,5 @@ lib_ldf_mode = deep lib_deps = olikraus/U8g2 @ ^2.28.8 joaolopesf/RemoteDebug @ ^2.1.2 - fastled/FastLED @ ^3.5.0 \ No newline at end of file + fastled/FastLED @ ^3.5.0 + sstaub/Ticker @ ^4.2.0 \ No newline at end of file diff --git a/Software/ChainLube/src/common.h b/Software/ChainLube/src/common.h index c5140d0..570888c 100644 --- a/Software/ChainLube/src/common.h +++ b/Software/ChainLube/src/common.h @@ -1,6 +1,9 @@ #ifndef _COMMON_H_ #define _COMMON_H_ +#define Q(x) #x +#define QUOTE(x) Q(x) + #define GPIO_BUTTON 14 #define GPIO_LED D6 #define GPIO_TRIGGER 2 @@ -14,8 +17,8 @@ #define OTA_DELAY 50 // ticks -> 10ms / tick #endif -#ifndef ADMIN_PASS -#error "You need to define ADMIN_PASS for OTA-Update" +#ifndef ADMIN_PASSWORD +#error "You need to define ADMIN_PASSWORD for OTA-Update" #endif #ifndef WIFI_PASSWORD #error "You must define an WIFI_PASSWORD for OTA-Update" @@ -23,5 +26,8 @@ #ifndef WIFI_SSID #error "You must define an WIFI_SSID for OTA-Update" #endif +#ifndef WIFI_AP_PASSWORD +#error "You must define an WIFI_AP_PASSWORD for Standalone AP-Mode" +#endif #endif \ No newline at end of file diff --git a/Software/ChainLube/src/lubeapp.cpp b/Software/ChainLube/src/lubeapp.cpp new file mode 100644 index 0000000..4c2538b --- /dev/null +++ b/Software/ChainLube/src/lubeapp.cpp @@ -0,0 +1,6 @@ +#include + +void RunLubeApp() +{ + +} \ No newline at end of file diff --git a/Software/ChainLube/src/main.cpp b/Software/ChainLube/src/main.cpp index 88f99be..e21621a 100644 --- a/Software/ChainLube/src/main.cpp +++ b/Software/ChainLube/src/main.cpp @@ -7,12 +7,15 @@ #include #include #include +#include #include "common.h" #include "rmtdbghelp.h" -const char *ssid = WIFI_SSID; -const char *password = WIFI_PASSWORD; +const char *ssid = QUOTE(WIFI_SSID); +const char *password = QUOTE(WIFI_PASSWORD); + +const uint32_t connectTimeoutMs = 5000; #ifdef DEBUG const bool debug_flag = true; @@ -23,17 +26,37 @@ const bool debug_flag = false; bool startSetupMode = false; char DeviceName[33]; +volatile uint32_t wheel_pulse = 0; + +typedef struct tLubeConfig +{ + uint32_t DistancePerLube_Default = 0; + uint32_t DistancePerLube_Rain = 0; + uint32_t tankCapacity_ml = 320; + uint32_t amountPerDose_µl = 0; + uint32_t tankRemain_µl = 0; + uint8_t TankRemindAtPercentage = 30; + uint8_t PulsePerRevolution = 1; + uint32_t DistancePerRevolution_mm = 2000; + +} sLubeConfig; + U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(-1); RemoteDebug Debug; ESP8266WiFiMulti wifiMulti; +CRGB leds[1]; // Function-Prototypes String IpAddress2String(const IPAddress &ipAddress); void processCmdRemoteDebug(); void RemotDebug_printSystemInfo(); void RemoteDebug_printWifiInfo(); +void wifiMaintainConnectionTicker_callback(); +void IRAM_ATTR trigger_ISR(); +extern void RunLubeApp(); +extern void initWebUI(); -CRGB leds[1]; +Ticker WiFiMaintainConnectionTicker(wifiMaintainConnectionTicker_callback, 1000, 0, MILLIS); void setup() { @@ -44,19 +67,23 @@ void setup() Serial.begin(115200); Serial.setDebugOutput(true); - Serial.println("Souko's ChainOiler Mk1"); + Serial.println("Souko's ChainLube Mk1"); Serial.println(DeviceName); u8x8.begin(); u8x8.setFont(u8x8_font_chroma48medium8_r); + FastLED.addLeds(leds, 1); // GRB ordering is assumed + pinMode(GPIO_TRIGGER, INPUT_PULLUP); pinMode(GPIO_BUTTON, INPUT_PULLUP); pinMode(GPIO_PUMP, OUTPUT); + attachInterrupt(digitalPinToInterrupt(GPIO_TRIGGER), trigger_ISR, FALLING); + WiFi.mode(WIFI_STA); WiFi.setHostname(DeviceName); - wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD); + wifiMulti.addAP(QUOTE(WIFI_SSID), QUOTE(WIFI_PASSWORD)); Serial.println("Connecting Wifi..."); if (wifiMulti.run() == WL_CONNECTED) @@ -67,6 +94,8 @@ void setup() Serial.print(WiFi.localIP()); } + WiFiMaintainConnectionTicker.start(); + if (MDNS.begin(DeviceName)) MDNS.addService("telnet", "tcp", 23); @@ -74,13 +103,14 @@ void setup() Debug.setResetCmdEnabled(true); // Enable the reset command Debug.showProfiler(true); // Profiler (Good to measure times, to optimize codes) Debug.showColors(true); // Colors + Debug.setPassword(QUOTE(ADMIN_PASSWORD)); Debug.setHelpProjectsCmds(helpCmd); Debug.setCallBackProjectCmds(&processCmdRemoteDebug); ArduinoOTA.setPort(8266); ArduinoOTA.setHostname(DeviceName); - ArduinoOTA.setPassword(ADMIN_PASS); + ArduinoOTA.setPassword(QUOTE(ADMIN_PASSWORD)); ArduinoOTA.onStart([]() { @@ -111,17 +141,20 @@ void setup() ArduinoOTA.begin(); u8x8.clearDisplay(); - u8x8.drawString(4, 4, "Souko's"); - u8x8.drawString(1, 5, "ChainLube Mk1"); + u8x8.drawString(4, 0, "Souko's"); + u8x8.drawString(1, 1, "ChainLube Mk1"); u8x8.refreshDisplay(); - FastLED.addLeds(leds, 1); // GRB ordering is assumed + initWebUI(); } void loop() { leds[0] = digitalRead(GPIO_BUTTON) ? CRGB::Green : CRGB::Red; + RunLubeApp(); + + WiFiMaintainConnectionTicker.update(); FastLED.show(); ArduinoOTA.handle(); Debug.handle(); @@ -157,8 +190,41 @@ void RemotDebug_printSystemInfo() debugA("Boot Mode: %u", ESP.getBootMode()); debugA("CPU Frequency: %u MHz", ESP.getCpuFreqMHz()); debugA("Reset reason: %s", ESP.getResetReason().c_str()); + + debugA("OTA-Pass: %s", QUOTE(ADMIN_PASSWORD)); } void RemoteDebug_printWifiInfo() { } + +void wifiMaintainConnectionTicker_callback() +{ + static uint32_t WiFiFailCount = 0; + const uint32_t WiFiFailMax = 20; + + if (wifiMulti.run(connectTimeoutMs) == WL_CONNECTED) + { + return; + } + else + { + if (WiFiFailCount < WiFiFailMax) + { + WiFiFailCount++; + } + else + { + Serial.println("WiFi not connected! - Start AP"); + WiFi.mode(WIFI_OFF); + WiFi.softAP(DeviceName, QUOTE(WIFI_AP_PASSWORD)); + WiFiMaintainConnectionTicker.stop(); + Serial.println("WiFi AP started, stopped Maintain-Timer"); + } + } +} + +void trigger_ISR() +{ + wheel_pulse++; +} \ No newline at end of file diff --git a/Software/ChainLube/src/webui.cpp b/Software/ChainLube/src/webui.cpp index ef6907a..7acbc7b 100644 --- a/Software/ChainLube/src/webui.cpp +++ b/Software/ChainLube/src/webui.cpp @@ -7,144 +7,24 @@ uint16_t status; void initWebUI(); -void numberCall( Control* sender, int type ) { - Serial.println( sender->value ); -} - -void textCall( Control* sender, int type ) { - Serial.print("Text: ID: "); - Serial.print(sender->id); - Serial.print(", Value: "); - Serial.println( sender->value );} void slider( Control* sender, int type ) { Serial.print("Slider: ID: "); Serial.print(sender->id); Serial.print(", Value: "); - Serial.println( sender->value );} - -void buttonCallback( Control* sender, int type ) { - switch ( type ) { - case B_DOWN: - Serial.println( "Button DOWN" ); - break; - - case B_UP: - Serial.println( "Button UP" ); - break; - } -} - -void buttonExample( Control* sender, int type ) { - switch ( type ) { - case B_DOWN: - Serial.println( "Status: Start" ); - ESPUI.updateControlValue( status, "Start" ); - - ESPUI.getControl( button1 )->color = ControlColor::Carrot; - ESPUI.updateControl( button1 ); - break; - - case B_UP: - Serial.println( "Status: Stop" ); - ESPUI.updateControlValue( status, "Stop" ); - - ESPUI.getControl( button1 )->color = ControlColor::Peterriver; - ESPUI.updateControl( button1 ); - break; - } -} - -void padExample( Control* sender, int value ) { - switch ( value ) { - case P_LEFT_DOWN: - Serial.print( "left down" ); - break; - - case P_LEFT_UP: - Serial.print( "left up" ); - break; - - case P_RIGHT_DOWN: - Serial.print( "right down" ); - break; - - case P_RIGHT_UP: - Serial.print( "right up" ); - break; - - case P_FOR_DOWN: - Serial.print( "for down" ); - break; - - case P_FOR_UP: - Serial.print( "for up" ); - break; - - case P_BACK_DOWN: - Serial.print( "back down" ); - break; - - case P_BACK_UP: - Serial.print( "back up" ); - break; - - case P_CENTER_DOWN: - Serial.print( "center down" ); - break; - - case P_CENTER_UP: - Serial.print( "center up" ); - break; - } - - Serial.print( " " ); - Serial.println( sender->id ); -} - -void switchExample( Control* sender, int value ) { - switch ( value ) { - case S_ACTIVE: - Serial.print( "Active:" ); - break; - - case S_INACTIVE: - Serial.print( "Inactive" ); - break; - } - - Serial.print( " " ); - Serial.println( sender->id ); -} - -void selectExample( Control* sender, int value ) { - Serial.print("Select: ID: "); - Serial.print(sender->id); - Serial.print(", Value: "); Serial.println( sender->value ); -} - -void otherSwitchExample( Control* sender, int value ) { - switch ( value ) { - case S_ACTIVE: - Serial.print( "Active:" ); - break; - - case S_INACTIVE: - Serial.print( "Inactive" ); - break; } - Serial.print( " " ); - Serial.println( sender->id ); -} void initWebUI (void){ - uint16_t tab1 = ESPUI.addControl( ControlType::Tab, "Settings 1", "Settings 1" ); - uint16_t tab2 = ESPUI.addControl( ControlType::Tab, "Settings 2", "Settings 2" ); - uint16_t tab3 = ESPUI.addControl( ControlType::Tab, "Settings 3", "Settings 3" ); + uint16_t tab_lube = ESPUI.addControl( ControlType::Tab, "Schmierung", "Schmierung" ); + uint16_t tab_tank = ESPUI.addControl( ControlType::Tab, "Tank", "Tank" ); + uint16_t tab_wifi = ESPUI.addControl( ControlType::Tab, "WiFi", "WiFi" ); + ESPUI.addControl( ControlType::Slider, "Distanz Normal", "10000", ControlColor::Alizarin, tab_lube, &slider ); + ESPUI.addControl( ControlType::Slider, "Distanz Regen", "10000", ControlColor::Alizarin, tab_lube, &slider ); +/* // shown above all tabs status = ESPUI.addControl( ControlType::Label, "Status:", "Stop", ControlColor::Turquoise ); @@ -166,7 +46,10 @@ void initWebUI (void){ ESPUI.addControl( ControlType::Slider, "Slider one", "30", ControlColor::Alizarin, tab1, &slider ); ESPUI.addControl( ControlType::Slider, "Slider two", "100", ControlColor::Alizarin, tab3, &slider ); ESPUI.addControl( ControlType::Number, "Number:", "50", ControlColor::Alizarin, tab3, &numberCall ); +*/ ESPUI.begin("ESPUI Control"); } + + diff --git a/Software/ChainLube/wifi_credentials.example.ini b/Software/ChainLube/wifi_credentials.example.ini index 4af929c..ef65110 100644 --- a/Software/ChainLube/wifi_credentials.example.ini +++ b/Software/ChainLube/wifi_credentials.example.ini @@ -1,4 +1,5 @@ [wifi_cred] -admin_pass = 'adminpass' -wifi_ssid = 'wifi-ssid' -wifi_password = 'wifi-pass' \ No newline at end of file +admin_password = adminpass +wifi_ap_password = wifiappass +wifi_ssid = wifi-ssid +wifi_password = wifi-pass \ No newline at end of file