Ping returns integer, ESP-NOW pings synchronous, increase LoRa DR buff, check for LoRa pkt overflow

pull/193/head
Jeff Lehman 3 months ago
parent 03d33e1897
commit 3477c70e55

@ -11,7 +11,7 @@
#define TXDELAYMS 300
#define SPBUFFSIZE 10
#define LORASIZE (250 / sizeof(DataReading))
#define DRBUFFSIZE LORASIZE
#define DRBUFFSIZE 100
#define ISBUFFEMPTY(buff) ((buff.endIdx == buff.startIdx) ? true: false)
#define ISBUFFFULL(buff) (((buff.endIdx + 1) % buff.size) == buff.startIdx ? true: false)
#define BUFFINCSTART(buff) (buff.startIdx = (buff.startIdx + 1) % buff.size)
@ -387,8 +387,13 @@ uint transmitSameAddrLoRa() {
break;
}
}
return count;
// check for data size greater than what can be sent via LoRa packets
if(count > LORASIZE) {
return LORASIZE;
}
else {
return count;
}
}
// Send time to LoRa broadcast and peers
@ -417,7 +422,7 @@ void sendTimeLoRa() {
return;
}
// Send time to LoRa node at specific address
// Send time to LoRa node at specific address - gateway
void sendTimeLoRa(uint16_t addr) {
SystemPacket spTimeLoRa = {.cmd = cmd_time, .param = now};
@ -426,7 +431,7 @@ void sendTimeLoRa(uint16_t addr) {
return;
}
// FDRS sends ping reply
// FDRS sends ping reply - gateway
bool pingReplyLoRa(uint16_t address)
{
SystemPacket sys_packet = {.cmd = cmd_ping, .param = ping_reply};
@ -661,14 +666,17 @@ crcResult LoRaTxRxOperation()
}
// FDRS Sensor pings address and listens for a defined amount of time for a reply
bool pingRequestLoRa(uint16_t address, uint32_t timeout)
int pingRequestLoRa(uint16_t address, uint32_t timeout)
{
int pingResult = -1;
// Check if a previous ping is already in process
if(loraPing.status == stReady) {
SystemPacket sys_packet = {.cmd = cmd_ping, .param = ping_request};
loraPing.timeout = timeout;
loraPing.address = address;
// Perform blocking ping if nothing else is in process
if(loraTxState == stReady) {
loraPing.status = stInProcess;
loraPing.start = millis();
@ -679,6 +687,7 @@ bool pingRequestLoRa(uint16_t address, uint32_t timeout)
}
if(loraPing.status == stCompleted) {
loraPing.response = millis() - loraPing.start;
pingResult = loraPing.response;
DBG1("LoRa Ping Returned: " + String(loraPing.response) + "ms.");
if(loraPing.address == timeSource.tmAddress) {
netTimeOffset = loraPing.response/2/1000;
@ -693,8 +702,8 @@ bool pingRequestLoRa(uint16_t address, uint32_t timeout)
loraPing.timeout = 0;
loraPing.address = 0;
loraPing.response = UINT32_MAX;
return true;
}
// Something else is in process, Most likely LoRa ACK, so queue up the ping
else {
if(transmitLoRaAsync(&address, &sys_packet, 1))
{
@ -706,7 +715,7 @@ bool pingRequestLoRa(uint16_t address, uint32_t timeout)
}
}
}
return false;
return pingResult;
}
// Sends packet to any node that is paired to this gateway
@ -744,6 +753,7 @@ void handleLoRa()
LoRaTxRxOperation();
// check for result of any ongoing async ping operations
if(loraPing.status == stCompleted) {
loraPing.response = millis() - loraPing.start;
DBG1("LoRa Ping Returned: " + String(loraPing.response) + "ms.");

@ -364,15 +364,16 @@ bool unsubscribeFDRS(uint16_t sub_id)
void pingFDRS(uint32_t timeout)
int pingFDRS(uint32_t timeout)
{
int pingResult = -1;
#ifdef USE_ESPNOW
pingFDRSEspNow(gatewayAddress, timeout);
pingResult = pingFDRSEspNow(gatewayAddress, timeout);
#endif
#ifdef USE_LORA
pingRequestLoRa(gtwyAddress, timeout);
pingResult = pingRequestLoRa(gtwyAddress, timeout);
#endif
return;
return pingResult;
}
bool reqTimeFDRS() {

@ -7,27 +7,12 @@
#include <esp_wifi.h>
#endif
enum PingStatusEspNow {
psNotStarted,
psWaiting,
psCompleted,
};
typedef struct EspNowPing {
PingStatusEspNow status = psNotStarted;
unsigned long start;
uint timeout;
uint8_t address[6];
uint32_t response = __UINT32_MAX__;
} EspNowPing;
EspNowPing espNowPing;
const uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
crcResult esp_now_ack_flag;
bool is_added = false;
uint32_t last_refresh = 0;
uint32_t gtwy_timeout = 300000;
bool pingFlag = false;
// Request time from gateway - Optionally used in sensors
bool reqTimeEspNow() {
@ -68,34 +53,6 @@ void recvTimeEspNow(uint32_t t) {
return;
}
uint32_t recvPingEspNow(uint8_t *srcAddr) {
uint32_t response = UINT32_MAX;
if(memcmp(espNowPing.address, srcAddr, sizeof(espNowPing.address)) == 0) {
if(TDIFF(espNowPing.start,espNowPing.timeout)) {
DBG1("No ESP-NOW ping returned within " + String(espNowPing.timeout) + "ms.");
}
else {
espNowPing.response = millis() - espNowPing.start;
DBG1("ESP-NOW Ping Reply in " + String(espNowPing.response) + "ms from 0x" + String(espNowPing.address[5], HEX));
response = espNowPing.response;
}
}
else {
DBG1("ESP-NOW ping reply from unexpected source 0x" + String(*(srcAddr + 5),HEX));
}
// reset status
espNowPing.status = psNotStarted;
espNowPing.timeout = 0;
espNowPing.start = 0;
memcpy(espNowPing.address, broadcast_mac, sizeof(espNowPing.address));
espNowPing.response = 0;
return response;
}
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
#if defined(ESP8266)
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus)
@ -136,7 +93,7 @@ void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
{
case cmd_ping:
if(command.param == ping_reply) {
recvPingEspNow(incMAC);
pingFlag = true;
}
break;
case cmd_add:
@ -166,25 +123,29 @@ void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
// FDRS node pings gateway and listens for a defined amount of time for a reply
// Asynchonous call so does not wait for a reply so we do not know how long the ping takes
// ESP-NOW is on the order of 10 milliseconds so happens very quickly. Not sure Async is warranted.
void pingFDRSEspNow(uint8_t *dstaddr, uint32_t timeout) {
int pingFDRSEspNow(uint8_t *dstaddr, uint32_t timeout) {
SystemPacket sys_packet = {.cmd = cmd_ping, .param = ping_request};
unsigned long pingTime = 0;
// ping function called again after previous ping not received for some reason
if(espNowPing.status == psWaiting) {
espNowPing.status = psNotStarted;
espNowPing.timeout = 0;
espNowPing.start = 0;
espNowPing.response = 0;
memcpy(espNowPing.address, broadcast_mac, sizeof(espNowPing.address));
}
espNowPing.status = psWaiting;
espNowPing.timeout = timeout;
espNowPing.start = millis();
espNowPing.response = 0;
memcpy(espNowPing.address, dstaddr, sizeof(espNowPing.address));
pingFlag = false;
pingTime = millis();
DBG1("ESP-NOW ping sent to 0x" + String(*(espNowPing.address + 5),HEX));
esp_now_send(dstaddr, (uint8_t *)&sys_packet, sizeof(SystemPacket));
while(pingFlag == false && (millis() - pingTime < timeout)) {
yield();
delay(0);
}
if(pingFlag == true) {
pingTime = millis() - pingTime;
DBG1("ESP-NOW Ping Reply in " + String(pingTime) + "ms from 0x" + String(espNowPing.address[5], HEX));
}
else {
DBG1("No ESP-NOW ping returned within " + String(espNowPing.timeout) + "ms.");
pingTime = -1;
}
pingFlag = false;
return pingTime;
}

Loading…
Cancel
Save