From 22cfa130b41a07a07d5cf1d2054ba4a4808ba29e Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 24 Sep 2022 16:49:32 -0500 Subject: [PATCH] Created HomePeer This is a self-contained library that is used for remote devices set up as HomeSpan Peers via ESP-NOW. --- src/{Now.cpp => HomePeer.cpp} | 25 ++++++++++++++----------- src/{Now.h => HomePeer.h} | 6 +++--- src/HomeSpan.cpp | 5 +++-- src/HomeSpan.h | 1 - 4 files changed, 20 insertions(+), 17 deletions(-) rename src/{Now.cpp => HomePeer.cpp} (60%) rename src/{Now.h => HomePeer.h} (92%) diff --git a/src/Now.cpp b/src/HomePeer.cpp similarity index 60% rename from src/Now.cpp rename to src/HomePeer.cpp index 18ed780..7204b1a 100644 --- a/src/Now.cpp +++ b/src/HomePeer.cpp @@ -1,12 +1,14 @@ -#include "Now.h" +#include "HomePeer.h" + #include #include #include +#include -void SpanNow::start(const char *macAddress, const char *password){ +void SpanPeer::start(const char *macAddress, const char *password){ - if(sscanf(macAddress,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",mac,mac+1,mac+2,mac+3,mac+4,mac+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("*** ERROR: Can't start HomeSpan NOW! Bad MAC Address '%s'\n\n",macAddress); return; } @@ -17,26 +19,27 @@ void SpanNow::start(const char *macAddress, const char *password){ esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); esp_now_init(); esp_now_register_send_cb(onDataSent); - + uint8_t lmk[32]; + uint8_t mac[6]; mbedtls_sha256_ret((const unsigned char *)password,strlen(password),lmk,0); esp_now_set_pmk(lmk+16); - memcpy(peerInfo.peer_addr, mac, 6); peerInfo.encrypt = true; memcpy(peerInfo.lmk, lmk, 16); esp_now_add_peer(&peerInfo); esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); - Serial.printf("Started HomeSpan NOW on Channel=%d. Hub MAC Address: %X:%X:%X:%X:%X:%X\n",channel,mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); + Serial.printf("Started HomePeer: MAC Address = %s HomeSpan Address = %X:%X:%X:%X:%X:%X\n",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]); started=true; } -boolean SpanNow::send(uint8_t *data, size_t len){ +boolean SpanPeer::send(uint8_t *data, size_t len){ if(!started){ - Serial.printf("*** ERROR: Can't send data until HomeSpanNOW has been started.\n\n"); + Serial.printf("*** ERROR: Can't send data until HomePeer has been started.\n\n"); return(false); } @@ -46,7 +49,7 @@ boolean SpanNow::send(uint8_t *data, size_t len){ if((1< #include - -class SpanNow { +class SpanPeer { esp_now_peer_info_t peerInfo; - uint8_t mac[6]; boolean started=false; static QueueHandle_t statusQueue; int channel=1; @@ -23,3 +21,5 @@ class SpanNow { boolean send(uint8_t *data, size_t len); }; + +extern SpanPeer homePeer; diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 947ffaa..70a9086 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -58,6 +58,8 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa this->modelName=modelName; sprintf(this->category,"%d",(int)catID); + 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 esp_task_wdt_delete(xTaskGetIdleTaskHandleForCPU(0)); // required to avoid watchdog timeout messages from ESP32-C3 @@ -138,9 +140,8 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa Serial.print(__TIME__); Serial.printf("\nPartition: %s",esp_ota_get_running_partition()->label); - + if(espNowEnabled){ - WiFi.mode(WIFI_AP_STA); // set mode to mixed AP/STA. AP mode will not be started, but WiFi will be properly configured for use with ESP-NOW esp_now_init(); // initialize ESP NOW Serial.print("\nESP-NOW: ENABLED"); } diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 02ac360..205aac8 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -48,7 +48,6 @@ #include "HAPConstants.h" #include "HapQR.h" #include "Characteristics.h" -#include "Now.h" using std::vector; using std::unordered_map;