Add LoRa ACK & CRC functionality for gateway #54

This commit is contained in:
Jeff Lehman 2022-07-25 13:06:14 -05:00
parent 7cf9ea113d
commit 0bf0015576
6 changed files with 355 additions and 75 deletions

View File

@ -20,10 +20,17 @@ enum {
event_lora2
};
enum crcResult{
CRC_NULL,
CRC_OK,
CRC_BAD,
} returnCRC = CRC_NULL;
enum {
cmd_clear,
cmd_ping,
cmd_add,
cmd_ack
};
#ifdef FDRS_DEBUG
@ -100,9 +107,13 @@ uint8_t ESPNOW2[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
#endif
#ifdef USE_LORA
uint8_t LoRa1[] = {mac_prefix[3], mac_prefix[4], LORA1_PEER};
uint8_t LoRa2[] = {mac_prefix[3], mac_prefix[4], LORA2_PEER};
//uint8_t LoRaAddress[] = {0x42, 0x00};
uint16_t LoRa1 = ((mac_prefix[4] << 8) | LORA1_PEER); // Use 2 bytes for LoRa addressing instead of previous 3 bytes
uint16_t LoRa2 = ((mac_prefix[4] << 8) | LORA2_PEER);
//uint16_t LoRaAddress = 0x4200;
uint16_t loraGwAddress = ((selfAddress[4] << 8) | selfAddress[5]); // last 2 bytes of gateway address
uint16_t loraBroadcast = 0xFFFF;
unsigned long receivedLoRaMsg = 0; // Number of total LoRa packets destined for us and of valid size
unsigned long ackOkLoRaMsg = 0; // Number of total LoRa packets with valid CRC
#endif
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
@ -317,32 +328,139 @@ void mqtt_publish(const char* payload) {
#endif
}
void printLoraPacket(uint8_t* p,int size) {
printf("Printing packet of size %d.",size);
for(int i = 0; i < size; i++ ) {
if(i % 2 == 0) printf("\n%02d: ", i);
printf("%02X ", p[i]);
}
printf("\n");
}
void getLoRa() {
#ifdef USE_LORA
int packetSize = LoRa.parsePacket();
if (packetSize) {
if((packetSize - 6) % sizeof(DataReading) == 0 && packetSize > 0) { // packet size should be 6 bytes plus multiple of size of DataReading
uint8_t packet[packetSize];
uint8_t incLORAMAC[2];
uint16_t packetCRC = 0x0000; // CRC Extracted from received LoRa packet
uint16_t calcCRC = 0x0000; // CRC calculated from received LoRa packet
uint16_t sourceMAC = 0x0000;
uint16_t destMAC = 0x0000;
LoRa.readBytes((uint8_t *)&packet, packetSize);
ln = (packetSize - 5) / sizeof(DataReading);
DBG("Incoming LoRa.");
if (memcmp(&packet, &selfAddress[3], 3) == 0) { //Check if addressed to this device
memcpy(&incLORAMAC, &packet[3], 2); //Split off address portion of packet
memcpy(&theData, &packet[5], packetSize - 5); //Split off data portion of packet
if (memcmp(&incLORAMAC, &LoRa1, 2) == 0) { //Check if it is from a registered sender
ln = (packetSize - 6) / sizeof(DataReading);
destMAC = (packet[0] << 8) | packet[1];
sourceMAC = (packet[2] << 8) | packet[3];
packetCRC = ((packet[packetSize - 2] << 8) | packet[packetSize - 1]);
//DBG("Packet Address: 0x" + String(packet[0],16) + String(packet[1],16) + " Self Address: 0x" + String(selfAddress[4],16) + String(selfAddress[5],16));
if (destMAC == (selfAddress[4] << 8 | selfAddress[5])) { //Check if addressed to this device (2 bytes, bytes 1 and 2)
//printLoraPacket(packet,sizeof(packet));
memcpy(&theData, &packet[4], packetSize - 6); //Split off data portion of packet (N - 6 bytes (6 bytes for headers and CRC))
if(receivedLoRaMsg != 0){ // Avoid divide by 0
DBG("Incoming LoRa. Size: " + String(packetSize) + " Bytes, RSSI: " + String(LoRa.packetRssi()) + "dBi, SNR: " + String(LoRa.packetSnr()) + "dB, PacketCRC: 0x" + String(packetCRC,16) + ", Total LoRa received: " + String(receivedLoRaMsg) + ", CRC Ok Pct " + String((float)ackOkLoRaMsg/receivedLoRaMsg*100) + "%");
}
else {
DBG("Incoming LoRa. Size: " + String(packetSize) + " Bytes, RSSI: " + String(LoRa.packetRssi()) + "dBi, SNR: " + String(LoRa.packetSnr()) + "dB, PacketCRC: 0x" + String(packetCRC,16) + ", Total LoRa received: " + String(receivedLoRaMsg));
}
receivedLoRaMsg++;
// Evaluate CRC
if(packetCRC == 0xFFFF) { // CRC is set that destination does not want ACK so do not send.
DBG("Sensor address 0x" + String(sourceMAC,16) + "(hex) does not want ACK");
ackOkLoRaMsg++;
}
else { // Calculate expected CRC and compare to what is in the packet
for(int i = 0; i < (packetSize - 2); i++) { // Last 2 bytes of packet are the CRC so do not include them in calculation
//printf("CRC: %02X : %d\n",calcCRC, i);
calcCRC = crc16_update(calcCRC, packet[i]);
}
if(calcCRC == packetCRC) {
SystemPacket ACK = { .cmd = cmd_ack, .param = CRC_OK };
DBG("CRC Match, sending ACK packet to sensor 0x" + String(sourceMAC,16) + "(hex)");
transmitLoRa(&sourceMAC, &ACK, 1); // Send ACK back to source
ackOkLoRaMsg++;
}
else {
SystemPacket NAK = { .cmd = cmd_ack, .param = CRC_BAD };
// Send NAK packet to sensor
DBG("CRC Mismatch! Packet CRC is 0x" + String(packetCRC,16) + ", Calculated CRC is 0x" + String(calcCRC,16) + " Sending NAK packet to sensor 0x" + String(sourceMAC,16) + "(hex)");
transmitLoRa(&sourceMAC, &NAK, 1); // CRC did not match so send NAK to source
newData = event_clear; // do not process data as data may be corrupt
return; // Exit function and do not update newData to send invalid data further on
}
}
if (memcmp(&sourceMAC, &LoRa1, 2) == 0) { //Check if it is from a registered sender
newData = event_lora1;
return;
}
if (memcmp(&incLORAMAC, &LoRa2, 2) == 0) {
if (memcmp(&sourceMAC, &LoRa2, 2) == 0) {
newData = event_lora2;
return;
}
newData = event_lorag;
}
else {
DBG("Incoming LoRa packet of " + String(packetSize) + " bytes received from address 0x" + String(sourceMAC,16) + " destined for node address 0x" + String(destMAC,16));
}
}
else {
if(packetSize != 0) {
DBG("Incoming LoRa packet of " + String(packetSize) + " not processed.");
}
}
#endif
}
#ifdef USE_LORA
void transmitLoRa(uint16_t* destMac, DataReading * packet, uint8_t len) {
uint16_t calcCRC = 0x0000;
uint8_t pkt[6 + (len * sizeof(DataReading))];
pkt[0] = (*destMac >> 8); // high byte of destination MAC
pkt[1] = (*destMac & 0x00FF); // low byte of destination MAC
pkt[2] = selfAddress[4]; // high byte of source MAC (ourselves)
pkt[3] = selfAddress[5]; // low byte of source MAC
memcpy(&pkt[4], packet, len * sizeof(DataReading)); // copy data portion of packet
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);
calcCRC = crc16_update(calcCRC, pkt[i]);
}
pkt[(len * sizeof(DataReading) + 4)] = (calcCRC >> 8); // Append calculated CRC to the last 2 bytes of the packet
pkt[(len * sizeof(DataReading) + 5)] = (calcCRC & 0x00FF);
DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC,16) + " to LoRa MAC 0x" + String(*destMac,16));
//printLoraPacket(pkt,sizeof(pkt));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
}
#endif
#ifdef USE_LORA
void transmitLoRa(uint16_t* destMac, SystemPacket * packet, uint8_t len) {
uint16_t calcCRC = 0x0000;
uint8_t pkt[6 + (len * sizeof(SystemPacket))];
pkt[0] = (*destMac >> 8); // high byte of destination MAC
pkt[1] = (*destMac & 0x00FF); // low byte of destination MAC
pkt[2] = selfAddress[4]; // high byte of source MAC (ourselves)
pkt[3] = selfAddress[5]; // low byte of source MAC
memcpy(&pkt[4], packet, len * sizeof(SystemPacket)); // copy data portion of packet
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);
calcCRC = crc16_update(calcCRC, pkt[i]);
}
pkt[(len * sizeof(SystemPacket) + 4)] = (calcCRC >> 8); // Append calculated CRC to the last 2 bytes of the packet
pkt[(len * sizeof(SystemPacket) + 5)] = (calcCRC & 0x00FF);
DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC,16) + " to LoRa MAC 0x" + String(*destMac,16));
//printLoraPacket(pkt,sizeof(pkt));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
}
#endif
void sendESPNOW(uint8_t address) {
DBG("Sending ESP-NOW.");
uint8_t temp_peer[] = {MAC_PREFIX, address};
@ -526,19 +644,6 @@ void releaseESPNOW(uint8_t interface) {
}
}
}
#ifdef USE_LORA
void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
DBG("Transmitting LoRa.");
uint8_t pkt[5 + (len * sizeof(DataReading))];
memcpy(&pkt, mac, 3);
memcpy(&pkt[3], &selfAddress[4], 2);
memcpy(&pkt[5], packet, len * sizeof(DataReading));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
}
#endif
void releaseLoRa(uint8_t interface) {
#ifdef USE_LORA
@ -552,12 +657,12 @@ void releaseLoRa(uint8_t interface) {
for (int i = 0; i < lenLORAG; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(broadcast_mac, thePacket, j);
transmitLoRa(&loraBroadcast, thePacket, j);
}
thePacket[j] = LORAGbuffer[i];
j++;
}
transmitLoRa(broadcast_mac, thePacket, j);
transmitLoRa(&loraBroadcast, thePacket, j);
lenLORAG = 0;
break;
@ -569,12 +674,12 @@ void releaseLoRa(uint8_t interface) {
for (int i = 0; i < lenLORA1; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(LoRa1, thePacket, j);
transmitLoRa(&LoRa1, thePacket, j);
}
thePacket[j] = LORA1buffer[i];
j++;
}
transmitLoRa(LoRa1, thePacket, j);
transmitLoRa(&LoRa1, thePacket, j);
lenLORA1 = 0;
break;
}
@ -585,18 +690,18 @@ void releaseLoRa(uint8_t interface) {
for (int i = 0; i < lenLORA2; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(LoRa2, thePacket, j);
transmitLoRa(&LoRa2, thePacket, j);
}
thePacket[j] = LORA2buffer[i];
j++;
}
transmitLoRa(LoRa2, thePacket, j);
transmitLoRa(&LoRa2, thePacket, j);
lenLORA2 = 0;
break;
}
}
#endif //USE_LORA
#endif
}
void releaseSerial() {
DBG("Releasing Serial.");
@ -759,6 +864,32 @@ void handleCommands() {
}
// CRC16 from https://github.com/4-20ma/ModbusMaster/blob/3a05ff87677a9bdd8e027d6906dc05ca15ca8ade/src/util/crc16.h#L71
/** @ingroup util_crc16
Processor-independent CRC-16 calculation.
Polynomial: x^16 + x^15 + x^2 + 1 (0xA001)<br>
Initial value: 0xFFFF
This CRC is normally used in disk-drive controllers.
@param uint16_t crc (0x0000..0xFFFF)
@param uint8_t a (0x00..0xFF)
@return calculated CRC (0x0000..0xFFFF)
*/
#endif //__FDRS_FUNCTIONS_H__
static uint16_t crc16_update(uint16_t crc, uint8_t a)
{
int i;
crc ^= a;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001;
else
crc = (crc >> 1);
}
return crc;
}
#endif //__FDRS_FUNCTIONS_H__

