Add LoRa MAC

This commit is contained in:
Timm Bogner 2022-02-17 00:12:18 -06:00 committed by GitHub
parent 478d13482b
commit 688e74c956
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 211 additions and 101 deletions

View File

@ -1,7 +1,27 @@
typedef struct DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;
typedef struct DataReading {
float d;
uint16_t id;
uint8_t t;
} DataReading;
#define STATUS_T 0 // Status
#define TEMP_T 1 // Temperature
#define TEMP2_T 2 // Temperature #2
#define HUMIDITY_T 3 // Relative Humidity
#define PRESSURE_T 4 // Atmospheric Pressure
#define LIGHT_T 5 // Light (lux)
#define SOIL_T 6 // Soil Moisture
#define SOIL2_T 7 // Soil Moisture #2
#define OXYGEN_T 8 // Oxygen
#define CO2_T 9 // Carbon Dioxide
#define WINDSPD_T 10 // Wind Speed
#define WINDHDG_T 11 // Wind Direction
#define RAINFALL_T 12 // Rainfall
#define MOTION_T 13 // Motion
#define VOLTAGE_T 14 // Voltage
#define VOLTAGE2_T 15 // Voltage #2
#define CURRENT_T 16 // Current
#define CURRENT2_T 17 // Current #2
#define IT_T 18 // Iterations

View File

