ESPMegaPRO-v3-SDK/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp

372 lines
9.9 KiB
C++
Raw Normal View History

2023-12-28 04:41:20 +00:00
#include <ESPMegaIoT.hpp>
2023-12-28 16:28:21 +00:00
#include <ETH.h>
2023-12-28 13:20:49 +00:00
#define NETWORK_CONFIG_ADDRESS 34
#define MQTT_CONFIG_ADDRESS 34 + sizeof(NetworkConfig)
2023-12-28 08:52:08 +00:00
ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient)
{
2023-12-28 08:18:37 +00:00
tcpClient.setTimeout(1);
2023-12-28 07:52:52 +00:00
// Initialize the components array
2023-12-28 08:52:08 +00:00
for (int i = 0; i < 255; i++)
{
2023-12-28 07:52:52 +00:00
components[i] = NULL;
}
active = false;
mqtt_connected = false;
}
2023-12-28 16:28:21 +00:00
ESPMegaIoT::~ESPMegaIoT()
{
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length)
{
2023-12-28 04:41:20 +00:00
// Create a null terminated string from the payload
memcpy(payload_buffer, payload, length);
payload_buffer[length] = '\0';
// Remove the base topic from the topic
char *topic_without_base = topic + strlen(base_topic) + 1;
2023-12-28 08:52:08 +00:00
if (user_relative_mqtt_callback != NULL)
{
user_relative_mqtt_callback(topic_without_base + 3, payload_buffer);
}
if (user_mqtt_callback != NULL)
{
user_mqtt_callback(topic, payload_buffer);
}
2023-12-28 04:41:20 +00:00
// Call the respective card's mqtt callback
// Note that after the base topic, there should be the card id
2023-12-28 08:52:08 +00:00
// /base_topic/card_id/...
2023-12-28 04:41:20 +00:00
// First, get the card id in integer form
char *card_id_str = strtok(topic_without_base, "/");
uint8_t card_id = atoi(card_id_str);
// Check if the card is registered
2023-12-28 08:52:08 +00:00
if (components[card_id] == NULL)
{
2023-12-28 04:41:20 +00:00
return;
}
2023-12-28 06:14:18 +00:00
components[card_id]->handleMqttMessage(topic_without_base + 3, payload_buffer);
2023-12-28 04:41:20 +00:00
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::setBaseTopic(char *base_topic)
{
2023-12-28 06:34:51 +00:00
strcpy(this->base_topic, base_topic);
base_topic_length = strlen(base_topic);
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::intr_begin(ExpansionCard *cards[])
{
2023-12-28 07:08:10 +00:00
this->cards = cards;
2023-12-28 07:52:52 +00:00
active = true;
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::loop()
{
if (!active)
2023-12-28 07:52:52 +00:00
return;
// Call each component's loop function
2023-12-28 08:52:08 +00:00
for (int i = 0; i < 255; i++)
{
if (components[i] != NULL)
{
2023-12-28 07:52:52 +00:00
components[i]->loop();
}
}
mqtt.loop();
sessionKeepAlive();
2023-12-28 06:34:51 +00:00
}
2023-12-28 07:08:10 +00:00
// Register Existing Card for use with IoT
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::registerCard(uint8_t card_id)
{
2023-12-28 07:08:10 +00:00
// Check if the card is already registered
2023-12-28 08:52:08 +00:00
if (components[card_id] != NULL)
{
2023-12-28 07:08:10 +00:00
return;
}
// Get the card type
uint8_t card_type = cards[card_id]->getType();
// Create the respective IoT component
2023-12-28 08:52:08 +00:00
switch (card_type)
{
// case CARD_TYPE_ANALOG:
// components[card_id] = new AnalogIoT();
// components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic);
// break;
2023-12-28 12:11:12 +00:00
case CARD_TYPE_DIGITAL_INPUT:
components[card_id] = new DigitalInputIoT();
components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic);
2023-12-28 16:28:21 +00:00
if (mqtt_connected)
{
components[card_id]->subscribe();
components[card_id]->publishReport();
}
2023-12-28 12:11:12 +00:00
break;
2023-12-28 08:52:08 +00:00
case CARD_TYPE_DIGITAL_OUTPUT:
components[card_id] = new DigitalOutputIoT();
components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic);
if (mqtt_connected)
{
components[card_id]->subscribe();
components[card_id]->publishReport();
2023-12-28 08:52:08 +00:00
}
break;
default:
2023-12-28 16:28:21 +00:00
Serial.println("Unsupported card type");
2023-12-28 08:52:08 +00:00
return;
}
}
void ESPMegaIoT::deregisterCard(uint8_t card_id)
{
2023-12-28 07:08:10 +00:00
// Check if the card is registered
2023-12-28 08:52:08 +00:00
if (components[card_id] == NULL)
{
2023-12-28 07:08:10 +00:00
return;
}
// Delete the IoT component
delete components[card_id];
components[card_id] = NULL;
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::publishCard(uint8_t card_id)
{
2023-12-28 07:08:10 +00:00
// Check if the card is registered
2023-12-28 08:52:08 +00:00
if (components[card_id] == NULL)
{
2023-12-28 07:08:10 +00:00
return;
}
// Publish the card
components[card_id]->publishReport();
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::subscribeToTopic(char *topic)
{
2023-12-28 07:08:10 +00:00
mqtt.subscribe(topic);
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::unsubscribeFromTopic(char *topic)
{
2023-12-28 07:08:10 +00:00
mqtt.unsubscribe(topic);
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::connectToWifi(char *ssid, char *password)
{
2023-12-28 07:08:10 +00:00
WiFi.begin(ssid, password);
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::connectToWifi(char *ssid)
{
2023-12-28 07:08:10 +00:00
WiFi.begin(ssid);
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::disconnectFromWifi()
{
2023-12-28 07:08:10 +00:00
WiFi.disconnect();
}
2023-12-28 08:52:08 +00:00
bool ESPMegaIoT::wifiConnected()
{
2023-12-28 07:08:10 +00:00
return WiFi.status() == WL_CONNECTED;
}
2023-12-28 08:52:08 +00:00
bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt_port, char *mqtt_user, char *mqtt_password)
{
2023-12-28 07:08:10 +00:00
mqtt.setServer(mqtt_server, mqtt_port);
2023-12-28 07:52:52 +00:00
auto boundCallback = std::bind(&ESPMegaIoT::mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
mqtt.setCallback(boundCallback);
2023-12-28 08:52:08 +00:00
if (mqtt.connect(client_id, mqtt_user, mqtt_password))
{
2023-12-28 07:52:52 +00:00
sessionKeepAlive();
mqttSubscribe();
// Publish all cards
for (int i = 0; i < 255; i++)
{
if (components[i] != NULL)
{
components[i]->publishReport();
}
}
2023-12-28 07:52:52 +00:00
mqtt_connected = true;
return true;
}
mqtt_connected = false;
return false;
}
2023-12-28 08:52:08 +00:00
bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt_port)
{
2023-12-28 07:52:52 +00:00
mqtt.setServer(mqtt_server, mqtt_port);
auto boundCallback = std::bind(&ESPMegaIoT::mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
mqtt.setCallback(boundCallback);
2023-12-28 08:52:08 +00:00
if (mqtt.connect(client_id))
{
2023-12-28 07:52:52 +00:00
sessionKeepAlive();
mqttSubscribe();
// Publish all cards
for (int i = 0; i < 255; i++)
{
if (components[i] != NULL)
{
components[i]->publishReport();
}
}
2023-12-28 08:52:08 +00:00
mqtt_connected = true;
2023-12-28 07:52:52 +00:00
return true;
}
2023-12-28 08:18:37 +00:00
Serial.println("Failed to connect to mqtt");
2023-12-28 07:52:52 +00:00
mqtt_connected = false;
return false;
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::disconnectFromMqtt()
{
2023-12-28 07:52:52 +00:00
mqtt.disconnect();
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::publishToTopic(char *topic, char *payload)
{
2023-12-28 07:52:52 +00:00
mqtt.publish(topic, payload);
2023-12-28 07:08:10 +00:00
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::registerMqttCallback(void (*callback)(char *, char *))
{
user_mqtt_callback = callback;
2023-12-28 07:52:52 +00:00
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::mqttSubscribe()
{
if (user_subscribe_callback != NULL)
{
user_subscribe_callback();
}
2023-12-28 07:52:52 +00:00
// Subscribe to all topics
2023-12-28 08:52:08 +00:00
for (int i = 0; i < 255; i++)
{
if (components[i] != NULL)
{
2023-12-28 07:52:52 +00:00
components[i]->subscribe();
}
}
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::publishRelative(uint8_t card_id, char *topic, char *payload)
{
2023-12-28 07:52:52 +00:00
char absolute_topic[100];
sprintf(absolute_topic, "%s/%d/%s", base_topic, card_id, topic);
mqtt.publish(absolute_topic, payload);
}
2023-12-28 08:52:08 +00:00
bool ESPMegaIoT::mqttReconnect()
{
2023-12-29 16:43:12 +00:00
if (this->mqtt_config.mqtt_useauth)
2023-12-28 08:52:08 +00:00
{
2023-12-29 16:43:12 +00:00
return this->connectToMqtt(this->network_config.hostname, this->mqtt_config.mqtt_server, this->mqtt_config.mqtt_port, this->mqtt_config.mqtt_user, this->mqtt_config.mqtt_password);
2023-12-28 08:52:08 +00:00
}
else
{
2023-12-29 16:43:12 +00:00
return this->connectToMqtt(this->network_config.hostname, this->mqtt_config.mqtt_server, this->mqtt_config.mqtt_port);
2023-12-28 07:52:52 +00:00
}
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::sessionKeepAlive()
{
2023-12-28 07:52:52 +00:00
static unsigned long lastSessionKeepAlive = 0;
2023-12-28 08:52:08 +00:00
if (millis() - lastSessionKeepAlive > 30000)
{
2023-12-28 07:52:52 +00:00
lastSessionKeepAlive = millis();
// Check if mqtt is connected
2023-12-28 08:52:08 +00:00
if (!mqtt.connected())
{
2023-12-28 07:52:52 +00:00
// Try to reconnect
mqtt_connected = mqttReconnect();
}
}
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::registerRelativeMqttCallback(void (*callback)(char *, char *))
{
2023-12-28 07:52:52 +00:00
user_relative_mqtt_callback = callback;
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::publishRelative(char *topic, char *payload)
{
2023-12-28 07:52:52 +00:00
char absolute_topic[100];
sprintf(absolute_topic, "%s/%s", base_topic, topic);
mqtt.publish(absolute_topic, payload);
}
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::subscribeRelative(char *topic)
{
2023-12-28 07:52:52 +00:00
char absolute_topic[100];
sprintf(absolute_topic, "%s/%s", base_topic, topic);
mqtt.subscribe(absolute_topic);
2023-12-28 08:52:08 +00:00
}
void ESPMegaIoT::registerSubscribeCallback(void (*callback)(void))
{
user_subscribe_callback = callback;
2023-12-28 13:20:49 +00:00
}
void ESPMegaIoT::setNetworkConfig(NetworkConfig network_config)
{
this->network_config = network_config;
}
void ESPMegaIoT::loadNetworkConfig()
{
// Load the network config from FRAM
fram->read(0, (uint8_t *)&network_config, sizeof(NetworkConfig));
}
void ESPMegaIoT::saveNetworkConfig()
{
// Save the network config to FRAM
fram->write(NETWORK_CONFIG_ADDRESS, (uint8_t *)&network_config, sizeof(NetworkConfig));
}
void ESPMegaIoT::ethernetBegin()
{
2023-12-28 16:28:21 +00:00
ethernetIface->setHostname(network_config.hostname);
2023-12-28 13:20:49 +00:00
}
void ESPMegaIoT::loadMqttConfig()
{
// Load the mqtt config from FRAM
2023-12-29 16:43:12 +00:00
fram->read(sizeof(NetworkConfig), (uint8_t *)&this->mqtt_config, sizeof(MqttConfig));
2023-12-28 13:20:49 +00:00
// Populate the mqtt connection parameters
}
void ESPMegaIoT::saveMqttConfig()
{
fram->write(MQTT_CONFIG_ADDRESS, (uint8_t *)&mqtt_config, sizeof(MqttConfig));
}
void ESPMegaIoT::connectToMqtt()
{
2023-12-29 16:43:12 +00:00
if (mqtt_config.mqtt_useauth)
this->connectToMqtt(network_config.hostname, mqtt_config.mqtt_server, mqtt_config.mqtt_port, mqtt_config.mqtt_user, mqtt_config.mqtt_password);
2023-12-28 13:20:49 +00:00
else
2023-12-29 16:43:12 +00:00
this->connectToMqtt(network_config.hostname, mqtt_config.mqtt_server, mqtt_config.mqtt_port);
2023-12-28 13:20:49 +00:00
}
void ESPMegaIoT::connectNetwork()
{
if (network_config.useWifi)
{
if (network_config.wifiUseAuth)
this->connectToWifi(network_config.ssid, network_config.password);
else
this->connectToWifi(network_config.ssid);
if (network_config.useStaticIp)
WiFi.config(network_config.ip, network_config.gateway, network_config.subnet);
else
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
}
else
{
this->ethernetBegin();
if (network_config.useStaticIp)
2023-12-28 16:28:21 +00:00
ethernetIface->config(network_config.ip, network_config.gateway, network_config.subnet, network_config.dns1, network_config.dns2);
2023-12-28 13:20:49 +00:00
}
}
void ESPMegaIoT::setMqttConfig(MqttConfig mqtt_config)
{
2023-12-29 16:43:12 +00:00
this->mqtt_config = mqtt_config;
2023-12-28 16:28:21 +00:00
}
void ESPMegaIoT::bindEthernetInterface(ETHClass *ethernetIface)
{
this->ethernetIface = ethernetIface;
}
2023-12-29 16:43:12 +00:00
IoTComponent *ESPMegaIoT::getComponent(uint8_t card_id)
2023-12-28 16:28:21 +00:00
{
return components[card_id];
2023-12-28 07:52:52 +00:00
}