transmitLoRa for node returns crcResult and cascade changes to sendFDRS()

This commit is contained in:
Jeff Lehman 2023-02-25 18:45:31 -06:00
parent cc218456fe
commit c6fa4f35a6
2 changed files with 54 additions and 16 deletions

View File

@ -169,6 +169,9 @@ void handleIncoming()
bool sendFDRS() bool sendFDRS()
{ {
if(data_count == 0) {
return false;
}
DBG("Sending FDRS Packet!"); DBG("Sending FDRS Packet!");
#ifdef USE_ESPNOW #ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *)&fdrsData, data_count * sizeof(DataReading)); esp_now_send(gatewayAddress, (uint8_t *)&fdrsData, data_count * sizeof(DataReading));
@ -189,12 +192,26 @@ bool sendFDRS()
} }
#endif #endif
#ifdef USE_LORA #ifdef USE_LORA
transmitLoRa(&gtwyAddress, fdrsData, data_count); crcReturned = transmitLoRa(&gtwyAddress, fdrsData, data_count);
// DBG(" LoRa sent.");
#ifdef LORA_ACK
if(crcReturned == CRC_OK) {
data_count = 0; data_count = 0;
returnCRC = CRC_NULL; return true;
}
#endif #endif
#ifndef LORA_ACK
if(crcReturned == CRC_OK || crcReturned == CRC_NULL) {
data_count = 0;
return true; return true;
} }
#endif
else {
data_count = 0;
return false;
}
#endif
}
void loadFDRS(float d, uint8_t t) void loadFDRS(float d, uint8_t t)
{ {

View File

@ -96,6 +96,8 @@ unsigned long msgOkLoRa = 0; // Number of total LoRa packets with vali
void printLoraPacket(uint8_t *p, int size); void printLoraPacket(uint8_t *p, int size);
uint16_t gtwyAddress = ((gatewayAddress[4] << 8) | GTWY_MAC); uint16_t gtwyAddress = ((gatewayAddress[4] << 8) | GTWY_MAC);
// Function prototypes
crcResult getLoRa(); crcResult getLoRa();
#if defined(ESP8266) || defined(ESP32) #if defined(ESP8266) || defined(ESP32)
@ -111,11 +113,13 @@ void setFlag(void)
} }
#endif // USE_LORA #endif // USE_LORA
void handleLoRa() crcResult handleLoRa()
{ {
#ifdef USE_LORA #ifdef USE_LORA
crcResult crcReturned = CRC_NULL;
if (operationDone) if (operationDone)
{ // the interrupt was triggered { // the interrupt was triggered
// DBG("Interrupt Triggered.");
enableInterrupt = false; enableInterrupt = false;
operationDone = false; operationDone = false;
if (transmitFlag) if (transmitFlag)
@ -126,7 +130,7 @@ void handleLoRa()
} }
else else
{ // the previous operation was reception { // the previous operation was reception
returnCRC = getLoRa(); crcReturned = getLoRa();
if (!transmitFlag) // return to listen if no transmission was begun if (!transmitFlag) // return to listen if no transmission was begun
{ {
radio.startReceive(); radio.startReceive();
@ -134,6 +138,7 @@ void handleLoRa()
enableInterrupt = true; enableInterrupt = true;
} }
} }
return crcReturned;
#endif // USE_LORA #endif // USE_LORA
} }
@ -167,6 +172,7 @@ void begin_lora()
#endif #endif
radio.setCRC(false); radio.setCRC(false);
LoRaAddress = ((radio.randomByte() << 8) | radio.randomByte()); LoRaAddress = ((radio.randomByte() << 8) | radio.randomByte());
DBG("LoRa node address is " + String(LoRaAddress, HEX) + " (hex).");
state = radio.startReceive(); // start listening for LoRa packets state = radio.startReceive(); // start listening for LoRa packets
if (state == RADIOLIB_ERR_NONE) if (state == RADIOLIB_ERR_NONE)
{ {
@ -180,9 +186,13 @@ void begin_lora()
#endif // USE_LORA #endif // USE_LORA
} }
void transmitLoRa(uint16_t *destMAC, DataReading *packet, uint8_t len) // Transmits Lora data by calling RadioLib library function
// Returns the CRC result if ACKs are enabled otherwise returns CRC_NULL
crcResult transmitLoRa(uint16_t *destMAC, DataReading *packet, uint8_t len)
{ {
#ifdef USE_LORA #ifdef USE_LORA
crcResult crcReturned = CRC_NULL;
uint8_t pkt[6 + (len * sizeof(DataReading))]; uint8_t pkt[6 + (len * sizeof(DataReading))];
uint16_t calcCRC = 0x0000; uint16_t calcCRC = 0x0000;
@ -225,17 +235,17 @@ void transmitLoRa(uint16_t *destMAC, DataReading *packet, uint8_t len)
unsigned long loraAckTimeout = millis() + FDRS_ACK_TIMEOUT; unsigned long loraAckTimeout = millis() + FDRS_ACK_TIMEOUT;
retries--; retries--;
delay(10); delay(10);
while (returnCRC == CRC_NULL && (millis() < loraAckTimeout)) while (crcReturned == CRC_NULL && (millis() < loraAckTimeout))
{ {
handleLoRa(); crcReturned = handleLoRa();
} }
if (returnCRC == CRC_OK) if (crcReturned == CRC_OK)
{ {
// DBG("LoRa ACK Received! CRC OK"); // DBG("LoRa ACK Received! CRC OK");
msgOkLoRa++; msgOkLoRa++;
return; // we're done return CRC_OK; // we're done
} }
else if (returnCRC == CRC_BAD) else if (crcReturned == CRC_BAD)
{ {
// DBG("LoRa ACK Received! CRC BAD"); // DBG("LoRa ACK Received! CRC BAD");
// Resend original packet again if retries are available // Resend original packet again if retries are available
@ -262,29 +272,38 @@ void transmitLoRa(uint16_t *destMAC, DataReading *packet, uint8_t len)
} }
transmitLoRaMsgwAck++; transmitLoRaMsgwAck++;
#endif // LORA_ACK #endif // LORA_ACK
return crcReturned;
#endif // USE_LORA #endif // USE_LORA
} }
// For now SystemPackets will not use ACK but will calculate CRC // For now SystemPackets will not use ACK but will calculate CRC
void transmitLoRa(uint16_t *destMAC, SystemPacket *packet, uint8_t len) // Returns CRC_NULL ask SystemPackets do not use ACKS at current time
crcResult transmitLoRa(uint16_t *destMAC, SystemPacket *packet, uint8_t len)
{ {
#ifdef USE_LORA #ifdef USE_LORA
crcResult crcReturned = CRC_NULL;
uint8_t pkt[6 + (len * sizeof(SystemPacket))]; uint8_t pkt[6 + (len * sizeof(SystemPacket))];
uint16_t calcCRC = 0x0000; uint16_t calcCRC = 0x0000;
// Building packet -- address portion - first 4 bytes
pkt[0] = (*destMAC >> 8); pkt[0] = (*destMAC >> 8);
pkt[1] = (*destMAC & 0x00FF); pkt[1] = (*destMAC & 0x00FF);
pkt[2] = (LoRaAddress >> 8); pkt[2] = (LoRaAddress >> 8);
pkt[3] = (LoRaAddress & 0x00FF); pkt[3] = (LoRaAddress & 0x00FF);
// Building packet -- data portion - 5 bytes
memcpy(&pkt[4], packet, len * sizeof(SystemPacket)); memcpy(&pkt[4], packet, len * sizeof(SystemPacket));
// Calculate CRC of address and data portion of the packet
// Last 2 bytes are CRC so do not include them in the calculation itself
for (int i = 0; i < (sizeof(pkt) - 2); i++) for (int i = 0; i < (sizeof(pkt) - 2); i++)
{ // Last 2 bytes are CRC so do not include them in the calculation itself {
// printf("CRC: %02X : %d\n",calcCRC, i); // printf("CRC: %02X : %d\n",calcCRC, i);
calcCRC = crc16_update(calcCRC, pkt[i]); calcCRC = crc16_update(calcCRC, pkt[i]);
} }
calcCRC = crc16_update(calcCRC, 0xA1); // Recalculate CRC for No ACK calcCRC = crc16_update(calcCRC, 0xA1); // Recalculate CRC for No ACK
// Building packet -- adding CRC - last 2 bytes
pkt[len * sizeof(SystemPacket) + 4] = (calcCRC >> 8); pkt[len * sizeof(SystemPacket) + 4] = (calcCRC >> 8);
pkt[len * sizeof(SystemPacket) + 5] = (calcCRC & 0x00FF); pkt[len * sizeof(SystemPacket) + 5] = (calcCRC & 0x00FF);
// Packet is constructed now transmit the packet
DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC, HEX) + " to destination 0x" + String(*destMAC, HEX)); DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC, HEX) + " to destination 0x" + String(*destMAC, HEX));
// printLoraPacket(pkt,sizeof(pkt)); // printLoraPacket(pkt,sizeof(pkt));
int state = radio.startTransmit(pkt, sizeof(pkt)); int state = radio.startTransmit(pkt, sizeof(pkt));
@ -298,9 +317,11 @@ void transmitLoRa(uint16_t *destMAC, SystemPacket *packet, uint8_t len)
while (true) while (true)
; ;
} }
return crcReturned;
#endif // USE_LORA #endif // USE_LORA
} }
// ****DO NOT CALL getLoRa() directly! ***** Call handleLoRa() instead!
// getLoRa for Sensors // getLoRa for Sensors
// USED to get ACKs (SystemPacket type) from LoRa gateway at this point. May be used in the future to get other data // USED to get ACKs (SystemPacket type) from LoRa gateway at this point. May be used in the future to get other data
// Return type is crcResult struct - CRC_OK, CRC_BAD, CRC_NULL. CRC_NULL used for non-ack data // Return type is crcResult struct - CRC_OK, CRC_BAD, CRC_NULL. CRC_NULL used for non-ack data
@ -322,7 +343,7 @@ crcResult getLoRa()
destMAC = (packet[0] << 8) | packet[1]; destMAC = (packet[0] << 8) | packet[1];
sourceMAC = (packet[2] << 8) | packet[3]; sourceMAC = (packet[2] << 8) | packet[3];
packetCRC = ((packet[packetSize - 2] << 8) | packet[packetSize - 1]); packetCRC = ((packet[packetSize - 2] << 8) | packet[packetSize - 1]);
// DBG("Packet Address: 0x" + String(packet[0], HEX) + String(packet[1], HEX) + " Self Address: 0x" + String(selfAddress[4], HEX) + String(selfAddress[5], HEX)); // DBG("Source Address: 0x" + String(packet[2], HEX) + String(packet[3], HEX) + " Destination Address: 0x" + String(packet[0], HEX) + String(packet[1], HEX));
if ((destMAC == LoRaAddress) || (destMAC == 0xFFFF)) if ((destMAC == LoRaAddress) || (destMAC == 0xFFFF))
{ // Check if addressed to this device or broadcast { // Check if addressed to this device or broadcast
// printLoraPacket(packet,sizeof(packet)); // printLoraPacket(packet,sizeof(packet));
@ -439,8 +460,8 @@ crcResult getLoRa()
} }
else else
{ {
// DBG("Incoming LoRa packet of " + String(packetSize) + " bytes received from address 0x" + String(sourceMAC, HEX) + " destined for node address 0x" + String(destMAC, HEX)); DBG("Incoming LoRa packet of " + String(packetSize) + " bytes received from address 0x" + String(sourceMAC, HEX) + " destined for node address 0x" + String(destMAC, HEX));
// printLoraPacket(packet,sizeof(packet)); // printLoraPacket(packet,sizeof(packet));
return CRC_NULL; return CRC_NULL;
} }
} }
@ -455,8 +476,8 @@ crcResult getLoRa()
return CRC_NULL; return CRC_NULL;
} }
} }
#endif // USE_LORA
return CRC_NULL; return CRC_NULL;
#endif // USE_LORA
} }
// FDRS Sensor pings gateway and listens for a defined amount of time for a reply // FDRS Sensor pings gateway and listens for a defined amount of time for a reply