diff --git a/manual/factory_firmware.md b/manual/factory_firmware.md index f5cd3d91..ba270651 100644 --- a/manual/factory_firmware.md +++ b/manual/factory_firmware.md @@ -60,9 +60,9 @@ For a 4MB flash size the upload should be finished within 22 seconds. Reboot your ESP an check if all your settings are present. -## Keep updated with 'Mainline' +## Get updated with 'Mainline' -From time to time a new version of AhoyDTU will be published. To get this changes into your alread prepared factory binary generation environment you have to do only a few steps: +From time to time a new version of AhoyDTU will be published. To get the changes into your already prepared factory binary generation environment you have to do only a few steps: 1. revert the changes of `platformio.ini` by executing from repository root: `git checkout src/platformio.ini` 2. pull new changes from remote: `git pull` diff --git a/src/app.cpp b/src/app.cpp index accd0c87..165db37a 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -112,9 +112,7 @@ void app::setup() { mPubSerial.setup(mConfig, &mSys, &mTimestamp); - #if !defined(ETHERNET) //mImprov.setup(this, mConfig->sys.deviceName, mVersion); - #endif #if defined(ENABLE_SIMULATOR) mSimulator.setup(&mSys, &mTimestamp, 0); @@ -174,9 +172,7 @@ void app::regularTickers(void) { everySec(std::bind(&DisplayType::tickerSecond, &mDisplay), "disp"); #endif every(std::bind(&PubSerialType::tick, &mPubSerial), 5, "uart"); - #if !defined(ETHERNET) //everySec([this]() { mImprov.tickSerial(); }, "impro"); - #endif #if defined(ENABLE_HISTORY) everySec(std::bind(&HistoryType::tickerSecond, &mHistory), "hist"); diff --git a/src/app.h b/src/app.h index 634696f2..20b505c5 100644 --- a/src/app.h +++ b/src/app.h @@ -45,8 +45,8 @@ #else #include "network/AhoyWifiEsp8266.h" #endif - #include "utils/improv.h" #endif /* defined(ETHERNET) */ +#include "utils/improv.h" #if defined(ENABLE_SIMULATOR) #include "hm/simulator.h" @@ -167,7 +167,6 @@ class app : public IApp, public ah::Scheduler { return mSaveReboot; } - #if !defined(ETHERNET) bool getAvailNetworks(JsonObject obj) override { return mNetwork->getAvailNetworks(obj, this); } @@ -183,7 +182,6 @@ class app : public IApp, public ah::Scheduler { return false; #endif } - #endif /* !defined(ETHERNET) */ String getIp(void) override { return mNetwork->getIp(); @@ -433,9 +431,7 @@ class app : public IApp, public ah::Scheduler { #endif PubSerialType mPubSerial; - #if !defined(ETHERNET) //Improv mImprov; - #endif #ifdef ESP32 CmtRadio<> mCmtRadio; #endif diff --git a/src/appInterface.h b/src/appInterface.h index f2292ec8..de5c97e9 100644 --- a/src/appInterface.h +++ b/src/appInterface.h @@ -28,11 +28,9 @@ class IApp { virtual void addOnce(ah::scdCb c, uint32_t timeout, const char *name) = 0; - #if !defined(ETHERNET) virtual bool getAvailNetworks(JsonObject obj) = 0; virtual void setupStation(void) = 0; virtual bool getWasInCh12to14(void) const = 0; - #endif /* defined(ETHERNET) */ virtual String getIp(void) = 0; virtual bool isApActive(void) = 0; diff --git a/src/config/settings.h b/src/config/settings.h index 25243fe7..abee0154 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -99,14 +99,13 @@ typedef struct { int8_t timezone; char apPwd[PWD_LEN]; -#if !defined(ETHERNET) // wifi char stationSsid[SSID_LEN]; char stationPwd[PWD_LEN]; bool isHidden; -#else + #if defined(ETHERNET) cfgEth_t eth; -#endif /* !defined(ETHERNET) */ + #endif cfgIp_t ip; } cfgSys_t; diff --git a/src/network/AhoyEthernet.h b/src/network/AhoyEthernet.h index 90255b26..24204004 100644 --- a/src/network/AhoyEthernet.h +++ b/src/network/AhoyEthernet.h @@ -12,10 +12,18 @@ #include #include "AhoyEthernetSpi.h" #include "AhoyNetwork.h" +#include "AhoyWifiEsp32.h" + +class AhoyEthernet : public AhoyWifi { + private: + enum class Mode { + WIRED, + WIRELESS + }; -class AhoyEthernet : public AhoyNetwork { public: void begin() override { + mMode = Mode::WIRELESS; mAp.enable(); if(!mConfig->sys.eth.enabled) @@ -25,8 +33,9 @@ class AhoyEthernet : public AhoyNetwork { ETH.setHostname(mConfig->sys.deviceName); } - void OnEvent(WiFiEvent_t event) override { + void OnEvent(WiFiEvent_t event) { switch(event) { + case SYSTEM_EVENT_STA_CONNECTED: case ARDUINO_EVENT_ETH_CONNECTED: if(NetworkState::CONNECTED != mStatus) { mStatus = NetworkState::CONNECTED; @@ -35,14 +44,30 @@ class AhoyEthernet : public AhoyNetwork { } break; + case SYSTEM_EVENT_STA_GOT_IP: + mStatus = NetworkState::GOT_IP; + if(mAp.isEnabled()) + mAp.disable(); + + mMode = Mode::WIRELESS; + if(!mConnected) { + mConnected = true; + ah::welcome(WiFi.localIP().toString(), F("Station WiFi")); + MDNS.begin(mConfig->sys.deviceName); + mOnNetworkCB(true); + } + break; + case ARDUINO_EVENT_ETH_GOT_IP: mStatus = NetworkState::GOT_IP; + mMode = Mode::WIRED; if(!mConnected) { mAp.disable(); mConnected = true; - ah::welcome(ETH.localIP().toString(), F("Station")); + ah::welcome(ETH.localIP().toString(), F("Station Ethernet")); MDNS.begin(mConfig->sys.deviceName); mOnNetworkCB(true); + WiFi.disconnect(); } break; @@ -51,9 +76,25 @@ class AhoyEthernet : public AhoyNetwork { case ARDUINO_EVENT_ETH_DISCONNECTED: mStatus = NetworkState::DISCONNECTED; if(mConnected) { + mMode = Mode::WIRELESS; mConnected = false; mOnNetworkCB(false); - mAp.enable(); + MDNS.end(); + AhoyWifi::begin(); + } + break; + + case ARDUINO_EVENT_WIFI_STA_LOST_IP: + [[fallthrough]]; + case ARDUINO_EVENT_WIFI_STA_STOP: + [[fallthrough]]; + case SYSTEM_EVENT_STA_DISCONNECTED: + mStatus = NetworkState::DISCONNECTED; + if(mConnected && (Mode::WIRELESS == mMode)) { + mConnected = false; + mOnNetworkCB(false); + MDNS.end(); + AhoyWifi::begin(); } break; @@ -62,24 +103,27 @@ class AhoyEthernet : public AhoyNetwork { } } - void tickNetworkLoop() override { - if(mAp.isEnabled()) - mAp.tickLoop(); - } - - String getIp(void) override { - return ETH.localIP().toString(); + String getIp(void) { + if(Mode::WIRELESS == mMode) + return AhoyWifi::getIp(); + else + return ETH.localIP().toString(); } private: void setStaticIp() override { setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool { - return ETH.config(ip, gateway, mask, dns1, dns2); + if(Mode::WIRELESS == mMode) + return WiFi.config(ip, gateway, mask, dns1, dns2); + else + return ETH.config(ip, gateway, mask, dns1, dns2); }); } private: AhoyEthernetSpi mEthSpi; + Mode mMode; + }; #endif /*ETHERNET*/ diff --git a/src/network/AhoyNetwork.h b/src/network/AhoyNetwork.h index 7bc0bab2..4267105e 100644 --- a/src/network/AhoyNetwork.h +++ b/src/network/AhoyNetwork.h @@ -88,7 +88,6 @@ class AhoyNetwork { return mAp.isEnabled(); } - #if !defined(ETHERNET) bool getAvailNetworks(JsonObject obj, IApp *app) { if(!mScanActive) { app->addOnce([this]() {scan();}, 1, "scan"); @@ -122,7 +121,6 @@ class AhoyNetwork { } WiFi.scanNetworks(true, true); } - #endif protected: virtual void setStaticIp() = 0; @@ -173,7 +171,6 @@ class AhoyNetwork { } } - #if !defined(ETHERNET) void sortRSSI(int *sort, int n) { for (int i = 0; i < n; i++) sort[i] = i; @@ -182,7 +179,6 @@ class AhoyNetwork { if (WiFi.RSSI(sort[j]) > WiFi.RSSI(sort[i])) std::swap(sort[i], sort[j]); } - #endif private: void sendNTPpacket(void) { @@ -234,9 +230,7 @@ class AhoyNetwork { uint32_t *mUtcTimestamp = nullptr; bool mConnected = false; bool mScanActive = false; - #if !defined(ETHERNET) bool mWifiConnecting = false; - #endif OnNetworkCB mOnNetworkCB; OnTimeCB mOnTimeCB; diff --git a/src/network/AhoyWifiEsp32.h b/src/network/AhoyWifiEsp32.h index 52ad6292..5233864a 100644 --- a/src/network/AhoyWifiEsp32.h +++ b/src/network/AhoyWifiEsp32.h @@ -6,7 +6,7 @@ #ifndef __AHOY_WIFI_ESP32_H__ #define __AHOY_WIFI_ESP32_H__ -#if defined(ESP32) && !defined(ETHERNET) +#if defined(ESP32) #include #include #include "AhoyNetwork.h" @@ -14,7 +14,7 @@ class AhoyWifi : public AhoyNetwork { public: - void begin() override { + virtual void begin() override { mAp.enable(); if(String(FB_WIFI_SSID) == mConfig->sys.stationSsid) @@ -34,7 +34,7 @@ class AhoyWifi : public AhoyNetwork { #endif } - void OnEvent(WiFiEvent_t event) override { + virtual void OnEvent(WiFiEvent_t event) override { switch(event) { case SYSTEM_EVENT_STA_CONNECTED: if(NetworkState::CONNECTED != mStatus) { @@ -87,12 +87,12 @@ class AhoyWifi : public AhoyNetwork { } private: - void setStaticIp() override { + virtual void setStaticIp() override { setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool { return WiFi.config(ip, gateway, mask, dns1, dns2); }); } }; -#endif /*ESP32 & !ETHERNET*/ +#endif /*ESP32*/ #endif /*__AHOY_WIFI_ESP32_H__*/