#include void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length) { // Create a null terminated string from the payload memcpy(payload_buffer, payload, length); payload_buffer[length] = '\0'; // Remove the base topic from the topic char *topic_without_base = topic + strlen(base_topic) + 1; // Call the respective card's mqtt callback // Note that after the base topic, there should be the card id // /base_topic/card_id/... // 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 if (components[card_id] == NULL) { return; } components[card_id]->handleMqttMessage(topic_without_base + 3, payload_buffer); } void ESPMegaIoT::setBaseTopic(char *base_topic) { strcpy(this->base_topic, base_topic); base_topic_length = strlen(base_topic); } void ESPMegaIoT::begin(ExpansionCard *cards[]) { this->cards = cards; } void ESPMegaIoT::loop(); // 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 *));