smart_garden/src/connections.cpp

229 lines
6.6 KiB
C++

#include <ArduinoJson.h>
#include <AutoConnect.h>
#include <PubSubClient.h>
#include <WebServer.h>
#include <WiFi.h>
#include <common.h>
#include <store.h>
#include <valve.h>
#include <ntpManager.h>
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#include <connections.h>
char MQTT_VALVE_COMMAND[72];
char MQTT_SOIL_PROPERTIES[72];
char MQTT_LIGHT_PROPERTIES[72];
char MQTT_AUTO_PROPERTIES[72];
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
TaskHandle_t mqttTask;
WebServer Server;
AutoConnect Portal(Server);
WiFiClient client;
AutoConnectConfig Config;
PubSubClient mqttClient(client);
// spins wifi up and activates green status light when wifi is conencted
void connectWiFi() {
yield();
Serial.println("Start WiFi...");
if (Portal.begin()) {
digitalWrite(PIN_LED_G, HIGH);
}
}
// handles MQTT messages and keeps track of MQTT connection. starts reconnect timer if connection lost.
void mqttLoop(void *parameter) {
do {
yield();
mqttClient.loop();
delay(100);
} while (mqttClient.connected());
Serial.println("Disconnected from MQTT.");
if (!mqttClient.connected()) {
Serial.println("Checking WiFi Connection.");
if (WiFi.isConnected()) {
Serial.println("WiFi OK. Starting reconnect timer for MQTT.");
xTimerStart(mqttReconnectTimer, 0);
}
}
vTaskDelete(NULL);
}
// setup MQTT connection and spins up task to handle connection and messages
void connectMQTT() {
yield();
if (mqttClient.connected()) return;
Serial.println("Connecting to MQTT...");
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
mqttClient.disconnect();
mqttClient.connect(getDeviceIDcharArr());
if (mqttClient.connected()) {
Serial.println("Connected!");
} else {
Serial.println("NOT Connected!");
}
mqttClient.subscribe(MQTT_PATH_SUB);
Serial.print("subscribed to: ");
Serial.println(MQTT_PATH_SUB);
xTaskCreatePinnedToCore(
mqttLoop,
"mqttLoop",
8192,
NULL,
2,
&mqttTask,
1);
}
// handles lost wifi connection
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectMQTT();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
digitalWrite(PIN_LED_G, LOW);
// workaround for 202 auth failed bug: https://github.com/espressif/arduino-esp32/issues/2501
if (info.disconnected.reason == 202) {
Serial.println("weirdbehaviour");
Serial.println(info.disconnected.reason);
ESP.restart();
}
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
// mqtt callback handles incoming messages
void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < payload_length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if (strcmp(topic, MQTT_LIGHT_PROPERTIES) == 0) {
Serial.println("receiving light threshold...");
Serial.println(topic);
StaticJsonDocument<1024> doc;
DeserializationError err = deserializeJson(doc, payload);
if (err == DeserializationError::Ok) {
int nm = doc["nm"];
int minLX = doc["minLX"];
setLightProperties(nm, minLX);
} else {
Serial.println(err.c_str());
}
}
if (strcmp(topic, MQTT_VALVE_COMMAND) == 0) {
Serial.println("toggling valve...");
Serial.println(topic);
toggleValve(false);
}
if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) {
Serial.println("receiving soil thresholds...");
Serial.println(topic);
StaticJsonDocument<1024> doc;
DeserializationError err = deserializeJson(doc, payload);
if (err == DeserializationError::Ok) {
int fc = doc["fc"];
int pwp = doc["pwp"];
int sat = doc["sat"];
setSoilProperties(fc, pwp, sat);
} else {
Serial.println(err.c_str());
}
}
if (strcmp(topic, MQTT_AUTO_PROPERTIES) == 0) {
Serial.println("receiving auto settings...");
Serial.println(topic);
StaticJsonDocument<1024> doc;
DeserializationError err = deserializeJson(doc, payload);
if (err == DeserializationError::Ok) {
bool ir = doc["irrigation"];
bool li = doc["light"];
setAutoProperties(li, ir);
} else {
Serial.println(err.c_str());
}
}
}
// generates mqtt topics based on device-uuid
void constructMQTTpaths() {
strcpy(MQTT_VALVE_COMMAND, MQTT_TOPIC_BASE_SUB "/");
strcat(MQTT_VALVE_COMMAND, getDeviceIDcharArr());
strcat(MQTT_VALVE_COMMAND, "/valve");
strcpy(MQTT_SOIL_PROPERTIES, MQTT_TOPIC_BASE_SUB "/");
strcat(MQTT_SOIL_PROPERTIES, getDeviceIDcharArr());
strcat(MQTT_SOIL_PROPERTIES, "/soil");
strcpy(MQTT_LIGHT_PROPERTIES, MQTT_TOPIC_BASE_SUB "/");
strcat(MQTT_LIGHT_PROPERTIES, getDeviceIDcharArr());
strcat(MQTT_LIGHT_PROPERTIES, "/light");
strcpy(MQTT_AUTO_PROPERTIES, MQTT_TOPIC_BASE_SUB "/");
strcat(MQTT_AUTO_PROPERTIES, getDeviceIDcharArr());
strcat(MQTT_AUTO_PROPERTIES, "/automatic");
Serial.println("MQTT_COMMANDS:");
Serial.println(MQTT_VALVE_COMMAND);
Serial.println(MQTT_SOIL_PROPERTIES);
Serial.println(MQTT_LIGHT_PROPERTIES);
Serial.println(MQTT_AUTO_PROPERTIES);
}
// initializes Wifi and MQTT connections
void setupConnections() {
constructMQTTpaths();
Serial.println();
Serial.println();
// disable Watchdog Task. This task made ESP stopped working, probably caused by working with arduino framework combined with multicore-concurrent patterns
disableCore0WDT();
Config.autoReconnect = true;
Portal.config(Config);
// FreeRTOS Timer to handle loss of connections
mqttReconnectTimer = xTimerCreate(
"mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void *)0, reinterpret_cast<TimerCallbackFunction_t>(connectMQTT));
wifiReconnectTimer = xTimerCreate(
"wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void *)0, reinterpret_cast<TimerCallbackFunction_t>(connectWiFi));
WiFi.onEvent(WiFiEvent);
connectWiFi();
connectMQTT();
mqttClient.setCallback(onMqttMessage);
setupNTP();
}
// method to delegate publish from other modules to connecions module
void publishMessage(const char *topic, const char *msg) {
if(mqttClient.connected()) {
mqttClient.publish(topic, msg);
Serial.print(topic);
Serial.println(" successfully sent.");
} else {
Serial.println("couldn't send message. waiting for reconnect.");
}
}