diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 205aac8..02ac360 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -48,6 +48,7 @@ #include "HAPConstants.h" #include "HapQR.h" #include "Characteristics.h" +#include "Now.h" using std::vector; using std::unordered_map; diff --git a/src/Now.cpp b/src/Now.cpp new file mode 100644 index 0000000..18ed780 --- /dev/null +++ b/src/Now.cpp @@ -0,0 +1,66 @@ + +#include "Now.h" +#include +#include +#include + +void SpanNow::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){ + Serial.printf("*** ERROR: Can't start HomeSpan NOW! Bad MAC Address '%s'\n\n",macAddress); + return; + } + + statusQueue = xQueueCreate(1,sizeof(esp_now_send_status_t)); + + WiFi.mode(WIFI_AP_STA); + esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); + esp_now_init(); + esp_now_register_send_cb(onDataSent); + + uint8_t lmk[32]; + + 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]); + started=true; +} + +boolean SpanNow::send(uint8_t *data, size_t len){ + + if(!started){ + Serial.printf("*** ERROR: Can't send data until HomeSpanNOW has been started.\n\n"); + return(false); + } + + esp_now_send_status_t status = ESP_NOW_SEND_FAIL; + + for(int c=0;c<13;c++){ + if((1< +#include + + +class SpanNow { + + esp_now_peer_info_t peerInfo; + uint8_t mac[6]; + boolean started=false; + static QueueHandle_t statusQueue; + int channel=1; + uint16_t channelMask=0x3FFE; + + static void onDataSent(const uint8_t *mac, esp_now_send_status_t status) { + xQueueOverwrite( statusQueue, &status ); + } + + public: + + void setChannelMask(uint16_t cm){channelMask = cm & 0x3FFE;} + void start(const char *macAddress, const char *password="HomeSpan"); + boolean send(uint8_t *data, size_t len); + +};