Add option to SpanPoint to use softAP MAC Address instead of STA

Needed to support ESP-NOW on ESP-8266 chips, which seem to only work if connecting into softAP MAC Address once HomeSpan is connected to WiFi network (prior to connection ESP-8266 will properly connect to normal STA address as well as softAP address).
This commit is contained in:
Gregg 2023-01-22 18:05:59 -06:00
parent fa787a9c63
commit 7eca1e776b
4 changed files with 72 additions and 44 deletions

View File

@ -61,7 +61,7 @@ void setup() {
// In the line below, replace the MAC Address with that of your MAIN HOMESPAN DEVICE // In the line below, replace the MAC Address with that of your MAIN HOMESPAN DEVICE
mainDevice=new SpanPoint("7C:DF:A1:61:E4:A8",sizeof(float),0); // create a SpanPoint with send size=sizeof(float) and receive size=0 mainDevice=new SpanPoint("84:CC:A8:11:B4:84",sizeof(float),0); // create a SpanPoint with send size=sizeof(float) and receive size=0
homeSpan.setLogLevel(1); homeSpan.setLogLevel(1);
} }

View File

@ -2187,7 +2187,7 @@ boolean SpanOTA::auth;
// SpanPoint // // SpanPoint //
/////////////////////////////// ///////////////////////////////
SpanPoint::SpanPoint(const char *macAddress, int sendSize, int receiveSize, int queueDepth){ SpanPoint::SpanPoint(const char *macAddress, int sendSize, int receiveSize, int queueDepth, boolean useAPaddress){
if(sscanf(macAddress,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",peerInfo.peer_addr,peerInfo.peer_addr+1,peerInfo.peer_addr+2,peerInfo.peer_addr+3,peerInfo.peer_addr+4,peerInfo.peer_addr+5)!=6){ if(sscanf(macAddress,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",peerInfo.peer_addr,peerInfo.peer_addr+1,peerInfo.peer_addr+2,peerInfo.peer_addr+3,peerInfo.peer_addr+4,peerInfo.peer_addr+5)!=6){
Serial.printf("\nFATAL ERROR! Can't create new SpanPoint(\"%s\") - Invalid MAC Address ***\n",macAddress); Serial.printf("\nFATAL ERROR! Can't create new SpanPoint(\"%s\") - Invalid MAC Address ***\n",macAddress);
@ -2204,17 +2204,20 @@ SpanPoint::SpanPoint(const char *macAddress, int sendSize, int receiveSize, int
this->sendSize=sendSize; this->sendSize=sendSize;
this->receiveSize=receiveSize; this->receiveSize=receiveSize;
Serial.printf("SpanPoint: Created link to device with MAC Address %02X:%02X:%02X:%02X:%02X:%02X. Send size=%d bytes, Receive size=%d bytes with queue depth=%d.\n",
peerInfo.peer_addr[0],peerInfo.peer_addr[1],peerInfo.peer_addr[2],peerInfo.peer_addr[3],peerInfo.peer_addr[4],peerInfo.peer_addr[5],sendSize,receiveSize,queueDepth);
if(receiveSize>0) if(receiveSize>0)
WiFi.mode(WIFI_AP_STA); WiFi.mode(WIFI_AP_STA);
else if(WiFi.getMode()==WIFI_OFF) else if(WiFi.getMode()==WIFI_OFF)
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
Serial.printf("SpanPoint: Created link from (%s) to (%02X:%02X:%02X:%02X:%02X:%02X). Send size=%d bytes, Receive size=%d bytes with queue depth=%d.\n",
useAPaddress?WiFi.softAPmacAddress().c_str():WiFi.macAddress().c_str(),
peerInfo.peer_addr[0],peerInfo.peer_addr[1],peerInfo.peer_addr[2],peerInfo.peer_addr[3],peerInfo.peer_addr[4],peerInfo.peer_addr[5],sendSize,receiveSize,queueDepth);
init(); // initialize SpanPoint init(); // initialize SpanPoint
peerInfo.channel=0; // 0 = matches current WiFi channel peerInfo.channel=0; // 0 = matches current WiFi channel
peerInfo.ifidx=WIFI_IF_STA; // must specify interface
peerInfo.ifidx=useAPaddress?WIFI_IF_AP:WIFI_IF_STA; // specify interface as either STA or AP
peerInfo.encrypt=true; // turn on encryption for this peer peerInfo.encrypt=true; // turn on encryption for this peer
memcpy(peerInfo.lmk, lmk, 16); // set local key memcpy(peerInfo.lmk, lmk, 16); // set local key
esp_now_add_peer(&peerInfo); // add peer to ESP-NOW esp_now_add_peer(&peerInfo); // add peer to ESP-NOW

View File

@ -876,7 +876,7 @@ class SpanPoint {
public: public:
SpanPoint(const char *macAddress, int sendSize, int receiveSize, int queueDepth=1); SpanPoint(const char *macAddress, int sendSize, int receiveSize, int queueDepth=1, boolean useAPaddress=false);
static void setPassword(const char *pwd){init(pwd);}; static void setPassword(const char *pwd){init(pwd);};
static void setChannelMask(uint16_t mask); static void setChannelMask(uint16_t mask);
boolean get(void *dataBuf); boolean get(void *dataBuf);

View File

@ -1,56 +1,81 @@
// This is a placeholder .ino file that allows you to easily edit the contents of this library using the Arduino IDE, // This is a placeholder .ino file that allows you to easily edit the contents of this library using the Arduino IDE,
// as well as compile and test from this point. This file is ignored when the library is included in other sketches. // as well as compile and test from this point. This file is ignored when the library is included in other sketches.
#include "HomeSpan.h" #include "HomeSpan.h"
CUSTOM_CHAR_DATA(DataTest, 87654321-079E-48FF-8F27-9C2605A29F52, PW+PR+EV); struct RemoteTempSensor : Service::TemperatureSensor {
Characteristic::DataTest *eveTest;
SpanCharacteristic *temp;
SpanCharacteristic *fault;
SpanPoint *remoteTemp;
const char *name;
float temperature;
RemoteTempSensor(const char *name, const char*macAddress, boolean is8266=false) : Service::TemperatureSensor(){
this->name=name;
temp=new Characteristic::CurrentTemperature(-10.0); // set initial temperature
temp->setRange(-50,100); // expand temperature range to allow negative values
fault=new Characteristic::StatusFault(1); // set initial state = fault
remoteTemp=new SpanPoint(macAddress,0,sizeof(float),1,is8266); // create a SpanPoint with send size=0 and receive size=sizeof(float)
} // end constructor
void loop(){
if(remoteTemp->get(&temperature)){ // if there is data from the remote sensor
temp->setVal(temperature); // update temperature
fault->setVal(0); // clear fault
LOG1("Sensor %s update: Temperature=%0.2f\n",name,temperature*9/5+32);
} else if(remoteTemp->time()>60000 && !fault->getVal()){ // else if it has been a while since last update (60 seconds), and there is no current fault
fault->setVal(1); // set fault state
LOG1("Sensor %s update: FAULT\n",name);
}
} // loop
};
//////////////////////////////////////
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
homeSpan.setLogLevel(2); homeSpan.setLogLevel(1);
homeSpan.begin(Category::Lighting,"HomeSpan LightBulb");
homeSpan.begin(Category::Bridges,"Sensor Hub");
new SpanAccessory(); new SpanAccessory();
new Service::AccessoryInformation(); new Service::AccessoryInformation();
new Characteristic::Identify(); new Characteristic::Identify();
new Service::LightBulb(); new SpanAccessory();
new Characteristic::On(); new Service::AccessoryInformation();
new Characteristic::ConfiguredName(); new Characteristic::Identify();
eveTest=new Characteristic::DataTest(); new Characteristic::Name("Indoor Temp");
new RemoteTempSensor("Device 1","AC:67:B2:77:42:20"); // pass MAC Address of Remote Device
uint8_t x[]={0x01,0x26,0xFF,0x01,0x26,0xFF}; new SpanAccessory();
eveTest->setData(x,6); new Service::AccessoryInformation();
uint8_t y[6]={0}; new Characteristic::Identify();
int n=eveTest->getData(y,10); new Characteristic::Name("Outdoor Temp");
Serial.printf("%d:",n); new RemoteTempSensor("Device 2","BC:FF:4D:40:8E:71",true); // pass MAC Address of Remote Device with 8266 flag set (will use AP MAC Address)
for(int i=0;i<n;i++){
Serial.printf(" %02X",y[i]);
y[i]++;
}
Serial.printf("\n\n");
eveTest->setData(y,6);
n=eveTest->getData(x,10);
Serial.printf("%d:",n);
for(int i=0;i<n;i++){
Serial.printf(" %02X",x[i]);
}
Serial.printf("\n\n");
} // end of setup()
}
////////////////////////////////////// //////////////////////////////////////
void loop(){ void loop(){
homeSpan.poll(); // run HomeSpan! homeSpan.poll();
} } // end of loop()
//////////////////////////////////////