Skip to content

Commit

Permalink
Expose STA MAC address along with softAP one
Browse files Browse the repository at this point in the history
  • Loading branch information
oscgonfer committed Oct 23, 2024
1 parent ff85be3 commit ebea01f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
10 changes: 8 additions & 2 deletions esp/src/SckESP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -444,7 +445,8 @@ bool SckESP::sendStartInfo()
{
StaticJsonDocument<NETBUFF_SIZE> jsonBuffer;
JsonObject jsonSend = jsonBuffer.to<JsonObject>();
jsonSend["mac"] = macAddr;
jsonSend["mac"] = macAddr; // SoftAP MAC Address
jsonSend["stamac"] = staMacAddr; // STA-MAC Address
jsonSend["ver"] = ESPversion;
jsonSend["bd"] = ESPbuildDate;

Expand Down Expand Up @@ -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("-")) + "\",";

Expand Down
3 changes: 2 additions & 1 deletion esp/src/SckESP.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion lib/Shared/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down
6 changes: 3 additions & 3 deletions sam/src/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion sam/src/SckBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ void SckBase::ESPbusUpdate()

ipAddress = json["ip"].as<String>();

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();
Expand Down Expand Up @@ -1151,13 +1151,17 @@ void SckBase::ESPbusUpdate()
JsonObject json = jsonBuffer.as<JsonObject>();

String macAddress = json["mac"].as<String>();
String staMacAddress = json["stamac"].as<String>();
ESPversion = json["ver"].as<String>();
ESPbuildDate = json["bd"].as<String>();

// 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");
Expand Down

0 comments on commit ebea01f

Please sign in to comment.