mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-10 07:10:42 +00:00
update global fdrs functions
This commit is contained in:
parent
2eb8755ce9
commit
e74c7f22fe
115
fdrs_functions.h
115
fdrs_functions.h
@ -78,6 +78,11 @@ uint8_t LoRa2[] = {mac_prefix[3], mac_prefix[4], LORA2_PEER};
|
|||||||
//uint8_t LoRaAddress[] = {0x42, 0x00};
|
//uint8_t LoRaAddress[] = {0x42, 0x00};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_SD_LOG
|
||||||
|
unsigned long last_millis = 0;
|
||||||
|
unsigned long seconds_since_reset = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
DataReading theData[256];
|
DataReading theData[256];
|
||||||
uint8_t ln;
|
uint8_t ln;
|
||||||
uint8_t newData = 0;
|
uint8_t newData = 0;
|
||||||
@ -113,6 +118,8 @@ CRGB leds[NUM_LEDS];
|
|||||||
#endif
|
#endif
|
||||||
#ifdef USE_WIFI
|
#ifdef USE_WIFI
|
||||||
PubSubClient client(espClient);
|
PubSubClient client(espClient);
|
||||||
|
WiFiUDP ntpUDP;
|
||||||
|
NTPClient timeClient(ntpUDP);
|
||||||
const char* ssid = FDRS_WIFI_SSID;
|
const char* ssid = FDRS_WIFI_SSID;
|
||||||
const char* password = FDRS_WIFI_PASS;
|
const char* password = FDRS_WIFI_PASS;
|
||||||
const char* mqtt_server = FDRS_MQTT_ADDR;
|
const char* mqtt_server = FDRS_MQTT_ADDR;
|
||||||
@ -126,6 +133,8 @@ const char* mqtt_user = NULL;
|
|||||||
const char* mqtt_pass = NULL;
|
const char* mqtt_pass = NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
|
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
|
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
|
||||||
@ -166,6 +175,56 @@ void getSerial() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void sendSD(const char filename[32]) {
|
||||||
|
#ifdef USE_SD_LOG
|
||||||
|
DBG("Logging to SD card.");
|
||||||
|
File logfile = SD.open(filename, FILE_WRITE);
|
||||||
|
for (int i = 0; i < ln; i++) {
|
||||||
|
#ifdef USE_WIFI
|
||||||
|
logfile.print(timeClient.getEpochTime());
|
||||||
|
#else
|
||||||
|
logfile.print(seconds_since_reset);
|
||||||
|
#endif
|
||||||
|
logfile.print(",");
|
||||||
|
logfile.print(theData[i].id);
|
||||||
|
logfile.print(",");
|
||||||
|
logfile.print(theData[i].t);
|
||||||
|
logfile.print(",");
|
||||||
|
logfile.println(theData[i].d);
|
||||||
|
}
|
||||||
|
logfile.close();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
void reconnect(int attempts, bool silent) {
|
||||||
|
#ifdef USE_WIFI
|
||||||
|
|
||||||
|
if(!silent) DBG("Connecting MQTT...");
|
||||||
|
|
||||||
|
for (int i = 1; i<=attempts; i++) {
|
||||||
|
// Attempt to connect
|
||||||
|
if (client.connect("FDRS_GATEWAY", mqtt_user, mqtt_pass)) {
|
||||||
|
// Subscribe
|
||||||
|
client.subscribe(TOPIC_COMMAND);
|
||||||
|
if(!silent) DBG(" MQTT Connected");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
if(!silent) {
|
||||||
|
char msg[15];
|
||||||
|
sprintf(msg, " Attempt %d/%d",i,attempts);
|
||||||
|
DBG(msg);
|
||||||
|
}
|
||||||
|
if(attempts=!1){
|
||||||
|
delay(3000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!silent) DBG(" Connecting MQTT failed.");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
void reconnect(int attempts){
|
||||||
|
reconnect(attempts, false);
|
||||||
|
}
|
||||||
void mqtt_callback(char* topic, byte * message, unsigned int length) {
|
void mqtt_callback(char* topic, byte * message, unsigned int length) {
|
||||||
String incomingString;
|
String incomingString;
|
||||||
DBG(topic);
|
DBG(topic);
|
||||||
@ -192,6 +251,14 @@ void mqtt_callback(char* topic, byte * message, unsigned int length) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void mqtt_publish(const char* payload){
|
||||||
|
#ifdef USE_WIFI
|
||||||
|
if(!client.publish(TOPIC_DATA, payload)){
|
||||||
|
DBG(" Error on sending MQTT");
|
||||||
|
sendSD(SD_FILENAME);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void getLoRa() {
|
void getLoRa() {
|
||||||
#ifdef USE_LORA
|
#ifdef USE_LORA
|
||||||
@ -275,7 +342,7 @@ void sendMQTT() {
|
|||||||
}
|
}
|
||||||
String outgoingString;
|
String outgoingString;
|
||||||
serializeJson(doc, outgoingString);
|
serializeJson(doc, outgoingString);
|
||||||
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
|
mqtt_publish((char*) outgoingString.c_str());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -496,25 +563,10 @@ void releaseMQTT() {
|
|||||||
}
|
}
|
||||||
String outgoingString;
|
String outgoingString;
|
||||||
serializeJson(doc, outgoingString);
|
serializeJson(doc, outgoingString);
|
||||||
client.publish(TOPIC_DATA, (char*) outgoingString.c_str());
|
mqtt_publish((char*) outgoingString.c_str());
|
||||||
lenMQTT = 0;
|
lenMQTT = 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
void reconnect() {
|
|
||||||
#ifdef USE_WIFI
|
|
||||||
// Loop until reconnected
|
|
||||||
while (!client.connected()) {
|
|
||||||
// Attempt to connect
|
|
||||||
if (client.connect("FDRS_GATEWAY", mqtt_user, mqtt_pass)) {
|
|
||||||
// Subscribe
|
|
||||||
client.subscribe(TOPIC_COMMAND);
|
|
||||||
} else {
|
|
||||||
DBG("Connecting MQTT.");
|
|
||||||
delay(5000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
void begin_espnow() {
|
void begin_espnow() {
|
||||||
DBG("Initializing ESP-NOW!");
|
DBG("Initializing ESP-NOW!");
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
@ -570,3 +622,32 @@ void begin_espnow() {
|
|||||||
#endif
|
#endif
|
||||||
DBG(" ESP-NOW Initialized.");
|
DBG(" ESP-NOW Initialized.");
|
||||||
}
|
}
|
||||||
|
void begin_lora(){
|
||||||
|
#ifdef USE_LORA
|
||||||
|
DBG("Initializing LoRa!");
|
||||||
|
#ifdef ESP32
|
||||||
|
SPI.begin(SCK, MISO, MOSI);
|
||||||
|
#endif
|
||||||
|
LoRa.setPins(SS, RST, DIO0);
|
||||||
|
if (!LoRa.begin(FDRS_BAND)) {
|
||||||
|
DBG(" Initialization failed!");
|
||||||
|
while (1);
|
||||||
|
}
|
||||||
|
LoRa.setSpreadingFactor(FDRS_SF);
|
||||||
|
DBG(" LoRa initialized.");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
void begin_SD(){
|
||||||
|
#ifdef USE_SD_LOG
|
||||||
|
DBG("Initializing SD card...");
|
||||||
|
#ifdef ESP32
|
||||||
|
SPI.begin(SCK, MISO, MOSI);
|
||||||
|
#endif
|
||||||
|
if (!SD.begin(SD_SS)) {
|
||||||
|
DBG(" Initialization failed!");
|
||||||
|
while (1);
|
||||||
|
}else{
|
||||||
|
DBG(" SD initialized.");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user