ESPMegaPRO-v3-SDK/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp

47 lines
1.8 KiB
C++

#include <ESPMegaIoT.hpp>
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() {
}
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);
void ESPMegaIoT::disconnectFromMqtt();
void ESPMegaIoT::publishToTopic(char *topic, char *payload);
void ESPMegaIoT::registerMqttCallback(void (*callback)(char *, char *));