added LoRa controller node capability

pull/136/head
Timm Bogner 1 year ago
parent 072d3362a0
commit 5b701228ff

@ -7,6 +7,7 @@
#include <esp_wifi.h>
#endif
#define PEER_TIMEOUT 300000
FDRSPeer peer_list[16];
const uint8_t espnow_size = 250 / sizeof(DataReading);
@ -133,7 +134,7 @@ int find_espnow_peer()
// Returns the index of the peer list array element that contains the provided MAC address, -1 if not found
int getFDRSPeer(uint8_t *mac)
{
DBG("Getting peer #");
//DBG("Getting peer #");
for (int i = 0; i < 16; i++)
{
@ -154,7 +155,7 @@ void add_espnow_peer()
int peer_num = getFDRSPeer(&incMAC[0]);
if (peer_num == -1) // if the device isn't registered
{
DBG("Device not yet registered, adding to peer list");
//DBG("Device not yet registered, adding to peer list");
int open_peer = find_espnow_peer(); // find open spot in peer_list
memcpy(&peer_list[open_peer].mac, &incMAC, 6); // save MAC to open spot
peer_list[open_peer].last_seen = millis();

@ -434,8 +434,8 @@ crcResult getLoRa()
}
else
{
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));
// 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));
return CRC_NULL;
}
}
@ -443,10 +443,10 @@ crcResult getLoRa()
{
if (packetSize != 0)
{
DBG("Incoming LoRa packet of " + String(packetSize) + "bytes not processed.");
// uint8_t packet[packetSize];
// radio.readData((uint8_t *)&packet, packetSize);
// printLoraPacket(packet,sizeof(packet));
// DBG("Incoming LoRa packet of " + String(packetSize) + "bytes not processed.");
// uint8_t packet[packetSize];
// radio.readData((uint8_t *)&packet, packetSize);
// printLoraPacket(packet,sizeof(packet));
return CRC_NULL;
}
}
@ -477,13 +477,16 @@ void sendLoRaNbr(uint8_t interface)
switch (interface)
{
case 1:
{
for (int i = 0; i < ln; i++)
{
LORA1Buffer.buffer[LORA1Buffer.len + i] = theData[i];
}
LORA1Buffer.len += ln;
break;
}
case 2:
{
for (int i = 0; i < ln; i++)
{
LORA2Buffer.buffer[LORA2Buffer.len + i] = theData[i];
@ -491,6 +494,7 @@ void sendLoRaNbr(uint8_t interface)
LORA2Buffer.len += ln;
break;
}
}
#endif // USE_LORA
}
#ifdef USE_LORA

@ -24,7 +24,6 @@
#define GLOBAL_LORA_BANDWIDTH 125.0 // LoRa link bandwidth in kHz. Allowed values are 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250 and 500 kHz.
#define GLOBAL_LORA_CR 5 // LoRa link coding rate denominator. Allowed values range from 5 to 8.
#define GLOBAL_LORA_SYNCWORD 0x12 // LoRa sync word. Can be used to distinguish different networks. Note that 0x34 is reserved for LoRaWAN.
#define GLOBAL_LORA_INTERVAL 10000 // Interval between LoRa buffer releases. Must be longer than transmission time-on-air.
#define GLOBAL_LORA_INTERVAL 5000 // Interval between LoRa buffer releases. Must be longer than transmission time-on-air.
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems.
#define PEER_TIMEOUT 300000