View File

@ -57,8 +57,9 @@
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
//#define LORA_BAND 915E6
//#define LORA_SF 7
#define LORA_BAND 915E6
#define LORA_SF 7
#define LORA_NOACK_CRC 0xFFFF
// Buffer Delays - in milliseconds - Uncomment to enable any buffer

View File

@ -57,8 +57,9 @@
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
//#define LORA_BAND 915E6
//#define LORA_SF 7
#define LORA_BAND 915E6
#define LORA_SF 7
#define LORA_NOACK_CRC 0xFFFF
// Buffer Delays - in milliseconds - Uncomment to enable any buffer

View File

@ -57,8 +57,9 @@
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
//#define LORA_BAND 915E6
//#define LORA_SF 7
#define LORA_BAND 915E6
#define LORA_SF 7
#define LORA_NOACK_CRC 0xFFFF
// Buffer Delays - in milliseconds - Uncomment to enable any buffer

View File

@ -57,8 +57,9 @@
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
//#define LORA_BAND 915E6
//#define LORA_SF 7
#define LORA_BAND 915E6
#define LORA_SF 7
#define LORA_NOACK_CRC 0xFFFF
// Buffer Delays - in milliseconds - Uncomment to enable any buffer

View File

@ -20,6 +20,19 @@ enum {
event_lora2
};
enum crcResult{
CRC_NULL,
CRC_OK,
CRC_BAD,
} returnCRC = CRC_NULL;
enum {
cmd_clear,
cmd_ping,
cmd_add,
cmd_ack
};
#ifdef FDRS_DEBUG
#define DBG(a) (Serial.println(a))
#else
@ -128,6 +141,11 @@ typedef struct __attribute__((packed)) DataReading {
} DataReading;
typedef struct __attribute__((packed)) SystemPacket {
uint8_t cmd;
uint32_t param;
} SystemPacket;
const uint8_t espnow_size = 250 / sizeof(DataReading);
const uint8_t lora_size = 256 / sizeof(DataReading);
const uint8_t mac_prefix[] = {MAC_PREFIX};
@ -152,12 +170,18 @@ uint8_t ESPNOW2[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
#endif
#ifdef USE_LORA
uint8_t LoRa1[] = {mac_prefix[3], mac_prefix[4], LORA1_PEER};
uint8_t LoRa2[] = {mac_prefix[3], mac_prefix[4], LORA2_PEER};
//uint8_t LoRaAddress[] = {0x42, 0x00};
uint16_t LoRa1 = ((mac_prefix[4] << 8) | LORA1_PEER); // Use 2 bytes for LoRa addressing instead of previous 3 bytes
uint16_t LoRa2 = ((mac_prefix[4] << 8) | LORA2_PEER);
//uint16_t LoRaAddress = 0x4200;
uint16_t loraGwAddress = ((selfAddress[4] << 8) | selfAddress[5]); // last 2 bytes of gateway address
uint16_t loraBroadcast = 0xFFFF;
unsigned long receivedLoRaMsg = 0; // Number of total LoRa packets destined for us and of valid size
unsigned long ackOkLoRaMsg = 0; // Number of total LoRa packets with valid CRC
#endif
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
unsigned long last_millis = 0;
unsigned long seconds_since_reset = 0;
char logBuffer[512];
uint16_t logBufferPos = 0; // datatype depends on size of sdBuffer
uint32_t timeLOGBUF = 0;
@ -462,32 +486,139 @@ void mqtt_publish(const char* payload) {
#endif
}
void printLoraPacket(uint8_t* p,int size) {
printf("Printing packet of size %d.",size);
for(int i = 0; i < size; i++ ) {
if(i % 2 == 0) printf("\n%02d: ", i);
printf("%02X ", p[i]);
}
printf("\n");
}
void getLoRa() {
#ifdef USE_LORA
int packetSize = LoRa.parsePacket();
if (packetSize) {
if((packetSize - 6) % sizeof(DataReading) == 0 && packetSize > 0) { // packet size should be 6 bytes plus multiple of size of DataReading
uint8_t packet[packetSize];
uint8_t incLORAMAC[2];
uint16_t packetCRC = 0x0000; // CRC Extracted from received LoRa packet
uint16_t calcCRC = 0x0000; // CRC calculated from received LoRa packet
uint16_t sourceMAC = 0x0000;
uint16_t destMAC = 0x0000;
LoRa.readBytes((uint8_t *)&packet, packetSize);
ln = (packetSize - 5) / sizeof(DataReading);
DBG("Incoming LoRa.");
if (memcmp(&packet, &selfAddress[3], 3) == 0) { //Check if addressed to this device
memcpy(&incLORAMAC, &packet[3], 2); //Split off address portion of packet
memcpy(&theData, &packet[5], packetSize - 5); //Split off data portion of packet
if (memcmp(&incLORAMAC, &LoRa1, 2) == 0) { //Check if it is from a registered sender
ln = (packetSize - 6) / sizeof(DataReading);
destMAC = (packet[0] << 8) | packet[1];
sourceMAC = (packet[2] << 8) | packet[3];
packetCRC = ((packet[packetSize - 2] << 8) | packet[packetSize - 1]);
//DBG("Packet Address: 0x" + String(packet[0],16) + String(packet[1],16) + " Self Address: 0x" + String(selfAddress[4],16) + String(selfAddress[5],16));
if (destMAC == (selfAddress[4] << 8 | selfAddress[5])) { //Check if addressed to this device (2 bytes, bytes 1 and 2)
//printLoraPacket(packet,sizeof(packet));
memcpy(&theData, &packet[4], packetSize - 6); //Split off data portion of packet (N - 6 bytes (6 bytes for headers and CRC))
if(receivedLoRaMsg != 0){ // Avoid divide by 0
DBG("Incoming LoRa. Size: " + String(packetSize) + " Bytes, RSSI: " + String(LoRa.packetRssi()) + "dBi, SNR: " + String(LoRa.packetSnr()) + "dB, PacketCRC: 0x" + String(packetCRC,16) + ", Total LoRa received: " + String(receivedLoRaMsg) + ", CRC Ok Pct " + String((float)ackOkLoRaMsg/receivedLoRaMsg*100) + "%");
}
else {
DBG("Incoming LoRa. Size: " + String(packetSize) + " Bytes, RSSI: " + String(LoRa.packetRssi()) + "dBi, SNR: " + String(LoRa.packetSnr()) + "dB, PacketCRC: 0x" + String(packetCRC,16) + ", Total LoRa received: " + String(receivedLoRaMsg));
}
receivedLoRaMsg++;
// Evaluate CRC
if(packetCRC == 0xFFFF) { // CRC is set that destination does not want ACK so do not send.
DBG("Sensor address 0x" + String(sourceMAC,16) + "(hex) does not want ACK");
ackOkLoRaMsg++;
}
else { // Calculate expected CRC and compare to what is in the packet
for(int i = 0; i < (packetSize - 2); i++) { // Last 2 bytes of packet are the CRC so do not include them in calculation
//printf("CRC: %02X : %d\n",calcCRC, i);
calcCRC = crc16_update(calcCRC, packet[i]);
}
if(calcCRC == packetCRC) {
SystemPacket ACK = { .cmd = cmd_ack, .param = CRC_OK };
DBG("CRC Match, sending ACK packet to sensor 0x" + String(sourceMAC,16) + "(hex)");
transmitLoRa(&sourceMAC, &ACK, 1); // Send ACK back to source
ackOkLoRaMsg++;
}
else {
SystemPacket NAK = { .cmd = cmd_ack, .param = CRC_BAD };
// Send NAK packet to sensor
DBG("CRC Mismatch! Packet CRC is 0x" + String(packetCRC,16) + ", Calculated CRC is 0x" + String(calcCRC,16) + " Sending NAK packet to sensor 0x" + String(sourceMAC,16) + "(hex)");
transmitLoRa(&sourceMAC, &NAK, 1); // CRC did not match so send NAK to source
newData = event_clear; // do not process data as data may be corrupt
return; // Exit function and do not update newData to send invalid data further on
}
}
if (memcmp(&sourceMAC, &LoRa1, 2) == 0) { //Check if it is from a registered sender
newData = event_lora1;
return;
}
if (memcmp(&incLORAMAC, &LoRa2, 2) == 0) {
if (memcmp(&sourceMAC, &LoRa2, 2) == 0) {
newData = event_lora2;
return;
}
newData = event_lorag;
}
else {
DBG("Incoming LoRa packet of " + String(packetSize) + " bytes received from address 0x" + String(sourceMAC,16) + " destined for node address 0x" + String(destMAC,16));
}
}
else {
if(packetSize != 0) {
DBG("Incoming LoRa packet of " + String(packetSize) + " not processed.");
}
}
#endif
}
#ifdef USE_LORA
void transmitLoRa(uint16_t* destMac, DataReading * packet, uint8_t len) {
uint16_t calcCRC = 0x0000;
uint8_t pkt[6 + (len * sizeof(DataReading))];
pkt[0] = (*destMac >> 8); // high byte of destination MAC
pkt[1] = (*destMac & 0x00FF); // low byte of destination MAC
pkt[2] = selfAddress[4]; // high byte of source MAC (ourselves)
pkt[3] = selfAddress[5]; // low byte of source MAC
memcpy(&pkt[4], packet, len * sizeof(DataReading)); // copy data portion of packet
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);
calcCRC = crc16_update(calcCRC, pkt[i]);
}
pkt[(len * sizeof(DataReading) + 4)] = (calcCRC >> 8); // Append calculated CRC to the last 2 bytes of the packet
pkt[(len * sizeof(DataReading) + 5)] = (calcCRC & 0x00FF);
DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC,16) + " to LoRa MAC 0x" + String(*destMac,16));
//printLoraPacket(pkt,sizeof(pkt));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
}
#endif
#ifdef USE_LORA
void transmitLoRa(uint16_t* destMac, SystemPacket * packet, uint8_t len) {
uint16_t calcCRC = 0x0000;
uint8_t pkt[6 + (len * sizeof(SystemPacket))];
pkt[0] = (*destMac >> 8); // high byte of destination MAC
pkt[1] = (*destMac & 0x00FF); // low byte of destination MAC
pkt[2] = selfAddress[4]; // high byte of source MAC (ourselves)
pkt[3] = selfAddress[5]; // low byte of source MAC
memcpy(&pkt[4], packet, len * sizeof(SystemPacket)); // copy data portion of packet
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);
calcCRC = crc16_update(calcCRC, pkt[i]);
}
pkt[(len * sizeof(SystemPacket) + 4)] = (calcCRC >> 8); // Append calculated CRC to the last 2 bytes of the packet
pkt[(len * sizeof(SystemPacket) + 5)] = (calcCRC & 0x00FF);
DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC,16) + " to LoRa MAC 0x" + String(*destMac,16));
//printLoraPacket(pkt,sizeof(pkt));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
}
#endif
void sendESPNOW(uint8_t address) {
DBG("Sending ESP-NOW.");
uint8_t NEWPEER[] = {MAC_PREFIX, address};
@ -673,20 +804,6 @@ void releaseESPNOW(uint8_t interface) {
}
}
#ifdef USE_LORA
void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
DBG("Transmitting LoRa.");
uint8_t pkt[5 + (len * sizeof(DataReading))];
memcpy(&pkt, mac, 3);
memcpy(&pkt[3], &selfAddress[4], 2);
memcpy(&pkt[5], packet, len * sizeof(DataReading));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
}
#endif
void releaseLoRa(uint8_t interface) {
#ifdef USE_LORA
DBG("Releasing LoRa.");
@ -699,12 +816,12 @@ void releaseLoRa(uint8_t interface) {
for (int i = 0; i < lenLORAG; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(broadcast_mac, thePacket, j);
transmitLoRa(&loraBroadcast, thePacket, j);
}
thePacket[j] = LORAGbuffer[i];
j++;
}
transmitLoRa(broadcast_mac, thePacket, j);
transmitLoRa(&loraBroadcast, thePacket, j);
lenLORAG = 0;
break;
@ -716,12 +833,12 @@ void releaseLoRa(uint8_t interface) {
for (int i = 0; i < lenLORA1; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(LoRa1, thePacket, j);
transmitLoRa(&LoRa1, thePacket, j);
}
thePacket[j] = LORA1buffer[i];
j++;
}
transmitLoRa(LoRa1, thePacket, j);
transmitLoRa(&LoRa1, thePacket, j);
lenLORA1 = 0;
break;
}
@ -732,18 +849,18 @@ void releaseLoRa(uint8_t interface) {
for (int i = 0; i < lenLORA2; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(LoRa2, thePacket, j);
transmitLoRa(&LoRa2, thePacket, j);
}
thePacket[j] = LORA2buffer[i];
j++;
}
transmitLoRa(LoRa2, thePacket, j);
transmitLoRa(&LoRa2, thePacket, j);
lenLORA2 = 0;
break;
}
}
#endif //USE_LORA
#endif
}
void releaseSerial() {
@ -883,4 +1000,32 @@ void begin_FS() {
#endif
}
#endif //__FDRS_FUNCTIONS_H__
// CRC16 from https://github.com/4-20ma/ModbusMaster/blob/3a05ff87677a9bdd8e027d6906dc05ca15ca8ade/src/util/crc16.h#L71
/** @ingroup util_crc16
Processor-independent CRC-16 calculation.
Polynomial: x^16 + x^15 + x^2 + 1 (0xA001)<br>
Initial value: 0xFFFF
This CRC is normally used in disk-drive controllers.
@param uint16_t crc (0x0000..0xFFFF)
@param uint8_t a (0x00..0xFF)
@return calculated CRC (0x0000..0xFFFF)
*/
static uint16_t crc16_update(uint16_t crc, uint8_t a)
{
int i;
crc ^= a;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001;
else
crc = (crc >> 1);
}
return crc;
}
#endif //__FDRS_FUNCTIONS_H__