2023-12-29 17:49:09 +00:00
|
|
|
#pragma once
|
2023-12-27 20:09:41 +00:00
|
|
|
#include <ExpansionCard.hpp>
|
|
|
|
#include <AnalogCard.hpp>
|
2023-12-28 04:41:20 +00:00
|
|
|
#include <AnalogIoT.hpp>
|
2023-12-27 20:09:41 +00:00
|
|
|
#include <DigitalInputCard.hpp>
|
2023-12-28 04:41:20 +00:00
|
|
|
#include <DigitalInputIoT.hpp>
|
2023-12-27 20:09:41 +00:00
|
|
|
#include <DigitalOutputCard.hpp>
|
2023-12-28 04:41:20 +00:00
|
|
|
#include <DigitalOutputIoT.hpp>
|
2023-12-30 08:39:16 +00:00
|
|
|
#include <ClimateCard.hpp>
|
|
|
|
#include <ClimateIoT.hpp>
|
2023-12-28 06:14:18 +00:00
|
|
|
#include <IoTComponent.hpp>
|
2023-12-27 20:09:41 +00:00
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include <ETH.h>
|
2023-12-28 13:20:49 +00:00
|
|
|
#include <WiFi.h>
|
|
|
|
#include <FRAM.h>
|
2023-12-31 16:29:40 +00:00
|
|
|
#include <map>
|
|
|
|
|
2024-01-01 04:14:33 +00:00
|
|
|
// MQTT Connection Parameters
|
2023-12-31 16:29:40 +00:00
|
|
|
#define TCP_TIMEOUT_SEC 5
|
|
|
|
#define MQTT_RECONNECT_INTERVAL 30000
|
2023-12-28 13:20:49 +00:00
|
|
|
|
2023-12-30 19:18:57 +00:00
|
|
|
// FRAM Address for ESPMegaPROIoT
|
|
|
|
// Starts from 34
|
|
|
|
// Ends at 300 (inclusive)
|
|
|
|
// Total of 267 bytes
|
2023-12-30 19:59:25 +00:00
|
|
|
#define IOT_FRAM_ADDRESS 34
|
2023-12-30 19:18:57 +00:00
|
|
|
|
2024-01-01 04:14:33 +00:00
|
|
|
/**
|
|
|
|
* @brief The network configuration struct
|
|
|
|
* @note This struct will be saved to FRAM when calling saveNetworkConfig
|
|
|
|
*/
|
2023-12-28 13:20:49 +00:00
|
|
|
struct NetworkConfig
|
|
|
|
{
|
2024-01-01 04:14:33 +00:00
|
|
|
IPAddress ip; ///< The IP address
|
|
|
|
IPAddress gateway; ///< The gateway address
|
|
|
|
IPAddress subnet; ///< The subnet mask
|
|
|
|
IPAddress dns1; ///< The primary DNS server
|
|
|
|
IPAddress dns2; ///< The secondary DNS server
|
|
|
|
char hostname[32]; ///< The hostname
|
|
|
|
bool useStaticIp; ///< Whether to use a static IP, if false, DHCP will be used
|
|
|
|
bool useWifi; ///< Whether to use WiFi or Ethernet, if false, Ethernet will be used
|
|
|
|
bool wifiUseAuth; ///< Whether to use WiFi authentication, if false, ssid and password will be ignored
|
|
|
|
char ssid[32]; ///< The WiFi SSID, even if wifiUseAuth is false, this should be set
|
|
|
|
char password[32]; ///< The WiFi password, even if wifiUseAuth is false, this should be set
|
2023-12-28 13:20:49 +00:00
|
|
|
};
|
|
|
|
|
2024-01-01 04:14:33 +00:00
|
|
|
/**
|
|
|
|
* @brief The MQTT configuration struct
|
|
|
|
* @note This struct will be saved to FRAM when calling saveMqttConfig
|
|
|
|
*/
|
2023-12-28 13:20:49 +00:00
|
|
|
struct MqttConfig
|
|
|
|
{
|
2024-01-01 04:14:33 +00:00
|
|
|
char mqtt_server[32]; ///< The MQTT server address
|
|
|
|
uint16_t mqtt_port; ///< The MQTT server port
|
|
|
|
char mqtt_user[32]; ///< The MQTT username, even if mqtt_useauth is false, this should be set
|
|
|
|
char mqtt_password[32]; ///< The MQTT password, even if mqtt_useauth is false, this should be set
|
|
|
|
bool mqtt_useauth; ///< Whether to use MQTT authentication, if false, mqtt_user and mqtt_password will be ignored
|
|
|
|
char base_topic[32]; ///< The base topic for the MQTT messages
|
2023-12-28 13:20:49 +00:00
|
|
|
};
|
2023-12-27 20:09:41 +00:00
|
|
|
|
2024-01-01 04:14:33 +00:00
|
|
|
/**
|
|
|
|
* @brief The ESPMegaIoT class is a class that is used to interface with the ESPMegaPRO IoT module
|
|
|
|
*
|
|
|
|
* This class allows you to register IoT components and interface with them through MQTT.
|
|
|
|
* This class also manages the network and MQTT connections for you.
|
|
|
|
* Supports both WiFi and Ethernet.
|
|
|
|
* Also allows you to save and load network and MQTT configurations to and from FRAM.
|
|
|
|
* Also provides MQTT helpers for publishing and subscribing to topics.
|
|
|
|
*/
|
2023-12-27 20:09:41 +00:00
|
|
|
class ESPMegaIoT
|
|
|
|
{
|
2023-12-28 07:52:52 +00:00
|
|
|
public:
|
2023-12-28 08:18:37 +00:00
|
|
|
ESPMegaIoT();
|
2023-12-28 16:28:21 +00:00
|
|
|
~ESPMegaIoT();
|
2023-12-28 07:52:52 +00:00
|
|
|
void intr_begin(ExpansionCard *cards[]);
|
|
|
|
void loop();
|
|
|
|
void registerCard(uint8_t card_id);
|
2023-12-31 06:41:48 +00:00
|
|
|
void unregisterCard(uint8_t card_id);
|
2023-12-28 07:52:52 +00:00
|
|
|
void publishCard(uint8_t card_id);
|
|
|
|
// Publish topic appended with base topic
|
2024-02-09 10:48:43 +00:00
|
|
|
void publishRelative(const char *topic, const char *payload);
|
2023-12-28 07:52:52 +00:00
|
|
|
// Subscribe topic appended with base topic
|
2024-02-09 10:48:43 +00:00
|
|
|
void subscribeRelative(const char *topic);
|
|
|
|
void subscribe(const char *topic);
|
|
|
|
void unsubscribeFromTopic(const char *topic);
|
|
|
|
void connectToWifi(const char *ssid, const char *password);
|
|
|
|
void connectToWifi(const char *ssid);
|
2023-12-28 07:52:52 +00:00
|
|
|
void disconnectFromWifi();
|
|
|
|
bool wifiConnected();
|
2023-12-28 13:20:49 +00:00
|
|
|
void ethernetBegin();
|
|
|
|
void loadNetworkConfig();
|
|
|
|
void saveNetworkConfig();
|
2023-12-29 17:49:09 +00:00
|
|
|
NetworkConfig* getNetworkConfig();
|
|
|
|
MqttConfig* getMqttConfig();
|
2023-12-28 13:20:49 +00:00
|
|
|
void setMqttConfig(MqttConfig mqtt_config);
|
|
|
|
void saveMqttConfig();
|
|
|
|
void loadMqttConfig();
|
|
|
|
void connectNetwork();
|
|
|
|
void setNetworkConfig(NetworkConfig network_config);
|
|
|
|
void connectToMqtt();
|
2023-12-28 07:52:52 +00:00
|
|
|
bool connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt_port, char *mqtt_user, char *mqtt_password);
|
|
|
|
bool connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt_port);
|
2023-12-29 17:49:09 +00:00
|
|
|
bool mqttConnected();
|
2023-12-28 07:52:52 +00:00
|
|
|
void disconnectFromMqtt();
|
2023-12-31 16:29:40 +00:00
|
|
|
void publish(const char *topic, const char *payload);
|
|
|
|
uint8_t registerMqttCallback(std::function<void(char *, char *)> callback);
|
|
|
|
void unregisterMqttCallback(uint8_t handler);
|
|
|
|
uint8_t registerRelativeMqttCallback(std::function<void(char *, char *)> callback);
|
|
|
|
void unregisterRelativeMqttCallback(uint8_t handler);
|
|
|
|
uint8_t registerSubscribeCallback(std::function<void(void)> callback);
|
|
|
|
void unregisterSubscribeCallback(uint8_t handler);
|
2023-12-28 07:52:52 +00:00
|
|
|
void setBaseTopic(char *base_topic);
|
2023-12-28 16:28:21 +00:00
|
|
|
void bindEthernetInterface(ETHClass *ethernetIface);
|
2023-12-29 17:49:09 +00:00
|
|
|
bool networkConnected();
|
2023-12-30 19:18:57 +00:00
|
|
|
void bindFRAM(FRAM *fram);
|
2023-12-29 16:43:12 +00:00
|
|
|
|
2023-12-28 16:28:21 +00:00
|
|
|
IoTComponent* getComponent(uint8_t card_id);
|
2023-12-28 07:52:52 +00:00
|
|
|
IPAddress getETHIp();
|
2024-01-01 09:38:14 +00:00
|
|
|
IPAddress getWifiIp();
|
|
|
|
IPAddress getIp();
|
|
|
|
|
|
|
|
String getETHMac();
|
|
|
|
String getWifiMac();
|
|
|
|
String getMac();
|
2023-12-28 07:52:52 +00:00
|
|
|
|
|
|
|
private:
|
2023-12-28 13:20:49 +00:00
|
|
|
FRAM *fram;
|
|
|
|
bool useWifi;
|
|
|
|
bool WifiUseAuth;
|
|
|
|
char ssid[32];
|
|
|
|
char password[32];
|
2023-12-28 08:18:37 +00:00
|
|
|
WiFiClient tcpClient;
|
2023-12-28 07:52:52 +00:00
|
|
|
void sessionKeepAlive();
|
|
|
|
bool mqttReconnect();
|
|
|
|
void wifiReconnect();
|
|
|
|
void mqttSubscribe();
|
|
|
|
void mqttCallback(char *topic, byte *payload, unsigned int length);
|
2023-12-31 16:29:40 +00:00
|
|
|
uint8_t mqtt_callbacks_handler_index;
|
|
|
|
uint8_t mqtt_relative_callbacks_handler_index;
|
|
|
|
uint8_t subscribe_callbacks_handler_index;
|
|
|
|
std::map<uint8_t, std::function<void(char*, char*)>> mqtt_callbacks;
|
|
|
|
std::map<uint8_t, std::function<void(char*, char*)>> mqtt_relative_callbacks;
|
|
|
|
std::map<uint8_t, std::function<void(void)>> subscribe_callbacks;
|
2023-12-28 07:52:52 +00:00
|
|
|
void publishRelative(uint8_t card_id, char *topic, char *payload);
|
|
|
|
bool active;
|
|
|
|
PubSubClient mqtt;
|
|
|
|
IoTComponent *components[255];
|
|
|
|
char payload_buffer[200];
|
|
|
|
uint8_t base_topic_length;
|
|
|
|
ExpansionCard **cards; // Points to card array in ESPMegaPRO Core
|
|
|
|
// MQTT Connection Parameters
|
|
|
|
bool mqtt_connected;
|
2023-12-28 13:20:49 +00:00
|
|
|
NetworkConfig network_config;
|
2023-12-29 16:43:12 +00:00
|
|
|
MqttConfig mqtt_config;
|
2023-12-28 16:28:21 +00:00
|
|
|
ETHClass *ethernetIface;
|
2023-12-28 04:41:20 +00:00
|
|
|
};
|