@ -4,8 +4,9 @@
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
//
#if defined(ESP8266)
#include "fdrs_config.h"
#include "DataReading.h"
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <espnow.h>
#elif defined(ESP32)
@ -13,25 +14,23 @@
#include <WiFi.h>
#include <esp_wifi.h>
#endif
#include "fdrs_config.h"
#include <ArduinoJson.h>
#include "DataReading.h"
#include <PubSubClient.h>
#include "fdrs_functions.h"
#ifdef USE_LORA
#include <LoRa.h>
#ifdef USE_WIFI
const char* ssid = WIFI_NET;
const char* password = WIFI_PASS;
const char* mqtt_server = MQTT_ADDR;
#endif
#include "fdrs_functions.h"
void setup() {
#if defined(ESP8266)
Serial.begin(115200);
#elif defined(ESP32)
#if defined(RXD2)
Serial.begin(115200, SERIAL_8N1, RXD2, TXD2);
#elif !defined(RXD2)
Serial.begin(115200);
#endif
#endif
begin_espnow();
#ifdef USE_WIFI
@ -66,7 +65,6 @@ void loop() {
if (lenESPNOW2 > 0) releaseESPNOW(2);
}
if (millis() > timeSERIAL) {
//Serial.println("timeSERIAL tripped: " + String(lenSERIAL));
timeSERIAL += SERIAL_DELAY;
if (lenSERIAL > 0) releaseSerial();
}
@ -74,23 +72,23 @@ void loop() {
timeMQTT += MQTT_DELAY;
if (lenMQTT > 0) releaseMQTT();
}
if (millis() > timeLORA) {
timeLORA += LORA_DELAY;
if (lenLORA > 0) releaseLoRa();
if (millis() > timeLORAG) {
timeLORAG += LORAG_DELAY;
if (lenLORAG > 0) releaseLoRa(0);
}
if (millis() > timeLORA1) {
timeLORA1 += LORA1_DELAY;
if (lenLORA1 > 0) releaseLoRa(1);
}
if (millis() > timeLORA2) {
timeLORA2 += LORA2_DELAY;
if (lenLORA2 > 0) releaseLoRa(2);
}
while (Serial.available()) {
getSerial();
}
#ifdef USE_LORA
int packetSize = LoRa.parsePacket();
if (packetSize)
{
LoRa.readBytes((uint8_t *)&theData, packetSize);
ln = packetSize;
newData = 6;
}
#endif
getLoRa();
#ifdef USE_WIFI
if (!client.connected()) {
reconnect();
@ -114,8 +112,14 @@ void loop() {
case 5: //MQTT
MQTT_ACT
break;
case 6: //LoRa
LORA_ACT
case 6: //LoRa General
LORAG_ACT
break;
case 7: //LoRa #1
LORA1_ACT
break;
case 8: //LoRa #2
LORA2_ACT
break;
}
newData = 0;

View File

@ -1,23 +1,29 @@
#define UNIT_MAC 0xFD // THIS UNIT
#define ESPNOW1_MAC 0xFE // ESPNOW1 Address
#define ESPNOW2_MAC 0xFF // ESPNOW2 Address
#define LORA1_MAC 0xFE // LoRa1 Address
#define LORA2_MAC 0xFF // LoRa2 Address
#define ESPNOW1_DELAY 0
#define ESPNOW2_DELAY 0
#define ESPNOWG_DELAY 0
#define SERIAL_DELAY 0
#define MQTT_DELAY 0
#define LORA_DELAY 0
#define LORAG_DELAY 250
#define LORA1_DELAY 250
#define LORA2_DELAY 250
#define ESPNOW1_ACT
#define ESPNOW2_ACT
#define ESPNOWG_ACT
#define SERIAL_ACT
#define MQTT_ACT
#define LORA_ACT
#define LORAG_ACT
#define LORA1_ACT
#define LORA2_ACT
#define RXD2 21
#define TXD2 22
//#define RXD2 21
//#define TXD2 22
#define WIFI_NET "Your SSID"
#define WIFI_PASS "Password"

View File

@ -4,30 +4,20 @@
#include "defaults.h"
#define UNIT_MAC 0x00 // THIS UNIT
#define UNIT_MAC 0x01 // THIS UNIT
//Actions -- Define what happens when a packet arrives at each interface:
//Current function options are: sendESPNOW(MAC), sendSerial(), sendMQTT(), bufferESPNOW(interface), bufferSerial(), and bufferLoRa().
//Current function options are: sendESPNOW(MAC), sendSerial(), sendMQTT(), bufferESPNOW(interface), bufferSerial(), and bufferLoRa(interface).
#define ESPNOWG_ACT sendSerial();
#define SERIAL_ACT
#define MQTT_ACT
#define LORA_ACT
#define ESPNOW1_ACT
#define ESPNOW2_ACT
//Buffer release intervals in milliseconds:
#define ESPNOW1_DELAY 1000
#define ESPNOW2_DELAY 1000
#define ESPNOWG_DELAY 1000
#define SERIAL_DELAY 1000
#define MQTT_DELAY 1000
#define LORA_DELAY 1000
#define MQTT_ACT sendSerial();
#define LORAG_ACT sendSerial();
//ESP32 Only
#define RXD2 21
#define TXD2 22
//#define RXD2 21
//#define TXD2 22
//#define USE_WIFI // You should not use ESP-NOW while WiFi is in use.
#define WIFI_NET "Your SSID"

View File

@ -1,11 +1,14 @@
const uint8_t espnow_size = 250 / sizeof(DataReading);
const uint8_t lora_size = 256 / sizeof(DataReading);
const uint8_t mac_prefix[] = {MAC_PREFIX};
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t ESPNOW1[] = {MAC_PREFIX, ESPNOW1_MAC};
uint8_t selfAddress[] = {MAC_PREFIX, UNIT_MAC};
uint8_t ESPNOW1[] = {MAC_PREFIX, ESPNOW1_MAC};
uint8_t ESPNOW2[] = {MAC_PREFIX, ESPNOW2_MAC};
uint8_t incMAC[6];
uint8_t LoRa1[] = {mac_prefix[4], LORA1_MAC};
uint8_t LoRa2[] = {mac_prefix[4], LORA2_MAC};
DataReading theData[256];
uint8_t ln;
@ -26,14 +29,26 @@ uint32_t timeSERIAL = 0;
DataReading MQTTbuffer[256];
uint8_t lenMQTT = 0;
uint32_t timeMQTT = 0;
DataReading LORAbuffer[256];
uint8_t lenLORA = 0;
uint32_t timeLORA = 0;
DataReading LORAGbuffer[256];
uint8_t lenLORAG = 0;
uint32_t timeLORAG = 0;
DataReading LORA1buffer[256];
uint8_t lenLORA1 = 0;
uint32_t timeLORA1 = 0;
DataReading LORA2buffer[256];
uint8_t lenLORA2 = 0;
uint32_t timeLORA2 = 0;
WiFiClient espClient;
PubSubClient client(espClient);
#ifdef USE_WIFI
const char* ssid = WIFI_NET;
const char* password = WIFI_PASS;
const char* mqtt_server = MQTT_ADDR;
#endif
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
#if defined(ESP8266)
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
@ -58,6 +73,7 @@ void getSerial() {
DeserializationError error = deserializeJson(doc, incomingString);
if (error) { // Test if parsing succeeds.
Serial.println("parse err");
Serial.println(incomingString);
return;
} else {
int s = doc.size();
@ -93,21 +109,28 @@ void mqtt_callback(char* topic, byte * message, unsigned int length) {
newData = 5;
}
}
#ifdef USE_LORA
void getLoRa() {
int packetSize = LoRa.parsePacket();
if (packetSize)
{
LoRa.readBytes((uint8_t *)&theData, packetSize);
ln = packetSize / sizeof(DataReading);
newData = 6;
}
}
#endif
void getLoRa() {
#ifdef USE_LORA
int packetSize = LoRa.parsePacket();
if (packetSize) {
uint8_t packet[packetSize];
uint8_t incLORAMAC[2];
LoRa.readBytes((uint8_t *)&packet, packetSize);
if (memcmp(&packet, &selfAddress[4], 2) == 0) { //Check if addressed to this device
memcpy(&incLORAMAC, &packet[2], 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) newData = 7; //Check if it is from a registered sender
else if (memcmp(&incLORAMAC, &LoRa2, 2) == 0) newData = 8;
else newData = 6;
ln = (packetSize - 5) / sizeof(DataReading);
newData = 6;
}
}
#endif
}
void sendESPNOW(uint8_t address) {
uint8_t NEWPEER[] = {MAC_PREFIX, address};
#if defined(ESP32)
esp_now_peer_info_t peerInfo;
@ -194,14 +217,35 @@ void bufferMQTT() {
}
lenMQTT += ln;
}
void bufferLoRa() {
for (int i = 0; i < ln; i++) {
LORAbuffer[lenLORA + i] = theData[i];
//void bufferLoRa() {
// for (int i = 0; i < ln; i++) {
// LORAbuffer[lenLORA + i] = theData[i];
// }
// lenLORA += ln;
//}
void bufferLoRa(uint8_t interface) {
switch (interface) {
case 0:
for (int i = 0; i < ln; i++) {
LORAGbuffer[lenLORAG + i] = theData[i];
}
lenLORAG += ln;
break;
case 1:
for (int i = 0; i < ln; i++) {
LORA1buffer[lenLORA1 + i] = theData[i];
}
lenLORA1 += ln;
break;
case 2:
for (int i = 0; i < ln; i++) {
LORA2buffer[lenLORA2 + i] = theData[i];
}
lenLORA2 += ln;
break;
}
lenLORA += ln;
}
void releaseESPNOW(uint8_t interface) {
switch (interface) {
case 0:
@ -209,7 +253,7 @@ void releaseESPNOW(uint8_t interface) {
DataReading thePacket[espnow_size];
int j = 0;
for (int i = 0; i < lenESPNOWG; i++) {
if ( j > 250 / sizeof(DataReading)) {
if ( j > espnow_size) {
j = 0;
esp_now_send(broadcast_mac, (uint8_t *) &thePacket, sizeof(thePacket));
}
@ -225,7 +269,7 @@ void releaseESPNOW(uint8_t interface) {
DataReading thePacket[espnow_size];
int j = 0;
for (int i = 0; i < lenESPNOW1; i++) {
if ( j > 250 / sizeof(DataReading)) {
if ( j > espnow_size) {
j = 0;
esp_now_send(ESPNOW1, (uint8_t *) &thePacket, sizeof(thePacket));
}
@ -241,7 +285,7 @@ void releaseESPNOW(uint8_t interface) {
DataReading thePacket[espnow_size];
int j = 0;
for (int i = 0; i < lenESPNOW2; i++) {
if ( j > 250 / sizeof(DataReading)) {
if ( j > espnow_size) {
j = 0;
esp_now_send(ESPNOW2, (uint8_t *) &thePacket, sizeof(thePacket));
}
@ -254,6 +298,74 @@ void releaseESPNOW(uint8_t interface) {
}
}
}
#ifdef USE_LORA
void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
uint8_t pkt[5 + (len * sizeof(DataReading))];
memcpy(&pkt, mac, 2);
memcpy(&pkt[2], &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
switch (interface) {
case 0:
{
DataReading thePacket[lora_size];
int j = 0;
for (int i = 0; i < lenLORAG; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(broadcast_mac, thePacket, j);
}
thePacket[j] = LORAGbuffer[i];
j++;
}
transmitLoRa(broadcast_mac, thePacket, j);
lenLORAG = 0;
break;
}
case 1:
{
DataReading thePacket[lora_size];
int j = 0;
for (int i = 0; i < lenLORA1; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(LoRa1, thePacket, j);
}
thePacket[j] = LORA1buffer[i];
j++;
}
transmitLoRa(LoRa1, thePacket, j);
lenLORA1 = 0;
break;
}
case 2:
{
DataReading thePacket[lora_size];
int j = 0;
for (int i = 0; i < lenLORA2; i++) {
if ( j > lora_size) {
j = 0;
transmitLoRa(LoRa2, thePacket, j);
}
thePacket[j] = LORA2buffer[i];
j++;
}
transmitLoRa(LoRa2, thePacket, j);
lenLORA2 = 0;
break;
}
}
#endif
}
void releaseSerial() {
DynamicJsonDocument doc(24576);
for (int i = 0; i < lenSERIAL; i++) {
@ -279,28 +391,6 @@ void releaseMQTT() {
lenMQTT = 0;
#endif
}
void releaseLoRa() {
#ifdef USE_LORA
DataReading thePacket[lora_size];
int j = 0;
for (int i = 0; i < lenLORA); i++) {
if ( j > lora_size)) {
j = 0;
LoRa.beginPacket();
LoRa.write((uint8_t*)&thePacket, j * sizeof(DataReading));
LoRa.endPacket();
}
thePacket[j] = LORAbuffer[i];
j++;
}
LoRa.beginPacket();
LoRa.write((uint8_t*)&thePacket, j * sizeof(DataReading));
LoRa.endPacket();
lenLORA = 0;
#endif
}
void reconnect() {
// Loop until reconnected
while (!client.connected()) {