diff --git a/ConfigFile.cpp b/ConfigFile.cpp index f1bcd30..e515bed 100644 --- a/ConfigFile.cpp +++ b/ConfigFile.cpp @@ -614,7 +614,7 @@ bool ShadeConfigFile::readNetRecord(restore_options_t &opts) { uint32_t startPos = this->file.position(); if(opts.network) { Serial.println("Reading network settings from file..."); - settings.connType = static_cast(this->readUInt8(static_cast(conn_types::unset))); + settings.connType = static_cast(this->readUInt8(static_cast(conn_types_t::unset))); settings.IP.dhcp = this->readBool(true); char ip[24]; this->readVarString(ip, sizeof(ip)); diff --git a/ConfigSettings.cpp b/ConfigSettings.cpp index a6bf603..2906efb 100644 --- a/ConfigSettings.cpp +++ b/ConfigSettings.cpp @@ -205,12 +205,12 @@ bool ConfigSettings::load() { pref.getString("hostname", this->hostname, sizeof(this->hostname)); this->ssdpBroadcast = pref.getBool("ssdpBroadcast", true); this->checkForUpdate = pref.getBool("checkForUpdate", true); - this->connType = static_cast(pref.getChar("connType", 0x00)); + this->connType = static_cast(pref.getChar("connType", 0x00)); //Serial.printf("Preference GFG Free Entries: %d\n", pref.freeEntries()); pref.end(); - if(this->connType == conn_types::unset) { + if(this->connType == conn_types_t::unset) { // We are doing this to convert the data from previous versions. - this->connType = conn_types::wifi; + this->connType = conn_types_t::wifi; pref.begin("WIFI"); pref.getString("hostname", this->hostname, sizeof(this->hostname)); this->ssdpBroadcast = pref.getBool("ssdpBroadcast", true); @@ -261,7 +261,7 @@ bool ConfigSettings::requiresAuth() { return this->Security.type != security_typ bool ConfigSettings::fromJSON(JsonObject &obj) { if(obj.containsKey("ssdpBroadcast")) this->ssdpBroadcast = obj["ssdpBroadcast"]; if(obj.containsKey("hostname")) this->parseValueString(obj, "hostname", this->hostname, sizeof(this->hostname)); - if(obj.containsKey("connType")) this->connType = static_cast(obj["connType"].as()); + if(obj.containsKey("connType")) this->connType = static_cast(obj["connType"].as()); if(obj.containsKey("checkForUpdate")) this->checkForUpdate = obj["checkForUpdate"]; return true; } @@ -269,8 +269,8 @@ void ConfigSettings::print() { this->Security.print(); Serial.printf("Connection Type: %u\n", (unsigned int) this->connType); this->NTP.print(); - if(this->connType == conn_types::wifi || this->connType == conn_types::unset) this->WIFI.print(); - if(this->connType == conn_types::ethernet || this->connType == conn_types::ethernetpref) this->Ethernet.print(); + if(this->connType == conn_types_t::wifi || this->connType == conn_types_t::unset) this->WIFI.print(); + if(this->connType == conn_types_t::ethernet || this->connType == conn_types_t::ethernetpref) this->Ethernet.print(); } void ConfigSettings::emitSockets() {} void ConfigSettings::emitSockets(uint8_t num) {} diff --git a/ConfigSettings.h b/ConfigSettings.h index 660a8ed..5f5db39 100644 --- a/ConfigSettings.h +++ b/ConfigSettings.h @@ -4,6 +4,14 @@ #define configsettings_h #include "WResp.h" #define FW_VERSION "v2.4.4" +enum class conn_types_t : byte { + unset = 0x00, + wifi = 0x01, + ethernet = 0x02, + ethernetpref = 0x03, + ap = 0x04 +}; + enum DeviceStatus { DS_OK = 0, DS_ERROR = 1, @@ -157,20 +165,13 @@ class MQTTSettings: BaseSettings { void toJSON(JsonResponse &json); bool fromJSON(JsonObject &obj); }; -enum class conn_types : byte { - unset = 0x00, - wifi = 0x01, - ethernet = 0x02, - ethernetpref = 0x03, - ap = 0x04 -}; class ConfigSettings: BaseSettings { public: static void printAvailHeap(); char serverId[10] = ""; char hostname[32] = "ESPSomfyRTS"; char chipModel[10] = "ESP32"; - conn_types connType = conn_types::unset; + conn_types_t connType = conn_types_t::unset; appver_t fwVersion; appver_t appVersion; bool ssdpBroadcast = true; @@ -197,5 +198,4 @@ class ConfigSettings: BaseSettings { uint16_t calcNetRecSize(); bool getAppVersion(); }; - #endif diff --git a/GitOTA.cpp b/GitOTA.cpp index 6dda09e..4560ea9 100644 --- a/GitOTA.cpp +++ b/GitOTA.cpp @@ -3,9 +3,9 @@ #include #include #include +#include "ConfigSettings.h" #include "GitOTA.h" #include "Utils.h" -#include "ConfigSettings.h" #include "Sockets.h" #include "Somfy.h" #include "Web.h" diff --git a/MQTT.cpp b/MQTT.cpp index 62e7d0b..e24c144 100644 --- a/MQTT.cpp +++ b/MQTT.cpp @@ -2,8 +2,8 @@ #include #include #include -#include "MQTT.h" #include "ConfigSettings.h" +#include "MQTT.h" #include "Somfy.h" #include "Network.h" #include "Utils.h" diff --git a/Network.cpp b/Network.cpp index 45b00bd..510b042 100644 --- a/Network.cpp +++ b/Network.cpp @@ -34,7 +34,7 @@ bool Network::setup() { WiFi.persistent(false); WiFi.onEvent(this->networkEvent); if(WiFi.status() == WL_CONNECTED) WiFi.disconnect(true, true); - if(settings.connType == conn_types::wifi || settings.connType == conn_types::unset) { + if(settings.connType == conn_types_t::wifi || settings.connType == conn_types_t::unset) { WiFi.persistent(false); if(settings.hostname[0] != '\0') WiFi.setHostname(settings.hostname); Serial.print("WiFi Mode: "); @@ -60,7 +60,7 @@ void Network::loop() { // Every 60 seconds we are going to look at wifi connectivity // to get around the roaming issues with ESP32. We will try to do this in an async manner. If // there is a channel that is better we will stop the wifi radio and reconnect - if(this->connType == conn_types::wifi && settings.WIFI.roaming && !this->softAPOpened) { + if(this->connType == conn_types_t::wifi && settings.WIFI.roaming && !this->softAPOpened) { // If we are not already scanning then we need to start a passive scan // and only respond if there is a better connection. // 1. If there is currently a waiting scan don't do anything @@ -71,7 +71,7 @@ void Network::loop() { this->lastMDNS = millis(); } if(_apScanning) { - if(!settings.WIFI.roaming || this->connType != conn_types::wifi || this->softAPOpened) _apScanning = false; + if(!settings.WIFI.roaming || this->connType != conn_types_t::wifi || this->softAPOpened) _apScanning = false; else { uint16_t n = WiFi.scanComplete(); if( n > 0) { @@ -107,7 +107,7 @@ bool Network::changeAP(const uint8_t *bssid, const int32_t channel) { void Network::emitSockets() { this->emitHeap(); if(this->needsBroadcast || - (this->connType == conn_types::wifi && (abs(abs(WiFi.RSSI()) - abs(this->lastRSSI)) > 1 || WiFi.channel() != this->lastChannel))) { + (this->connType == conn_types_t::wifi && (abs(abs(WiFi.RSSI()) - abs(this->lastRSSI)) > 1 || WiFi.channel() != this->lastChannel))) { this->emitSockets(255); sockEmit.loop(); this->needsBroadcast = false; @@ -115,7 +115,7 @@ void Network::emitSockets() { } void Network::emitSockets(uint8_t num) { //char buf[128]; - if(this->connType == conn_types::ethernet) { + if(this->connType == conn_types_t::ethernet) { JsonSockEvent *json = sockEmit.beginEmit("ethernet"); json->beginObject(); json->addElem("connected", this->connected()); @@ -183,11 +183,11 @@ void Network::emitSockets(uint8_t num) { } this->emitHeap(num); } -void Network::setConnected(conn_types connType) { +void Network::setConnected(conn_types_t connType) { this->connType = connType; this->connectTime = millis(); connectRetries = 0; - if(this->connType == conn_types::wifi) { + if(this->connType == conn_types_t::wifi) { if(this->softAPOpened && WiFi.softAPgetStationNum() == 0) { WiFi.softAPdisconnect(true); WiFi.mode(WIFI_STA); @@ -199,7 +199,7 @@ void Network::setConnected(conn_types connType) { this->channel = WiFi.channel(); this->connectAttempts++; } - else if(this->connType == conn_types::ethernet) { + else if(this->connType == conn_types_t::ethernet) { if(this->softAPOpened) { Serial.println("Disonnecting from SoftAP"); WiFi.softAPdisconnect(true); @@ -213,7 +213,7 @@ void Network::setConnected(conn_types connType) { //sockEmit.begin(); if(this->connectAttempts == 1) { Serial.println(); - if(this->connType == conn_types::wifi) { + if(this->connType == conn_types_t::wifi) { Serial.print("Successfully Connected to WiFi!!!!"); Serial.print(WiFi.localIP()); Serial.print(" ("); @@ -257,7 +257,7 @@ void Network::setConnected(conn_types connType) { Serial.print("Reconnected after "); Serial.print(1.0 * (millis() - this->connectStart)/1000); Serial.print("sec IP: "); - if(this->connType == conn_types::wifi) { + if(this->connType == conn_types_t::wifi) { Serial.print(WiFi.localIP()); Serial.print(" "); Serial.print(this->mac); @@ -327,12 +327,12 @@ bool Network::connectWired() { WiFi.disconnect(true); WiFi.mode(WIFI_OFF); } - if(this->connType != conn_types::ethernet) this->setConnected(conn_types::ethernet); + if(this->connType != conn_types_t::ethernet) this->setConnected(conn_types_t::ethernet); this->wifiFallback = false; return true; } else if(this->ethStarted) { - if(settings.connType == conn_types::ethernetpref && settings.WIFI.ssid[0] != '\0') + if(settings.connType == conn_types_t::ethernetpref && settings.WIFI.ssid[0] != '\0') return this->connectWiFi(); } if(this->connectAttempts > 0) { @@ -342,8 +342,8 @@ bool Network::connectWired() { else Serial.println("Connecting to Wired Ethernet"); this->_connecting = true; - this->connTarget = conn_types::ethernet; - this->connType = conn_types::unset; + this->connTarget = conn_types_t::ethernet; + this->connType = conn_types_t::unset; if(!this->ethStarted) { this->ethStarted = true; WiFi.mode(WIFI_OFF); @@ -357,7 +357,7 @@ bool Network::connectWired() { if(!ETH.begin(settings.Ethernet.phyAddress, settings.Ethernet.PWRPin, settings.Ethernet.MDCPin, settings.Ethernet.MDIOPin, settings.Ethernet.phyType, settings.Ethernet.CLKMode)) { Serial.println("Ethernet Begin failed"); this->ethStarted = false; - if(settings.connType == conn_types::ethernetpref) { + if(settings.connType == conn_types_t::ethernetpref) { this->wifiFallback = true; return connectWiFi(); } @@ -379,7 +379,7 @@ bool Network::connectWired() { } void Network::updateHostname() { if(settings.hostname[0] != '\0' && this->connected()) { - if(this->connType == conn_types::ethernet && + if(this->connType == conn_types_t::ethernet && strcmp(settings.hostname, ETH.getHostname()) != 0) { Serial.printf("Updating host name to %s...\n", settings.hostname); ETH.setHostname(settings.hostname); @@ -398,7 +398,7 @@ bool Network::connectWiFi() { if(this->softAPOpened && WiFi.softAPgetStationNum() > 0) { WiFi.disconnect(false); this->_connecting = false; - this->connType = conn_types::unset; + this->connType = conn_types_t::unset; return true; } @@ -410,8 +410,8 @@ bool Network::connectWiFi() { } if(this->_connecting) return true; this->_connecting = true; - this->connTarget = conn_types::wifi; - this->connType = conn_types::unset; + this->connTarget = conn_types_t::wifi; + this->connType = conn_types_t::unset; if(this->connectAttempts > 0) { Serial.print("Connection Lost..."); Serial.print(this->mac); @@ -458,16 +458,16 @@ bool Network::connect() { // to connect to the network. If the connection type is set then we need to // finish the connection. If it is not then we need to fall back to AP or in // the case where the target was originally ethernet then we need to open the softAP. - if(this->connType == conn_types::unset) { + if(this->connType == conn_types_t::unset) { // If we reached our timeout for the connection then we need to open the soft ap. if(millis() > this->connectStart + CONNECT_TIMEOUT) { esp_task_wdt_reset(); - if(this->connTarget == conn_types::ethernet && settings.connType == conn_types::ethernetpref && settings.WIFI.ssid[0] != '\0') + if(this->connTarget == conn_types_t::ethernet && settings.connType == conn_types_t::ethernetpref && settings.WIFI.ssid[0] != '\0') this->connectWiFi(); else if(this->softAPOpened) { - if(settings.connType == conn_types::ethernet || settings.connType == conn_types::ethernetpref) + if(settings.connType == conn_types_t::ethernet || settings.connType == conn_types_t::ethernetpref) this->connectWired(); - else if(settings.connType == conn_types::wifi && strlen(settings.WIFI.ssid) > 0) + else if(settings.connType == conn_types_t::wifi && strlen(settings.WIFI.ssid) > 0) this->connectWiFi(); } else { @@ -479,16 +479,16 @@ bool Network::connect() { else this->setConnected(this->connTarget); } - else if(settings.connType == conn_types::ethernet || settings.connType == conn_types::ethernetpref) + else if(settings.connType == conn_types_t::ethernet || settings.connType == conn_types_t::ethernetpref) this->connectWired(); - else if(settings.connType == conn_types::wifi && strlen(settings.WIFI.ssid) > 0) + else if(settings.connType == conn_types_t::wifi && strlen(settings.WIFI.ssid) > 0) this->connectWiFi(); else this->openSoftAP(); if(this->softAPOpened && this->connected() && WiFi.softAPgetStationNum() == 0) { Serial.println("Closing uneeded SoftAP"); WiFi.softAPdisconnect(true); - if(this->connType == conn_types::wifi) WiFi.mode(WIFI_STA); + if(this->connType == conn_types_t::wifi) WiFi.mode(WIFI_STA); } return true; } @@ -535,10 +535,10 @@ bool Network::openSoftAP() { } bool Network::connected() { if(this->connecting()) return false; - else if(this->connType == conn_types::unset) return false; - else if(this->connType == conn_types::wifi) return WiFi.status() == WL_CONNECTED; - else if(this->connType == conn_types::ethernet) return ETH.linkUp(); - else return this->connType != conn_types::unset; + else if(this->connType == conn_types_t::unset) return false; + else if(this->connType == conn_types_t::wifi) return WiFi.status() == WL_CONNECTED; + else if(this->connType == conn_types_t::ethernet) return ETH.linkUp(); + else return this->connType != conn_types_t::unset; return false; } bool Network::connecting() { @@ -559,7 +559,7 @@ void Network::networkEvent(WiFiEvent_t event) { case ARDUINO_EVENT_WIFI_STA_GOT_IP: Serial.print("Got WiFi IP: "); Serial.println(WiFi.localIP()); - net.connType = conn_types::wifi; + net.connType = conn_types_t::wifi; net.connectTime = millis(); break; case ARDUINO_EVENT_WIFI_STA_LOST_IP: Serial.println("Lost IP address and IP address is reset to 0"); break; @@ -569,14 +569,14 @@ void Network::networkEvent(WiFiEvent_t event) { Serial.print("Got Ethernet IP "); Serial.println(ETH.localIP()); net.connectTime = millis(); - net.connType = conn_types::ethernet; + net.connType = conn_types_t::ethernet; break; case ARDUINO_EVENT_ETH_CONNECTED: Serial.print("(evt) Ethernet Connected "); break; case ARDUINO_EVENT_ETH_DISCONNECTED: Serial.println("(evt) Ethernet Disconnected"); - net.connType = conn_types::unset; + net.connType = conn_types_t::unset; break; case ARDUINO_EVENT_ETH_START: Serial.println("(evt) Ethernet Started"); @@ -584,7 +584,7 @@ void Network::networkEvent(WiFiEvent_t event) { break; case ARDUINO_EVENT_ETH_STOP: Serial.println("(evt) Ethernet Stopped"); - net.connType = conn_types::unset; + net.connType = conn_types_t::unset; net.ethStarted = false; break; case ARDUINO_EVENT_WIFI_AP_START: diff --git a/Network.h b/Network.h index 0d90256..9f4e443 100644 --- a/Network.h +++ b/Network.h @@ -3,6 +3,8 @@ #ifndef Network_h #define Network_h +//enum class conn_types_t : byte; + #define CONNECT_TIMEOUT 20000 class Network { protected: @@ -18,8 +20,8 @@ class Network { bool softAPOpened = false; bool openingSoftAP = false; bool needsBroadcast = true; - conn_types connType = conn_types::unset; - conn_types connTarget = conn_types::unset; + conn_types_t connType = conn_types_t::unset; + conn_types_t connTarget = conn_types_t::unset; bool connected(); bool connecting(); String ssid; @@ -34,7 +36,7 @@ class Network { bool connect(); bool connectWiFi(); bool connectWired(); - void setConnected(conn_types connType); + void setConnected(conn_types_t connType); bool getStrongestAP(const char *ssid, uint8_t *bssid, int32_t *channel); bool changeAP(const uint8_t *bssid, const int32_t channel); //int getStrengthByMac(const char *mac); diff --git a/Somfy.cpp b/Somfy.cpp index 2e994e6..257deaf 100644 --- a/Somfy.cpp +++ b/Somfy.cpp @@ -307,6 +307,19 @@ void somfy_frame_t::encode80BitFrame(byte *frame, uint8_t repeat) { frame[9] = ((repeat + 1) & 0x0F) << 4; frame[9] |= this->calc80Checksum(frame[7], frame[8], frame[9]); break; + case somfy_commands::Up: + frame[7] = 132; + frame[8] = 32; + frame[9] = 0x00; + frame[9] |= this->calc80Checksum(frame[7], frame[8], frame[9]); + break; + case somfy_commands::Down: + frame[7] = 132; + frame[8] = 44; + frame[9] = 0x80; + frame[9] |= this->calc80Checksum(frame[7], frame[8], frame[9]); + break; + default: break; } @@ -3154,7 +3167,7 @@ int8_t SomfyShade::validateJSON(JsonObject &obj) { (myPin != 255 && somfy.transceiver.usesPin(myPin))) ret = -10; } - if(settings.connType == conn_types::ethernet || settings.connType == conn_types::ethernetpref) { + if(settings.connType == conn_types_t::ethernet || settings.connType == conn_types_t::ethernetpref) { if((upPin != 255 && settings.Ethernet.usesPin(upPin)) || (downPin != 255 && somfy.transceiver.usesPin(downPin)) || (myPin != 255 && somfy.transceiver.usesPin(myPin))) diff --git a/SomfyController.ino.esp32.bin b/SomfyController.ino.esp32.bin index b0fc392..6a17752 100644 Binary files a/SomfyController.ino.esp32.bin and b/SomfyController.ino.esp32.bin differ diff --git a/SomfyController.ino.esp32s3.bin b/SomfyController.ino.esp32s3.bin index 54c0774..2b38c92 100644 Binary files a/SomfyController.ino.esp32s3.bin and b/SomfyController.ino.esp32s3.bin differ diff --git a/Utils.h b/Utils.h index b31e3ab..51a29b2 100644 --- a/Utils.h +++ b/Utils.h @@ -9,6 +9,7 @@ [[maybe_unused]] static void SETCHARPROP(char *prop, const char *value, size_t size) {strncpy(prop, value, size); prop[size - 1] = '\0';} +/* namespace util { // Createa a custom to_string function. C++ can be annoying // with all the trailing 0s on number formats. @@ -23,6 +24,8 @@ namespace util { return str; } } +*/ + static void _ltrim(char *str) { int s = 0, j, k = 0; int e = strlen(str); diff --git a/Web.cpp b/Web.cpp index fdbc058..425d1b2 100644 --- a/Web.cpp +++ b/Web.cpp @@ -791,8 +791,8 @@ void Web::handleDiscovery(WebServer &server) { if (method == HTTP_POST || method == HTTP_GET) { Serial.println("Discovery Requested"); char connType[10] = "Unknown"; - if(net.connType == conn_types::ethernet) strcpy(connType, "Ethernet"); - else if(net.connType == conn_types::wifi) strcpy(connType, "Wifi"); + if(net.connType == conn_types_t::ethernet) strcpy(connType, "Ethernet"); + else if(net.connType == conn_types_t::wifi) strcpy(connType, "Wifi"); JsonResponse resp; resp.beginResponse(&server, g_content, sizeof(g_content)); @@ -2374,13 +2374,13 @@ void Web::begin() { // Parse out all the inputs. bool reboot = false; if(obj.containsKey("connType") && obj["connType"].as() != static_cast(settings.connType)) { - settings.connType = static_cast(obj["connType"].as()); + settings.connType = static_cast(obj["connType"].as()); settings.save(); reboot = true; } if(obj.containsKey("wifi")) { JsonObject objWifi = obj["wifi"]; - if(settings.connType == conn_types::wifi) { + if(settings.connType == conn_types_t::wifi) { if(objWifi.containsKey("ssid") && objWifi["ssid"].as().compareTo(settings.WIFI.ssid) != 0) { if(WiFi.softAPgetStationNum() == 0) reboot = true; } @@ -2395,7 +2395,7 @@ void Web::begin() { { JsonObject objEth = obj["ethernet"]; // This is an ethernet connection so if anything changes we need to reboot. - if(settings.connType == conn_types::ethernet || settings.connType == conn_types::ethernetpref) + if(settings.connType == conn_types_t::ethernet || settings.connType == conn_types_t::ethernetpref) reboot = true; settings.Ethernet.fromJSON(objEth); settings.Ethernet.save();