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
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);
}

View File

@ -2187,7 +2187,7 @@ boolean SpanOTA::auth;
// 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){
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->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)
WiFi.mode(WIFI_AP_STA);
else if(WiFi.getMode()==WIFI_OFF)
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
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
memcpy(peerInfo.lmk, lmk, 16); // set local key
esp_now_add_peer(&peerInfo); // add peer to ESP-NOW

View File

@ -876,7 +876,7 @@ class SpanPoint {
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 setChannelMask(uint16_t mask);
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,
// as well as compile and test from this point. This file is ignored when the library is included in other sketches.
#include "HomeSpan.h"
CUSTOM_CHAR_DATA(DataTest, 87654321-079E-48FF-8F27-9C2605A29F52, PW+PR+EV);
Characteristic::DataTest *eveTest;
struct RemoteTempSensor : Service::TemperatureSensor {
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() {
Serial.begin(115200);
homeSpan.setLogLevel(2);
homeSpan.begin(Category::Lighting,"HomeSpan LightBulb");
new SpanAccessory();
homeSpan.setLogLevel(1);
new Service::AccessoryInformation();
new Characteristic::Identify();
homeSpan.begin(Category::Bridges,"Sensor Hub");
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Service::LightBulb();
new Characteristic::On();
new Characteristic::ConfiguredName();
eveTest=new Characteristic::DataTest();
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
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};
eveTest->setData(x,6);
uint8_t y[6]={0};
int n=eveTest->getData(y,10);
Serial.printf("%d:",n);
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");
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Identify();
new Characteristic::Name("Outdoor Temp");
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)
}
} // end of setup()
//////////////////////////////////////
void loop(){
homeSpan.poll(); // run HomeSpan!
}
homeSpan.poll();
} // end of loop()
//////////////////////////////////////