2022-06-24 16:31:50 +00:00
|
|
|
/* FARM DATA RELAY SYSTEM
|
|
|
|
*
|
|
|
|
* "fdrs_sensor.h"
|
|
|
|
*
|
|
|
|
* Developed by Timm Bogner (timmbogner@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
|
|
|
* Condensed and refactored to a single file pair by Binder Tronics (info@bindertronics.com).
|
|
|
|
*/
|
|
|
|
|
2022-06-21 19:12:10 +00:00
|
|
|
#ifndef __FDRS_GATEWAY_H__
|
|
|
|
#define __FDRS_GATEWAY_H__
|
|
|
|
|
2022-06-24 12:54:55 +00:00
|
|
|
#include "HardwareSerial.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-23 22:39:26 +00:00
|
|
|
#include "LoRa.h"
|
2022-06-21 20:31:47 +00:00
|
|
|
|
2022-06-24 15:40:06 +00:00
|
|
|
|
|
|
|
#define USE_LORA
|
|
|
|
|
2022-06-21 19:00:16 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define DBG(a) (Serial.println(a))
|
|
|
|
#else
|
|
|
|
#define DBG(a)
|
|
|
|
#endif
|
|
|
|
|
2022-06-22 23:06:13 +00:00
|
|
|
class FDRSGateWayBase{
|
|
|
|
public:
|
|
|
|
|
2022-06-24 15:40:06 +00:00
|
|
|
FDRSGateWayBase();
|
2022-06-22 23:06:13 +00:00
|
|
|
~FDRSGateWayBase();
|
|
|
|
|
|
|
|
static void add_data(DataReading_t *data);
|
|
|
|
|
|
|
|
void release(void);
|
2022-06-24 15:40:06 +00:00
|
|
|
void flush(void);
|
2022-06-22 23:06:13 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static uint32_t peer_id;
|
|
|
|
static std::vector<DataReading_t> _data;
|
|
|
|
virtual void send(std::vector<DataReading_t> data) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ESP_FDRSGateWay: public FDRSGateWayBase{
|
|
|
|
public:
|
2022-06-24 15:40:06 +00:00
|
|
|
ESP_FDRSGateWay(void);
|
2022-06-22 23:06:13 +00:00
|
|
|
|
2022-06-24 15:40:06 +00:00
|
|
|
void init(uint8_t inturnal_mac[5]);
|
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-24 15:40:06 +00:00
|
|
|
static std::vector<Peer_t> peer_list;
|
|
|
|
static std::vector<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-24 15:40:06 +00:00
|
|
|
MQTT_FDRSGateWay(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;
|
|
|
|
|
|
|
|
void reconnect();
|
|
|
|
void send(std::vector<DataReading_t> data) override;
|
|
|
|
|
2022-06-24 12:54:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Serial_FDRSGateWay: public FDRSGateWayBase{
|
|
|
|
|
|
|
|
public:
|
2022-06-24 15:40:06 +00:00
|
|
|
Serial_FDRSGateWay(HardwareSerial *serial, uint32_t baud);
|
2022-06-24 12:54:55 +00:00
|
|
|
|
|
|
|
void init(void);
|
|
|
|
#if defined(ESP32)
|
|
|
|
void init(int mode, int rx_pin, int tx_pin);
|
|
|
|
#endif
|
|
|
|
void get(void);
|
|
|
|
private:
|
|
|
|
|
|
|
|
HardwareSerial *_serial;
|
|
|
|
uint32_t _baud;
|
|
|
|
static void setup(void);
|
|
|
|
void pull(void);
|
|
|
|
void send(std::vector<DataReading_t> data) override;
|
2022-06-23 21:40:35 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2022-06-24 12:54:55 +00:00
|
|
|
|
2022-06-24 15:40:06 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-06-24 12:54:55 +00:00
|
|
|
|
2022-06-21 19:12:10 +00:00
|
|
|
#endif
|