@ -2,7 +2,7 @@
//
// "fdrs_node.h"
//
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
// Developed by Timm Bogner (timmbogner@gmail.com) in Urbana, Illinois, USA.
//
#include <fdrs_datatypes.h>
#include <fdrs_globals.h>
@ -45,6 +45,10 @@ static uint16_t crc16_update(uint16_t crc, uint8_t a)
// #include "fdrs_checkConfig.h"
#endif
SystemPacket theCmd;
DataReading theData[256];
uint8_t ln;
bool newData;
uint8_t gatewayAddress[] = {MAC_PREFIX, GTWY_MAC};
const uint16_t espnow_size = 250 / sizeof(DataReading);
@ -133,6 +137,30 @@ void beginFDRS()
#endif // DEBUG_CONFIG
}
void handleIncoming()
{
if (newData)
{
newData = false;
for (int i = 0; i < ln; i++)
{ // Cycle through array of incoming DataReadings for any we are subbed to
for (int j = 0; j < 255; j++)
{ // Cycle through subscriptions for active entries
if (active_subs[j])
{
if (theData[i].id == subscription_list[j])
{
(*callback_ptr)(theData[i]);
}
}
}
}
}
}
bool sendFDRS()
{
DBG("Sending FDRS Packet!");
@ -206,13 +234,16 @@ void sleepFDRS(int sleep_time)
void loopFDRS()
{
if (is_added)
{
if ((millis() - last_refresh) >= gtwy_timeout)
{
last_refresh = millis();
}
}
handleLoRa();
handleIncoming();
// // TO-DO:
// if (is_added)
// {
// if ((millis() - last_refresh) >= gtwy_timeout)
// {
// last_refresh = millis();
// }
// }
}
bool subscribeFDRS(uint16_t sub_id)
@ -221,7 +252,7 @@ bool subscribeFDRS(uint16_t sub_id)
{
if ((subscription_list[i] == sub_id) && (active_subs[i]))
{
DBG("You're already subscribed to that!");
DBG("You're already subscribed to ID " + String(sub_id));
return true;
}
}
@ -229,7 +260,7 @@ bool subscribeFDRS(uint16_t sub_id)
{
if (!active_subs[i])
{
DBG("Adding subscription at position " + String(i));
DBG("Subscribing to DataReading ID " + String(sub_id));
subscription_list[i] = sub_id;
active_subs[i] = true;
return true;
@ -244,7 +275,7 @@ bool unsubscribeFDRS(uint16_t sub_id)
{
if ((subscription_list[i] == sub_id) && (active_subs[i]))
{
DBG("Removing subscription.");
DBG("Removing subscription to ID " + String(sub_id));
active_subs[i] = false;
return true;
}
@ -253,11 +284,36 @@ bool unsubscribeFDRS(uint16_t sub_id)
return false;
}
bool addFDRS(int timeout, void (*new_cb_ptr)(DataReading))
bool addFDRS(void (*new_cb_ptr)(DataReading))
{
callback_ptr = new_cb_ptr;
#ifdef USE_ESPNOW
SystemPacket sys_packet = {.cmd = cmd_add, .param = 0};
esp_now_send(gatewayAddress, (uint8_t *)&sys_packet, sizeof(SystemPacket));
DBG("ESP-NOW peer registration request submitted to " + String(gatewayAddress[5]));
uint32_t add_start = millis();
is_added = false;
while ((millis() - add_start) <= 1000) // 1000ms timeout
{
yield();
if (is_added)
{
DBG("Registration accepted. Timeout: " + String(gtwy_timeout));
last_refresh = millis();
return true;
}
}
DBG("No gateways accepted the request");
return false;
#endif // USE_ESPNOW
return true;
}
bool addFDRS(int timeout, void (*new_cb_ptr)(DataReading))
{
callback_ptr = new_cb_ptr;
#ifdef USE_ESPNOW
SystemPacket sys_packet = {.cmd = cmd_add, .param = 0};
esp_now_send(gatewayAddress, (uint8_t *)&sys_packet, sizeof(SystemPacket));
DBG("ESP-NOW peer registration request submitted to " + String(gatewayAddress[5]));
uint32_t add_start = millis();
@ -274,7 +330,8 @@ bool addFDRS(int timeout, void (*new_cb_ptr)(DataReading))
}
DBG("No gateways accepted the request");
return false;
#endif
#endif // USE_ESPNOW
return true;
}
uint32_t pingFDRS(int timeout)

@ -42,9 +42,6 @@ void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
{
#endif
memcpy(&incMAC, mac, sizeof(incMAC));
if (len < sizeof(DataReading))
{
SystemPacket command;
@ -62,42 +59,9 @@ void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
}
else
{
memcpy(&incData, incomingData, len);
int pkt_readings = len / sizeof(DataReading);
for (int i = 0; i <= pkt_readings; i++)
{ // Cycle through array of incoming DataReadings for any addressed to this device
for (int j = 0; j < 255; j++)
{ // Cycle through subscriptions for active entries
if (active_subs[j])
{
if (incData[i].id == subscription_list[j])
{
(*callback_ptr)(incData[i]);
}
}
}
}
memcpy(&theData, incomingData, len);
ln = len / sizeof(DataReading);
newData = true;
}
}
#endif // USE_ESPNOW
bool seekFDRS(int timeout)
{
SystemPacket sys_packet = {.cmd = cmd_ping, .param = 0};
#ifdef USE_ESPNOW
esp_now_send(broadcast_mac, (uint8_t *)&sys_packet, sizeof(SystemPacket));
DBG("Seeking nearby gateways");
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("Responded:" + String(incMAC[5]));
return true;
}
}
return false;
#endif
}

