initial mqtt implementation
This commit is contained in:
parent
17160e586b
commit
da277e1e4a
|
@ -1,5 +1,6 @@
|
||||||
{
|
{
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
|
"*.cps": "javascript",
|
||||||
"array": "cpp",
|
"array": "cpp",
|
||||||
"deque": "cpp",
|
"deque": "cpp",
|
||||||
"string": "cpp",
|
"string": "cpp",
|
||||||
|
@ -8,7 +9,10 @@
|
||||||
"vector": "cpp",
|
"vector": "cpp",
|
||||||
"string_view": "cpp",
|
"string_view": "cpp",
|
||||||
"initializer_list": "cpp",
|
"initializer_list": "cpp",
|
||||||
"adafruit_ads1x15.h": "c"
|
"adafruit_ads1x15.h": "c",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"random": "cpp"
|
||||||
},
|
},
|
||||||
"cmake.configureOnOpen": true,
|
"cmake.configureOnOpen": true,
|
||||||
"cmake.sourceDirectory": "D:/Git/ESPMegaPRO-v3-SDK/Template Project/.pio/libdeps/wt32-eth01/Adafruit BusIO"
|
"cmake.sourceDirectory": "D:/Git/ESPMegaPRO-v3-SDK/Template Project/.pio/libdeps/wt32-eth01/Adafruit BusIO"
|
||||||
|
|
|
@ -5,7 +5,7 @@ class AnalogIoT : public IoTComponent {
|
||||||
public:
|
public:
|
||||||
AnalogIoT();
|
AnalogIoT();
|
||||||
~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 handleMqttMessage(char *topic, char *payload);
|
||||||
void publishADCs();
|
void publishADCs();
|
||||||
void setADCsPublishInterval(uint32_t interval);
|
void setADCsPublishInterval(uint32_t interval);
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
class DigitalInputIoT : public IoTComponent {
|
class DigitalInputIoT : public IoTComponent {
|
||||||
public:
|
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 handleMqttMessage(char *topic, char *payload);
|
||||||
void publishDigitalInputs();
|
void publishDigitalInputs();
|
||||||
void setDigitalInputsPublishEnabled(bool enabled);
|
void setDigitalInputsPublishEnabled(bool enabled);
|
||||||
|
|
|
@ -10,11 +10,12 @@ DigitalOutputIoT::~DigitalOutputIoT() {
|
||||||
delete[] this->value_report_topic;
|
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->mqtt = mqtt;
|
||||||
this->base_topic = base_topic;
|
this->base_topic = base_topic;
|
||||||
this->publishRelative = publishRelative;
|
|
||||||
this->card = (DigitalOutputCard *) card;
|
this->card = (DigitalOutputCard *) card;
|
||||||
|
if(!card->begin()) return false;
|
||||||
|
this-> card_id = card_id;
|
||||||
this->set_state_length = strlen(SET_STATE_TOPIC);
|
this->set_state_length = strlen(SET_STATE_TOPIC);
|
||||||
this->set_value_length = strlen(SET_VALUE_TOPIC);
|
this->set_value_length = strlen(SET_VALUE_TOPIC);
|
||||||
this->state_length = strlen(STATE_TOPIC);
|
this->state_length = strlen(STATE_TOPIC);
|
||||||
|
|
|
@ -7,7 +7,7 @@ class DigitalOutputIoT : public IoTComponent {
|
||||||
public:
|
public:
|
||||||
DigitalOutputIoT();
|
DigitalOutputIoT();
|
||||||
~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 handleMqttMessage(char *topic, char *payload);
|
||||||
void publishDigitalOutputs();
|
void publishDigitalOutputs();
|
||||||
void publishDigitalOutput(uint8_t pin);
|
void publishDigitalOutput(uint8_t pin);
|
||||||
|
|
|
@ -24,23 +24,92 @@ void ESPMegaIoT::setBaseTopic(char *base_topic) {
|
||||||
base_topic_length = strlen(base_topic);
|
base_topic_length = strlen(base_topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPMegaIoT::begin() {
|
void ESPMegaIoT::begin(ExpansionCard *cards[]) {
|
||||||
|
this->cards = cards;
|
||||||
}
|
}
|
||||||
void ESPMegaIoT::loop();
|
void ESPMegaIoT::loop();
|
||||||
void ESPMegaIoT::registerCard(uint8_t card_id);
|
|
||||||
void ESPMegaIoT::deregisterCard(uint8_t card_id);
|
// Register Existing Card for use with IoT
|
||||||
void ESPMegaIoT::publishCard(uint8_t card_id);
|
void ESPMegaIoT::registerCard(uint8_t card_id) {
|
||||||
void ESPMegaIoT::subscribeToTopic(char *topic);
|
// Check if the card is already registered
|
||||||
void ESPMegaIoT::unsubscribeFromTopic(char *topic);
|
if (components[card_id] != NULL) {
|
||||||
void ESPMegaIoT::connectToEthernet();
|
return;
|
||||||
bool ESPMegaIoT::ethernetConnected();
|
}
|
||||||
void ESPMegaIoT::connectToWifi(char *ssid, char *password);
|
// Get the card type
|
||||||
void ESPMegaIoT::connectToWifi(char *ssid);
|
uint8_t card_type = cards[card_id]->getType();
|
||||||
void ESPMegaIoT::disconnectFromWifi();
|
// Create the respective IoT component
|
||||||
bool ESPMegaIoT::wifiConnected();
|
switch (card_type) {
|
||||||
void ESPMegaIoT::connectToMqtt(char *mqtt_server, uint16_t mqtt_port, char *mqtt_user, char *mqtt_password);
|
case CARD_TYPE_ANALOG:
|
||||||
void ESPMegaIoT::connectToMqtt(char *mqtt_server, uint16_t mqtt_port);
|
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::disconnectFromMqtt();
|
||||||
void ESPMegaIoT::publishToTopic(char *topic, char *payload);
|
void ESPMegaIoT::publishToTopic(char *topic, char *payload);
|
||||||
void ESPMegaIoT::registerMqttCallback(void (*callback)(char *, char *));
|
void ESPMegaIoT::registerMqttCallback(void (*callback)(char *, char *));
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
class ESPMegaIoT
|
class ESPMegaIoT
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void begin();
|
void begin(ExpansionCard *cards[]);
|
||||||
void loop();
|
void loop();
|
||||||
void registerCard(uint8_t card_id);
|
void registerCard(uint8_t card_id);
|
||||||
void deregisterCard(uint8_t card_id);
|
void deregisterCard(uint8_t card_id);
|
||||||
|
@ -25,24 +25,23 @@ class ESPMegaIoT
|
||||||
void connectToWifi(char *ssid);
|
void connectToWifi(char *ssid);
|
||||||
void disconnectFromWifi();
|
void disconnectFromWifi();
|
||||||
bool wifiConnected();
|
bool wifiConnected();
|
||||||
void connectToMqtt(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, 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);
|
||||||
void disconnectFromMqtt();
|
void disconnectFromMqtt();
|
||||||
void publishToTopic(char *topic, char *payload);
|
void publishToTopic(char *topic, char *payload);
|
||||||
void registerMqttCallback(void (*callback)(char *, char *));
|
void registerMqttCallback(void (*callback)(char *, char *));
|
||||||
void setBaseTopic(char *base_topic);
|
void setBaseTopic(char *base_topic);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void sessionKeepAlive();
|
void sessionKeepAlive();
|
||||||
void mqttReconnect();
|
void mqttReconnect();
|
||||||
void wifiReconnect();
|
void wifiReconnect();
|
||||||
void mqttCallback(char *topic, byte *payload, unsigned int length);
|
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;
|
PubSubClient mqtt;
|
||||||
IoTComponent *components[255];
|
IoTComponent *components[255];
|
||||||
bool card_publish_enabled[255];
|
bool card_publish_enabled[255];
|
||||||
char payload_buffer[200];
|
char payload_buffer[200];
|
||||||
char base_topic[100];
|
char base_topic[100];
|
||||||
uint8_t base_topic_length;
|
uint8_t base_topic_length;
|
||||||
|
ExpansionCard **cards; // Points to card array in ESPMegaPRO Core
|
||||||
};
|
};
|
|
@ -2,4 +2,10 @@
|
||||||
|
|
||||||
void IoTComponent::setMqttClient(PubSubClient *mqtt) {
|
void IoTComponent::setMqttClient(PubSubClient *mqtt) {
|
||||||
this->mqtt = 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);
|
||||||
}
|
}
|
|
@ -3,13 +3,14 @@
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
class IoTComponent {
|
class IoTComponent {
|
||||||
public:
|
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);
|
virtual void handleMqttMessage(char *topic, char *payload);
|
||||||
void setMqttClient(PubSubClient *mqtt);
|
void setMqttClient(PubSubClient *mqtt);
|
||||||
virtual void publishReport();
|
virtual void publishReport();
|
||||||
virtual uint8_t getType();
|
virtual uint8_t getType();
|
||||||
protected:
|
protected:
|
||||||
char *base_topic;
|
char *base_topic;
|
||||||
void (*publishRelative)(uint8_t, char *, char *);
|
void publishRelative(char *topic, char *payload);
|
||||||
PubSubClient *mqtt;
|
PubSubClient *mqtt;
|
||||||
|
uint8_t card_id;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue