From 2d411bab82aedc382f4655ca52d696d75148d311 Mon Sep 17 00:00:00 2001 From: Gregg Date: Mon, 3 Oct 2022 22:04:51 -0500 Subject: [PATCH] Updated SpanPoint so that WiFi_AP_STA is only used if receiveSize>0 Next up: explore power-reducing modes and sleep modes for remote sensors to extend battery life. --- .../RemoteSensors/MainDevice/MainDevice.ino | 2 +- .../RemoteTempSensor/RemoteTempSensor.ino | 4 +-- src/HomeSpan.cpp | 11 +++++--- src/src.ino | 27 ++++++++++--------- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/Other Examples/RemoteSensors/MainDevice/MainDevice.ino b/Other Examples/RemoteSensors/MainDevice/MainDevice.ino index 3783ea2..270f4e4 100644 --- a/Other Examples/RemoteSensors/MainDevice/MainDevice.ino +++ b/Other Examples/RemoteSensors/MainDevice/MainDevice.ino @@ -72,7 +72,7 @@ struct RemoteTempSensor : Service::TemperatureSensor { LOG1("Sensor %s update: Temperature=%0.2f\n",name,temperature*9/5+32); - } else if(remoteTemp->time()>120000 && !fault->getVal()){ // else if it has been a while since last update (60 seconds), and there is no current fault + } 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); } diff --git a/Other Examples/RemoteSensors/RemoteTempSensor/RemoteTempSensor.ino b/Other Examples/RemoteSensors/RemoteTempSensor/RemoteTempSensor.ino index 65af908..9e67cf4 100644 --- a/Other Examples/RemoteSensors/RemoteTempSensor/RemoteTempSensor.ino +++ b/Other Examples/RemoteSensors/RemoteTempSensor/RemoteTempSensor.ino @@ -58,7 +58,7 @@ #define I2C_ADD 0x48 // ICS Address to use for the Adafruit ADT7410 SpanPoint *mainDevice; -uint32_t timer=0; // keep track of time since last update +uint32_t timer=-30000; // keep track of time since last update void setup() { @@ -90,7 +90,7 @@ void setup() { void loop() { - if(millis()-timer>60000){ // only sample every 5 seconds + if(millis()-timer>30000){ // only sample every 30 seconds timer=millis(); Wire.beginTransmission(I2C_ADD); // setup transmission diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 56660db..003601e 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -60,8 +60,6 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa sprintf(this->category,"%d",(int)catID); SpanPoint::setAsHub(); - - WiFi.mode(WIFI_AP_STA); // set mode to mixed AP/STA. This does not start any servers, just configures the WiFi radio to ensure it does not sleep (required for ESP-NOW) statusLED=new Blinker(statusDevice,autoOffLED); // create Status LED, even is statusDevice is NULL @@ -2183,6 +2181,11 @@ SpanPoint::SpanPoint(const char *macAddress, int sendSize, int receiveSize, int 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); init(); // initialize SpanPoint peerInfo.channel=0; // 0 = matches current WiFi channel @@ -2204,8 +2207,8 @@ void SpanPoint::init(const char *password){ if(initialized) return; - if(WiFi.getMode()!=WIFI_AP_STA) - WiFi.mode(WIFI_AP_STA); // set mode to mixed AP/STA. This does not start any servers, just configures the WiFi radio to ensure it does not sleep (required for ESP-NOW) + if(WiFi.getMode()==WIFI_OFF) + WiFi.mode(WIFI_STA); uint8_t hash[32]; mbedtls_sha256_ret((const unsigned char *)password,strlen(password),hash,0); // produce 256-bit bit hash from password diff --git a/src/src.ino b/src/src.ino index 7f16cf8..b66e78d 100644 --- a/src/src.ino +++ b/src/src.ino @@ -52,16 +52,17 @@ void setup() { homeSpan.enableWebLog(10,"pool.ntp.org","UTC","myLog"); // creates a web log on the URL /HomeSpan-[DEVICE-ID].local:[TCP-PORT]/myLog + SpanPoint::setChannelMask(1<<13); + SpanPoint::setPassword("Hello Thert"); homeSpan.setLogLevel(1); - dev1=new SpanPoint("AC:67:B2:77:42:20",sizeof(int),0); dev2=new SpanPoint("7C:DF:A1:61:E4:A8",sizeof(int),sizeof(message_t)); + dev1=new SpanPoint("AC:67:B2:77:42:20",sizeof(int),0); homeSpan.begin(Category::Lighting,"HomeSpan Lamp Server","homespan"); - SpanPoint::setChannelMask(1<<13); new SpanAccessory(); // Begin by creating a new Accessory using SpanAccessory(), which takes no arguments @@ -122,16 +123,16 @@ unsigned long alarmTime=0; void loop(){ homeSpan.poll(); - if(dev1->get(&message)) - Serial.printf("DEV1: '%s' %d %f %d\n",message.a,message.b,message.c,message.d); - if(dev2->get(&message)) - Serial.printf("DEV2: '%s' %d %f %d\n",message.a,message.b,message.c,message.d); - - if(millis()-alarmTime>5000){ - alarmTime=millis(); - boolean success = dev2->send(&alarmTime); - Serial.printf("Success = %d\n",success); - } +// if(dev1->get(&message)) +// Serial.printf("DEV1: '%s' %d %f %d\n",message.a,message.b,message.c,message.d); +// if(dev2->get(&message)) +// Serial.printf("DEV2: '%s' %d %f %d\n",message.a,message.b,message.c,message.d); +// +// if(millis()-alarmTime>5000){ +// alarmTime=millis(); +// boolean success = dev2->send(&alarmTime); +// Serial.printf("Success = %d\n",success); +// } } // end of loop() @@ -146,6 +147,8 @@ void myWiFiAP(){ void wifiEstablished(){ Serial.print("IN CALLBACK FUNCTION\n\n"); + Serial.printf("MODE = %d\n",WiFi.getMode()); + } //////////////////////////////////////