mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-10 07:10:42 +00:00
lora compiles
This commit is contained in:
parent
3663c9db26
commit
e1c59038c7
@ -32,10 +32,11 @@
|
||||
|
||||
#define USE_WIFI
|
||||
|
||||
#define ESPNOW_PEER_1 0x0E // ESPNOW1 Address
|
||||
#define ESPNOW_PEER_2 0x0F // ESPNOW2 Address
|
||||
#ifdef USE_LED
|
||||
CRGB leds[NUM_LEDS];
|
||||
#endif
|
||||
|
||||
|
||||
uint8_t broadcast_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
uint8_t selfAddress[6] = {MAC_PREFIX, UNIT_MAC};
|
||||
|
||||
|
||||
@ -51,16 +52,27 @@ uint8_t ESPNOW2[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
#endif
|
||||
|
||||
|
||||
MQTT_FDRSGateWay MQTT(1000,WIFI_SSID,WIFI_PASS,MQTT_ADDR,MQTT_PORT);
|
||||
#ifdef LORA_PEER_1
|
||||
uint8_t LoRa1[6] = {MAC_PREFIX, LORA_PEER_1};
|
||||
#endif
|
||||
|
||||
ESP_FDRSGateWay ESPNow(broadcast_mac,selfAddress,1000);
|
||||
#ifdef LORA_PEER_2
|
||||
uint8_t LoRa2[6] = {MAC_PREFIX, LORA_PEER_2};
|
||||
#endif
|
||||
|
||||
|
||||
MQTT_FDRSGateWay MQTT(WIFI_SSID,WIFI_PASS,MQTT_ADDR,MQTT_PORT);
|
||||
|
||||
ESP_FDRSGateWay ESPNow;
|
||||
|
||||
#if defined(ESP32)
|
||||
Serial_FDRSGateWay SerialGW(&Serial1,115200,1000);
|
||||
Serial_FDRSGateWay SerialGW(&Serial1,115200);
|
||||
#else
|
||||
Serial_FDRSGateWay SerialGW(&Serial,115200,1000);
|
||||
Serial_FDRSGateWay SerialGW(&Serial,115200);
|
||||
#endif
|
||||
|
||||
LoRa_FDRSGateWay LoRaGW(MISO,MOSI,SCK,SS,RST,DIO0,BAND,SF);
|
||||
|
||||
void setup() {
|
||||
|
||||
#ifdef USE_LED
|
||||
@ -68,10 +80,10 @@ void setup() {
|
||||
leds[0] = CRGB::Blue;
|
||||
FastLED.show();
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
|
||||
MQTT.init();
|
||||
#else
|
||||
ESPNow.init();
|
||||
|
||||
ESPNow.init(selfAddress);
|
||||
|
||||
#ifdef ESPNOW_PEER_1
|
||||
ESPNow.add_peer(ESPNOW1);
|
||||
@ -80,84 +92,42 @@ void setup() {
|
||||
#ifdef ESPNOW_PEER_2
|
||||
ESPNow.add_peer(ESPNOW2);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(ESP32)
|
||||
SerialGW.init(SERIAL_8N1,RXD2,TXD2);
|
||||
#else
|
||||
SerialGW.init();
|
||||
#endif
|
||||
// #ifdef USE_LORA
|
||||
// DBG("Initializing LoRa!");
|
||||
// SPI.begin(SCK, MISO, MOSI, SS);
|
||||
// LoRa.setPins(SS, RST, DIO0);
|
||||
// if (!LoRa.begin(FDRS_BAND)) {
|
||||
// while (1);
|
||||
// }
|
||||
// LoRa.setSpreadingFactor(FDRS_SF);
|
||||
// DBG(" LoRa initialized.");
|
||||
// #endif
|
||||
|
||||
//DBG(sizeof(DataReading));
|
||||
|
||||
#ifdef USE_LORA
|
||||
LoRaGW.init(selfAddress);
|
||||
#endif
|
||||
|
||||
#ifdef ESPNOW_PEER_1
|
||||
ESPNow.add_peer(ESPNOW1);
|
||||
#endif
|
||||
|
||||
#ifdef ESPNOW_PEER_2
|
||||
ESPNow.add_peer(ESPNOW2);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
|
||||
LoRaGW.get();
|
||||
SerialGW.get();
|
||||
|
||||
ESPNow.release();
|
||||
#ifdef ESPNOWG_DELAY
|
||||
if (millis() > timeESPNOWG) {
|
||||
timeESPNOWG += ESPNOWG_DELAY;
|
||||
if (lenESPNOWG > 0) releaseESPNOW(0);
|
||||
}
|
||||
#endif
|
||||
#ifdef ESPNOW1_DELAY
|
||||
if (millis() > timeESPNOW1) {
|
||||
timeESPNOW1 += ESPNOW1_DELAY;
|
||||
if (lenESPNOW1 > 0) releaseESPNOW(1);
|
||||
}
|
||||
#endif
|
||||
#ifdef ESPNOW2_DELAY
|
||||
if (millis() > timeESPNOW2) {
|
||||
timeESPNOW2 += ESPNOW2_DELAY;
|
||||
if (lenESPNOW2 > 0) releaseESPNOW(2);
|
||||
}
|
||||
#endif
|
||||
#ifdef SERIAL_DELAY
|
||||
if (millis() > timeSERIAL) {
|
||||
timeSERIAL += SERIAL_DELAY;
|
||||
if (lenSERIAL > 0) releaseSerial();
|
||||
}
|
||||
#endif
|
||||
#ifdef MQTT_DELAY
|
||||
if (millis() > timeMQTT) {
|
||||
timeMQTT += MQTT_DELAY;
|
||||
if (lenMQTT > 0) releaseMQTT();
|
||||
}
|
||||
#endif
|
||||
MQTT.release();
|
||||
SerialGW.release();
|
||||
LoRaGW.release();
|
||||
|
||||
// #ifdef LORAG_DELAY
|
||||
// if (millis() > timeLORAG) {
|
||||
// timeLORAG += LORAG_DELAY;
|
||||
// if (lenLORAG > 0) releaseLoRa(0);
|
||||
// }
|
||||
// #endif
|
||||
// #ifdef LORA1_DELAY
|
||||
// if (millis() > timeLORA1) {
|
||||
// timeLORA1 += LORA1_DELAY;
|
||||
// if (lenLORA1 > 0) releaseLoRa(1);
|
||||
// }
|
||||
// #endif
|
||||
// #ifdef LORA2_DELAY
|
||||
// if (millis() > timeLORA2) {
|
||||
// timeLORA2 += LORA2_DELAY;
|
||||
// if (lenLORA2 > 0) releaseLoRa(2);
|
||||
// }
|
||||
// #endif
|
||||
|
||||
|
||||
// getLoRa();
|
||||
//It does not matter witch one you call.
|
||||
//it will clear all the data that has been received.
|
||||
ESPNow.flush();
|
||||
MQTT.flush();
|
||||
SerialGW.flush();
|
||||
LoRaGW.flush();
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
//#include <fdrs_globals.h> //Uncomment if you install the globals file
|
||||
#define DEBUG
|
||||
|
||||
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems.
|
||||
|
||||
#define UNIT_MAC 0x03 // The address of this gateway
|
||||
|
||||
@ -20,8 +21,11 @@
|
||||
|
||||
// Peer addresses
|
||||
|
||||
#define LORA1_PEER 0x0E // LoRa1 Address
|
||||
#define LORA2_PEER 0x0F // LoRa2 Address
|
||||
#define LORA_PEER_1 0x0E // LoRa1 Address
|
||||
#define LORA_PEER_2 0x0F // LoRa2 Address
|
||||
|
||||
#define ESPNOW_PEER_1 0x0C // ESPNOW1 Address
|
||||
#define ESPNOW_PEER_2 0x0D // ESPNOW2 Address
|
||||
|
||||
// Peer Actions
|
||||
#define ESPNOW1_ACT
|
||||
@ -34,10 +38,6 @@
|
||||
#define WIFI_PASS "Your Password"
|
||||
#define MQTT_ADDR "192.168.0.8"
|
||||
#define MQTT_PORT 1883
|
||||
// MQTT Topics
|
||||
#define TOPIC_DATA "fdrs/data"
|
||||
#define TOPIC_STATUS "fdrs/status"
|
||||
#define TOPIC_COMMAND "fdrs/command"
|
||||
|
||||
//Pins for UART data interface (ESP32 only)
|
||||
#define RXD2 14
|
||||
@ -56,16 +56,6 @@
|
||||
#define BAND 915E6
|
||||
#define SF 7
|
||||
|
||||
// Buffer Delays - in milliseconds
|
||||
//#define ESPNOW1_DELAY 0
|
||||
//#define ESPNOW2_DELAY 0
|
||||
//#define ESPNOWG_DELAY 0
|
||||
//#define SERIAL_DELAY 0
|
||||
//#define MQTT_DELAY 0
|
||||
#define LORAG_DELAY 1000
|
||||
//#define LORA1_DELAY 1000
|
||||
//#define LORA2_DELAY 1000
|
||||
|
||||
//#define USE_LED //Not yet fully implemented
|
||||
#define LED_PIN 32
|
||||
#define NUM_LEDS 4
|
||||
|
133
Examples/Universal_Gateway/Universal_Gateway.ino
Normal file
133
Examples/Universal_Gateway/Universal_Gateway.ino
Normal file
@ -0,0 +1,133 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000
|
||||
//
|
||||
// Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
||||
//
|
||||
|
||||
#include "fdrs_config.h"
|
||||
|
||||
|
||||
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <espnow.h>
|
||||
#elif defined(ESP32)
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
#include <ArduinoJson.h>
|
||||
#ifdef USE_WIFI
|
||||
#include <PubSubClient.h>
|
||||
#endif
|
||||
#ifdef USE_LORA
|
||||
#include <LoRa.h>
|
||||
#endif
|
||||
#ifdef USE_LED
|
||||
#include <FastLED.h>
|
||||
#endif
|
||||
#include "fdrs_gateway.h"
|
||||
#include "fdrs_config.h"
|
||||
|
||||
#define USE_WIFI
|
||||
|
||||
#ifdef USE_LED
|
||||
CRGB leds[NUM_LEDS];
|
||||
#endif
|
||||
|
||||
|
||||
uint8_t selfAddress[6] = {MAC_PREFIX, UNIT_MAC};
|
||||
|
||||
|
||||
#ifdef ESPNOW_PEER_1
|
||||
uint8_t ESPNOW1[] = {MAC_PREFIX, ESPNOW_PEER_1};
|
||||
#else
|
||||
uint8_t ESPNOW1[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
#endif
|
||||
#ifdef ESPNOW_PEER_2
|
||||
uint8_t ESPNOW2[] = {MAC_PREFIX, ESPNOW_PEER_2};
|
||||
#else
|
||||
uint8_t ESPNOW2[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef LORA_PEER_1
|
||||
uint8_t LoRa1[6] = {MAC_PREFIX, LORA_PEER_1};
|
||||
#endif
|
||||
|
||||
#ifdef LORA_PEER_2
|
||||
uint8_t LoRa2[6] = {MAC_PREFIX, LORA_PEER_2};
|
||||
#endif
|
||||
|
||||
|
||||
MQTT_FDRSGateWay MQTT(WIFI_SSID,WIFI_PASS,MQTT_ADDR,MQTT_PORT);
|
||||
|
||||
ESP_FDRSGateWay ESPNow;
|
||||
|
||||
#if defined(ESP32)
|
||||
Serial_FDRSGateWay SerialGW(&Serial1,115200);
|
||||
#else
|
||||
Serial_FDRSGateWay SerialGW(&Serial,115200);
|
||||
#endif
|
||||
|
||||
LoRa_FDRSGateWay LoRaGW(MISO,MOSI,SCK,SS,RST,DIO0,BAND,SF);
|
||||
|
||||
void setup() {
|
||||
|
||||
#ifdef USE_LED
|
||||
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
|
||||
leds[0] = CRGB::Blue;
|
||||
FastLED.show();
|
||||
#endif
|
||||
|
||||
MQTT.init();
|
||||
|
||||
ESPNow.init(selfAddress);
|
||||
|
||||
#ifdef ESPNOW_PEER_1
|
||||
ESPNow.add_peer(ESPNOW1);
|
||||
#endif
|
||||
|
||||
#ifdef ESPNOW_PEER_2
|
||||
ESPNow.add_peer(ESPNOW2);
|
||||
#endif
|
||||
|
||||
#if defined(ESP32)
|
||||
SerialGW.init(SERIAL_8N1,RXD2,TXD2);
|
||||
#else
|
||||
SerialGW.init();
|
||||
#endif
|
||||
|
||||
#ifdef USE_LORA
|
||||
LoRaGW.init(selfAddress);
|
||||
#endif
|
||||
|
||||
#ifdef ESPNOW_PEER_1
|
||||
ESPNow.add_peer(ESPNOW1);
|
||||
#endif
|
||||
|
||||
#ifdef ESPNOW_PEER_2
|
||||
ESPNow.add_peer(ESPNOW2);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
LoRaGW.get();
|
||||
SerialGW.get();
|
||||
|
||||
ESPNow.release();
|
||||
MQTT.release();
|
||||
SerialGW.release();
|
||||
LoRaGW.release();
|
||||
|
||||
//It does not matter witch one you call.
|
||||
//it will clear all the data that has been received.
|
||||
ESPNow.flush();
|
||||
MQTT.flush();
|
||||
SerialGW.flush();
|
||||
LoRaGW.flush();
|
||||
|
||||
}
|
61
Examples/Universal_Gateway/fdrs_config.h
Normal file
61
Examples/Universal_Gateway/fdrs_config.h
Normal file
@ -0,0 +1,61 @@
|
||||
// FARM DATA RELAY SYSTEM
|
||||
//
|
||||
// GATEWAY 2.000 Configuration
|
||||
|
||||
//#include <fdrs_globals.h> //Uncomment if you install the globals file
|
||||
#define DEBUG
|
||||
|
||||
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems.
|
||||
|
||||
#define UNIT_MAC 0x03 // The address of this gateway
|
||||
|
||||
//Actions -- Define what happens when a packet arrives at each interface:
|
||||
//Current function options are: sendESPNOW(MAC), sendSerial(), sendMQTT(), bufferESPNOW(interface), bufferSerial(), and bufferLoRa(interface).
|
||||
|
||||
#define SERIAL_ACT
|
||||
#define MQTT_ACT
|
||||
#define LORAG_ACT
|
||||
|
||||
//#define USE_LORA
|
||||
//#define USE_WIFI //Used only for MQTT gateway
|
||||
|
||||
// Peer addresses
|
||||
|
||||
#define LORA_PEER_1 0x0E // LoRa1 Address
|
||||
#define LORA_PEER_2 0x0F // LoRa2 Address
|
||||
|
||||
#define ESPNOW_PEER_1 0x0C // ESPNOW1 Address
|
||||
#define ESPNOW_PEER_2 0x0D // ESPNOW2 Address
|
||||
|
||||
// Peer Actions
|
||||
#define ESPNOW1_ACT
|
||||
#define ESPNOW2_ACT
|
||||
#define LORA1_ACT
|
||||
#define LORA2_ACT
|
||||
|
||||
//WiFi and MQTT Credentials -- Needed only for MQTT gateway
|
||||
#define WIFI_SSID "Your SSID"
|
||||
#define WIFI_PASS "Your Password"
|
||||
#define MQTT_ADDR "192.168.0.8"
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
//Pins for UART data interface (ESP32 only)
|
||||
#define RXD2 14
|
||||
#define TXD2 15
|
||||
|
||||
//LoRa Configuration -- Needed only if using LoRa
|
||||
#define SCK 5
|
||||
#define MISO 19
|
||||
#define MOSI 27
|
||||
#define SS 18
|
||||
#define RST 14
|
||||
#define DIO0 26
|
||||
//433E6 for Asia
|
||||
//866E6 for Europe
|
||||
//915E6 for North America
|
||||
#define BAND 915E6
|
||||
#define SF 7
|
||||
|
||||
//#define USE_LED //Not yet fully implemented
|
||||
#define LED_PIN 32
|
||||
#define NUM_LEDS 4
|
@ -1,549 +0,0 @@
|
||||
#ifdef DEBUG
|
||||
#define DBG(a) (Serial.println(a))
|
||||
#else
|
||||
#define DBG(a)
|
||||
#endif
|
||||
|
||||
#if defined (ESP32)
|
||||
#define UART_IF Serial1
|
||||
#else
|
||||
#define UART_IF Serial
|
||||
#endif
|
||||
|
||||
#ifdef GLOBALS
|
||||
#define FDRS_WIFI_SSID GLOBAL_SSID
|
||||
#define FDRS_WIFI_PASS GLOBAL_PASS
|
||||
#define FDRS_MQTT_ADDR GLOBAL_MQTT_ADDR
|
||||
#define FDRS_BAND GLOBAL_BAND
|
||||
#define FDRS_SF GLOBAL_SF
|
||||
#else
|
||||
#define FDRS_WIFI_SSID WIFI_SSID
|
||||
#define FDRS_WIFI_PASS WIFI_PASS
|
||||
#define FDRS_MQTT_ADDR MQTT_ADDR
|
||||
#define FDRS_BAND BAND
|
||||
#define FDRS_SF SF
|
||||
#endif
|
||||
|
||||
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems.
|
||||
|
||||
typedef struct __attribute__((packed)) DataReading {
|
||||
float d;
|
||||
uint16_t id;
|
||||
uint8_t t;
|
||||
|
||||
} DataReading;
|
||||
|
||||
const uint8_t espnow_size = 250 / sizeof(DataReading);
|
||||
const uint8_t lora_size = 256 / sizeof(DataReading);
|
||||
const uint8_t mac_prefix[] = {MAC_PREFIX};
|
||||
|
||||
#ifdef ESP32
|
||||
esp_now_peer_info_t peerInfo;
|
||||
#endif
|
||||
|
||||
uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
uint8_t selfAddress[] = {MAC_PREFIX, UNIT_MAC};
|
||||
uint8_t incMAC[6];
|
||||
|
||||
#ifdef ESPNOW1_PEER
|
||||
uint8_t ESPNOW1[] = {MAC_PREFIX, ESPNOW1_PEER};
|
||||
#else
|
||||
uint8_t ESPNOW1[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
#endif
|
||||
#ifdef ESPNOW2_PEER
|
||||
uint8_t ESPNOW2[] = {MAC_PREFIX, ESPNOW2_PEER};
|
||||
#else
|
||||
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};
|
||||
#endif
|
||||
|
||||
DataReading theData[256];
|
||||
uint8_t ln;
|
||||
uint8_t newData = 0;
|
||||
|
||||
DataReading ESPNOW1buffer[256];
|
||||
uint8_t lenESPNOW1 = 0;
|
||||
uint32_t timeESPNOW1 = 0;
|
||||
DataReading ESPNOW2buffer[256];
|
||||
uint8_t lenESPNOW2 = 0;
|
||||
uint32_t timeESPNOW2 = 0;
|
||||
DataReading ESPNOWGbuffer[256];
|
||||
uint8_t lenESPNOWG = 0;
|
||||
uint32_t timeESPNOWG = 0;
|
||||
DataReading SERIALbuffer[256];
|
||||
uint8_t lenSERIAL = 0;
|
||||
uint32_t timeSERIAL = 0;
|
||||
DataReading MQTTbuffer[256];
|
||||
uint8_t lenMQTT = 0;
|
||||
uint32_t timeMQTT = 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;
|
||||
#ifdef USE_LED
|
||||
CRGB leds[NUM_LEDS];
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
PubSubClient client(espClient);
|
||||
const char* ssid = FDRS_WIFI_SSID;
|
||||
const char* password = FDRS_WIFI_PASS;
|
||||
const char* mqtt_server = FDRS_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) {
|
||||
}
|
||||
void OnDataRecv(uint8_t* mac, uint8_t *incomingData, uint8_t len) {
|
||||
#elif defined(ESP32)
|
||||
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(&theData, incomingData, sizeof(theData));
|
||||
memcpy(&incMAC, mac, sizeof(incMAC));
|
||||
DBG("Incoming ESP-NOW.");
|
||||
ln = len / sizeof(DataReading);
|
||||
if (memcmp(&incMAC, &ESPNOW1, 6) == 0) newData = 1;
|
||||
else if (memcmp(&incMAC, &ESPNOW2, 6) == 0) newData = 2;
|
||||
else newData = 3;
|
||||
}
|
||||
void getSerial() {
|
||||
String incomingString = UART_IF.readStringUntil('\n');
|
||||
DynamicJsonDocument doc(24576);
|
||||
DeserializationError error = deserializeJson(doc, incomingString);
|
||||
if (error) { // Test if parsing succeeds.
|
||||
// DBG("json parse err");
|
||||
// DBG(incomingString);
|
||||
return;
|
||||
} else {
|
||||
int s = doc.size();
|
||||
//UART_IF.println(s);
|
||||
for (int i = 0; i < s; i++) {
|
||||
theData[i].id = doc[i]["id"];
|
||||
theData[i].t = doc[i]["type"];
|
||||
theData[i].d = doc[i]["data"];
|
||||
}
|
||||
ln = s;
|
||||
newData = 4;
|
||||
DBG("Incoming Serial.");
|
||||
|
||||
}
|
||||
}
|
||||
void mqtt_callback(char* topic, byte * message, unsigned int length) {
|
||||
String incomingString;
|
||||
DBG(topic);
|
||||
for (int i = 0; i < length; i++) {
|
||||
incomingString += (char)message[i];
|
||||
}
|
||||
StaticJsonDocument<2048> doc;
|
||||
DeserializationError error = deserializeJson(doc, incomingString);
|
||||
if (error) { // Test if parsing succeeds.
|
||||
DBG("json parse err");
|
||||
DBG(incomingString);
|
||||
return;
|
||||
} else {
|
||||
int s = doc.size();
|
||||
//UART_IF.println(s);
|
||||
for (int i = 0; i < s; i++) {
|
||||
theData[i].id = doc[i]["id"];
|
||||
theData[i].t = doc[i]["type"];
|
||||
theData[i].d = doc[i]["data"];
|
||||
}
|
||||
ln = s;
|
||||
newData = 5;
|
||||
DBG("Incoming MQTT.");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
// for (int i = 0; i < packetSize; i++) {
|
||||
// UART_IF.println(packet[i], HEX);
|
||||
// }
|
||||
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) 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;
|
||||
DBG("Incoming LoRa.");
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void sendESPNOW(uint8_t address) {
|
||||
DBG("Sending ESP-NOW.");
|
||||
uint8_t NEWPEER[] = {MAC_PREFIX, address};
|
||||
#if defined(ESP32)
|
||||
esp_now_peer_info_t peerInfo;
|
||||
peerInfo.ifidx = WIFI_IF_STA;
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
memcpy(peerInfo.peer_addr, NEWPEER, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
DBG("Failed to add peer");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
DataReading thePacket[ln];
|
||||
int j = 0;
|
||||
for (int i = 0; i < ln; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(NEWPEER, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = theData[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(NEWPEER, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
esp_now_del_peer(NEWPEER);
|
||||
}
|
||||
|
||||
void sendSerial() {
|
||||
DBG("Sending Serial.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < ln; i++) {
|
||||
doc[i]["id"] = theData[i].id;
|
||||
doc[i]["type"] = theData[i].t;
|
||||
doc[i]["data"] = theData[i].d;
|
||||
}
|
||||
serializeJson(doc, UART_IF);
|
||||
UART_IF.println();
|
||||
|
||||
#ifndef ESP8266
|
||||
serializeJson(doc, Serial);
|
||||
Serial.println();
|
||||
#endif
|
||||
|
||||
}
|
||||
void sendMQTT() {
|
||||
#ifdef USE_WIFI
|
||||
DBG("Sending MQTT.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < ln; i++) {
|
||||
doc[i]["id"] = theData[i].id;
|
||||
doc[i]["type"] = theData[i].t;
|
||||
doc[i]["data"] = theData[i].d;
|
||||
}
|
||||
String outgoingString;
|
||||
serializeJson(doc, outgoingString);
|
||||
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
void bufferESPNOW(uint8_t interface) {
|
||||
DBG("Buffering ESP-NOW.");
|
||||
|
||||
switch (interface) {
|
||||
case 0:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
ESPNOWGbuffer[lenESPNOWG + i] = theData[i];
|
||||
}
|
||||
lenESPNOWG += ln;
|
||||
break;
|
||||
case 1:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
ESPNOW1buffer[lenESPNOW1 + i] = theData[i];
|
||||
}
|
||||
lenESPNOW1 += ln;
|
||||
break;
|
||||
case 2:
|
||||
for (int i = 0; i < ln; i++) {
|
||||
ESPNOW2buffer[lenESPNOW2 + i] = theData[i];
|
||||
}
|
||||
lenESPNOW2 += ln;
|
||||
break;
|
||||
}
|
||||
}
|
||||
void bufferSerial() {
|
||||
DBG("Buffering Serial.");
|
||||
for (int i = 0; i < ln; i++) {
|
||||
SERIALbuffer[lenSERIAL + i] = theData[i];
|
||||
}
|
||||
lenSERIAL += ln;
|
||||
//UART_IF.println("SENDSERIAL:" + String(lenSERIAL) + " ");
|
||||
}
|
||||
void bufferMQTT() {
|
||||
DBG("Buffering MQTT.");
|
||||
for (int i = 0; i < ln; i++) {
|
||||
MQTTbuffer[lenMQTT + i] = theData[i];
|
||||
}
|
||||
lenMQTT += ln;
|
||||
}
|
||||
//void bufferLoRa() {
|
||||
// for (int i = 0; i < ln; i++) {
|
||||
// LORAbuffer[lenLORA + i] = theData[i];
|
||||
// }
|
||||
// lenLORA += ln;
|
||||
//}
|
||||
void bufferLoRa(uint8_t interface) {
|
||||
DBG("Buffering LoRa.");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void releaseESPNOW(uint8_t interface) {
|
||||
DBG("Releasing ESP-NOW.");
|
||||
switch (interface) {
|
||||
case 0:
|
||||
{
|
||||
DataReading thePacket[espnow_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenESPNOWG; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(broadcast_mac, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = ESPNOWGbuffer[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(broadcast_mac, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
lenESPNOWG = 0;
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
DataReading thePacket[espnow_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenESPNOW1; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(ESPNOW1, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = ESPNOW1buffer[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(ESPNOW1, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
lenESPNOW1 = 0;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
DataReading thePacket[espnow_size];
|
||||
int j = 0;
|
||||
for (int i = 0; i < lenESPNOW2; i++) {
|
||||
if ( j > espnow_size) {
|
||||
j = 0;
|
||||
esp_now_send(ESPNOW2, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||
}
|
||||
thePacket[j] = ESPNOW2buffer[i];
|
||||
j++;
|
||||
}
|
||||
esp_now_send(ESPNOW2, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||
lenESPNOW2 = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#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.");
|
||||
|
||||
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() {
|
||||
DBG("Releasing Serial.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < lenSERIAL; i++) {
|
||||
doc[i]["id"] = SERIALbuffer[i].id;
|
||||
doc[i]["type"] = SERIALbuffer[i].t;
|
||||
doc[i]["data"] = SERIALbuffer[i].d;
|
||||
}
|
||||
serializeJson(doc, UART_IF);
|
||||
UART_IF.println();
|
||||
lenSERIAL = 0;
|
||||
}
|
||||
void releaseMQTT() {
|
||||
#ifdef USE_WIFI
|
||||
DBG("Releasing MQTT.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < lenMQTT; i++) {
|
||||
doc[i]["id"] = MQTTbuffer[i].id;
|
||||
doc[i]["type"] = MQTTbuffer[i].t;
|
||||
doc[i]["data"] = MQTTbuffer[i].d;
|
||||
}
|
||||
String outgoingString;
|
||||
serializeJson(doc, outgoingString);
|
||||
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
|
||||
lenMQTT = 0;
|
||||
#endif
|
||||
}
|
||||
void reconnect() {
|
||||
#ifdef USE_WIFI
|
||||
// Loop until reconnected
|
||||
while (!client.connected()) {
|
||||
// Attempt to connect
|
||||
if (client.connect("FDRS_GATEWAY")) {
|
||||
// Subscribe
|
||||
client.subscribe(TOPIC_COMMAND);
|
||||
} else {
|
||||
DBG("Connecting MQTT.");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
void begin_espnow() {
|
||||
DBG("Initializing ESP-NOW!");
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
|
||||
#if defined(ESP8266)
|
||||
wifi_set_macaddr(STATION_IF, selfAddress);
|
||||
if (esp_now_init() != 0) {
|
||||
return;
|
||||
}
|
||||
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
|
||||
esp_now_register_send_cb(OnDataSent);
|
||||
esp_now_register_recv_cb(OnDataRecv);
|
||||
// Register peers
|
||||
#ifdef ESPNOW1_PEER
|
||||
esp_now_add_peer(ESPNOW1, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
#endif
|
||||
#ifdef ESPNOW2_PEER
|
||||
esp_now_add_peer(ESPNOW2, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
#endif
|
||||
#elif defined(ESP32)
|
||||
esp_wifi_set_mac(WIFI_IF_STA, &selfAddress[0]);
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
DBG("Error initializing ESP-NOW");
|
||||
return;
|
||||
}
|
||||
esp_now_register_send_cb(OnDataSent);
|
||||
esp_now_register_recv_cb(OnDataRecv);
|
||||
|
||||
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;
|
||||
}
|
||||
#ifdef ESPNOW1_PEER
|
||||
memcpy(peerInfo.peer_addr, ESPNOW1, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
DBG("Failed to add peer 1");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef ESPNOW2_PEER
|
||||
memcpy(peerInfo.peer_addr, ESPNOW2, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
DBG("Failed to add peer 2");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
DBG(" ESP-NOW Initialized.");
|
||||
}
|
355
fdrs_gateway.cpp
355
fdrs_gateway.cpp
@ -6,19 +6,15 @@
|
||||
// #define USE_WIFI
|
||||
|
||||
std::vector<DataReading_t> FDRSGateWayBase::_data;
|
||||
std::vector<FDRSGateWayBase*> FDRSGateWayBase::_object_list;
|
||||
|
||||
bool ESP_FDRSGateWay::is_init = false;
|
||||
std::vector<ESP_Peer_t> ESP_FDRSGateWay::peer_list;
|
||||
std::vector<ESP_Peer_t> ESP_FDRSGateWay::unknow_peer;
|
||||
std::vector<Peer_t> ESP_FDRSGateWay::peer_list;
|
||||
std::vector<Peer_t> ESP_FDRSGateWay::unknow_peer;
|
||||
|
||||
uint8_t newData = 0;
|
||||
uint8_t ln = 0;
|
||||
DataReading_t theData[256];
|
||||
|
||||
DataReadingBuffer_t SERIALbuffer;
|
||||
uint32_t timeSERIAL = 0;
|
||||
|
||||
DataReadingBuffer_t MQTTbuffer;
|
||||
uint32_t timeMQTT = 0;
|
||||
|
||||
@ -32,7 +28,6 @@ DataReadingBuffer_t LORA2buffer;
|
||||
uint32_t timeLORA2 = 0;
|
||||
|
||||
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
|
||||
|
||||
void ESP_FDRSGateWay::OnDataRecv(uint8_t * mac, const uint8_t *incomingData, int len){
|
||||
|
||||
|
||||
@ -57,7 +52,7 @@ void ESP_FDRSGateWay::OnDataRecv(uint8_t * mac, const uint8_t *incomingData, int
|
||||
}
|
||||
}
|
||||
|
||||
ESP_Peer_t peer;
|
||||
Peer_t peer;
|
||||
peer._copy(mac);
|
||||
unknow_peer.push_back(peer);
|
||||
}
|
||||
@ -82,204 +77,36 @@ void ESP32OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len)
|
||||
}
|
||||
#endif
|
||||
|
||||
FDRSGateWayBase::FDRSGateWayBase(){
|
||||
|
||||
|
||||
// void getLoRa() {
|
||||
// #ifdef USE_LORA
|
||||
// int packetSize = LoRa.parsePacket();
|
||||
// if (packetSize== 0) {
|
||||
// return;
|
||||
// }
|
||||
// uint8_t packet[packetSize];
|
||||
// uint8_t incLORAMAC[2];
|
||||
// LoRa.readBytes((uint8_t *)&packet, packetSize);
|
||||
// // for (int i = 0; i < packetSize; i++) {
|
||||
// // UART_IF.println(packet[i], HEX);
|
||||
// // }
|
||||
|
||||
// //Check if addressed to this device
|
||||
// if (memcmp(&packet, &selfAddress[3], 3) != 0) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// memcpy(&incLORAMAC, &packet[3], 2); //Split off address portion of packet
|
||||
// memcpy(&theData, &packet[5], packetSize - 5); //Split off data portion of packet
|
||||
|
||||
// //Check if it is from a registered sender
|
||||
// if(memcmp(&incLORAMAC, &LoRa1, 2) == 0){
|
||||
// newData = 7;
|
||||
// }
|
||||
// else if(memcmp(&incLORAMAC, &LoRa2, 2) == 0){
|
||||
// newData = 8;
|
||||
// }
|
||||
|
||||
// newData = 6;
|
||||
// ln = (packetSize - 5) / sizeof(DataReading_t);
|
||||
|
||||
// DBG("Incoming LoRa.");
|
||||
// #endif
|
||||
// }
|
||||
|
||||
// void transmitLoRa(uint8_t* mac, DataReading_t * packet, uint8_t len) {
|
||||
// #ifdef USE_LORA
|
||||
// DBG("Transmitting LoRa.");
|
||||
|
||||
// uint8_t pkt[5 + (len * sizeof(DataReading_t))];
|
||||
// memcpy(&pkt, mac, 3);
|
||||
// memcpy(&pkt[3], &selfAddress[4], 2);
|
||||
// memcpy(&pkt[5], packet, len * sizeof(DataReading_t));
|
||||
// LoRa.beginPacket();
|
||||
// LoRa.write((uint8_t*)&pkt, sizeof(pkt));
|
||||
// LoRa.endPacket();
|
||||
// #endif
|
||||
// }
|
||||
|
||||
// void LoRaSend(uint8_t *mac,DataReading_t *buffer, uint16_t *len){
|
||||
|
||||
// DataReading_t thePacket[espnow_size];
|
||||
// int j = 0;
|
||||
// for (int i = 0; i < *len; i++) {
|
||||
// if ( j > espnow_size) {
|
||||
// j = 0;
|
||||
// transmitLoRa(mac, thePacket, j);
|
||||
// }
|
||||
// thePacket[j] = buffer[i];
|
||||
// j++;
|
||||
// }
|
||||
// transmitLoRa(broadcast_mac, thePacket, j);
|
||||
// *len = 0;
|
||||
// }
|
||||
|
||||
// void releaseLoRa(uint8_t interface) {
|
||||
// #ifdef USE_LORA
|
||||
// DBG("Releasing LoRa.");
|
||||
|
||||
// switch (interface) {
|
||||
// case 0:
|
||||
// LoRaSend(broadcast_mac,LORAGbuffer.buffer,&LORAGbuffer.len);
|
||||
// break;
|
||||
// case 1:
|
||||
// LoRaSend(LoRa1,LORA1buffer.buffer,&LORA1buffer.len);
|
||||
// break;
|
||||
// case 2:
|
||||
// LoRaSend(LoRa2,LORA2buffer.buffer,&LORA2buffer.len);
|
||||
// break;
|
||||
// }
|
||||
// #endif
|
||||
// }
|
||||
|
||||
void bufferLoRa(uint8_t interface) {
|
||||
DBG("Buffering LoRa.");
|
||||
switch (interface) {
|
||||
case 0:
|
||||
memcpy(&LORAGbuffer.buffer[LORAGbuffer.len],&theData[0],ln);
|
||||
LORAGbuffer.len += ln;
|
||||
break;
|
||||
case 1:
|
||||
memcpy(&LORA1buffer.buffer[LORA1buffer.len],&theData[0],ln);
|
||||
LORA1buffer.len += ln;
|
||||
break;
|
||||
case 2:
|
||||
memcpy(&LORA2buffer.buffer[LORA2buffer.len],&theData[0],ln);
|
||||
LORA2buffer.len += ln;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void getSerial() {
|
||||
String incomingString = UART_IF.readStringUntil('\n');
|
||||
DynamicJsonDocument doc(24576);
|
||||
DeserializationError error = deserializeJson(doc, incomingString);
|
||||
// Test if parsing succeeds.
|
||||
if (error) {
|
||||
// DBG("json parse err");
|
||||
// DBG(incomingString);
|
||||
return;
|
||||
}
|
||||
|
||||
int s = doc.size();
|
||||
//UART_IF.println(s);
|
||||
for (int i = 0; i < s; i++) {
|
||||
theData[i].id = doc[i]["id"];
|
||||
theData[i].type = doc[i]["type"];
|
||||
theData[i].data = doc[i]["data"];
|
||||
}
|
||||
ln = s;
|
||||
newData = 4;
|
||||
DBG("Incoming Serial.");
|
||||
}
|
||||
|
||||
void sendSerial() {
|
||||
DBG("Sending Serial.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < ln; i++) {
|
||||
doc[i]["id"] = theData[i].id;
|
||||
doc[i]["type"] = theData[i].type;
|
||||
doc[i]["data"] = theData[i].data;
|
||||
}
|
||||
serializeJson(doc, UART_IF);
|
||||
UART_IF.println();
|
||||
|
||||
#ifndef ESP8266
|
||||
serializeJson(doc, Serial);
|
||||
Serial.println();
|
||||
#endif
|
||||
}
|
||||
|
||||
void bufferSerial() {
|
||||
DBG("Buffering Serial.");
|
||||
memcpy(&SERIALbuffer.buffer[SERIALbuffer.len],&theData[0],ln);
|
||||
SERIALbuffer.len += ln;
|
||||
//UART_IF.println("SENDSERIAL:" + String(SERIALbuffer.len) + " ");
|
||||
}
|
||||
|
||||
void releaseSerial() {
|
||||
DBG("Releasing Serial.");
|
||||
DynamicJsonDocument doc(24576);
|
||||
for (int i = 0; i < SERIALbuffer.len; i++) {
|
||||
doc[i]["id"] = SERIALbuffer.buffer[i].id;
|
||||
doc[i]["type"] = SERIALbuffer.buffer[i].type;
|
||||
doc[i]["data"] = SERIALbuffer.buffer[i].data;
|
||||
}
|
||||
serializeJson(doc, UART_IF);
|
||||
UART_IF.println();
|
||||
SERIALbuffer.len = 0;
|
||||
}
|
||||
|
||||
FDRSGateWayBase::FDRSGateWayBase(uint32_t send_delay): _send_delay(send_delay){
|
||||
_object_list.push_back(this);
|
||||
}
|
||||
|
||||
FDRSGateWayBase::~FDRSGateWayBase(){
|
||||
if(_object_list.size() == 0){
|
||||
return;
|
||||
}
|
||||
_object_list.erase(std::find(_object_list.begin(),_object_list.end(),this));
|
||||
}
|
||||
|
||||
void FDRSGateWayBase::release(void){
|
||||
send(_data);
|
||||
}
|
||||
|
||||
for(int i =0; i < _object_list.size();i++){
|
||||
_object_list[i]->send(_data);
|
||||
}
|
||||
void flush(void){
|
||||
_data.clear();
|
||||
|
||||
}
|
||||
|
||||
void FDRSGateWayBase::add_data(DataReading_t *data){
|
||||
_data.push_back(*data);
|
||||
}
|
||||
|
||||
ESP_FDRSGateWay::ESP_FDRSGateWay(uint8_t broadcast_mac[6],uint8_t inturnal_mac[5], uint32_t send_delay) :
|
||||
FDRSGateWayBase(send_delay)
|
||||
ESP_FDRSGateWay::ESP_FDRSGateWay(void)
|
||||
{
|
||||
|
||||
memcpy(_broadcast_mac,broadcast_mac,6);
|
||||
memcpy(_inturnal_mac,inturnal_mac,6);
|
||||
memset(_broadcast_mac,0xFF,6);
|
||||
memset(_inturnal_mac,0,6);
|
||||
|
||||
}
|
||||
|
||||
void ESP_FDRSGateWay::init(void){
|
||||
void ESP_FDRSGateWay::init(uint8_t inturnal_mac[5]){
|
||||
|
||||
memcpy(_inturnal_mac,inturnal_mac,6);
|
||||
|
||||
#if defined(ESP8266)
|
||||
wifi_set_macaddr(STATION_IF, _inturnal_mac);
|
||||
@ -354,7 +181,7 @@ void ESP_FDRSGateWay::add_peer(uint8_t peer_mac[6]){
|
||||
|
||||
list_peer(peer_mac);
|
||||
|
||||
ESP_Peer_t peer;
|
||||
Peer_t peer;
|
||||
peer._copy(peer_mac);
|
||||
//esp_now_del_peer(NEWPEER);
|
||||
|
||||
@ -369,9 +196,9 @@ void ESP_FDRSGateWay::remove_peer(uint8_t peer_mac[6]){
|
||||
if(peer_list.size() == 0){
|
||||
return;
|
||||
}
|
||||
ESP_Peer_t peer;
|
||||
Peer_t peer;
|
||||
peer._copy(peer_mac);
|
||||
//peer_list.erase(std::find(peer_list.begin(),peer_list.end(),peer));
|
||||
peer_list.erase(std::find(peer_list.begin(),peer_list.end(),peer));
|
||||
|
||||
}
|
||||
|
||||
@ -410,7 +237,6 @@ void ESP_FDRSGateWay::send(std::vector<DataReading_t> data){
|
||||
}
|
||||
|
||||
uint8_t d = data.size() / espnow_size;
|
||||
uint8_t r = data.size() % espnow_size;
|
||||
|
||||
DataReading_t buffer1[d];
|
||||
for(i = 0; i < d; i++){
|
||||
@ -419,14 +245,6 @@ void ESP_FDRSGateWay::send(std::vector<DataReading_t> data){
|
||||
|
||||
esp_now_send(NULL, (uint8_t *) buffer1, d * sizeof(DataReading_t));
|
||||
|
||||
for(i = 0; i < r; i++){
|
||||
buffer1[i] = data[i + d];
|
||||
}
|
||||
|
||||
esp_now_send(NULL, (uint8_t *) buffer1, r * sizeof(DataReading_t));
|
||||
|
||||
|
||||
|
||||
for(i = 0; i < unknow_peer.size(); i++){
|
||||
unlist_peer(unknow_peer[i]._data());
|
||||
}
|
||||
@ -435,8 +253,7 @@ void ESP_FDRSGateWay::send(std::vector<DataReading_t> data){
|
||||
|
||||
}
|
||||
|
||||
MQTT_FDRSGateWay::MQTT_FDRSGateWay(uint32_t send_delay, const char *ssid, const char *password, const char *server,int port):
|
||||
FDRSGateWayBase(send_delay),
|
||||
MQTT_FDRSGateWay::MQTT_FDRSGateWay(const char *ssid, const char *password, const char *server,int port):
|
||||
_ssid(ssid),
|
||||
_password(password),
|
||||
_server(server),
|
||||
@ -539,8 +356,7 @@ void MQTT_FDRSGateWay::send(std::vector<DataReading_t> data) {
|
||||
}
|
||||
|
||||
|
||||
Serial_FDRSGateWay::Serial_FDRSGateWay(HardwareSerial *serial, uint32_t baud, uint32_t send_delay):
|
||||
FDRSGateWayBase(send_delay),
|
||||
Serial_FDRSGateWay::Serial_FDRSGateWay(HardwareSerial *serial, uint32_t baud):
|
||||
_serial(serial),
|
||||
_baud(baud)
|
||||
{
|
||||
@ -558,7 +374,7 @@ void Serial_FDRSGateWay::init(int mode, int rx_pin, int tx_pin){
|
||||
#endif
|
||||
|
||||
void Serial_FDRSGateWay::pull(void){
|
||||
//TDDO: this is blocking. Some method of escaping is required.
|
||||
//TDDO: This is blocking. Some method of escaping is required.
|
||||
// At the momment we are just hoping we get a \n
|
||||
String incomingString = _serial->readStringUntil('\n');
|
||||
DynamicJsonDocument doc(24576);
|
||||
@ -585,7 +401,6 @@ void Serial_FDRSGateWay::pull(void){
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Serial_FDRSGateWay::get(void){
|
||||
while(_serial->available()){
|
||||
pull();
|
||||
@ -603,4 +418,134 @@ void Serial_FDRSGateWay::send(std::vector<DataReading_t> data){
|
||||
}
|
||||
serializeJson(doc, *_serial);
|
||||
_serial->println();
|
||||
}
|
||||
}
|
||||
|
||||
LoRa_FDRSGateWay::LoRa_FDRSGateWay(uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,double band,uint8_t sf):
|
||||
_miso(miso),
|
||||
_mosi(mosi),
|
||||
_sck(sck),
|
||||
_ss(ss),
|
||||
_rst(rst),
|
||||
_dio0(dio0),
|
||||
_band(band),
|
||||
_sf(sf)
|
||||
{
|
||||
memset(_mac,0,6);
|
||||
_peer_list.clear();
|
||||
}
|
||||
|
||||
void LoRa_FDRSGateWay::init(uint8_t mac[6]){
|
||||
|
||||
memcpy(_mac,mac,6);
|
||||
|
||||
DBG("Initializing LoRa!");
|
||||
SPI.begin(_sck, _miso, _mosi, _ss);
|
||||
LoRa.setPins(_ss, _rst, _dio0);
|
||||
if (!LoRa.begin(_band)) {
|
||||
DBG(" LoRa initialize failed");
|
||||
return;
|
||||
}
|
||||
LoRa.setSpreadingFactor(_sf);
|
||||
DBG(" LoRa initialized.");
|
||||
}
|
||||
|
||||
void LoRa_FDRSGateWay::add_peer(uint8_t peer_mac[6]){
|
||||
|
||||
uint32_t i = 0;
|
||||
|
||||
for(uint32_t i = 0; i < _peer_list.size();i++){
|
||||
if(memcmp(_peer_list[i]._data(),peer_mac,6) == 0){
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Peer_t peer;
|
||||
peer._copy(peer_mac);
|
||||
_peer_list.push_back(peer);
|
||||
|
||||
}
|
||||
|
||||
void LoRa_FDRSGateWay::remove_peer(uint8_t peer_mac[6]){
|
||||
|
||||
if(_peer_list.size() == 0){
|
||||
return;
|
||||
}
|
||||
Peer_t peer;
|
||||
peer._copy(peer_mac);
|
||||
_peer_list.erase(std::find(_peer_list.begin(),_peer_list.end(),peer));
|
||||
|
||||
}
|
||||
|
||||
void LoRa_FDRSGateWay::get(void){
|
||||
|
||||
int packetSize = LoRa.parsePacket();
|
||||
if (packetSize== 0) {
|
||||
return;
|
||||
}
|
||||
uint8_t packet[packetSize];
|
||||
uint8_t incLORAMAC[2];
|
||||
LoRa.readBytes((uint8_t *)&packet, packetSize);
|
||||
// for (int i = 0; i < packetSize; i++) {
|
||||
// UART_IF.println(packet[i], HEX);
|
||||
// }
|
||||
|
||||
//Check if addressed to this device
|
||||
if (memcmp(&packet, &_mac[3], 3) != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(&incLORAMAC, &packet[3], 2); //Split off address portion of packet
|
||||
memcpy(&theData, &packet[5], packetSize - 5); //Split off data portion of packet
|
||||
|
||||
//Check if it is from a registered sender
|
||||
for(uint32_t i = 0; i < _peer_list.size();i++){
|
||||
if(memcmp(&_peer_list[i].peer[3],incLORAMAC,2) == 0){
|
||||
DataReading_t data;
|
||||
uint32_t i = 0;
|
||||
uint8_t d = packetSize / sizeof(DataReading_t);
|
||||
|
||||
memset(&data,0,sizeof(DataReading_t));
|
||||
|
||||
for(i = 0; i < d; i++){
|
||||
memcpy(&data,&theData[i*sizeof(DataReading_t)],sizeof(DataReading_t));
|
||||
FDRSGateWayBase::add_data(&data);
|
||||
memset(&data,0,sizeof(DataReading_t));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
DBG("Incoming LoRa.");
|
||||
|
||||
}
|
||||
|
||||
void LoRa_FDRSGateWay::send(std::vector<DataReading_t> data){
|
||||
|
||||
const uint8_t espnow_size = 250 / sizeof(DataReading_t);
|
||||
|
||||
uint32_t i = 0;
|
||||
uint8_t d = data.size() / espnow_size;
|
||||
|
||||
DataReading_t buffer1[d];
|
||||
for(i = 0; i < d; i++){
|
||||
buffer1[i] = data[i];
|
||||
}
|
||||
|
||||
transmit(buffer1, d * sizeof(DataReading_t));
|
||||
|
||||
}
|
||||
|
||||
void LoRa_FDRSGateWay::transmit(DataReading_t *packet, uint8_t len) {
|
||||
DBG("Transmitting LoRa.");
|
||||
uint8_t pkt[5 + (len * sizeof(DataReading_t))];
|
||||
memcpy(&pkt, _mac, 3);
|
||||
memcpy(&pkt[3], &_mac[4], 2);
|
||||
memcpy(&pkt[5], packet, len * sizeof(DataReading_t));
|
||||
LoRa.beginPacket();
|
||||
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
|
||||
LoRa.endPacket();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
133
fdrs_gateway.h
133
fdrs_gateway.h
@ -14,116 +14,37 @@
|
||||
#include "PubSubClient.h"
|
||||
#include "LoRa.h"
|
||||
|
||||
|
||||
#define USE_LORA
|
||||
|
||||
#ifdef DEBUG
|
||||
#define DBG(a) (Serial.println(a))
|
||||
#else
|
||||
#define DBG(a)
|
||||
#endif
|
||||
|
||||
#if defined (ESP32)
|
||||
#define UART_IF Serial1
|
||||
#else
|
||||
#define UART_IF Serial
|
||||
#endif
|
||||
|
||||
#ifdef GLOBALS
|
||||
#define FDRS_WIFI_SSID GLOBAL_SSID
|
||||
#define FDRS_WIFI_PASS GLOBAL_PASS
|
||||
#define FDRS_MQTT_ADDR GLOBAL_MQTT_ADDR
|
||||
#define FDRS_BAND GLOBAL_BAND
|
||||
#define FDRS_SF GLOBAL_SF
|
||||
#else
|
||||
#define FDRS_WIFI_SSID WIFI_SSID
|
||||
#define FDRS_WIFI_PASS WIFI_PASS
|
||||
#define FDRS_MQTT_ADDR MQTT_ADDR
|
||||
#define FDRS_BAND BAND
|
||||
#define FDRS_SF SF
|
||||
#endif
|
||||
|
||||
#define USE_LORA
|
||||
|
||||
#define MAC_PREFIX 0xAA, 0xBB, 0xCC, 0xDD, 0xEE // Should only be changed if implementing multiple FDRS systems.
|
||||
|
||||
|
||||
|
||||
const uint8_t espnow_size = 250 / sizeof(DataReading_t);
|
||||
const uint8_t lora_size = 256 / sizeof(DataReading_t);
|
||||
// const uint8_t mac_prefix[] = {MAC_PREFIX};
|
||||
|
||||
// uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
// uint8_t selfAddress[] = {MAC_PREFIX, UNIT_MAC};
|
||||
// uint8_t incMAC[6];
|
||||
|
||||
// #ifdef ESPNOW1_PEER
|
||||
// uint8_t ESPNOW1[] = {MAC_PREFIX, ESPNOW1_PEER};
|
||||
// #else
|
||||
// uint8_t ESPNOW1[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
// #endif
|
||||
// #ifdef ESPNOW2_PEER
|
||||
// uint8_t ESPNOW2[] = {MAC_PREFIX, ESPNOW2_PEER};
|
||||
// #else
|
||||
// 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};
|
||||
// #endif
|
||||
|
||||
|
||||
//WiFiClient espClient;
|
||||
#ifdef USE_LED
|
||||
CRGB leds[NUM_LEDS];
|
||||
#endif
|
||||
#ifdef USE_WIFI
|
||||
PubSubClient client(espClient);
|
||||
const char* ssid = FDRS_WIFI_SSID;
|
||||
const char* password = FDRS_WIFI_PASS;
|
||||
const char* mqtt_server = FDRS_MQTT_ADDR;
|
||||
#endif
|
||||
|
||||
void getLoRa();
|
||||
|
||||
void bufferLoRa(uint8_t interface);
|
||||
|
||||
void transmitLoRa(uint8_t* mac, DataReading_t * packet, uint8_t len);
|
||||
|
||||
void releaseLoRa(uint8_t interface);
|
||||
|
||||
void getSerial(void);
|
||||
|
||||
void sendSerial();
|
||||
|
||||
void bufferSerial();
|
||||
|
||||
void releaseSerial();
|
||||
|
||||
|
||||
class FDRSGateWayBase{
|
||||
public:
|
||||
|
||||
FDRSGateWayBase(uint32_t send_delay);
|
||||
FDRSGateWayBase();
|
||||
~FDRSGateWayBase();
|
||||
|
||||
static void add_data(DataReading_t *data);
|
||||
|
||||
void release(void);
|
||||
void flush(void);
|
||||
|
||||
private:
|
||||
uint32_t _send_delay;
|
||||
static uint32_t peer_id;
|
||||
static std::vector<DataReading_t> _data;
|
||||
static std::vector<FDRSGateWayBase*> _object_list;
|
||||
|
||||
virtual void send(std::vector<DataReading_t> data) = 0;
|
||||
};
|
||||
|
||||
class ESP_FDRSGateWay: public FDRSGateWayBase{
|
||||
public:
|
||||
ESP_FDRSGateWay(uint8_t broadcast_mac[6],uint8_t inturnal_mac[5], uint32_t send_delay);
|
||||
ESP_FDRSGateWay(void);
|
||||
|
||||
void init(void);
|
||||
void init(uint8_t inturnal_mac[5]);
|
||||
|
||||
void add_peer(uint8_t peer_mac[6]);
|
||||
void remove_peer(uint8_t peer_mac[6]);
|
||||
@ -134,8 +55,8 @@ private:
|
||||
static bool is_init;
|
||||
uint8_t _broadcast_mac[6];
|
||||
uint8_t _inturnal_mac[6];
|
||||
static std::vector<ESP_Peer_t> peer_list;
|
||||
static std::vector<ESP_Peer_t> unknow_peer;
|
||||
static std::vector<Peer_t> peer_list;
|
||||
static std::vector<Peer_t> unknow_peer;
|
||||
|
||||
static void setup(void);
|
||||
|
||||
@ -151,7 +72,7 @@ class MQTT_FDRSGateWay: public FDRSGateWayBase{
|
||||
|
||||
public:
|
||||
|
||||
MQTT_FDRSGateWay(uint32_t send_delay, const char *ssid, const char *password, const char *server,int port = 1883);
|
||||
MQTT_FDRSGateWay(const char *ssid, const char *password, const char *server,int port = 1883);
|
||||
~MQTT_FDRSGateWay(void);
|
||||
|
||||
void init(void);
|
||||
@ -179,7 +100,7 @@ private:
|
||||
class Serial_FDRSGateWay: public FDRSGateWayBase{
|
||||
|
||||
public:
|
||||
Serial_FDRSGateWay(HardwareSerial *serial, uint32_t baud, uint32_t send_delay);
|
||||
Serial_FDRSGateWay(HardwareSerial *serial, uint32_t baud);
|
||||
|
||||
void init(void);
|
||||
#if defined(ESP32)
|
||||
@ -197,5 +118,37 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class LoRa_FDRSGateWay: public FDRSGateWayBase{
|
||||
|
||||
public:
|
||||
LoRa_FDRSGateWay(uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,double band,uint8_t sf);
|
||||
|
||||
void init(uint8_t mac[6]);
|
||||
void get(void);
|
||||
void add_peer(uint8_t peer_mac[6]);
|
||||
void remove_peer(uint8_t peer_mac[6]);
|
||||
|
||||
private:
|
||||
|
||||
uint8_t _mac[6];
|
||||
uint8_t _miso;
|
||||
uint8_t _mosi;
|
||||
uint8_t _sck;
|
||||
uint8_t _ss;
|
||||
uint8_t _rst;
|
||||
uint8_t _dio0;
|
||||
uint32_t _band;
|
||||
uint8_t _sf;
|
||||
|
||||
std::vector<Peer_t> _peer_list;
|
||||
|
||||
void transmit(DataReading_t *packet, uint8_t len);
|
||||
|
||||
|
||||
void send(std::vector<DataReading_t> data) override;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
13
fdrs_types.h
13
fdrs_types.h
@ -25,10 +25,10 @@ typedef struct DataReadingBuffer_t{
|
||||
}DataReadingBuffer_t;
|
||||
|
||||
|
||||
typedef struct ESP_Peer_t{
|
||||
typedef struct Peer_t{
|
||||
|
||||
uint8_t peer[6];
|
||||
ESP_Peer_t(){
|
||||
Peer_t(){
|
||||
memset(peer,0,6);
|
||||
}
|
||||
|
||||
@ -39,6 +39,13 @@ typedef struct ESP_Peer_t{
|
||||
return peer;
|
||||
}
|
||||
|
||||
}ESP_Peer_t;
|
||||
bool operator==(Peer_t c){
|
||||
if(memcmp(this->peer,c.peer,6) == 0){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}Peer_t;
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user