@ -83,9 +83,16 @@ RADIOLIB_MODULE radio = new Module(LORA_SS, LORA_DIO, LORA_RST, -1);
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
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
uint16_t LoRaAddress = ((UniqueID8[6] << 8) | UniqueID8[7]);
unsigned long transmitLoRaMsgwAck = 0; // Number of total LoRa packets destined for us and of valid size
unsigned long msgOkLoRa = 0; // Number of total LoRa packets with valid CRC
void printLoraPacket(uint8_t *p, int size);
uint16_t gtwyAddress = ((gatewayAddress[4] << 8) | GTWY_MAC);
crcResult getLoRa();
@ -118,7 +125,10 @@ void handleLoRa()
else
{ // the previous operation was reception
returnCRC = getLoRa();
radio.startReceive(); // fixes the problem?
if (!transmitFlag) // return to listen if no transmission was begun
{
radio.startReceive();
}
enableInterrupt = true;
}
}
@ -150,6 +160,7 @@ void begin_lora()
DBG("LoRa Initialized. Frequency: " + String(FDRS_LORA_FREQUENCY) + " Bandwidth: " + String(FDRS_LORA_BANDWIDTH) + " SF: " + String(FDRS_LORA_SF) + " CR: " + String(FDRS_LORA_CR) + " SyncWord: " + String(FDRS_LORA_SYNCWORD) + " Tx Power: " + String(FDRS_LORA_TXPWR) + "dBm");
radio.setDio0Action(setFlag);
radio.setCRC(false);
LoRaAddress = ((radio.randomByte() << 8) | radio.randomByte());
state = radio.startReceive(); // start listening for LoRa packets
if (state == RADIOLIB_ERR_NONE)
{
@ -287,105 +298,143 @@ void transmitLoRa(uint16_t *destMAC, SystemPacket *packet, uint8_t len)
// 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
// Return type is crcResult struct - CRC_OK, CRC_BAD, CRC_NULL. CRC_NULL used for non-ack data
crcResult getLoRa()
{
#ifdef USE_LORA
int packetSize = radio.getPacketLength();
if ((packetSize - 6) % sizeof(SystemPacket) == 0 && packetSize > 0)
{ // packet size should be 6 bytes plus multiple of size of SystemPacket
if ((((packetSize - 6) % sizeof(DataReading) == 0) || ((packetSize - 6) % sizeof(SystemPacket) == 0)) && packetSize > 0)
{ // packet size should be 6 bytes plus multiple of size of DataReading
uint8_t packet[packetSize];
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;
unsigned int ln = (packetSize - 6) / sizeof(SystemPacket);
SystemPacket receiveData[ln];
radio.readData((uint8_t *)&packet, packetSize);
destMAC = (packet[0] << 8) | packet[1];
sourceMAC = (packet[2] << 8) | packet[3];
packetCRC = ((packet[packetSize - 2] << 8) | packet[packetSize - 1]);
DBG("Incoming LoRa. Size: " + String(packetSize) + " Bytes, RSSI: " + String(radio.getRSSI()) + "dBm, SNR: " + String(radio.getSNR()) + "dB, PacketCRC: 0x" + String(packetCRC, HEX));
if (destMAC == LoRaAddress)
{ // The packet is for us so let's process it
// DBG("Packet Address: 0x" + String(packet[0], HEX) + String(packet[1], HEX) + " Self Address: 0x" + String(selfAddress[4], HEX) + String(selfAddress[5], HEX));
if ((destMAC == LoRaAddress) || (destMAC == 0xFFFF))
{ // Check if addressed to this device or broadcast
// printLoraPacket(packet,sizeof(packet));
if (receivedLoRaMsg != 0)
{ // Avoid divide by 0
DBG("Incoming LoRa. Size: " + String(packetSize) + " Bytes, RSSI: " + String(radio.getRSSI()) + "dBm, SNR: " + String(radio.getSNR()) + "dB, PacketCRC: 0x" + String(packetCRC, HEX) + ", Total LoRa received: " + String(receivedLoRaMsg) + ", CRC Ok Pct " + String((float)ackOkLoRaMsg / receivedLoRaMsg * 100) + "%");
}
else
{
DBG("Incoming LoRa. Size: " + String(packetSize) + " Bytes, RSSI: " + String(radio.getRSSI()) + "dBm, SNR: " + String(radio.getSNR()) + "dB, PacketCRC: 0x" + String(packetCRC, HEX) + ", Total LoRa received: " + String(receivedLoRaMsg));
}
receivedLoRaMsg++;
// Evaluate CRC
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)
{
memcpy(receiveData, &packet[4], packetSize - 6); // Split off data portion of packet (N bytes)
if (ln == 1 && receiveData[0].cmd == cmd_ack)
if ((packetSize - 6) % sizeof(DataReading) == 0)
{ // DataReading type packet
if (calcCRC == packetCRC)
{
DBG("ACK Received - CRC Match");
SystemPacket ACK = {.cmd = cmd_ack, .param = CRC_OK};
DBG("CRC Match, sending ACK packet to node 0x" + String(sourceMAC, HEX) + "(hex)");
transmitLoRa(&sourceMAC, &ACK, 1); // Send ACK back to source
}
else if (ln == 1 && receiveData[0].cmd == cmd_ping)
{ // We have received a ping request or reply??
if (receiveData[0].param == 1)
{ // This is a reply to our ping request
is_ping = true;
DBG("We have received a ping reply via LoRa from address 0x" + String(sourceMAC, HEX));
}
else if (receiveData[0].param == 0)
{
DBG("We have received a ping request from 0x" + String(sourceMAC, HEX) + ", Replying.");
SystemPacket pingReply = {.cmd = cmd_ping, .param = 1};
transmitLoRa(&sourceMAC, &pingReply, 1);
}
else if (packetCRC == crc16_update(calcCRC, 0xA1))
{ // Sender does not want ACK and CRC is valid
DBG("Node address 0x" + String(sourceMAC, 16) + "(hex) does not want ACK");
}
else
{ // data we have received is not yet programmed. How we handle is future enhancement.
DBG("Received some LoRa SystemPacket data that is not yet handled. To be handled in future enhancement.");
DBG("ln: " + String(ln) + "data type: " + String(receiveData[0].cmd));
{
SystemPacket NAK = {.cmd = cmd_ack, .param = CRC_BAD};
// Send NAK packet to sensor
DBG("CRC Mismatch! Packet CRC is 0x" + String(packetCRC, HEX) + ", Calculated CRC is 0x" + String(calcCRC, HEX) + " Sending NAK packet to node 0x" + String(sourceMAC, HEX) + "(hex)");
transmitLoRa(&sourceMAC, &NAK, 1); // CRC did not match so send NAK to source
return CRC_BAD; // Exit function and do not update newData to send invalid data further on
}
memcpy(&theData, &packet[4], packetSize - 6); // Split off data portion of packet (N - 6 bytes (6 bytes for headers and CRC))
ln = (packetSize - 6) / sizeof(DataReading);
newData = true;
ackOkLoRaMsg++;
return CRC_OK;
}
else if (packetCRC == crc16_update(calcCRC, 0xA1))
{ // Sender does not want ACK and CRC is valid
memcpy(receiveData, &packet[4], packetSize - 6); // Split off data portion of packet (N bytes)
if (ln == 1 && receiveData[0].cmd == cmd_ack)
else if ((packetSize - 6) == sizeof(SystemPacket))
{
uint ln = (packetSize - 6) / sizeof(SystemPacket);
SystemPacket receiveData[ln];
if (calcCRC == packetCRC)
{
DBG("ACK Received - CRC Match");
}
else if (ln == 1 && receiveData[0].cmd == cmd_ping)
{ // We have received a ping request or reply??
if (receiveData[0].param == 1)
{ // This is a reply to our ping request
is_ping = true;
DBG("We have received a ping reply via LoRa from address 0x" + String(sourceMAC, HEX));
memcpy(receiveData, &packet[4], packetSize - 6); // Split off data portion of packet (N bytes)
if (ln == 1 && receiveData[0].cmd == cmd_ack)
{
DBG("ACK Received - CRC Match");
}
else if (ln == 1 && receiveData[0].cmd == cmd_ping)
{ // We have received a ping request or reply??
if (receiveData[0].param == 1)
{ // This is a reply to our ping request
is_ping = true;
DBG("We have received a ping reply via LoRa from address " + String(sourceMAC, HEX));
}
else if (receiveData[0].param == 0)
{
DBG("We have received a ping request from 0x" + String(sourceMAC, HEX) + ", Replying.");
SystemPacket pingReply = {.cmd = cmd_ping, .param = 1};
transmitLoRa(&sourceMAC, &pingReply, 1);
}
}
else
{ // data we have received is not yet programmed. How we handle is future enhancement.
DBG("Received some LoRa SystemPacket data that is not yet handled. To be handled in future enhancement.");
DBG("ln: " + String(ln) + "data type: " + String(receiveData[0].cmd));
}
else if (receiveData[0].param == 0)
ackOkLoRaMsg++;
return CRC_OK;
}
else if (packetCRC == crc16_update(calcCRC, 0xA1))
{ // Sender does not want ACK and CRC is valid
memcpy(receiveData, &packet[4], packetSize - 6); // Split off data portion of packet (N bytes)
if (ln == 1 && receiveData[0].cmd == cmd_ack)
{
DBG("We have received a ping request from 0x" + String(sourceMAC, HEX) + ", Replying.");
SystemPacket pingReply = {.cmd = cmd_ping, .param = 1};
transmitLoRa(&sourceMAC, &pingReply, 1);
DBG("ACK Received - CRC Match");
}
else if (ln == 1 && receiveData[0].cmd == cmd_ping)
{ // We have received a ping request or reply??
if (receiveData[0].param == 1)
{ // This is a reply to our ping request
is_ping = true;
DBG("We have received a ping reply via LoRa from address " + String(sourceMAC, HEX));
}
else if (receiveData[0].param == 0)
{
DBG("We have received a ping request from 0x" + String(sourceMAC, HEX) + ", Replying.");
SystemPacket pingReply = {.cmd = cmd_ping, .param = 1};
transmitLoRa(&sourceMAC, &pingReply, 1);
}
}
else
{ // data we have received is not yet programmed. How we handle is future enhancement.
DBG("Received some LoRa SystemPacket data that is not yet handled. To be handled in future enhancement.");
DBG("ln: " + String(ln) + "data type: " + String(receiveData[0].cmd));
}
ackOkLoRaMsg++;
return CRC_OK;
}
else
{ // data we have received is not yet programmed. How we handle is future enhancement.
DBG("Received some LoRa SystemPacket data that is not yet handled. To be handled in future enhancement.");
DBG("ln: " + String(ln) + "data type: " + String(receiveData[0].cmd));
{
DBG("ACK Received CRC Mismatch! Packet CRC is 0x" + String(packetCRC, HEX) + ", Calculated CRC is 0x" + String(calcCRC, HEX));
return CRC_BAD;
}
return CRC_OK;
}
else
{
DBG("ACK Received CRC Mismatch! Packet CRC is 0x" + String(packetCRC, HEX) + ", Calculated CRC is 0x" + String(calcCRC, HEX));
return CRC_BAD;
}
}
else if ((packetSize - 6) % sizeof(DataReading) == 0 && packetSize > 0)
{ // packet size should be 6 bytes plus multiple of size of DataReading)
DBG("Incoming LoRa packet of " + String(packetSize) + " bytes received, with DataReading data to be processed.");
return CRC_NULL;
}
else
{
DBG("Incoming LoRa packet of " + String(packetSize) + " bytes received, not destined to our address.");
//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));
return CRC_NULL;
}
}
@ -393,16 +442,16 @@ crcResult getLoRa()
{
if (packetSize != 0)
{
DBG("Incoming LoRa packet of " + String(packetSize) + " bytes received");
//DBG("Incoming LoRa packet of " + String(packetSize) + "bytes not processed.");
// uint8_t packet[packetSize];
// radio.readData((uint8_t *)&packet, packetSize);
// printLoraPacket(packet,sizeof(packet));
return CRC_NULL;
}
}
return CRC_NULL;
#else
return CRC_NULL;
#endif // USE_LORA
return CRC_NULL;
}
void printLoraPacket(uint8_t *p, int size)
{
printf("Printing packet of size %d.", size);

Loading…
Cancel
Save