ESPMegaPRO-v3-SDK/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.hpp

116 lines
3.4 KiB
C++
Raw Normal View History

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>
struct NetworkConfig
{
IPAddress ip;
IPAddress gateway;
IPAddress subnet;
IPAddress dns1;
IPAddress dns2;
char hostname[32];
bool useStaticIp;
bool useWifi;
bool wifiUseAuth;
char ssid[32];
char password[32];
};
struct MqttConfig
{
char mqtt_server[32];
uint16_t mqtt_port;
char mqtt_user[32];
char mqtt_password[32];
bool mqtt_useauth;
2023-12-28 16:28:21 +00:00
char base_topic[32];
2023-12-28 13:20:49 +00:00
};
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);
void deregisterCard(uint8_t card_id);
void publishCard(uint8_t card_id);
// Publish topic appended with base topic
void publishRelative(char *topic, char *payload);
// Subscribe topic appended with base topic
void subscribeRelative(char *topic);
void subscribeToTopic(char *topic);
void unsubscribeFromTopic(char *topic);
void connectToWifi(char *ssid, char *password);
void connectToWifi(char *ssid);
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();
void publishToTopic(char *topic, char *payload);
void registerMqttCallback(void (*callback)(char *, char *));
void registerRelativeMqttCallback(void (*callback)(char *, char *));
2023-12-28 08:52:08 +00:00
void registerSubscribeCallback(void (*callback)(void));
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-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();
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);
void (*user_mqtt_callback)(char *, char *);
void (*user_relative_mqtt_callback)(char *, char *);
2023-12-28 08:52:08 +00:00
void (*user_subscribe_callback)(void);
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];
char base_topic[100];
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
};