From 308cb76a3b4a92585c451be531606a8acb28a193 Mon Sep 17 00:00:00 2001 From: Ewoud Date: Tue, 24 Sep 2024 22:22:50 +0200 Subject: [PATCH] Network add net->localIP(), ArduinoJson 7.1.0 to 7.2.0 pio.ini -ArduinoJson 7.1.0 to 7.2.0 SysModNetwork.cpp - add localIP() SysModInstances and SysModWeb: - use net->localIP() --- platformio.ini | 2 +- src/Sys/SysModInstances.h | 17 +++++++++-------- src/Sys/SysModNetwork.cpp | 13 +++++++++++++ src/Sys/SysModNetwork.h | 3 +++ src/Sys/SysModWeb.cpp | 7 ++++--- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/platformio.ini b/platformio.ini index 319aaf7a..3d811e2d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -89,7 +89,7 @@ build_flags = ${STARBASE_USERMOD_LIVE.build_flags} ;+222.204 bytes 11.7% lib_deps = ${ESPAsyncWebServer.lib_deps} ;alternatively PsychicHttp - https://github.com/bblanchon/ArduinoJson.git # 7.1.0 + https://github.com/bblanchon/ArduinoJson.git # 7.2.0 ; https://github.com/Jason2866/ESP32_Show_Info.git ;optional: ${STARBASE_USERMOD_E131.lib_deps} diff --git a/src/Sys/SysModInstances.h b/src/Sys/SysModInstances.h index 65fa65d0..db36436a 100644 --- a/src/Sys/SysModInstances.h +++ b/src/Sys/SysModInstances.h @@ -15,6 +15,7 @@ #include "../User/UserModE131.h" #endif #include "SysModSystem.h" +#include "SysModNetwork.h" //for localIP struct DMX { byte universe:3; //3 bits / 8 @@ -285,7 +286,7 @@ class SysModInstances:public SysModule { //do not set this initially!!! if (rowNr != UINT8_MAX) { //if this instance update directly, otherwise send over network - if (instances[rowNr].ip == WiFi.localIP()) { + if (instances[rowNr].ip == net->localIP()) { mdl->setValue(var, mdl->getValue(insVar, rowNr).as()); //this will call sendDataWS (tbd...), do not set for rowNr } else { sendMessageUDP(instances[rowNr].ip, Variable(var).id(), mdl->getValue(insVar, rowNr)); @@ -494,7 +495,7 @@ class SysModInstances:public SysModule { starMessage.sysData.type = 0; //WLED - if (starMessage.header.ip0 == WiFi.localIP()[0]) { // checksum - no other type of message + if (starMessage.header.ip0 == net->localIP()[0]) { // checksum - no other type of message updateInstance(starMessage); found = true; } @@ -505,7 +506,7 @@ class SysModInstances:public SysModule { byte *udpIn = (byte *)&starMessage; instanceUDP.read(udpIn, packetSize); - if (starMessage.header.ip0 == WiFi.localIP()[0]) { // checksum - no other type of message + if (starMessage.header.ip0 == net->localIP()[0]) { // checksum - no other type of message updateInstance(starMessage); found = true; } @@ -520,7 +521,7 @@ class SysModInstances:public SysModule { if (error) ppf("handleNotifications i:%d no json l: %d e:%s\n", instanceUDP.remoteIP()[3], strlen(buffer), error.c_str()); else { - if (instanceUDP.remoteIP()[3] != WiFi.localIP()[3]) { //only others + if (instanceUDP.remoteIP()[3] != net->localIP()[3]) { //only others InstanceInfo *instance = findInstance(instanceUDP.remoteIP()); //if not exist, created char group1[32]; @@ -572,7 +573,7 @@ class SysModInstances:public SysModule { if(!mdls->isConnected) return; if (!udp2Connected) return; - IPAddress localIP = WiFi.localIP(); + IPAddress localIP = net->localIP(); if (!localIP || localIP == IPAddress(255,255,255,255)) localIP = IPAddress(4,3,2,1); UDPStarMessage starMessage; @@ -619,7 +620,7 @@ class SysModInstances:public SysModule { //other way around: first set instance variables, then fill starMessage for (InstanceInfo &instance: instances) { - if (instance.ip == WiFi.localIP()) { + if (instance.ip == net->localIP()) { instance.jsonData.to(); //clear //send dash values @@ -718,7 +719,7 @@ class SysModInstances:public SysModule { strncpy(instance.name, udpStarMessage.header.name, sizeof(instance.name)-1); instance.version = udpStarMessage.header.version; - if (instance.ip == WiFi.localIP()) { + if (instance.ip == net->localIP()) { esp_wifi_get_mac((wifi_interface_t)ESP_IF_WIFI_STA, instance.sysData.macAddress); // ppf("macaddress %02X:%02X:%02X:%02X:%02X:%02X\n", instance.macAddress[0], instance.macAddress[1], instance.macAddress[2], instance.macAddress[3], instance.macAddress[4], instance.macAddress[5]); } @@ -726,7 +727,7 @@ class SysModInstances:public SysModule { if (udpStarMessage.sysData.type >= 1) {//StarBase, StarLight and forks only instance.sysData = udpStarMessage.sysData; - if (instance.ip != WiFi.localIP()) { //send from localIP will be done after updateInstance + if (instance.ip != net->localIP()) { //send from localIP will be done after updateInstance char group1[32]; char group2[32]; if (groupOfName(instance.name, group1) && groupOfName(mdl->getValue("name"), group2) && strcmp(group1, group2) == 0) { diff --git a/src/Sys/SysModNetwork.cpp b/src/Sys/SysModNetwork.cpp index f87dc045..2f056a9d 100644 --- a/src/Sys/SysModNetwork.cpp +++ b/src/Sys/SysModNetwork.cpp @@ -274,6 +274,19 @@ void SysModNetwork::loop10s() { } } +IPAddress SysModNetwork::localIP() { + #ifdef STARBASE_ETHERNET + if (ETH.localIP()[0] != 0) + return ETH.localIP(); + #endif + if (WiFi.localIP()[0] != 0) + return WiFi.localIP(); + if (WiFi.softAPIP()[0] != 0) + return WiFi.softAPIP(); + + return IPAddress(); +} + void SysModNetwork::initWiFiConnection() { if (wfActive ) //allready success && WiFi.localIP()[0] != 0 diff --git a/src/Sys/SysModNetwork.h b/src/Sys/SysModNetwork.h index 8af2623f..17c84dfb 100644 --- a/src/Sys/SysModNetwork.h +++ b/src/Sys/SysModNetwork.h @@ -9,6 +9,7 @@ @license For non GPL-v3 usage, commercial licenses must be purchased. Contact moonmodules@icloud.com */ +#pragma once #include "SysModule.h" #include @@ -39,6 +40,8 @@ class SysModNetwork:public SysModule { void handleAP(); void stopAP(); + IPAddress localIP(); + private: #ifdef STARBASE_ETHERNET bool ethActive = false; //currently only one call to ETH.begin is possible, wether successful or not: reboot needed for other attempt diff --git a/src/Sys/SysModWeb.cpp b/src/Sys/SysModWeb.cpp index bc11b59f..7a222422 100644 --- a/src/Sys/SysModWeb.cpp +++ b/src/Sys/SysModWeb.cpp @@ -15,6 +15,7 @@ #include "SysModFiles.h" #include "SysModules.h" #include "SysModPins.h" +#include "SysModNetwork.h" //for localIP #include "User/UserModMDNS.h" // got multiple definition error here ??? see workaround below @@ -569,7 +570,7 @@ void SysModWeb::serveUpdate(WebRequest *request, const String& fileName, size_t char message[64]; const char * name = mdl->getValue("name"); - print->fFormat(message, sizeof(message)-1, "Update of %s (...%d) %s", name, WiFi.localIP()[3], success?"Successful":"Failed"); + print->fFormat(message, sizeof(message)-1, "Update of %s (...%d) %s", name, net->localIP()[3], success?"Successful":"Failed"); ppf("%s\n", message); request->send(200, "text/plain", message); @@ -637,7 +638,7 @@ void SysModWeb::clientsToJson(JsonArray array, bool nameOnly, const char * filte bool SysModWeb::captivePortal(WebRequest *request) { - ppf("captivePortal %d %d\n", WiFi.localIP()[3], request->client()->localIP()[3]); + ppf("captivePortal %d %d\n", net->localIP()[3], request->client()->localIP()[3]); if (ON_STA_FILTER(request)) return false; //only serve captive in AP mode String hostH; @@ -719,7 +720,7 @@ void SysModWeb::serializeInfo(JsonObject root) { // docInfo["wifi"]["rssi"] = WiFi.RSSI();// mdl->getValue("rssi"); (ro) root["mac"] = JsonString(mdns->escapedMac.c_str(), JsonString::Copied); - root["ip"] = JsonString(WiFi.localIP().toString().c_str(), JsonString::Copied); + root["ip"] = JsonString(net->localIP().toString().c_str(), JsonString::Copied); // print->printJson("serveJson", root); }