Tweaks to pingFDRS

This commit is contained in:
Jeff Lehman 2023-02-25 18:24:34 -06:00
parent 62032a1d2a
commit e901a2f16a
5 changed files with 65 additions and 25 deletions

View File

@ -41,7 +41,6 @@ DataReading theData[256];
uint8_t ln;
uint8_t newData = event_clear;
uint8_t newCmd = cmd_clear;
bool is_ping = false;
DataReading fdrsData[256]; // buffer for loadFDRS()
uint8_t data_count = 0;

View File

@ -381,7 +381,7 @@ crcResult getLoRa()
{ // We have received a ping request or reply??
if (receiveData[0].param == 1)
{ // This is a reply to our ping request
is_ping = true;
pingFlag = true;
DBG("We have received a ping reply via LoRa from address " + String(sourceMAC, HEX));
}
else if (receiveData[0].param == 0)
@ -410,7 +410,7 @@ crcResult getLoRa()
{ // We have received a ping request or reply??
if (receiveData[0].param == 1)
{ // This is a reply to our ping request
is_ping = true;
pingFlag = true;
DBG("We have received a ping reply via LoRa from address " + String(sourceMAC, HEX));
}
else if (receiveData[0].param == 0)

View File

@ -58,7 +58,6 @@ DataReading fdrsData[espnow_size];
DataReading incData[espnow_size];
uint8_t data_count = 0;
bool is_ping = false;
uint32_t last_refresh;
void (*callback_ptr)(DataReading);
@ -341,27 +340,14 @@ bool addFDRS(int timeout, void (*new_cb_ptr)(DataReading))
return true;
}
uint32_t pingFDRS(int timeout)
uint32_t pingFDRS(uint32 timeout)
{
SystemPacket sys_packet = {.cmd = cmd_ping, .param = 0};
#ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *)&sys_packet, sizeof(SystemPacket));
DBG(" ESP-NOW ping sent.");
uint32_t ping_start = millis();
is_ping = false;
while ((millis() - ping_start) <= timeout)
{
yield(); // do I need to yield or does it automatically?
if (is_ping)
{
DBG("Ping Returned:" + String(millis() - ping_start) + " from " + String(incMAC[5]));
return millis() - ping_start;
}
}
uint32_t pingResponseMs = pingFDRSEspNow(gatewayAddress, timeout);
return pingResponseMs;
#endif
#ifdef USE_LORA
// transmitLoRa(gtwyAddress, sys_packet, data_count); // TODO: Make this congruent to esp_now_send()
DBG(" LoRa ping not sent because it isn't implemented.");
uint32_t pingResponseMs = pingFDRSLoRa(&gtwyAddress, timeout);
return pingResponseMs;
#endif
return 0;
}

View File

@ -10,6 +10,7 @@
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
crcResult esp_now_ack_flag;
bool is_added = false;
bool pingFlag = false;
#ifdef USE_ESPNOW
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
@ -49,7 +50,7 @@ void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
switch (command.cmd)
{
case cmd_ping:
is_ping = true;
pingFlag = true;
break;
case cmd_add:
is_added = true;
@ -64,4 +65,28 @@ void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
newData = true;
}
}
// FDRS node pings gateway and listens for a defined amount of time for a reply
// Blocking function for timeout amount of time (up to timeout time waiting for reply)(IE no callback)
// Returns the amount of time in ms that the ping takes or predefined value if ping fails within timeout
uint32_t pingFDRSEspNow(uint16_t *address, uint32_t timeout) {
SystemPacket sys_packet = {.cmd = cmd_ping, .param = 0};
esp_now_send(gatewayAddress, (uint8_t *)&sys_packet, sizeof(SystemPacket));
DBG(" ESP-NOW ping sent.");
uint32_t ping_start = millis();
pingFlag = false;
while ((millis() - ping_start) <= timeout)
{
yield(); // do I need to yield or does it automatically?
if (pingFlag)
{
DBG("Ping Returned:" + String(millis() - ping_start) + " from " + String(incMAC[5]));
return (millis() - ping_start);
}
}
DBG("No ESP-NOW ping returned within " + String(timeout) + "ms.");
return UINT32_MAX;
}
#endif // USE_ESPNOW

View File

@ -80,6 +80,8 @@ RADIOLIB_MODULE radio = new Module(LORA_SS, LORA_DIO, LORA_RST, -1, LORA_SPI);
#else
RADIOLIB_MODULE radio = new Module(LORA_SS, LORA_DIO, LORA_RST, -1);
#endif // CUSTOM_SPI
bool pingFlag = false;
bool transmitFlag = false; // flag to indicate transmission or reception state
volatile bool enableInterrupt = true; // disable interrupt when it's not needed
volatile bool operationDone = false; // flag to indicate that a packet was sent or received
@ -381,7 +383,7 @@ crcResult getLoRa()
{ // We have received a ping request or reply??
if (receiveData[0].param == 1)
{ // This is a reply to our ping request
is_ping = true;
pingFlag = true;
DBG("We have received a ping reply via LoRa from address " + String(sourceMAC, HEX));
}
else if (receiveData[0].param == 0)
@ -410,7 +412,7 @@ crcResult getLoRa()
{ // We have received a ping request or reply??
if (receiveData[0].param == 1)
{ // This is a reply to our ping request
is_ping = true;
pingFlag = true;
DBG("We have received a ping reply via LoRa from address " + String(sourceMAC, HEX));
}
else if (receiveData[0].param == 0)
@ -456,6 +458,34 @@ crcResult getLoRa()
#endif // USE_LORA
return CRC_NULL;
}
// FDRS Sensor pings gateway and listens for a defined amount of time for a reply
// Blocking function for timeout amount of time (up to timeout time waiting for reply)(IE no callback)
// Returns the amount of time in ms that the ping takes or predefined value if ping fails within timeout
uint32_t pingFDRSLoRa(uint16_t *address, uint32_t timeout)
{
SystemPacket sys_packet = {.cmd = cmd_ping, .param = 0};
transmitLoRa(address, &sys_packet, 1);
DBG("LoRa ping sent to address: 0x" + String(*address, HEX));
uint32_t ping_start = millis();
pingFlag = false;
while ((millis() - ping_start) <= timeout)
{
handleLoRa();
yield(); //do I need to yield or does it automatically?
if (pingFlag)
{
DBG("LoRa Ping Returned: " + String(millis() - ping_start) + "ms.");
pingFlag = false;
return (millis() - ping_start);
}
}
DBG("No LoRa ping returned within " + String(timeout) + "ms.");
return UINT32_MAX;
}
void printLoraPacket(uint8_t *p, int size)
{
printf("Printing packet of size %d.", size);