From ebea01f19b2f89550c8320099664bc243bb25027 Mon Sep 17 00:00:00 2001 From: oscgonfer Date: Wed, 23 Oct 2024 20:10:07 +0200 Subject: [PATCH] Expose STA MAC address along with softAP one --- esp/src/SckESP.cpp | 10 ++++++++-- esp/src/SckESP.h | 3 ++- lib/Shared/Config.h | 2 +- sam/src/Commands.cpp | 6 +++--- sam/src/SckBase.cpp | 6 +++++- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/esp/src/SckESP.cpp b/esp/src/SckESP.cpp index 87c13ad..c3c99db 100644 --- a/esp/src/SckESP.cpp +++ b/esp/src/SckESP.cpp @@ -32,6 +32,7 @@ void SckESP::setup() // Create hostname macAddr = WiFi.softAPmacAddress(); + staMacAddr = WiFi.macAddress(); String tailMacAddr = macAddr.substring(macAddr.length() - 5); tailMacAddr.replace(":", ""); strncpy(hostname, "Smartcitizen", 20); @@ -444,7 +445,8 @@ bool SckESP::sendStartInfo() { StaticJsonDocument jsonBuffer; JsonObject jsonSend = jsonBuffer.to(); - jsonSend["mac"] = macAddr; + jsonSend["mac"] = macAddr; // SoftAP MAC Address + jsonSend["stamac"] = staMacAddr; // STA-MAC Address jsonSend["ver"] = ESPversion; jsonSend["bd"] = ESPbuildDate; @@ -731,10 +733,14 @@ void SckESP::webStatus(AsyncWebServerRequest *request) // Hostname json += "{\"hostname\":\"" + String(hostname) + "\","; - // MAC address + // MAC address (softAP) String tmac = WiFi.softAPmacAddress(); json += "\"mac\":\"" + tmac + "\","; + // MAC address (STA) + String tmacsta = WiFi.macAddress(); + json += "\"mac_sta\":\"" + tmacsta + "\","; + // ESP firmware version json += "\"ESPversion\":\"" + ESPversion.substring(0, ESPversion.indexOf("-")) + "\","; diff --git a/esp/src/SckESP.h b/esp/src/SckESP.h index 169f8eb..ae3197c 100644 --- a/esp/src/SckESP.h +++ b/esp/src/SckESP.h @@ -78,7 +78,8 @@ class SckESP // Wifi related char hostname[20]; - String macAddr; + String macAddr; // SoftAP MAC Address + String staMacAddr; // STA-MAC Address String ipAddr; int currentWIFIStatus; void tryConnection(); diff --git a/lib/Shared/Config.h b/lib/Shared/Config.h index 15e5737..6946776 100644 --- a/lib/Shared/Config.h +++ b/lib/Shared/Config.h @@ -49,7 +49,7 @@ struct Credentials { bool set=false; char ssid[64]="null"; char pass[64]="null"; struct Token { bool set=false; char token[7]="null"; }; struct Mqtt { char server[64]="mqtt.smartcitizen.me"; uint16_t port=1883; }; struct Ntp { char server[64]="ntp.smartcitizen.me"; uint16_t port=80; }; -struct MAC { bool valid=false; char address[18]="not synced"; }; +struct MAC { bool valid=false; char address[18]="not synced"; char staaddress[18]="not synced";}; struct BattConf { int16_t chargeCurrent=768; uint32_t battCapacity=2000; }; struct Extra { bool ccsBaselineValid=false; uint16_t ccsBaseline; bool pmPowerSave=true; uint32_t pmWarmUpPeriod=15; }; // Here we save variables that don't have an specific place struct Offline { uint32_t retry = default_publish_interval * 5; int8_t start=-1; int8_t end=-1; }; diff --git a/sam/src/Commands.cpp b/sam/src/Commands.cpp index 26062a6..2943ba5 100644 --- a/sam/src/Commands.cpp +++ b/sam/src/Commands.cpp @@ -51,7 +51,7 @@ void getVersion_com(SckBase* base, String parameters) base->getUniqueID(); sprintf(base->outBuff, "Hardware Version: %s\r\nSAM Hardware ID: %s\r\nSAM version: %s\r\nSAM build date: %s", base->hardwareVer.c_str(), base->uniqueID_str, base->SAMversion.c_str(), base->SAMbuildDate.c_str()); base->sckOut(); - sprintf(base->outBuff, "ESP MAC address: %s\r\nESP version: %s\r\nESP build date: %s", base->config.mac.address, base->ESPversion.c_str(), base->ESPbuildDate.c_str()); + sprintf(base->outBuff, "ESP version: %s\r\nESP build date: %s", base->ESPversion.c_str(), base->ESPbuildDate.c_str()); base->sckOut(); } void resetCause_com(SckBase* base, String parameters) @@ -1026,8 +1026,8 @@ void config_com(SckBase* base, String parameters) else sprintf(base->outBuff, "Token: not configured"); base->sckOut(); - sprintf(base->outBuff, "Mac address: %s", base->config.mac.address); - base->sckOut(); + // sprintf(base->outBuff, "Mac address: %s", base->config.mac.address); + // base->sckOut(); sprintf(base->outBuff, "Sanity reset (every 24 hours) is: %s", base->config.sanityResetFlag ? "on" : "off"); base->sckOut(); diff --git a/sam/src/SckBase.cpp b/sam/src/SckBase.cpp index 97a12f5..dd1a163 100644 --- a/sam/src/SckBase.cpp +++ b/sam/src/SckBase.cpp @@ -1048,7 +1048,7 @@ void SckBase::ESPbusUpdate() ipAddress = json["ip"].as(); - sprintf(outBuff, "\r\nHostname: %s\r\nIP address: %s\r\nMAC address: %s", hostname, ipAddress.c_str(), config.mac.address); + sprintf(outBuff, "\r\nHostname: %s\r\nIP address: %s\r\nAP MAC address: %s\r\nSTA MAC address: %s", hostname, ipAddress.c_str(), config.mac.address, config.mac.staaddress); sckOut(); sprintf(outBuff, "ESP version: %s\r\nESP build date: %s", ESPversion.c_str(), ESPbuildDate.c_str()); sckOut(); @@ -1151,13 +1151,17 @@ void SckBase::ESPbusUpdate() JsonObject json = jsonBuffer.as(); String macAddress = json["mac"].as(); + String staMacAddress = json["stamac"].as(); ESPversion = json["ver"].as(); ESPbuildDate = json["bd"].as(); // Udate mac address and hostname if we haven't yet if (!config.mac.valid) { sckOut("Updated MAC address"); + sckOut("AP MAC address"); sprintf(config.mac.address, "%s", macAddress.c_str()); + sckOut("STA MAC address"); + sprintf(config.mac.staaddress, "%s", staMacAddress.c_str()); config.mac.valid = true; saveConfig(); snprintf(hostname, sizeof(hostname), "%s", "Smartcitizen");