From 7eca1e776be5fc26e32f26ab3bdb4440973319b2 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 22 Jan 2023 18:05:59 -0600 Subject: [PATCH] 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). --- .../RemoteDevice/RemoteDevice.ino | 2 +- src/HomeSpan.cpp | 13 ++- src/HomeSpan.h | 2 +- src/src.ino | 99 ++++++++++++------- 4 files changed, 72 insertions(+), 44 deletions(-) diff --git a/examples/Other Examples/RemoteSensors/RemoteDevice/RemoteDevice.ino b/examples/Other Examples/RemoteSensors/RemoteDevice/RemoteDevice.ino index 04b9dca..9859362 100644 --- a/examples/Other Examples/RemoteSensors/RemoteDevice/RemoteDevice.ino +++ b/examples/Other Examples/RemoteSensors/RemoteDevice/RemoteDevice.ino @@ -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); } diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 185dfda..c51ca4e 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -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 diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 40d3dc8..9c37a49 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -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); diff --git a/src/src.ino b/src/src.ino index 6a9026f..b63e1df 100644 --- a/src/src.ino +++ b/src/src.ino @@ -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;isetData(y,6); - n=eveTest->getData(x,10); - Serial.printf("%d:",n); - for(int i=0;i