diff --git a/src/HomePeer.cpp b/src/HomePeer.cpp index 7204b1a..b50f155 100644 --- a/src/HomePeer.cpp +++ b/src/HomePeer.cpp @@ -26,6 +26,8 @@ void SpanPeer::start(const char *macAddress, const char *password){ mbedtls_sha256_ret((const unsigned char *)password,strlen(password),lmk,0); esp_now_set_pmk(lmk+16); + peerInfo.channel=0; + peerInfo.ifidx=WIFI_IF_STA; peerInfo.encrypt = true; memcpy(peerInfo.lmk, lmk, 16); esp_now_add_peer(&peerInfo); diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index c77ed3f..07873d1 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -2154,15 +2154,8 @@ void SpanOTA::error(ota_error_t err){ // SpanPoint // /////////////////////////////// -SpanPoint::SpanPoint(const char *macAddress){ +SpanPoint::SpanPoint(const char *macAddress, int qLength, int nItems){ - if(homeSpan.Accessories.empty() || homeSpan.Accessories.back()->Services.empty()){ - Serial.printf("\nFATAL ERROR! Can't create new SpanPoint(\"%s\") without a defined Service ***\n",macAddress); - Serial.printf("\n=== PROGRAM HALTED ==="); - while(1); - } - - init(); 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); @@ -2170,11 +2163,17 @@ SpanPoint::SpanPoint(const char *macAddress){ while(1); } - peerInfo.channel=0; // 0=matches current WiFi channel + init(); // initialize SpanPoint + peerInfo.channel=0; // 0 = matches current WiFi channel peerInfo.ifidx=WIFI_IF_STA; // must specify interface 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 + + this->qLength=qLength; + dataQueue = xQueueCreate(nItems,qLength); + + SpanPoints.push_back(this); } /////////////////////////////// @@ -2197,12 +2196,33 @@ void SpanPoint::init(const char *password){ /////////////////////////////// -void SpanPoint::dataReceived(const uint8_t *mac, const uint8_t *incomingData, int len){ - Serial.printf("SpanPoint: %d bytes received from %02X:%02X:%02X:%02X:%02X:%02X\n",len,mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); +boolean SpanPoint::get(void *dataBuf){ + + return(xQueueReceive(dataQueue, dataBuf, 0)); } /////////////////////////////// +void SpanPoint::dataReceived(const uint8_t *mac, const uint8_t *incomingData, int len){ + + auto it=SpanPoints.begin(); + for(;it!=SpanPoints.end() && memcmp((*it)->peerInfo.peer_addr,mac,6)!=0; it++); + + if(it==SpanPoints.end()) + return; + + if(len!=(*it)->qLength){ + Serial.printf("SpanPoint Warning! %d bytes received from %02X:%02X:%02X:%02X:%02X:%02X does not match %d-byte queue size\n",len,mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],(*it)->qLength); + return; + } + + xQueueSend((*it)->dataQueue, incomingData, pdMS_TO_TICKS(1000)); +} + +/////////////////////////////// +// MISC // +/////////////////////////////// + void __attribute__((weak)) loop(){ } @@ -2214,5 +2234,6 @@ boolean SpanOTA::enabled=false; boolean SpanOTA::auth; uint8_t SpanPoint::lmk[16]; boolean SpanPoint::initialized=false; +vector SpanPoint::SpanPoints; diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 75ed259..57f358d 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -160,17 +160,21 @@ class SpanPoint { friend class Span; - esp_now_peer_info_t peerInfo; - + int qLength; // length of 1 queue item (in bytes) + esp_now_peer_info_t peerInfo; // structure for all ESP-NOW peer data + QueueHandle_t dataQueue; // queue to store data after it is received + static uint8_t lmk[16]; static boolean initialized; + static vector SpanPoints; static void dataReceived(const uint8_t *mac, const uint8_t *incomingData, int len); static void init(const char *password="HomeSpan"); public: - SpanPoint(const char *macAddress); + SpanPoint(const char *macAddress, int qLength, int nItems=1); + boolean get(void *dataBuf); }; /////////////////////////////// @@ -339,7 +343,6 @@ class SpanAccessory{ friend class SpanCharacteristic; friend class SpanButton; friend class SpanRange; - friend class SpanPoint; uint32_t aid=0; // Accessory Instance ID (HAP Table 6-1) int iidCount=0; // running count of iid to use for Services and Characteristics associated with this Accessory diff --git a/src/src.ino b/src/src.ino index 11f4331..16c3da2 100644 --- a/src/src.ino +++ b/src/src.ino @@ -15,6 +15,16 @@ CUSTOM_CHAR(LightMode, AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA, PR, STRING, "ANY_VALUE", NULL, NULL, true); CUSTOM_CHAR_STRING(DarkMode, AAAAAAAA-BBBB-AAAA-AAAA-AAAAAAAAAAAA, PR, "MY_VALUE"); +SpanPoint *dev1; +SpanPoint *dev2; + +struct message_t { + char a[32]; + int b; + float c; + bool d; +} message; + void setup() { Serial.begin(115200); @@ -44,6 +54,9 @@ void setup() { homeSpan.setSpanPointPassword("Hello Thert"); + dev1=new SpanPoint("AC:67:B2:77:42:20",sizeof(message_t)); + dev2=new SpanPoint("7C:DF:A1:61:E4:A8",sizeof(message_t)); + new SpanAccessory(); // Begin by creating a new Accessory using SpanAccessory(), which takes no arguments new Service::AccessoryInformation(); // HAP requires every Accessory to implement an AccessoryInformation Service, which has 6 required Characteristics @@ -65,7 +78,8 @@ void setup() { new Characteristic::Name("Light 1"); new Characteristic::ColorTemperature(); new Characteristic::Active(); - new SpanPoint("AC:67:B2:77:42:20"); + + new Service::LightBulb(); new Characteristic::On(0,true); (new Characteristic::Brightness(50,false))->setRange(10,100,5); @@ -99,6 +113,10 @@ void setup() { 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); } // end of loop()