2022-06-21 19:12:10 +00:00
|
|
|
#ifndef __FDRS_GATEWAY_H__
|
|
|
|
#define __FDRS_GATEWAY_H__
|
|
|
|
|
2022-06-21 20:31:47 +00:00
|
|
|
#include "fdrs_types.h"
|
2022-06-22 23:06:13 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <esp_now.h>
|
|
|
|
#include <WiFi.h>
|
|
|
|
#include <esp_wifi.h>
|
|
|
|
#include "ArduinoJson.h"
|
2022-06-23 21:40:35 +00:00
|
|
|
#include "PubSubClient.h"
|
2022-06-21 20:31:47 +00:00
|
|
|
|
2022-06-21 19:00:16 +00:00
|
|
|
#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.
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-06-22 23:06:13 +00:00
|
|
|
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};
|
2022-06-21 19:00:16 +00:00
|
|
|
|
2022-06-22 23:06:13 +00:00
|
|
|
// uint8_t broadcast_mac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
|
|
|
// uint8_t selfAddress[] = {MAC_PREFIX, UNIT_MAC};
|
|
|
|
// uint8_t incMAC[6];
|
2022-06-21 19:00:16 +00:00
|
|
|
|
2022-06-22 23:06:13 +00:00
|
|
|
// #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
|
2022-06-21 19:00:16 +00:00
|
|
|
|
2022-06-22 23:06:13 +00:00
|
|
|
// #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
|
2022-06-21 19:00:16 +00:00
|
|
|
|
|
|
|
|
2022-06-23 20:31:51 +00:00
|
|
|
//WiFiClient espClient;
|
2022-06-21 19:00:16 +00:00
|
|
|
#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 getSerial(void);
|
|
|
|
|
|
|
|
void mqtt_callback(char* topic, byte * message, unsigned int length);
|
|
|
|
|
|
|
|
void getLoRa();
|
|
|
|
|
|
|
|
void sendSerial();
|
|
|
|
|
2022-06-21 20:31:47 +00:00
|
|
|
void sendMQTT();
|
2022-06-21 19:00:16 +00:00
|
|
|
|
2022-06-21 20:31:47 +00:00
|
|
|
void bufferESPNOW(uint8_t interface);
|
2022-06-21 19:00:16 +00:00
|
|
|
|
2022-06-21 20:31:47 +00:00
|
|
|
void bufferSerial();
|
2022-06-21 19:00:16 +00:00
|
|
|
|
2022-06-22 23:06:13 +00:00
|
|
|
void bufferLoRa(uint8_t interface);
|
2022-06-21 19:00:16 +00:00
|
|
|
|
2022-06-22 23:06:13 +00:00
|
|
|
void transmitLoRa(uint8_t* mac, DataReading_t * packet, uint8_t len);
|
2022-06-21 19:00:16 +00:00
|
|
|
|
2022-06-21 20:31:47 +00:00
|
|
|
void releaseLoRa(uint8_t interface);
|
2022-06-21 19:00:16 +00:00
|
|
|
|
2022-06-21 20:31:47 +00:00
|
|
|
void releaseSerial();
|
2022-06-21 19:00:16 +00:00
|
|
|
|
2022-06-21 20:31:47 +00:00
|
|
|
void releaseMQTT();
|
2022-06-21 19:00:16 +00:00
|
|
|
|
2022-06-21 20:31:47 +00:00
|
|
|
void reconnect();
|
2022-06-21 19:12:10 +00:00
|
|
|
|
2022-06-22 23:06:13 +00:00
|
|
|
|
|
|
|
class FDRSGateWayBase{
|
|
|
|
public:
|
|
|
|
|
|
|
|
FDRSGateWayBase(uint32_t send_delay);
|
|
|
|
~FDRSGateWayBase();
|
|
|
|
|
|
|
|
static void add_data(DataReading_t *data);
|
|
|
|
|
|
|
|
void release(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);
|
|
|
|
|
2022-06-23 22:14:36 +00:00
|
|
|
void init(void);
|
2022-06-22 23:06:13 +00:00
|
|
|
|
|
|
|
void add_peer(uint8_t peer_mac[6]);
|
2022-06-23 20:31:51 +00:00
|
|
|
void remove_peer(uint8_t peer_mac[6]);
|
2022-06-22 23:06:13 +00:00
|
|
|
static void OnDataRecv(uint8_t * mac, const uint8_t *incomingData, int len);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
static bool is_init;
|
|
|
|
uint8_t _broadcast_mac[6];
|
|
|
|
uint8_t _inturnal_mac[6];
|
2022-06-23 20:31:51 +00:00
|
|
|
static std::vector<ESP_Peer_t> peer_list;
|
|
|
|
static std::vector<ESP_Peer_t> unknow_peer;
|
2022-06-22 23:06:13 +00:00
|
|
|
|
|
|
|
static void setup(void);
|
|
|
|
|
2022-06-23 21:40:35 +00:00
|
|
|
void send(std::vector<DataReading_t> data) override;
|
2022-06-22 23:06:13 +00:00
|
|
|
|
2022-06-23 20:31:51 +00:00
|
|
|
void list_peer(uint8_t peer_mac[6]);
|
|
|
|
void unlist_peer(uint8_t peer_mac[6]);
|
|
|
|
|
2022-06-22 23:06:13 +00:00
|
|
|
};
|
|
|
|
|
2022-06-23 21:40:35 +00:00
|
|
|
|
|
|
|
class MQTT_FDRSGateWay: public FDRSGateWayBase{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2022-06-23 22:14:36 +00:00
|
|
|
MQTT_FDRSGateWay(uint32_t send_delay, const char *ssid, const char *password, const char *server,int port = 1883);
|
2022-06-23 21:40:35 +00:00
|
|
|
~MQTT_FDRSGateWay(void);
|
|
|
|
|
2022-06-23 22:14:36 +00:00
|
|
|
void init(void);
|
2022-06-23 21:40:35 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
#define TOPIC_DATA "fdrs/data"
|
|
|
|
#define TOPIC_STATUS "fdrs/status"
|
|
|
|
#define TOPIC_COMMAND "fdrs/command"
|
|
|
|
|
|
|
|
static void mqtt_callback(char* topic, byte * message, unsigned int length);
|
|
|
|
|
2022-06-23 22:14:36 +00:00
|
|
|
const char *_ssid;
|
|
|
|
const char *_password;
|
|
|
|
const char *_server;
|
2022-06-23 21:40:35 +00:00
|
|
|
int _port;
|
|
|
|
WiFiClient espClient;
|
|
|
|
PubSubClient *_client;
|
|
|
|
|
|
|
|
static bool is_init;
|
|
|
|
|
|
|
|
static void setup(void);
|
|
|
|
|
|
|
|
void reconnect();
|
|
|
|
void send(std::vector<DataReading_t> data) override;
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2022-06-21 19:12:10 +00:00
|
|
|
#endif
|