initial mqtt implementation

This commit is contained in:
Siwat Sirichai 2023-12-28 14:08:10 +07:00
parent 17160e586b
commit da277e1e4a
9 changed files with 109 additions and 29 deletions

View File

@ -1,5 +1,6 @@
{
"files.associations": {
"*.cps": "javascript",
"array": "cpp",
"deque": "cpp",
"string": "cpp",
@ -8,7 +9,10 @@
"vector": "cpp",
"string_view": "cpp",
"initializer_list": "cpp",
"adafruit_ads1x15.h": "c"
"adafruit_ads1x15.h": "c",
"*.tcc": "cpp",
"memory": "cpp",
"random": "cpp"
},
"cmake.configureOnOpen": true,
"cmake.sourceDirectory": "D:/Git/ESPMegaPRO-v3-SDK/Template Project/.pio/libdeps/wt32-eth01/Adafruit BusIO"

View File

@ -5,7 +5,7 @@ class AnalogIoT : public IoTComponent {
public:
AnalogIoT();
~AnalogIoT();
bool begin(AnalogCard *card, void (*publishRelative)(uint8_t, char *, char *), PubSubClient *mqtt, char *base_topic);
bool begin(uint8_t card_id, AnalogCard *card, PubSubClient *mqtt, char *base_topic);
void handleMqttMessage(char *topic, char *payload);
void publishADCs();
void setADCsPublishInterval(uint32_t interval);

View File

@ -5,7 +5,7 @@
class DigitalInputIoT : public IoTComponent {
public:
bool begin(DigitalInputCard *card, void (*publishRelative)(uint8_t, char *, char *), PubSubClient *mqtt, char *base_topic);
bool begin(uint8_t card_id, DigitalInputCard *card, PubSubClient *mqtt, char *base_topic);
void handleMqttMessage(char *topic, char *payload);
void publishDigitalInputs();
void setDigitalInputsPublishEnabled(bool enabled);

View File

@ -10,11 +10,12 @@ DigitalOutputIoT::~DigitalOutputIoT() {
delete[] this->value_report_topic;
}
bool DigitalOutputIoT::begin(ExpansionCard *card, void (*publishRelative)(uint8_t, char *, char *), PubSubClient *mqtt, char *base_topic) {
bool DigitalOutputIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic) {
this->mqtt = mqtt;
this->base_topic = base_topic;
this->publishRelative = publishRelative;
this->card = (DigitalOutputCard *) card;
if(!card->begin()) return false;
this-> card_id = card_id;
this->set_state_length = strlen(SET_STATE_TOPIC);
this->set_value_length = strlen(SET_VALUE_TOPIC);
this->state_length = strlen(STATE_TOPIC);

View File

@ -7,7 +7,7 @@ class DigitalOutputIoT : public IoTComponent {
public:
DigitalOutputIoT();
~DigitalOutputIoT();
bool begin(ExpansionCard *card, void (*publishRelative)(uint8_t, char *, char *), PubSubClient *mqtt, char *base_topic);
bool begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic);
void handleMqttMessage(char *topic, char *payload);
void publishDigitalOutputs();
void publishDigitalOutput(uint8_t pin);

View File

@ -24,23 +24,92 @@ void ESPMegaIoT::setBaseTopic(char *base_topic) {
base_topic_length = strlen(base_topic);
}
void ESPMegaIoT::begin() {
void ESPMegaIoT::begin(ExpansionCard *cards[]) {
this->cards = cards;
}
void ESPMegaIoT::loop();
void ESPMegaIoT::registerCard(uint8_t card_id);
void ESPMegaIoT::deregisterCard(uint8_t card_id);
void ESPMegaIoT::publishCard(uint8_t card_id);
void ESPMegaIoT::subscribeToTopic(char *topic);
void ESPMegaIoT::unsubscribeFromTopic(char *topic);
void ESPMegaIoT::connectToEthernet();
bool ESPMegaIoT::ethernetConnected();
void ESPMegaIoT::connectToWifi(char *ssid, char *password);
void ESPMegaIoT::connectToWifi(char *ssid);
void ESPMegaIoT::disconnectFromWifi();
bool ESPMegaIoT::wifiConnected();
void ESPMegaIoT::connectToMqtt(char *mqtt_server, uint16_t mqtt_port, char *mqtt_user, char *mqtt_password);
void ESPMegaIoT::connectToMqtt(char *mqtt_server, uint16_t mqtt_port);
// Register Existing Card for use with IoT
void ESPMegaIoT::registerCard(uint8_t card_id) {
// Check if the card is already registered
if (components[card_id] != NULL) {
return;
}
// Get the card type
uint8_t card_type = cards[card_id]->getType();
// Create the respective IoT component
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;
case CARD_TYPE_DIGITAL_INPUT:
components[card_id] = new DigitalInputIoT();
components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic);
break;
case CARD_TYPE_DIGITAL_OUTPUT:
components[card_id] = new DigitalOutputIoT();
components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic);
break;
default:
return;
}
// Initialize the IoT component
// Enable publishing for the card
card_publish_enabled[card_id] = true;
}
void ESPMegaIoT::deregisterCard(uint8_t card_id) {
// Check if the card is registered
if (components[card_id] == NULL) {
return;
}
// Delete the IoT component
delete components[card_id];
components[card_id] = NULL;
// Disable publishing for the card
card_publish_enabled[card_id] = false;
}
void ESPMegaIoT::publishCard(uint8_t card_id) {
// Check if the card is registered
if (components[card_id] == NULL) {
return;
}
// Publish the card
components[card_id]->publishReport();
}
void ESPMegaIoT::subscribeToTopic(char *topic) {
mqtt.subscribe(topic);
}
void ESPMegaIoT::unsubscribeFromTopic(char *topic) {
mqtt.unsubscribe(topic);
}
void ESPMegaIoT::connectToEthernet() {
ETH.begin();
}
bool ESPMegaIoT::ethernetConnected() {
return ETH.linkUp();
}
void ESPMegaIoT::connectToWifi(char *ssid, char *password) {
WiFi.begin(ssid, password);
}
void ESPMegaIoT::connectToWifi(char *ssid) {
WiFi.begin(ssid);
}
void ESPMegaIoT::disconnectFromWifi() {
WiFi.disconnect();
}
bool ESPMegaIoT::wifiConnected() {
return WiFi.status() == WL_CONNECTED;
}
void ESPMegaIoT::connectToMqtt(char*client_id, char *mqtt_server, uint16_t mqtt_port, char *mqtt_user, char *mqtt_password) {
mqtt.setServer(mqtt_server, mqtt_port);
mqtt.setCallback(mqttCallback);
mqtt.connect(client_id, mqtt_user, mqtt_password);
sessionKeepAlive();
}
void ESPMegaIoT::connectToMqtt(char* client_id, char *mqtt_server, uint16_t mqtt_port);
void ESPMegaIoT::disconnectFromMqtt();
void ESPMegaIoT::publishToTopic(char *topic, char *payload);
void ESPMegaIoT::registerMqttCallback(void (*callback)(char *, char *));

View File

@ -12,7 +12,7 @@
class ESPMegaIoT
{
public:
void begin();
void begin(ExpansionCard *cards[]);
void loop();
void registerCard(uint8_t card_id);
void deregisterCard(uint8_t card_id);
@ -25,24 +25,23 @@ class ESPMegaIoT
void connectToWifi(char *ssid);
void disconnectFromWifi();
bool wifiConnected();
void connectToMqtt(char *mqtt_server, uint16_t mqtt_port, char *mqtt_user, char *mqtt_password);
void connectToMqtt(char *mqtt_server, uint16_t mqtt_port);
void connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt_port, char *mqtt_user, char *mqtt_password);
void connectToMqtt(char* client_id, char *mqtt_server, uint16_t mqtt_port);
void disconnectFromMqtt();
void publishToTopic(char *topic, char *payload);
void registerMqttCallback(void (*callback)(char *, char *));
void setBaseTopic(char *base_topic);
private:
void sessionKeepAlive();
void mqttReconnect();
void wifiReconnect();
void mqttCallback(char *topic, byte *payload, unsigned int length);
void publishRelative(char *topic, char *payload);
void publishRelative(uint8_t card_id, char *topic, char *payload);
PubSubClient mqtt;
IoTComponent *components[255];
bool card_publish_enabled[255];
char payload_buffer[200];
char base_topic[100];
uint8_t base_topic_length;
ExpansionCard **cards; // Points to card array in ESPMegaPRO Core
};

View File

@ -3,3 +3,9 @@
void IoTComponent::setMqttClient(PubSubClient *mqtt) {
this->mqtt = mqtt;
}
void IoTComponent::publishRelative(char *topic, char *payload) {
char absolute_topic[100];
sprintf(absolute_topic, "%s/%d/%s", base_topic, card_id, topic);
mqtt->publish(absolute_topic, payload);
}

View File

@ -3,13 +3,14 @@
#include <PubSubClient.h>
class IoTComponent {
public:
virtual bool begin(ExpansionCard *card, void (*publishRelative)(uint8_t, char *, char *), PubSubClient *mqtt, char *base_topic);
virtual bool begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic);
virtual void handleMqttMessage(char *topic, char *payload);
void setMqttClient(PubSubClient *mqtt);
virtual void publishReport();
virtual uint8_t getType();
protected:
char *base_topic;
void (*publishRelative)(uint8_t, char *, char *);
void publishRelative(char *topic, char *payload);
PubSubClient *mqtt;
uint8_t card_id;
};