Merge pull request #98 from timmbogner/two-way-comms

Two-way communications - take 2
This commit is contained in:
Timm Bogner 2022-08-31 16:31:09 -05:00 committed by GitHub
commit 40f415f405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 462 additions and 234 deletions

View File

@ -13,7 +13,7 @@ Install the libraries that you need:
- [ArduinoJson](https://arduinojson.org/) (mandatory)
- [LoRa library](https://github.com/sandeepmistry/arduino-LoRa) by sandeepmistry (required if using LoRa)
- [PubSubClient](https://github.com/knolleary/pubsubclient/) (required for MQTT Gateways)
- [ArduinoUniqueID](https://github.com/ricaun/ArduinoUniqueID) (required for LoRa sensors/controllers)
### [Sensors](/extras/Sensor.md)
Sensors gather data and send it to a gateway via ESP-NOW or LoRa.

View File

@ -86,6 +86,7 @@ void setup() {
}
void loop() {
handleCommands();
#ifdef ESPNOWG_DELAY
if ((millis() - timeESPNOWG) >= ESPNOWG_DELAY) {
timeESPNOWG = millis();

View File

@ -78,6 +78,8 @@
//#define LORA1_DELAY 1000
//#define LORA2_DELAY 1000
#define PEER_TIMEOUT 300000
// FastLED -- Not yet fully implemented
//#define USE_LED
#define LED_PIN 32
@ -96,4 +98,4 @@
// MQTT Topics
#define TOPIC_DATA "fdrs/data"
#define TOPIC_STATUS "fdrs/status"
#define TOPIC_COMMAND "fdrs/command"
#define TOPIC_COMMAND "fdrs/command"

View File

@ -48,12 +48,14 @@ void setup() {
DBG("Address:" + String (UNIT_MAC, HEX));
#ifdef DEBUG_NODE_CONFIG
#ifdef ESP32
// find out the reset reason
esp_reset_reason_t resetReason;
resetReason = esp_reset_reason();
if (resetReason != ESP_RST_DEEPSLEEP) {
checkConfig();
}
#endif //ESP32
#endif //DEBUG_NODE_CONFIG
#ifdef USE_LED
@ -95,8 +97,8 @@ void setup() {
client.publish(TOPIC_STATUS, "FDRS initialized");
#endif
}
void loop() {
handleCommands();
#ifdef ESPNOWG_DELAY
if ((millis() - timeESPNOWG) >= ESPNOWG_DELAY) {
timeESPNOWG = millis();

View File

@ -13,7 +13,7 @@
// bufferLoRa(interface), bufferESPNOW(interface), bufferSerial(), and bufferMQTT().
#define ESPNOWG_ACT sendSerial();
#define SERIAL_ACT sendESPNOW(0x02); bufferLoRa(1);
#define SERIAL_ACT sendESPNOW(0x02); bufferLoRa(1); sendESPNOWpeers();
#define MQTT_ACT
#define LORAG_ACT sendSerial();
@ -21,7 +21,7 @@
// Warning: ESP-NOW and WiFi should not be used simultaneously.
#define USE_ESPNOW
#define USE_LORA
//#define USE_LORA
//#define USE_WIFI //Used only for MQTT gateway
// Peer addresses
@ -74,9 +74,11 @@
//#define SERIAL_DELAY 0
//#define MQTT_DELAY 0
//#define LORAG_DELAY 1000
#define LORA1_DELAY 500
//#define LORA1_DELAY 500
//#define LORA2_DELAY 1000
#define PEER_TIMEOUT 300000
// FastLED -- Not yet fully implemented
//#define USE_LED
#define LED_PIN 32

View File

@ -86,6 +86,7 @@ void setup() {
}
void loop() {
handleCommands();
#ifdef ESPNOWG_DELAY
if ((millis() - timeESPNOWG) >= ESPNOWG_DELAY) {
timeESPNOWG = millis();

View File

@ -78,6 +78,9 @@
//#define LORA1_DELAY 1000
//#define LORA2_DELAY 1000
#define PEER_TIMEOUT 300000
// FastLED -- Not yet fully implemented
//#define USE_LED
#define LED_PIN 32

View File

@ -86,6 +86,7 @@ void setup() {
}
void loop() {
handleCommands();
#ifdef ESPNOWG_DELAY
if ((millis() - timeESPNOWG) >= ESPNOWG_DELAY) {
timeESPNOWG = millis();

View File

@ -77,6 +77,8 @@
#define LORA1_DELAY 5000
//#define LORA2_DELAY 1000
#define PEER_TIMEOUT 300000
// FastLED -- Not yet fully implemented
//#define USE_LED
#define LED_PIN 32

View File

@ -0,0 +1,31 @@
// FARM DATA RELAY SYSTEM
//
// ESP-NOW Sensor Example
//
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
// An example of how to send data via ESP-NOW using FDRS.
//
#include "fdrs_sensor_config.h"
#include <fdrs_node.h>
void fdrs_recv_cb(DataReading theData) {
DBG("ID: " + String(theData.id));
DBG("Type: " + String(theData.t));
DBG("Data: " + String(theData.d));
}
void setup() {
beginFDRS();
//pingFDRS(1000);
addFDRS(1000, fdrs_recv_cb);
subscribeFDRS(READING_ID);
}
void loop() {
// data1 = readHum();
// loadFDRS(data1, HUMIDITY_T);
// data2 = readTemp();
// loadFDRS(data2, TEMP_T);
// sendFDRS();
// sleepFDRS(10); //Sleep time in seconds
}

View File

@ -0,0 +1,33 @@
// FARM DATA RELAY SYSTEM
//
// Sensor Configuration
#include <fdrs_globals.h>
#define READING_ID 1 //Unique ID for this sensor
#define GTWY_MAC 0x01 //Address of the nearest gateway
#define USE_ESPNOW
//#define USE_LORA
//#define DEEP_SLEEP
//#define POWER_CTRL 14
#define FDRS_DEBUG
//SPI Configuration -- Needed only on chipsets with multiple SPI interfaces (ESP32)
#define SPI_SCK 5
#define SPI_MISO 19
#define SPI_MOSI 27
//LoRa Configuration
#define LORA_SS 18
#define LORA_RST 14
#define LORA_DIO0 26
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
//#define LORA_BAND 915E6 // LoRa Frequency Band
//#define LORA_SF 7 // LoRa Spreading Factor
//#define LORA_TXPWR 17 // LoRa TX power in dBm (+2dBm - +20dBm), default is +17dBm
//#define LORA_ACK // Request LoRa acknowledgment. Increases battery usage.
//#define LORA_ACK_TIMEOUT 400 // LoRa ACK timeout in ms. (Minimum = 200)
//#define LORA_RETRIES 2 // LoRa ACK retries [0 - 3]

View File

@ -7,7 +7,7 @@
//
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#include <fdrs_node.h>
float data1;
float data2;

View File

@ -7,6 +7,12 @@
#define READING_ID 1 //Unique ID for this sensor
#define GTWY_MAC 0x01 //Address of the nearest gateway
#define CONTROL_1 133 //Address for controller 1
#define CONTROL_2 134 //Address for controller 2
#define CONTROL_3 135 //Address for controller 3
#define CONTROL_4 136 //Address for controller 4
#define USE_ESPNOW
//#define USE_LORA
#define DEEP_SLEEP

View File

@ -6,7 +6,7 @@
//
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#include <fdrs_node.h>
void setup() {
beginFDRS();

View File

@ -7,7 +7,7 @@
//
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#include <fdrs_node.h>
float data1;
float data2;

View File

@ -6,7 +6,7 @@
#include "fdrs_sensor_config.h"
#include <Adafruit_AHTX0.h>
#include <fdrs_sensor.h>
#include <fdrs_node.h>
Adafruit_AHTX0 aht;

View File

@ -6,7 +6,7 @@
#include "fdrs_sensor_config.h"
#include <Adafruit_BME280.h>
#include <fdrs_sensor.h>
#include <fdrs_node.h>
Adafruit_BME280 bme;

View File

@ -7,7 +7,7 @@
#include "fdrs_sensor_config.h"
#include <Adafruit_BMP280.h>
#include <fdrs_sensor.h>
#include <fdrs_node.h>
Adafruit_BMP280 bmp;

View File

@ -4,7 +4,7 @@
// Modified by Timm Bogner for Farm Data Relay System -- Untested because I don't have a DHT sensor onhand.
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#include <fdrs_node.h>
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor

View File

@ -6,7 +6,7 @@
#define ONE_WIRE_BUS 13 //Pin that the DS18B20 is connected to
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#include <fdrs_node.h>
#include <OneWire.h>
#include <DallasTemperature.h>

View File

@ -7,7 +7,7 @@
//
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#include <fdrs_node.h>
#define SERIAL1_RX 34 // TX pin of GPS sensor
#define SERIAL1_TX 12 // RX pin of GPS sensor

View File

@ -15,7 +15,7 @@
#define DS18B20_PIN 21
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#include <fdrs_node.h>
#include <BH1750.h>
#include <Adafruit_BME280.h>

View File

@ -7,7 +7,7 @@
#include "fdrs_sensor_config.h"
#include <Adafruit_AHT10.h>
#include <fdrs_sensor.h>
#include <fdrs_node.h>
Adafruit_AHT10 aht;

View File

@ -7,7 +7,7 @@
#define REED_PIN 2
#include "fdrs_sensor_config.h"
#include <fdrs_sensor.h>
#include <fdrs_node.h>
unsigned int theCount = 0;
unsigned long lastTrigger = 0;

View File

@ -1,4 +1,3 @@
#ifdef USE_ESPNOW
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
#if defined(ESP8266)
@ -10,6 +9,7 @@ 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)) {
DBG("ESP-NOW System Packet");
memcpy(&theCmd, incomingData, sizeof(theCmd));
@ -17,7 +17,6 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
return;
}
memcpy(&theData, incomingData, sizeof(theData));
memcpy(&incMAC, mac, sizeof(incMAC));
DBG("Incoming ESP-NOW.");
ln = len / sizeof(DataReading);
if (memcmp(&incMAC, &ESPNOW1, 6) == 0) {
@ -91,6 +90,23 @@ void begin_espnow() {
#endif //USE_ESPNOW
}
void sendESPNOWpeers() {
#ifdef USE_ESPNOW
DBG("Sending to ESP-NOW peers.");
DataReading thePacket[ln];
int j = 0;
for (int i = 0; i < ln; i++) {
if ( j > espnow_size) {
j = 0;
esp_now_send(0, (uint8_t *) &thePacket, sizeof(thePacket));
}
thePacket[j] = theData[i];
j++;
}
esp_now_send(0, (uint8_t *) &thePacket, j * sizeof(DataReading));
#endif // USE_ESPNOW
}
void sendESPNOW(uint8_t address) {
#ifdef USE_ESPNOW

View File

@ -146,7 +146,11 @@ enum {
#ifdef DEBUG_NODE_CONFIG
#include "fdrs_checkConfig.h"
#endif
typedef struct FDRSPeer {
uint8_t mac[6];
uint32_t last_seen = 0;
} FDRSPeer;
typedef struct __attribute__((packed)) DataReading {
float d;
uint16_t id;
@ -159,6 +163,7 @@ typedef struct __attribute__((packed)) SystemPacket {
uint32_t param;
} SystemPacket;
FDRSPeer peer_list[16];
const uint8_t espnow_size = 250 / sizeof(DataReading);
const uint8_t lora_size = 256 / sizeof(DataReading);
const uint8_t mac_prefix[] = {MAC_PREFIX};
@ -608,35 +613,119 @@ void begin_FS() {
#endif // USE_FS_LOG
}
int getFDRSPeer(uint8_t *mac) { // Returns the index of the array element that contains the provided MAC address
DBG("Getting peer #");
for (int i = 0; i < 16; i++) {
if (memcmp(mac, &peer_list[i].mac, 6) == 0) {
DBG("Peer is entry #" + String(i));
return i;
}
}
//DBG("Couldn't find peer");
return -1;
}
int findOpenPeer() { // Returns an expired entry in peer_list, -1 if full.
//uint8_t zero_addr[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
for (int i = 0; i < 16; i++) {
if (peer_list[i].last_seen == 0){
DBG("Using peer entry " + String(i));
return i;
}
}
for (int i = 0; i < 16; i++) {
if ((millis() - peer_list[i].last_seen) >= PEER_TIMEOUT){
DBG("Recycling peer entry " + String(i));
esp_now_del_peer(peer_list[i].mac);
return i;
}
}
DBG("No open peers");
return -1;
}
int checkPeerExpired() { // Checks whether any entries in the peer_list have expired. Not currently used.
for (int i = 0; i < 16; i++) {
if ((millis() - peer_list[i].last_seen) >= PEER_TIMEOUT) {
esp_now_del_peer(incMAC);
}
return -1;
}
}
void handleCommands() {
switch (theCmd.cmd) {
case cmd_ping:
DBG("Ping back to sender");
SystemPacket sys_packet;
sys_packet.cmd = cmd_ping;
#if defined(ESP32)
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, incMAC, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer");
return;
}
if (!esp_now_is_peer_exist(incMAC)) {
#ifdef ESP8266
esp_now_add_peer(incMAC, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#endif
esp_now_send(incMAC, (uint8_t *) &sys_packet, sizeof(SystemPacket));
#if defined(ESP32)
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, incMAC, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer");
return;
}
#endif
esp_now_send(incMAC, (uint8_t *) &sys_packet, sizeof(SystemPacket));
esp_now_del_peer(incMAC);
} else {
esp_now_send(incMAC, (uint8_t *) &sys_packet, sizeof(SystemPacket));
}
break;
case cmd_add:
DBG("Add sender to peer list (not completed)");
DBG("Device requesting peer registration");
int peer_num = getFDRSPeer(&incMAC[0]);
if (peer_num == -1) { //if the device isn't registered
DBG("Device not yet registered, adding to internal peer list");
int open_peer = findOpenPeer(); // 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();
#if defined(ESP32)
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, incMAC, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer");
return;
}
#endif
#if defined(ESP8266)
esp_now_add_peer(incMAC, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#endif
SystemPacket sys_packet = { .cmd = cmd_add, .param = PEER_TIMEOUT };
esp_now_send(incMAC, (uint8_t *) &sys_packet, sizeof(SystemPacket));
} else {
DBG("Refreshing existing peer registration");
peer_list[peer_num].last_seen = millis();
SystemPacket sys_packet = { .cmd = cmd_add, .param = PEER_TIMEOUT };
esp_now_send(incMAC, (uint8_t *) &sys_packet, sizeof(SystemPacket));
}
break;
}
is_ping = false;
theCmd.cmd = cmd_clear;
theCmd.param = 0;
}
#endif //__FDRS_FUNCTIONS_H__

View File

@ -14,6 +14,7 @@
#include <esp_wifi.h>
#endif
#ifdef USE_LORA
#include <ArduinoUniqueID.h>
#include <LoRa.h>
#endif
@ -27,7 +28,7 @@
#define FDRS_BAND LORA_BAND
#elif defined (GLOBAL_LORA_BAND)
#define FDRS_BAND GLOBAL_LORA_BAND
#else
#else
// ASSERT("NO LORA-BAND defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
#endif //LORA_BAND
@ -36,19 +37,10 @@
#define FDRS_SF LORA_SF
#elif defined (GLOBAL_LORA_SF)
#define FDRS_SF GLOBAL_LORA_SF
#else
#else
// ASSERT("NO LORA-SF defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
#endif //LORA_SF
// select LoRa TXPWR configuration
#if defined(LORA_TXPWR)
#define FDRS_TXPWR LORA_TXPWR
#elif defined (GLOBAL_LORA_TXPWR)
#define FDRS_TXPWR GLOBAL_LORA_TXPWR
#else
// ASSERT("NO LORA-TXPWR defined! Please define in fdrs_globals.h (recommended) or in fdrs_sensor_config.h");
#endif //LORA_TXPWR
#endif //USE_LORA
#ifdef FDRS_DEBUG
@ -60,7 +52,7 @@
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems.
#ifdef DEBUG_NODE_CONFIG
#include "fdrs_checkConfig.h"
//#include "fdrs_checkConfig.h"
#endif
typedef struct __attribute__((packed)) DataReading {
@ -71,11 +63,11 @@ typedef struct __attribute__((packed)) DataReading {
} DataReading;
enum crcResult{
enum crcResult {
CRC_NULL,
CRC_OK,
CRC_BAD,
} returnCRC = CRC_NULL;
} returnCRC;
enum {
cmd_clear,
@ -90,18 +82,27 @@ typedef struct __attribute__((packed)) SystemPacket {
} SystemPacket;
const uint16_t espnow_size = 250 / sizeof(DataReading);
const uint8_t gatewayAddress[] = {MAC_PREFIX, GTWY_MAC};
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t gatewayAddress[] = {MAC_PREFIX, GTWY_MAC};
uint16_t gtwyAddress = ((gatewayAddress[4] << 8) | GTWY_MAC);
const uint16_t LoRaAddress = 0x4200;
const uint16_t sensorAddress = LoRaAddress;
unsigned long transmitLoRaMsgwAck = 0; // Number of total LoRa packets transmitted and we expect ACK in return
unsigned long msgOkLoRa = 0; // Number of total LoRa packets ACKed with valid CRC
#ifdef USE_LORA
uint16_t LoRaAddress = ((UniqueID8[6] << 8) | UniqueID8[7]);
unsigned long transmitLoRaMsg = 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
#endif
uint32_t gtwy_timeout = 0;
uint8_t incMAC[6];
uint32_t wait_time = 0;
DataReading fdrsData[espnow_size];
DataReading incData[espnow_size];
uint8_t data_count = 0;
bool is_ping = false;
bool is_added = false;
uint32_t last_refresh;
void (*callback_ptr)(DataReading);
uint16_t subscription_list[256] = {};
bool active_subs[256] = {};
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
#if defined(ESP8266)
@ -113,24 +114,43 @@ 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;
memcpy(&command, incomingData, sizeof(command));
if (command.cmd == cmd_ping) {
is_ping = true;
return;
switch (command.cmd) {
case cmd_ping:
is_ping = true;
break;
case cmd_add:
is_added = true;
gtwy_timeout = command.param;
break;
}
} 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]);
}
}
}
}
}
}
static uint16_t crc16_update(uint16_t, uint8_t); // function prototype for Arduino compilation purposes
void beginFDRS() {
#ifdef FDRS_DEBUG
Serial.begin(115200);
// find out the reset reason
esp_reset_reason_t resetReason;
resetReason = esp_reset_reason();
// // find out the reset reason
// esp_reset_reason_t resetReason;
// resetReason = esp_reset_reason();
#endif
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
DBG(" Gateway: " + String (GTWY_MAC, HEX));
@ -149,18 +169,28 @@ void beginFDRS() {
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_recv_cb(OnDataRecv);
// Register peers
esp_now_add_peer(gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#elif defined(ESP32)
if (esp_now_init() != ESP_OK) {
DBG("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(OnDataRecv);
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Register first peer
memcpy(peerInfo.peer_addr, broadcast_mac, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer bcast");
return;
}
memcpy(peerInfo.peer_addr, gatewayAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer");
@ -180,95 +210,96 @@ void beginFDRS() {
while (1);
}
LoRa.setSpreadingFactor(FDRS_SF);
LoRa.setTxPower(FDRS_TXPWR);
DBG("LoRa Initialized. Band: " + String(FDRS_BAND) + " SF: " + String(FDRS_SF) + " Tx Power: " + String(FDRS_TXPWR) + " dBm");
DBG(" LoRa Initialized.");
DBG("LoRa Band: " + String(FDRS_BAND));
DBG("LoRa SF : " + String(FDRS_SF));
#endif // USE_LORA
#ifdef DEBUG_NODE_CONFIG
if (resetReason != ESP_RST_DEEPSLEEP) {
checkConfig();
}
// if (resetReason != ESP_RST_DEEPSLEEP) {
//checkConfig();
// }
#endif //DEBUG_NODE_CONFIG
}
// 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
// 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;
}
// USED to get ACKs 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 = LoRa.parsePacket();
if((packetSize - 6) % sizeof(SystemPacket) == 0 && packetSize > 0) { // packet size should be 6 bytes plus multiple of size of SystemPacket
if ((packetSize - 6) % sizeof(SystemPacket) == 0 && packetSize > 0) { // packet size should be 6 bytes plus multiple of size of SystemPacket
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;
uint16_t packetCRC = 0x0000; // CRC Extracted from received LoRa packet
uint16_t calcCRC = 0x0000; // CRC calculated from received LoRa packet
uint ln = (packetSize - 6) / sizeof(SystemPacket);
SystemPacket receiveData[ln];
LoRa.readBytes((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(LoRa.packetRssi()) + "dBm, SNR: " + String(LoRa.packetSnr()) + "dB, PacketCRC: 0x" + String(packetCRC, HEX));
if (destMAC == LoRaAddress) { // The packet is for us so let's process it
DBG("Incoming LoRa. Size: " + String(packetSize) + " Bytes, RSSI: " + String(LoRa.packetRssi()) + "dBi, SNR: " + String(LoRa.packetSnr()) + "dB, PacketCRC: 0x" + String(packetCRC, 16));
if (destMAC == LoRaAddress) {
//printLoraPacket(packet,sizeof(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]);
memcpy(receiveData, &packet[4], packetSize - 6); //Split off data portion of packet (N bytes)
if (ln == 1 && receiveData[0].cmd == cmd_ack) { // We have received an ACK packet
if (packetCRC == 0xFFFF) {
DBG("ACK Received - address 0x" + String(sourceMAC, 16) + "(hex) does not want ACKs");
return CRC_OK;
}
else {
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) {
DBG("ACK Received - CRC Match");
return CRC_OK;
}
else {
DBG("ACK Received CRC Mismatch! Packet CRC is 0x" + String(packetCRC, 16) + ", Calculated CRC is 0x" + String(calcCRC, 16));
return CRC_BAD;
}
}
}
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) {
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));
}
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));
}
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("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));
}
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));
}
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 { // data we have received is not of type ACK_T. How we handle is future enhancement.
DBG("Received some LoRa SystemPacket data that is not of type ACK. To be handled in future enhancement.");
DBG("ln: " + String(ln) + "data type: " + String(receiveData[0].cmd));
return CRC_NULL;
}
}
else if((packetSize - 6) % sizeof(DataReading) == 0 && packetSize > 0) { // packet size should be 6 bytes plus multiple of size of DataReading)
else if ((packetSize - 6) % sizeof(DataReading) == 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;
}
@ -277,16 +308,24 @@ crcResult getLoRa() {
return CRC_NULL;
}
}
else {
if(packetSize != 0) {
else {
if (packetSize != 0) {
DBG("Incoming LoRa packet of " + String(packetSize) + " bytes received");
return CRC_NULL;
}
}
return CRC_NULL;
return CRC_NULL;
#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 transmitLoRa(uint16_t* destMAC, DataReading * packet, uint8_t len) {
#ifdef USE_LORA
uint8_t pkt[6 + (len * sizeof(DataReading))];
@ -297,7 +336,7 @@ void transmitLoRa(uint16_t* destMAC, DataReading * packet, uint8_t len) {
pkt[2] = (LoRaAddress >> 8);
pkt[3] = (LoRaAddress & 0x00FF);
memcpy(&pkt[4], packet, len * sizeof(DataReading));
for(int i = 0; i < (sizeof(pkt) - 2); i++) { // Last 2 bytes are CRC so do not include them in the calculation itself
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]);
}
@ -308,28 +347,28 @@ void transmitLoRa(uint16_t* destMAC, DataReading * packet, uint8_t len) {
pkt[len * sizeof(DataReading) + 5] = (calcCRC & 0x00FF);
#ifdef LORA_ACK // Wait for ACK
int retries = LORA_RETRIES + 1;
while(retries != 0) {
if(transmitLoRaMsgwAck != 0)
DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC, HEX) + " to gateway 0x" + String(*destMAC, HEX) + ". Retries remaining: " + String(retries - 1) + ", Ack Ok " + String((float)msgOkLoRa/transmitLoRaMsgwAck*100) + "%");
else
DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC, HEX) + " to gateway 0x" + String(*destMAC, HEX) + ". Retries remaining: " + String(retries - 1));
while (retries != 0) {
if (transmitLoRaMsg != 0)
DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC, 16) + " to gateway 0x" + String(*destMAC, 16) + ". Retries remaining: " + String(retries - 1) + ", CRC OK " + String((float)msgOkLoRa / transmitLoRaMsg * 100) + "%");
else
DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC, 16) + " to gateway 0x" + String(*destMAC, 16) + ". Retries remaining: " + String(retries - 1));
//printLoraPacket(pkt,sizeof(pkt));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
transmitLoRaMsgwAck++;
unsigned long loraAckTimeout = millis() + LORA_ACK_TIMEOUT;
transmitLoRaMsg++;
unsigned long loraAckTimeout = millis() + LORA_ACK_TIMEOUT;
retries--;
delay(10);
while(returnCRC == CRC_NULL && (millis() < loraAckTimeout)) {
while (returnCRC == CRC_NULL && (millis() < loraAckTimeout)) {
returnCRC = getLoRa();
}
if(returnCRC == CRC_OK) {
if (returnCRC == CRC_OK) {
//DBG("LoRa ACK Received! CRC OK");
msgOkLoRa++;
return; // we're done
}
else if(returnCRC == CRC_BAD) {
else if (returnCRC == CRC_BAD) {
//DBG("LoRa ACK Received! CRC BAD");
// Resend original packet again if retries are available
}
@ -339,42 +378,16 @@ void transmitLoRa(uint16_t* destMAC, DataReading * packet, uint8_t len) {
}
}
#else // Send and do not wait for ACK reply
DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC, HEX) + " to gateway 0x" + String(*destMAC, HEX));
DBG("Transmitting LoRa message of size " + String(sizeof(pkt)) + " bytes with CRC 0x" + String(calcCRC, 16) + " to gateway 0x" + String(*destMAC, 16));
//printLoraPacket(pkt,sizeof(pkt));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
transmitLoRaMsgwAck++;
transmitLoRaMsg++;
#endif // LORA_ACK
#endif // USE_LORA
}
// For now SystemPackets will not use ACK but will calculate CRC
void transmitLoRa(uint16_t* destMAC, SystemPacket* packet, uint8_t len) {
#ifdef USE_LORA
uint8_t pkt[6 + (len * sizeof(SystemPacket))];
uint16_t calcCRC = 0x0000;
pkt[0] = (*destMAC >> 8);
pkt[1] = (*destMAC & 0x00FF);
pkt[2] = (LoRaAddress >> 8);
pkt[3] = (LoRaAddress & 0x00FF);
memcpy(&pkt[4], packet, len * sizeof(SystemPacket));
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]);
}
calcCRC = crc16_update(calcCRC, 0xA1); // Recalculate CRC for No ACK
pkt[len * sizeof(SystemPacket) + 4] = (calcCRC >> 8);
pkt[len * sizeof(SystemPacket) + 5] = (calcCRC & 0x00FF);
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));
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
#endif // USE_LORA
}
void sendFDRS() {
DBG("Sending FDRS Packet!");
#ifdef USE_ESPNOW
@ -384,10 +397,6 @@ void sendFDRS() {
#endif
#ifdef USE_LORA
transmitLoRa(&gtwyAddress, fdrsData, data_count);
DBG(" LoRa sent.");
#endif
data_count = 0;
returnCRC = CRC_NULL;
@ -421,10 +430,61 @@ void sleepFDRS(int sleep_time) {
}
void pingFDRS(int timeout) {
SystemPacket sys_packet;
sys_packet.cmd = cmd_ping;
sys_packet.param = 0; // 0 for Ping Request and 1 for Ping Reply???
void loopFDRS() {
if (is_added) {
if ((millis() - last_refresh) >= gtwy_timeout) {
last_refresh = millis();
}
}
}
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
}
bool addFDRS(int timeout, void (*new_cb_ptr)(DataReading)) {
callback_ptr = new_cb_ptr;
SystemPacket sys_packet = { .cmd = cmd_add, .param = 0 };
#ifdef USE_ESPNOW
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) <= 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
}
uint32_t pingFDRS(int timeout) {
SystemPacket sys_packet = { .cmd = cmd_ping, .param = 0 };
#ifdef USE_ESPNOW
esp_now_send(gatewayAddress, (uint8_t *) &sys_packet, sizeof(SystemPacket));
DBG(" ESP-NOW ping sent.");
@ -433,67 +493,46 @@ void pingFDRS(int timeout) {
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));
is_ping = false;
break;
DBG("Ping Returned:" + String(millis() - ping_start) + " from " + String(incMAC[5]));
return millis() - ping_start;
}
}
#endif // USE_ESPNOW
#endif
#ifdef USE_LORA
transmitLoRa(&gtwyAddress, &sys_packet, 1); // TODO: Make this congruent to esp_now_send()
DBG("LoRa ping sent to gateway address: 0x" + String(gtwyAddress, HEX));
uint32_t ping_start = millis();
is_ping = false;
while ((millis() - ping_start) <= timeout) {
getLoRa();
yield(); //do I need to yield or does it automatically?
if(is_ping) {
DBG("LoRa Ping Returned: " + String(millis() - ping_start) + "ms.");
break;
//transmitLoRa(gtwyAddress, sys_packet, data_count); // TODO: Make this congruent to esp_now_send()
DBG(" LoRa ping not sent because it isn't implemented.");
#endif
}
bool subscribeFDRS(uint16_t sub_id){
for(int i = 0; i < 255; i++){
if ((subscription_list[i] == sub_id) && (active_subs[i])){
DBG("You're already subscribed to that!");
return true;
}
}
if(!is_ping) {
DBG("No LoRa ping returned within " + String(timeout) + "ms.");
for(int i = 0; i < 255; i++){
if (!active_subs[i]){
DBG("Adding subscription at position " + String(i));
subscription_list[i] = sub_id;
active_subs[i] = true;
return true;
}
}
is_ping = false;
#endif // USE_LORA
DBG("No subscription could be established!");
return false;
}
// 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);
bool unsubscribeFDRS(uint16_t sub_id){
for(int i = 0; i < 255; i++){
if ((subscription_list[i] == sub_id) && (active_subs[i])){
DBG("Removing subscription.");
active_subs[i] = false;
return true;
}
}
DBG("No subscription to remove");
return false;
return crc;
}
#ifdef USE_LORA
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");
}
#endif // USE_LORA