Tweaks to pingFDRS

pull/139/head
Jeff Lehman 2 years ago
parent 62032a1d2a
commit e901a2f16a

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

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

@ -58,7 +58,6 @@ DataReading fdrsData[espnow_size];
DataReading incData[espnow_size]; DataReading incData[espnow_size];
uint8_t data_count = 0; uint8_t data_count = 0;
bool is_ping = false;
uint32_t last_refresh; uint32_t last_refresh;
void (*callback_ptr)(DataReading); void (*callback_ptr)(DataReading);
@ -341,27 +340,14 @@ bool addFDRS(int timeout, void (*new_cb_ptr)(DataReading))
return true; return true;
} }
uint32_t pingFDRS(int timeout) uint32_t pingFDRS(uint32 timeout)
{ {
SystemPacket sys_packet = {.cmd = cmd_ping, .param = 0};
#ifdef USE_ESPNOW #ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *)&sys_packet, sizeof(SystemPacket)); uint32_t pingResponseMs = pingFDRSEspNow(gatewayAddress, timeout);
DBG(" ESP-NOW ping sent."); return pingResponseMs;
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;
}
}
#endif #endif
#ifdef USE_LORA #ifdef USE_LORA
// transmitLoRa(gtwyAddress, sys_packet, data_count); // TODO: Make this congruent to esp_now_send() uint32_t pingResponseMs = pingFDRSLoRa(&gtwyAddress, timeout);
DBG(" LoRa ping not sent because it isn't implemented."); return pingResponseMs;
#endif #endif
return 0;
} }

@ -10,6 +10,7 @@
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 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;
#ifdef USE_ESPNOW #ifdef USE_ESPNOW
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32 // 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) switch (command.cmd)
{ {
case cmd_ping: case cmd_ping:
is_ping = true; pingFlag = true;
break; break;
case cmd_add: case cmd_add:
is_added = true; is_added = true;
@ -64,4 +65,28 @@ void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
newData = true; 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 #endif // USE_ESPNOW

@ -80,6 +80,8 @@ RADIOLIB_MODULE radio = new Module(LORA_SS, LORA_DIO, LORA_RST, -1, LORA_SPI);
#else #else
RADIOLIB_MODULE radio = new Module(LORA_SS, LORA_DIO, LORA_RST, -1); RADIOLIB_MODULE radio = new Module(LORA_SS, LORA_DIO, LORA_RST, -1);
#endif // CUSTOM_SPI #endif // CUSTOM_SPI
bool pingFlag = false;
bool transmitFlag = false; // flag to indicate transmission or reception state bool transmitFlag = false; // flag to indicate transmission or reception state
volatile bool enableInterrupt = true; // disable interrupt when it's not needed 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 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?? { // We have received a ping request or reply??
if (receiveData[0].param == 1) if (receiveData[0].param == 1)
{ // This is a reply to our ping request { // 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)); DBG("We have received a ping reply via LoRa from address " + String(sourceMAC, HEX));
} }
else if (receiveData[0].param == 0) else if (receiveData[0].param == 0)
@ -410,7 +412,7 @@ crcResult getLoRa()
{ // We have received a ping request or reply?? { // We have received a ping request or reply??
if (receiveData[0].param == 1) if (receiveData[0].param == 1)
{ // This is a reply to our ping request { // 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)); DBG("We have received a ping reply via LoRa from address " + String(sourceMAC, HEX));
} }
else if (receiveData[0].param == 0) else if (receiveData[0].param == 0)
@ -456,6 +458,34 @@ crcResult getLoRa()
#endif // USE_LORA #endif // USE_LORA
return CRC_NULL; 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) void printLoraPacket(uint8_t *p, int size)
{ {
printf("Printing packet of size %d.", size); printf("Printing packet of size %d.", size);

Loading…
Cancel
Save