From 378d50eb87f07cc38ca5c113198d1dc67d330356 Mon Sep 17 00:00:00 2001 From: Timm Bogner <64260873+timmbogner@users.noreply.github.com> Date: Mon, 22 Aug 2022 20:55:20 -0500 Subject: [PATCH] added physical addressing --- README.md | 2 +- src/fdrs_node.h | 62 ++++++++++++++++++++++++------------------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index d2eefed..d389d23 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/fdrs_node.h b/src/fdrs_node.h index 02ab5f9..edd13bf 100644 --- a/src/fdrs_node.h +++ b/src/fdrs_node.h @@ -14,6 +14,7 @@ #include #endif #ifdef USE_LORA +#include #include #endif @@ -82,15 +83,14 @@ typedef struct __attribute__((packed)) SystemPacket { const uint16_t espnow_size = 250 / sizeof(DataReading); 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); -uint16_t LoRaAddress = 0x4200; +#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]; @@ -231,6 +231,33 @@ void beginFDRS() { } +// 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)
+ 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() { @@ -481,30 +508,3 @@ uint32_t pingFDRS(int timeout) { DBG(" LoRa ping not sent because it isn't implemented."); #endif } - -// 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)
- 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; -}