smart_garden/src/connections.cpp

139 lines
3.3 KiB
C++

#include <header.h>
extern "C"
{
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
WebServer Server;
AutoConnect Portal(Server);
WiFiClient client;
AutoConnectConfig Config;
// Config.autoReconnect = true;
// WiFi.config(Config);
void connectWiFi()
{
Serial.println("Start WiFi...");
if (Portal.begin())
{
digitalWrite(PIN_LED_G, HIGH);
}
}
void connectMQTT()
{
Serial.println("Connecting to MQTT...");
mqttClient.setClientId(MQTT_DEVICE_ID);
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event)
{
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);
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
void onMqttConnect(bool sessionPresent)
{
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
uint16_t packetIdSub = mqttClient.subscribe(MQTT_PATH_SUB, 2);
Serial.print("subscribed to: ");
Serial.println(MQTT_PATH_SUB);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
{
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected())
{
xTimerStart(mqttReconnectTimer, 0);
}
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos)
{
Serial.print("Subscribe acknowledged:");
Serial.print(" packetId: ");
Serial.print(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId)
{
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)
{
Serial.print("Publish received: ");
Serial.print(" topic: ");
Serial.println(topic);
if (strcmp(topic, "smartgarden/commands") == 0 ) {
Serial.println("executing command...");
Serial.println(topic);
toggleValve();
}
}
void onMqttPublish(uint16_t packetId)
{
Serial.print("Publish acknowledged: ");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void setupConnections()
{
Serial.println();
Serial.println();
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);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
connectWiFi();
}
void publishMessage(const char *topic, const char *msg) {
mqttClient.publish(topic, 1, true, msg);
}