ESPMegaPRO-v3-SDK/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp

823 lines
23 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
2024-01-01 04:14:33 +00:00
/**
* @brief Create a new ESPMegaIoT object
*
* @note You shold not create this object directly, Instead, you should use the ESPMegaPRO::iot object
*/
2023-12-28 08:52:08 +00:00
ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient)
{
2023-12-31 16:29:40 +00:00
tcpClient.setTimeout(TCP_TIMEOUT_SEC);
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;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Destroy the ESPMegaIoT object
*/
2023-12-28 16:28:21 +00:00
ESPMegaIoT::~ESPMegaIoT()
{
}
2024-01-01 04:14:33 +00:00
/**
* @brief The mqtt callback function, This function is called when a message is received on a subscribed topic
*
* This function is called when a message is received on a subscribed topic
* The payload is copied to a buffer and a null terminator is added
* The payload is then passed to the respective card's mqtt callback
*
* @param topic The topic of the message
* @param payload The payload of the message in byte form
* @param length The length of the payload
*/
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';
// If the topic is not appended with the base topic, call only the absolute callbacks
if (strncmp(topic, this->mqtt_config.base_topic, base_topic_length) != 0)
{
for (const auto &callback : mqtt_callbacks)
{
callback.second(topic, payload_buffer);
}
return;
}
2023-12-28 04:41:20 +00:00
// Remove the base topic from the topic
2023-12-31 16:29:40 +00:00
char *topic_without_base = topic + strlen(this->mqtt_config.base_topic) + 1;
for (const auto &callback : mqtt_relative_callbacks)
2023-12-28 08:52:08 +00:00
{
2024-01-11 18:15:28 +00:00
callback.second(topic_without_base, payload_buffer);
2023-12-28 08:52:08 +00:00
}
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
}
2024-01-01 04:14:33 +00:00
/**
* @brief Set the base topic for the IoT
*
* @param base_topic The base topic
*/
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::setBaseTopic(char *base_topic)
{
2023-12-31 16:29:40 +00:00
strcpy(this->mqtt_config.base_topic, base_topic);
base_topic_length = strlen(this->mqtt_config.base_topic);
2023-12-28 06:34:51 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Begin the ESPMegaIoT object
*
* @param cards The array of ExpansionCard objects
*/
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;
}
2024-01-01 04:14:33 +00:00
/**
* @brief The main loop for the ESPMegaIoT object
*
* @note Normally you should not call this function, Instead, you should call ESPMegaPRO::loop()
*/
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
2024-01-01 04:14:33 +00:00
/**
* @brief Register an existing card for use with IoT
*
* This function registers an existing card for use with IoT
* The card should be installed using ESPMegaPRO::installCard() before calling this function
*
* @param card_id The id of the card
*/
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)
{
2023-12-30 07:42:52 +00:00
case CARD_TYPE_ANALOG:
components[card_id] = new AnalogIoT();
2023-12-31 16:29:40 +00:00
components[card_id]->begin(card_id, cards[card_id], &mqtt, this->mqtt_config.base_topic);
2023-12-30 07:42:52 +00:00
if (mqtt_connected)
{
components[card_id]->subscribe();
components[card_id]->publishReport();
}
break;
2023-12-28 12:11:12 +00:00
case CARD_TYPE_DIGITAL_INPUT:
components[card_id] = new DigitalInputIoT();
2023-12-31 16:29:40 +00:00
components[card_id]->begin(card_id, cards[card_id], &mqtt, this->mqtt_config.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();
2023-12-31 16:29:40 +00:00
components[card_id]->begin(card_id, cards[card_id], &mqtt, this->mqtt_config.base_topic);
2023-12-28 08:52:08 +00:00
if (mqtt_connected)
{
components[card_id]->subscribe();
components[card_id]->publishReport();
2023-12-28 08:52:08 +00:00
}
break;
2023-12-30 08:39:16 +00:00
case CARD_TYPE_CLIMATE:
components[card_id] = new ClimateIoT();
2023-12-31 16:29:40 +00:00
components[card_id]->begin(card_id, cards[card_id], &mqtt, this->mqtt_config.base_topic);
2023-12-30 08:39:16 +00:00
if (mqtt_connected)
{
components[card_id]->subscribe();
components[card_id]->publishReport();
}
break;
2024-02-12 09:20:16 +00:00
case CARD_TYPE_CT:
components[card_id] = new CurrentTransformerIoT();
components[card_id]->begin(card_id, cards[card_id], &mqtt, this->mqtt_config.base_topic);
if (mqtt_connected)
{
components[card_id]->subscribe();
components[card_id]->publishReport();
}
2023-12-28 08:52:08 +00:00
default:
2023-12-30 11:47:52 +00:00
ESP_LOGE("ESPMegaIoT", "Registering card %d failed: Unknown card", card_id);
2023-12-28 08:52:08 +00:00
return;
}
}
2024-01-01 04:14:33 +00:00
/**
* @brief Unregister a card
*
* @param card_id The id of the card
*/
2023-12-31 06:41:48 +00:00
void ESPMegaIoT::unregisterCard(uint8_t card_id)
2023-12-28 08:52:08 +00:00
{
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;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Publish all cards's reports
*/
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();
}
2024-01-01 04:14:33 +00:00
/**
* @brief Subscribe to a topic
*
* @param topic The topic to subscribe to
*/
void ESPMegaIoT::subscribe(const char *topic)
2023-12-28 08:52:08 +00:00
{
2023-12-28 07:08:10 +00:00
mqtt.subscribe(topic);
}
2024-01-01 04:14:33 +00:00
/**
* @brief Unsubscribe from a topic
*
* @param topic The topic to unsubscribe from
*/
void ESPMegaIoT::unsubscribeFromTopic(const char *topic)
2023-12-28 08:52:08 +00:00
{
2023-12-28 07:08:10 +00:00
mqtt.unsubscribe(topic);
}
2024-01-01 04:14:33 +00:00
/**
* @brief Connect to a wifi network
*
* @param ssid The SSID of the wifi network
* @param password The password of the wifi network
*/
void ESPMegaIoT::connectToWifi(const char *ssid, const char *password)
2023-12-28 08:52:08 +00:00
{
2023-12-28 07:08:10 +00:00
WiFi.begin(ssid, password);
}
2024-01-01 04:14:33 +00:00
/**
* @brief Connect to a unsecured wifi network
*
* @param ssid The SSID of the wifi network
*/
void ESPMegaIoT::connectToWifi(const char *ssid)
2023-12-28 08:52:08 +00:00
{
2023-12-28 07:08:10 +00:00
WiFi.begin(ssid);
}
2024-01-01 04:14:33 +00:00
/**
* @brief Disconnect from the wifi network
*/
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::disconnectFromWifi()
{
2023-12-28 07:08:10 +00:00
WiFi.disconnect();
}
2024-01-01 04:14:33 +00:00
/**
* @brief Check if the wifi is connected
*
* @return True if the wifi is connected, false otherwise
*/
2023-12-28 08:52:08 +00:00
bool ESPMegaIoT::wifiConnected()
{
2023-12-28 07:08:10 +00:00
return WiFi.status() == WL_CONNECTED;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Connect to a MQTT broker with authentication
*
* @param client_id The client id to use
* @param mqtt_server The MQTT server to connect to
* @param mqtt_port The MQTT port to connect to
* @param mqtt_user The MQTT username to use
* @param mqtt_password The MQTT password to use
* @return True if the connection is successful, false otherwise
*/
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-31 10:55:13 +00:00
if (mqtt_user == nullptr || mqtt_password == nullptr || strlen(mqtt_user) == 0 || strlen(mqtt_password) == 0)
{
mqtt_connected = false;
ESP_LOGE("ESPMegaIoT", "MQTT Connection failed: Username or password not set but MQTT use_auth is true");
return false;
}
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;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Connect to a MQTT broker without authentication
*
* @param client_id The client id to use
* @param mqtt_server The MQTT server to connect to
* @param mqtt_port The MQTT port to connect to
* @return True if the connection is successful, false otherwise
*/
2023-12-28 08:52:08 +00:00
bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt_port)
{
2023-12-31 10:55:13 +00:00
ESP_LOGD("ESPMegaIoT", "Setting MQTT server to %s:%d", mqtt_server, 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);
2023-12-31 10:55:13 +00:00
ESP_LOGD("ESPMegaIoT", "Binding MQTT callback");
2023-12-28 07:52:52 +00:00
mqtt.setCallback(boundCallback);
2023-12-28 08:52:08 +00:00
if (mqtt.connect(client_id))
{
2023-12-31 10:55:13 +00:00
ESP_LOGD("ESPMegaIoT", "MQTT Connected, Calling session keep alive");
2023-12-28 07:52:52 +00:00
sessionKeepAlive();
2023-12-31 10:55:13 +00:00
ESP_LOGD("ESPMegaIoT", "Subscribing to topics");
2023-12-28 07:52:52 +00:00
mqttSubscribe();
2023-12-31 10:55:13 +00:00
ESP_LOGD("ESPMegaIoT", "Publishing reports");
// Publish all cards
for (int i = 0; i < 255; i++)
{
if (components[i] != NULL)
{
components[i]->publishReport();
}
}
2023-12-31 10:55:13 +00:00
ESP_LOGI("ESPMegaIoT", "MQTT Connected OK.");
2023-12-28 08:52:08 +00:00
mqtt_connected = true;
2023-12-28 07:52:52 +00:00
return true;
}
2023-12-30 11:47:52 +00:00
ESP_LOGW("ESPMegaIoT", "MQTT Connection failed: %d", mqtt.state());
2023-12-28 07:52:52 +00:00
mqtt_connected = false;
return false;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Disconnect from the MQTT broker
*/
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::disconnectFromMqtt()
{
2023-12-28 07:52:52 +00:00
mqtt.disconnect();
}
2024-01-01 04:14:33 +00:00
/**
* @brief Publish a message to a topic
*
* @param topic The topic to publish to
* @param payload The payload to publish
*/
2023-12-31 16:29:40 +00:00
void ESPMegaIoT::publish(const char *topic, const char *payload)
2023-12-28 08:52:08 +00:00
{
2023-12-28 07:52:52 +00:00
mqtt.publish(topic, payload);
2023-12-28 07:08:10 +00:00
}
2023-12-31 16:29:40 +00:00
2024-01-01 04:14:33 +00:00
/**
* @brief Register a callback for MQTT messages
*
* @param callback The callback function
* @return The handler for the callback
*/
2023-12-31 16:29:40 +00:00
uint8_t ESPMegaIoT::registerMqttCallback(std::function<void(char *, char *)> callback)
{
mqtt_callbacks[mqtt_callbacks_handler_index] = callback;
return mqtt_callbacks_handler_index++;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Unregister a callback
*
* @param handler The handler of the callback
*/
2023-12-31 16:29:40 +00:00
void ESPMegaIoT::unregisterMqttCallback(uint8_t handler)
2023-12-28 08:52:08 +00:00
{
2023-12-31 16:29:40 +00:00
mqtt_callbacks.erase(handler);
2023-12-28 07:52:52 +00:00
}
2023-12-31 16:29:40 +00:00
2024-01-01 04:14:33 +00:00
/**
* @brief Subscribe to all components's topics
*/
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::mqttSubscribe()
{
2023-12-31 10:55:13 +00:00
ESP_LOGD("ESPMegaIoT", "Begin MQTT Subscription");
2023-12-31 16:29:40 +00:00
for (const auto &callback : subscribe_callbacks)
2023-12-28 08:52:08 +00:00
{
2023-12-31 16:29:40 +00:00
callback.second();
2023-12-31 10:55:13 +00:00
mqtt.loop();
2023-12-28 08:52:08 +00:00
}
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-31 10:55:13 +00:00
ESP_LOGD("ESPMegaIoT","Subscribing component %d", i);
2023-12-28 07:52:52 +00:00
components[i]->subscribe();
2023-12-31 10:55:13 +00:00
mqtt.loop();
2023-12-28 07:52:52 +00:00
}
}
}
2024-01-01 04:14:33 +00:00
/**
* @brief Publish relative to the base topic
*/
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];
2023-12-31 16:29:40 +00:00
sprintf(absolute_topic, "%s/%d/%s", this->mqtt_config.base_topic, card_id, topic);
2023-12-28 07:52:52 +00:00
mqtt.publish(absolute_topic, payload);
}
2024-01-01 04:14:33 +00:00
/**
* @brief Subscribe relative to the base topic
*/
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
}
}
2024-01-01 04:14:33 +00:00
/**
* @brief Keep the MQTT session alive
* @note This function is called automatically by the ESPMegaIoT object, You should not call this function directly
*/
2023-12-28 08:52:08 +00:00
void ESPMegaIoT::sessionKeepAlive()
{
2023-12-31 16:29:40 +00:00
// This reconnect the MQTT if it disconnect.
// If a disconnect happens, this will reconnect the MQTT within 1 second.
// A connection attempt will be made at most once every MQTT_RECONNECT_INTERVAL
// This have the effect of reconnecting to the server immediately if the connection is lost
// and the connection was previously stable for at least MQTT_RECONNECT_INTERVAL
// But will not reconnect if the connection was unstable and the connection was lost
2023-12-28 07:52:52 +00:00
static unsigned long lastSessionKeepAlive = 0;
2023-12-31 16:29:40 +00:00
static unsigned long lastConnectionAttempt = 0;
if (millis() - lastSessionKeepAlive > 1000)
2023-12-28 08:52:08 +00:00
{
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-31 16:29:40 +00:00
// Try to reconnect if lastConnectionAttempt exceed MQTT_RECONNECT_INTERVAL
if (millis() - lastConnectionAttempt > MQTT_RECONNECT_INTERVAL)
{
lastConnectionAttempt = millis();
mqtt_connected = mqttReconnect();
}
2023-12-28 07:52:52 +00:00
}
}
}
2024-01-01 04:14:33 +00:00
/**
* @brief Register a callback for MQTT messages relative to the base topic
*
* The message's base topic will be removed before calling the callback
*
* @param callback The callback function
* @return The handler for the callback
*/
2023-12-31 16:29:40 +00:00
uint8_t ESPMegaIoT::registerRelativeMqttCallback(std::function<void(char *, char *)> callback)
{
mqtt_relative_callbacks[mqtt_relative_callbacks_handler_index] = callback;
return mqtt_relative_callbacks_handler_index++;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Unregister a relative MQTT callback
*
* @param handler The handler of the callback
*/
2023-12-31 16:29:40 +00:00
void ESPMegaIoT::unregisterRelativeMqttCallback(uint8_t handler)
2023-12-28 08:52:08 +00:00
{
2023-12-31 16:29:40 +00:00
mqtt_relative_callbacks.erase(handler);
2023-12-28 07:52:52 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Publish a message relative to the base topic
*
* @param topic The topic to publish to
* @param payload The payload to publish
*/
void ESPMegaIoT::publishRelative(const char *topic, const char *payload)
2023-12-28 08:52:08 +00:00
{
2023-12-28 07:52:52 +00:00
char absolute_topic[100];
2023-12-31 16:29:40 +00:00
sprintf(absolute_topic, "%s/%s", this->mqtt_config.base_topic, topic);
2023-12-28 07:52:52 +00:00
mqtt.publish(absolute_topic, payload);
2023-12-31 10:55:13 +00:00
mqtt.loop();
2023-12-28 07:52:52 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Subscribe to a topic relative to the base topic
*
* @param topic The topic to subscribe to
*/
void ESPMegaIoT::subscribeRelative(const char *topic)
2023-12-28 08:52:08 +00:00
{
2023-12-28 07:52:52 +00:00
char absolute_topic[100];
2023-12-31 16:29:40 +00:00
sprintf(absolute_topic, "%s/%s", this->mqtt_config.base_topic, topic);
2023-12-28 07:52:52 +00:00
mqtt.subscribe(absolute_topic);
2023-12-31 10:55:13 +00:00
mqtt.loop();
2023-12-28 08:52:08 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Register a function to be called when the ESPMegaIoT object is subscribing to topics
*
* @param callback The callback function
* @return The handler for the callback
*/
2023-12-31 16:29:40 +00:00
uint8_t ESPMegaIoT::registerSubscribeCallback(std::function<void(void)> callback)
{
subscribe_callbacks[subscribe_callbacks_handler_index] = callback;
return subscribe_callbacks_handler_index++;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Unregister a subscribe callback
*
* @param handler The handler of the callback
*/
2023-12-31 16:29:40 +00:00
void ESPMegaIoT::unregisterSubscribeCallback(uint8_t handler)
2023-12-28 08:52:08 +00:00
{
2023-12-31 16:29:40 +00:00
subscribe_callbacks.erase(handler);
2023-12-28 13:20:49 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Set the network config
*
* @param network_config The network config struct
*/
2023-12-28 13:20:49 +00:00
void ESPMegaIoT::setNetworkConfig(NetworkConfig network_config)
{
this->network_config = network_config;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Load the network config from FRAM
*/
2023-12-28 13:20:49 +00:00
void ESPMegaIoT::loadNetworkConfig()
{
// Load the network config from FRAM
2023-12-30 19:59:25 +00:00
network_config.ip = fram->read32(IOT_FRAM_ADDRESS);
network_config.gateway = fram->read32(IOT_FRAM_ADDRESS + 4);
network_config.subnet = fram->read32(IOT_FRAM_ADDRESS + 8);
network_config.dns1 = fram->read32(IOT_FRAM_ADDRESS + 12);
network_config.dns2 = fram->read32(IOT_FRAM_ADDRESS + 16);
2023-12-31 10:55:13 +00:00
fram->read(IOT_FRAM_ADDRESS + 20, (uint8_t *)network_config.hostname, 32);
2023-12-30 19:59:25 +00:00
network_config.useStaticIp = fram->read8(IOT_FRAM_ADDRESS + 52);
network_config.useWifi = fram->read8(IOT_FRAM_ADDRESS + 53);
network_config.wifiUseAuth = fram->read8(IOT_FRAM_ADDRESS + 54);
2023-12-31 10:55:13 +00:00
fram->read(IOT_FRAM_ADDRESS + 55, (uint8_t *)network_config.ssid, 32);
fram->read(IOT_FRAM_ADDRESS + 87, (uint8_t *)network_config.password, 32);
2023-12-28 13:20:49 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Save the network config to FRAM
*/
2023-12-28 13:20:49 +00:00
void ESPMegaIoT::saveNetworkConfig()
{
// Save the network config to FRAM
2023-12-30 19:59:25 +00:00
fram->write32(IOT_FRAM_ADDRESS, network_config.ip);
fram->write32(IOT_FRAM_ADDRESS + 4, network_config.gateway);
fram->write32(IOT_FRAM_ADDRESS + 8, network_config.subnet);
fram->write32(IOT_FRAM_ADDRESS + 12, network_config.dns1);
fram->write32(IOT_FRAM_ADDRESS + 16, network_config.dns2);
2023-12-31 10:55:13 +00:00
fram->write(IOT_FRAM_ADDRESS + 20, (uint8_t *)network_config.hostname, 32);
2023-12-30 19:59:25 +00:00
fram->write8(IOT_FRAM_ADDRESS + 52, network_config.useStaticIp);
fram->write8(IOT_FRAM_ADDRESS + 53, network_config.useWifi);
fram->write8(IOT_FRAM_ADDRESS + 54, network_config.wifiUseAuth);
2023-12-31 10:55:13 +00:00
fram->write(IOT_FRAM_ADDRESS + 55, (uint8_t *)network_config.ssid, 32);
fram->write(IOT_FRAM_ADDRESS + 87, (uint8_t *)network_config.password, 32);
2023-12-28 13:20:49 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Begin the ethernet interface
*/
2023-12-28 13:20:49 +00:00
void ESPMegaIoT::ethernetBegin()
{
2023-12-28 16:28:21 +00:00
ethernetIface->setHostname(network_config.hostname);
2023-12-28 13:20:49 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Load the MQTT config from FRAM
*/
2023-12-28 13:20:49 +00:00
void ESPMegaIoT::loadMqttConfig()
{
// Load the mqtt config from FRAM
2023-12-30 19:18:57 +00:00
// We skip bytes 119-127 because they are reserved for the network config
2023-12-30 19:59:25 +00:00
mqtt_config.mqtt_port = fram->read16(IOT_FRAM_ADDRESS + 128);
2023-12-31 10:55:13 +00:00
fram->read(IOT_FRAM_ADDRESS + 130, (uint8_t *)mqtt_config.mqtt_server, 32);
fram->read(IOT_FRAM_ADDRESS + 162, (uint8_t *)mqtt_config.mqtt_user, 32);
fram->read(IOT_FRAM_ADDRESS + 194, (uint8_t *)mqtt_config.mqtt_password, 32);
2023-12-30 19:59:25 +00:00
mqtt_config.mqtt_useauth = fram->read8(IOT_FRAM_ADDRESS + 226);
2023-12-31 10:55:13 +00:00
fram->read(IOT_FRAM_ADDRESS + 227, (uint8_t *)mqtt_config.base_topic, 32);
2023-12-31 16:29:40 +00:00
this->base_topic_length = strlen(mqtt_config.base_topic);
2023-12-28 13:20:49 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Save the MQTT config to FRAM
*/
2023-12-28 13:20:49 +00:00
void ESPMegaIoT::saveMqttConfig()
{
2023-12-30 19:59:25 +00:00
fram->write16(IOT_FRAM_ADDRESS + 128, mqtt_config.mqtt_port);
2023-12-31 10:55:13 +00:00
fram->write(IOT_FRAM_ADDRESS + 130, (uint8_t *)mqtt_config.mqtt_server, 32);
fram->write(IOT_FRAM_ADDRESS + 162, (uint8_t *)mqtt_config.mqtt_user, 32);
fram->write(IOT_FRAM_ADDRESS + 194, (uint8_t *)mqtt_config.mqtt_password, 32);
2023-12-30 19:59:25 +00:00
fram->write8(IOT_FRAM_ADDRESS + 226, mqtt_config.mqtt_useauth);
2023-12-31 10:55:13 +00:00
fram->write(IOT_FRAM_ADDRESS + 227, (uint8_t *)mqtt_config.base_topic, 32);
2023-12-28 13:20:49 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Connect to MQTT with the current config
*/
2023-12-28 13:20:49 +00:00
void ESPMegaIoT::connectToMqtt()
{
2023-12-29 16:43:12 +00:00
if (mqtt_config.mqtt_useauth)
2023-12-31 10:55:13 +00:00
{
ESP_LOGD("ESPMegaIoT", "Connecting to MQTT with auth");
2023-12-29 16:43:12 +00:00
this->connectToMqtt(network_config.hostname, mqtt_config.mqtt_server, mqtt_config.mqtt_port, mqtt_config.mqtt_user, mqtt_config.mqtt_password);
2023-12-31 10:55:13 +00:00
}
2023-12-28 13:20:49 +00:00
else
2023-12-31 10:55:13 +00:00
{
ESP_LOGD("ESPMegaIoT", "Connecting to MQTT without auth");
2023-12-29 16:43:12 +00:00
this->connectToMqtt(network_config.hostname, mqtt_config.mqtt_server, mqtt_config.mqtt_port);
2023-12-31 10:55:13 +00:00
}
2023-12-28 13:20:49 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Connect to the network using the current config
*/
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
}
}
2024-01-01 04:14:33 +00:00
/**
* @brief Set the MQTT config
*
* @param mqtt_config The MQTT config struct
*/
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-31 16:29:40 +00:00
this->base_topic_length = strlen(mqtt_config.base_topic);
2023-12-28 16:28:21 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Bind an ethernet interface to the ESPMegaIoT object
*
* @param ethernetIface The ethernet interface to bind (ETH for ESPMegaPRO R3)
*/
2023-12-28 16:28:21 +00:00
void ESPMegaIoT::bindEthernetInterface(ETHClass *ethernetIface)
{
this->ethernetIface = ethernetIface;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Get the IoTComponent object for a card
*
* @param card_id The id of the card
* @return The IoTComponent object for the card
*/
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-29 17:49:09 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Get the network config
*
* @warning You should not modify the returned struct directly
*
* @return The network config struct
*/
2023-12-31 10:55:13 +00:00
NetworkConfig *ESPMegaIoT::getNetworkConfig()
2023-12-29 17:49:09 +00:00
{
return &network_config;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Get the MQTT config
*
* @warning You should not modify the returned struct directly
*
* @return The MQTT config struct
*/
2023-12-31 10:55:13 +00:00
MqttConfig *ESPMegaIoT::getMqttConfig()
2023-12-29 17:49:09 +00:00
{
return &mqtt_config;
}
2024-01-01 04:14:33 +00:00
/**
* @brief Check if the MQTT is connected
*
* @return True if the MQTT is connected, false otherwise
*/
2023-12-29 17:49:09 +00:00
bool ESPMegaIoT::mqttConnected()
{
2023-12-31 13:49:22 +00:00
//return mqtt_connected;
return mqtt.connected();
2023-12-29 17:49:09 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Check if the network is connected
*
* @return True if the network is connected, false otherwise
*/
2023-12-29 17:49:09 +00:00
bool ESPMegaIoT::networkConnected()
{
if (network_config.useWifi)
return WiFi.status() == WL_CONNECTED;
else
return ethernetIface->linkUp();
2023-12-30 19:18:57 +00:00
}
2024-01-01 04:14:33 +00:00
/**
* @brief Bind a FRAM object to the ESPMegaIoT object
* @note This class is hardcode to use the FRAM address 34-300
*
* @param fram The FRAM object to bind
*/
2023-12-30 19:18:57 +00:00
void ESPMegaIoT::bindFRAM(FRAM *fram)
{
this->fram = fram;
2024-01-01 09:38:14 +00:00
}
/**
* @brief Get the Wifi IP address
*
* @return The Wifi IP address
*/
IPAddress ESPMegaIoT::getWifiIp() {
return WiFi.localIP();
}
/**
* @brief Get the Ethernet IP Address
*
* @return The Ethernet IP Address
*/
IPAddress ESPMegaIoT::getETHIp() {
return ETH.localIP();
}
/**
* @brief Get the IP address of the currently active network interface
*
* @return The IP address of the currently active network interface
*/
IPAddress ESPMegaIoT::getIp() {
if (network_config.useWifi)
return this->getWifiIp();
else
return this->getETHIp();
}
/**
* @brief Get the MAC Address of the Ethernet interface
*
* @return The MAC Address of the Ethernet interface
*/
String ESPMegaIoT::getETHMac() {
return ETH.macAddress();
}
/**
* @brief Get the MAC Address of the Wifi interface
*
* @return The MAC Address of the Wifi interface
*/
String ESPMegaIoT::getWifiMac() {
return WiFi.macAddress();
}
/**
* @brief Get the MAC Address of the currently active network interface
*
* @return The MAC Address of the currently active network interface
*/
String ESPMegaIoT::getMac() {
if (network_config.useWifi)
return this->getWifiMac();
else
return this->getETHMac();
}