serial compiles

This commit is contained in:
DevilBinder 2022-06-24 14:54:55 +02:00
parent 27a199cb3b
commit 3663c9db26
3 changed files with 105 additions and 15 deletions

View File

@ -55,6 +55,12 @@ MQTT_FDRSGateWay MQTT(1000,WIFI_SSID,WIFI_PASS,MQTT_ADDR,MQTT_PORT);
ESP_FDRSGateWay ESPNow(broadcast_mac,selfAddress,1000); ESP_FDRSGateWay ESPNow(broadcast_mac,selfAddress,1000);
#if defined(ESP32)
Serial_FDRSGateWay SerialGW(&Serial1,115200,1000);
#else
Serial_FDRSGateWay SerialGW(&Serial,115200,1000);
#endif
void setup() { void setup() {
#ifdef USE_LED #ifdef USE_LED
@ -74,10 +80,12 @@ void setup() {
#ifdef ESPNOW_PEER_2 #ifdef ESPNOW_PEER_2
ESPNow.add_peer(ESPNOW2); ESPNow.add_peer(ESPNOW2);
#endif #endif
#endif #endif
#if defined(ESP32)
SerialGW.init(SERIAL_8N1,RXD2,TXD2);
#else
SerialGW.init();
#endif
// #ifdef USE_LORA // #ifdef USE_LORA
// DBG("Initializing LoRa!"); // DBG("Initializing LoRa!");
// SPI.begin(SCK, MISO, MOSI, SS); // SPI.begin(SCK, MISO, MOSI, SS);
@ -95,8 +103,10 @@ void setup() {
void loop() { void loop() {
ESPNow.release();
SerialGW.get();
ESPNow.release();
#ifdef ESPNOWG_DELAY #ifdef ESPNOWG_DELAY
if (millis() > timeESPNOWG) { if (millis() > timeESPNOWG) {
timeESPNOWG += ESPNOWG_DELAY; timeESPNOWG += ESPNOWG_DELAY;
@ -147,9 +157,7 @@ void loop() {
// } // }
// #endif // #endif
while (UART_IF.available()) {
getSerial();
}
// getLoRa(); // getLoRa();
} }

View File

@ -12,8 +12,6 @@ bool ESP_FDRSGateWay::is_init = false;
std::vector<ESP_Peer_t> ESP_FDRSGateWay::peer_list; std::vector<ESP_Peer_t> ESP_FDRSGateWay::peer_list;
std::vector<ESP_Peer_t> ESP_FDRSGateWay::unknow_peer; std::vector<ESP_Peer_t> ESP_FDRSGateWay::unknow_peer;
bool MQTT_FDRSGateWay::is_init = false;
uint8_t newData = 0; uint8_t newData = 0;
uint8_t ln = 0; uint8_t ln = 0;
DataReading_t theData[256]; DataReading_t theData[256];
@ -483,8 +481,6 @@ void MQTT_FDRSGateWay::mqtt_callback(char* topic, byte * message, unsigned int l
FDRSGateWayBase::add_data(&data); FDRSGateWayBase::add_data(&data);
memset(&data,0,sizeof(DataReading_t)); memset(&data,0,sizeof(DataReading_t));
} }
ln = s;
newData = 5;
DBG("Incoming MQTT."); DBG("Incoming MQTT.");
} }
@ -540,4 +536,71 @@ void MQTT_FDRSGateWay::send(std::vector<DataReading_t> data) {
reconnect(); reconnect();
_client->loop(); _client->loop();
}
Serial_FDRSGateWay::Serial_FDRSGateWay(HardwareSerial *serial, uint32_t baud, uint32_t send_delay):
FDRSGateWayBase(send_delay),
_serial(serial),
_baud(baud)
{
}
void Serial_FDRSGateWay::init(void){
_serial->begin(_baud);
}
#if defined(ESP32)
void Serial_FDRSGateWay::init(int mode, int rx_pin, int tx_pin){
_serial->begin(_baud,mode,rx_pin,tx_pin);
}
#endif
void Serial_FDRSGateWay::pull(void){
//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);
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);
DataReading_t data;
memset(&data,0,sizeof(DataReading_t));
for (int i = 0; i < s; i++) {
data.id = doc[i]["id"];
data.type = doc[i]["type"];
data.data = doc[i]["data"];
FDRSGateWayBase::add_data(&data);
memset(&data,0,sizeof(DataReading_t));
}
DBG("Incoming Serial.");
}
void Serial_FDRSGateWay::get(void){
while(_serial->available()){
pull();
}
}
void Serial_FDRSGateWay::send(std::vector<DataReading_t> data){
DBG("Releasing Serial.");
DynamicJsonDocument doc(24576);
for (int i = 0; i < data.size(); i++) {
doc[i]["id"] = data[i].id;
doc[i]["type"] = data[i].type;
doc[i]["data"] = data[i].data;
}
serializeJson(doc, *_serial);
_serial->println();
} }

View File

@ -1,6 +1,7 @@
#ifndef __FDRS_GATEWAY_H__ #ifndef __FDRS_GATEWAY_H__
#define __FDRS_GATEWAY_H__ #define __FDRS_GATEWAY_H__
#include "HardwareSerial.h"
#include "fdrs_types.h" #include "fdrs_types.h"
#include <string.h> #include <string.h>
#include <vector> #include <vector>
@ -169,14 +170,32 @@ private:
WiFiClient espClient; WiFiClient espClient;
PubSubClient *_client; PubSubClient *_client;
static bool is_init;
static void setup(void);
void reconnect(); void reconnect();
void send(std::vector<DataReading_t> data) override; void send(std::vector<DataReading_t> data) override;
};
class Serial_FDRSGateWay: public FDRSGateWayBase{
public:
Serial_FDRSGateWay(HardwareSerial *serial, uint32_t baud, uint32_t send_delay);
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;
}; };
#endif #endif