node registration refreshment

Nodes now automatically re-register with their gateway after a period of time
This commit is contained in:
Timm Bogner 2023-09-27 05:43:36 -05:00
parent f1f1850579
commit cd1c6347a7
2 changed files with 89 additions and 59 deletions

View File

@ -44,14 +44,12 @@ uint8_t gatewayAddress[] = {MAC_PREFIX, GTWY_MAC};
const uint16_t espnow_size = 250 / sizeof(DataReading); const uint16_t espnow_size = 250 / sizeof(DataReading);
crcResult crcReturned = CRC_NULL; crcResult crcReturned = CRC_NULL;
uint32_t gtwy_timeout = 0;
uint8_t incMAC[6]; uint8_t incMAC[6];
DataReading fdrsData[espnow_size]; DataReading fdrsData[espnow_size];
DataReading incData[espnow_size]; DataReading incData[espnow_size];
uint8_t data_count = 0; uint8_t data_count = 0;
uint32_t last_refresh;
void (*callback_ptr)(DataReading); void (*callback_ptr)(DataReading);
uint16_t subscription_list[256] = {}; uint16_t subscription_list[256] = {};
bool active_subs[256] = {}; bool active_subs[256] = {};
@ -249,6 +247,7 @@ void loadFDRS(float d, uint8_t t, uint16_t id)
fdrsData[data_count] = dr; fdrsData[data_count] = dr;
data_count++; data_count++;
} }
void sleepFDRS(int sleep_time) void sleepFDRS(int sleep_time)
{ {
#ifdef DEEP_SLEEP #ifdef DEEP_SLEEP
@ -265,20 +264,23 @@ void sleepFDRS(int sleep_time)
delay(sleep_time * 1000); delay(sleep_time * 1000);
} }
void loopFDRS() void loopFDRS()
{ {
#ifdef USE_LORA #ifdef USE_LORA
handleLoRa(); handleLoRa();
#endif #endif
handleIncoming(); handleIncoming();
// // TO-DO:
// if (is_added) if (is_added)
// { {
// if ((millis() - last_refresh) >= gtwy_timeout) if ((millis() - last_refresh) >= gtwy_timeout)
// { {
// last_refresh = millis(); refresh_registration();
// } last_refresh = millis();
// } }
}
} }
bool subscribeFDRS(uint16_t sub_id) bool subscribeFDRS(uint16_t sub_id)
@ -319,55 +321,7 @@ bool unsubscribeFDRS(uint16_t sub_id)
return false; return false;
} }
bool addFDRS(void (*new_cb_ptr)(DataReading))
{
callback_ptr = new_cb_ptr;
#ifdef USE_ESPNOW
SystemPacket sys_packet = {.cmd = cmd_add, .param = 0};
esp_now_send(gatewayAddress, (uint8_t *)&sys_packet, sizeof(SystemPacket));
DBG("ESP-NOW peer registration request submitted to " + String(gatewayAddress[5]));
uint32_t add_start = millis();
is_added = false;
while ((millis() - add_start) <= 1000) // 1000ms timeout
{
yield();
if (is_added)
{
DBG("Registration accepted. Timeout: " + String(gtwy_timeout));
last_refresh = millis();
return true;
}
}
DBG("No gateways accepted the request");
return false;
#endif // USE_ESPNOW
return true;
}
bool addFDRS(int timeout, void (*new_cb_ptr)(DataReading))
{
callback_ptr = new_cb_ptr;
#ifdef USE_ESPNOW
SystemPacket sys_packet = {.cmd = cmd_add, .param = 0};
esp_now_send(gatewayAddress, (uint8_t *)&sys_packet, sizeof(SystemPacket));
DBG("ESP-NOW peer registration request submitted to " + String(gatewayAddress[5]));
uint32_t add_start = millis();
is_added = false;
while ((millis() - add_start) <= timeout)
{
yield();
if (is_added)
{
DBG("Registration accepted. Timeout: " + String(gtwy_timeout));
last_refresh = millis();
return true;
}
}
DBG("No gateways accepted the request");
return false;
#endif // USE_ESPNOW
return true;
}
uint32_t pingFDRS(uint32_t timeout) uint32_t pingFDRS(uint32_t timeout)
{ {

View File

@ -11,6 +11,8 @@ uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
crcResult esp_now_ack_flag; crcResult esp_now_ack_flag;
bool is_added = false; bool is_added = false;
bool pingFlag = false; bool pingFlag = false;
uint32_t last_refresh = 0;
uint32_t gtwy_timeout = 300000;
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32 // Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
#if defined(ESP8266) #if defined(ESP8266)
@ -86,4 +88,78 @@ uint32_t pingFDRSEspNow(uint8_t *address, uint32_t timeout) {
} }
DBG("No ESP-NOW ping returned within " + String(timeout) + "ms."); DBG("No ESP-NOW ping returned within " + String(timeout) + "ms.");
return UINT32_MAX; return UINT32_MAX;
} }
bool refresh_registration()
{
#ifdef USE_ESPNOW
SystemPacket sys_packet = {.cmd = cmd_add, .param = 0};
esp_now_send(gatewayAddress, (uint8_t *)&sys_packet, sizeof(SystemPacket));
DBG("ESP-NOW peer registration request submitted to " + String(gatewayAddress[5]));
uint32_t add_start = millis();
is_added = false;
while ((millis() - add_start) <= 1000) // 1000ms timeout
{
yield();
if (is_added)
{
DBG("Registration accepted. Timeout: " + String(gtwy_timeout));
last_refresh = millis();
return true;
}
}
DBG("No gateways accepted the request");
return false;
#endif // USE_ESPNOW
return true;
}
bool addFDRS(void (*new_cb_ptr)(DataReading))
{
callback_ptr = new_cb_ptr;
#ifdef USE_ESPNOW
SystemPacket sys_packet = {.cmd = cmd_add, .param = 0};
esp_now_send(gatewayAddress, (uint8_t *)&sys_packet, sizeof(SystemPacket));
DBG("ESP-NOW peer registration request submitted to " + String(gatewayAddress[5]));
uint32_t add_start = millis();
is_added = false;
while ((millis() - add_start) <= 1000) // 1000ms timeout
{
yield();
if (is_added)
{
DBG("Registration accepted. Timeout: " + String(gtwy_timeout));
last_refresh = millis();
return true;
}
}
DBG("No gateways accepted the request");
return false;
#endif // USE_ESPNOW
return true;
}
bool addFDRS(int timeout, void (*new_cb_ptr)(DataReading))
{
callback_ptr = new_cb_ptr;
#ifdef USE_ESPNOW
SystemPacket sys_packet = {.cmd = cmd_add, .param = 0};
esp_now_send(gatewayAddress, (uint8_t *)&sys_packet, sizeof(SystemPacket));
DBG("ESP-NOW peer registration request submitted to " + String(gatewayAddress[5]));
uint32_t add_start = millis();
is_added = false;
while ((millis() - add_start) <= timeout)
{
yield();
if (is_added)
{
DBG("Registration accepted. Timeout: " + String(gtwy_timeout));
last_refresh = millis();
return true;
}
}
DBG("No gateways accepted the request");
return false;
#endif // USE_ESPNOW
return true;
}