diff --git a/Template Project/lib/ESPMegaPRO/AnalogIoT.hpp b/Template Project/lib/ESPMegaPRO/AnalogIoT.hpp index a64a1b7..07447b3 100644 --- a/Template Project/lib/ESPMegaPRO/AnalogIoT.hpp +++ b/Template Project/lib/ESPMegaPRO/AnalogIoT.hpp @@ -3,11 +3,15 @@ #include class AnalogIoT : public IoTComponent { public: + AnalogIoT(); + ~AnalogIoT(); bool begin(AnalogCard *card); void handleMqttMessage(char *topic, char *payload); void publishADCs(); void setADCsPublishInterval(uint32_t interval); void setADCsPublishEnabled(bool enabled); + void publishReport(); + uint8_t getType(); private: char *adc_topic; char *dac_topic; diff --git a/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp index 706bddc..c910175 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp +++ b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp @@ -11,7 +11,10 @@ class DigitalInputIoT : public IoTComponent { void setDigitalInputsPublishEnabled(bool enabled); void handleValueChange(uint8_t pin, uint8_t value); void registerValueChangeCallback(void (*callback)(uint8_t, uint8_t)); + void publishReport(); + uint8_t getType(); private: bool digital_inputs_publish_enabled = false; + DigitalInputCard *card; }; \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.cpp b/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.cpp index b354c51..d9c8bab 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.cpp +++ b/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.cpp @@ -1,11 +1,23 @@ #include +DigitalOutputIoT::DigitalOutputIoT() { + this->state_report_topic = new char[10]; + this->value_report_topic = new char[10]; +} + +DigitalOutputIoT::~DigitalOutputIoT() { + delete[] this->state_report_topic; + delete[] this->value_report_topic; +} + bool DigitalOutputIoT::begin(ExpansionCard *card) { this->card = (DigitalOutputCard *) card; this->set_state_length = strlen(SET_STATE_TOPIC); this->set_value_length = strlen(SET_VALUE_TOPIC); this->state_length = strlen(STATE_TOPIC); this->value_length = strlen(VALUE_TOPIC); + strcpy(this->state_report_topic, "00/state"); + strcpy(this->value_report_topic, "00/value"); return true; } @@ -100,28 +112,56 @@ bool DigitalOutputIoT::processRequestStateMessage(char *topic, char *payload, ui } void DigitalOutputIoT::publishDigitalOutputs() { - + if(!digital_outputs_publish_enabled) return; + for(int i = 1; i <= 16; i++) { + publishDigitalOutput(i); + } } void DigitalOutputIoT::publishDigitalOutput(uint8_t pin) { - char ]; + publishDigitalOutputState(pin); + publishDigitalOutputValue(pin); +} + +void DigitalOutputIoT::publishDigitalOutputState(uint8_t pin) { + if(!digital_outputs_publish_enabled) return; + state_report_topic[0] = pin / 10 + '0'; + state_report_topic[1] = pin % 10 + '0'; + mqtt->publish(state_report_topic, card->getState(pin) ? "1" : "0"); +} + +void DigitalOutputIoT::publishDigitalOutputValue(uint8_t pin) { + if(!digital_outputs_publish_enabled) return; + value_report_topic[0] = pin / 10 + '0'; + value_report_topic[1] = pin % 10 + '0'; + char payload[5]; + sprintf(payload, "%d", card->getValue(pin)); + mqtt->publish(value_report_topic, payload); } void DigitalOutputIoT::setDigitalOutputsPublishEnabled(bool enabled) { digital_outputs_publish_enabled = enabled; } -void DigitalOutputIoT::handleValueChange(uint8_t pin, bool value, uint16_t time) { - if (digital_outputs_publish_enabled) { - char payload[17]; - for (uint8_t i = 1; i <= 16; i++) { - payload[i - 1] = card->getDigitalOutput(i) ? '1' : '0'; - } - payload[16] = '\0'; - mqtt->publish(digital_outputs_topic, payload); +void DigitalOutputIoT::handleValueChange(uint8_t pin, bool state, uint16_t value) { + publishDigitalOutput(pin); + if(value_change_callback != NULL) { + value_change_callback(pin, state, value); } } void DigitalOutputIoT::registerValueChangeCallback(void (*callback)(uint8_t, bool, uint16_t)) { - card->registerDigitalOutputValueChangeCallback(callback); + value_change_callback = callback; } + +void DigitalOutputIoT::deregisterValueChangeCallback() { + value_change_callback = NULL; +} + +void DigitalOutputIoT::publishReport() { + publishDigitalOutputs(); +} + +uint8_t DigitalOutputIoT::getType() { + return CARD_TYPE_DIGITAL_OUTPUT; +} \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.hpp b/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.hpp index b92cee7..5d30070 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.hpp +++ b/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.hpp @@ -5,19 +5,29 @@ class DigitalOutputIoT : public IoTComponent { public: + DigitalOutputIoT(); + ~DigitalOutputIoT(); bool begin(ExpansionCard *card); void handleMqttMessage(char *topic, char *payload); void publishDigitalOutputs(); void publishDigitalOutput(uint8_t pin); + void publishDigitalOutputState(uint8_t pin); + void publishDigitalOutputValue(uint8_t pin); void setDigitalOutputsPublishEnabled(bool enabled); - void handleValueChange(uint8_t pin, uint8_t value); + void handleValueChange(uint8_t pin, bool state, uint16_t value); void registerValueChangeCallback(void (*callback)(uint8_t, bool, uint16_t)); + void deregisterValueChangeCallback(); + void publishReport(); + uint8_t getType(); private: bool digital_outputs_publish_enabled = false; bool processSetStateMessage(char *topic, char *payload, uint8_t topic_length); bool processSetValueMessage(char *topic, char *payload, uint8_t topic_length); bool processRequestStateMessage(char *topic, char *payload, uint8_t topic_length); + void (*value_change_callback)(uint8_t, bool, uint16_t); DigitalOutputCard *card; + char *state_report_topic; + char *value_report_topic; uint8_t set_state_length; uint8_t set_value_length; uint8_t state_length; diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp index ce24553..bd1a364 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -13,22 +13,9 @@ void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length) { char *card_id_str = strtok(topic_without_base, "/"); uint8_t card_id = atoi(card_id_str); // Check if the card is registered - if (expansionCards[card_id] == NULL) { + if (components[card_id] == NULL) { return; } - // Get the card type - uint8_t card_type = expansionCards[card_id]->getType(); - // Cast the card to the respective type - switch(card_type) { - case CARD_TYPE_ANALOG_INPUT: - ((AnalogIoT *)expansionCards[card_id])->mqttCallback(topic_without_base + strlen(card_id_str) + 1, payload_buffer); - break; - case CARD_TYPE_DIGITAL_INPUT: - ((DigitalInputIoT *)expansionCards[card_id])->mqttCallback(topic_without_base + strlen(card_id_str) + 1, payload_buffer); - break; - case CARD_TYPE_DIGITAL_OUTPUT: - ((DigitalOutputIoT *)expansionCards[card_id])->mqttCallback(topic_without_base + strlen(card_id_str) + 1, payload_buffer); - break; - } + components[card_id]->handleMqttMessage(topic_without_base + 3, payload_buffer); } diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp index 1237fb6..7eed58c 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -25,10 +26,10 @@ class ESPMegaIoT 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 disconnectFromMqtt(); void publishToTopic(char *topic, char *payload); void registerMqttCallback(void (*callback)(char *, char *)); - void checkCardType(uint8_t card_id); private: void sessionKeepAlive(); @@ -36,7 +37,7 @@ class ESPMegaIoT void wifiReconnect(); void mqttCallback(char *topic, byte *payload, unsigned int length); PubSubClient mqtt; - ExpansionCard *expansionCards[255]; + IoTComponent *components[255]; bool card_publish_enabled[255]; char payload_buffer[200]; char base_topic[100]; diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp index d4420ce..e23c5fa 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp @@ -2,10 +2,9 @@ #include #include #include +#include #include #include -#include -#include #include #include #include @@ -38,6 +37,7 @@ class ESPMegaPRO { FRAM fram; DigitalInputCard inputs = DigitalInputCard(INPUT_BANK_A_ADDRESS, INPUT_BANK_B_ADDRESS); DigitalOutputCard outputs = DigitalOutputCard(PWM_BANK_ADDRESS); + ESPMegaIoT iot = ESPMegaIoT(); private: ExpansionCard* cards[255]; bool cardInstalled[255]; diff --git a/Template Project/lib/ESPMegaPRO/IoTComponent.hpp b/Template Project/lib/ESPMegaPRO/IoTComponent.hpp index 582ffc8..3321857 100644 --- a/Template Project/lib/ESPMegaPRO/IoTComponent.hpp +++ b/Template Project/lib/ESPMegaPRO/IoTComponent.hpp @@ -6,6 +6,8 @@ class IoTComponent { virtual bool begin(ExpansionCard *card); virtual void handleMqttMessage(char *topic, char *payload); void setMqttClient(PubSubClient *mqtt); - private: + virtual void publishReport(); + virtual uint8_t getType(); + protected: PubSubClient *mqtt; }; diff --git a/Template Project/src/cardsdemo.cpp b/Template Project/src/cardsdemo.cpp.disabled similarity index 100% rename from Template Project/src/cardsdemo.cpp rename to Template Project/src/cardsdemo.cpp.disabled diff --git a/Template Project/src/iotdemo.cpp b/Template Project/src/iotdemo.cpp new file mode 100644 index 0000000..efbb182 --- /dev/null +++ b/Template Project/src/iotdemo.cpp @@ -0,0 +1,38 @@ +#include + +// Instantiate ESPMega +ESPMegaPRO espmega = ESPMegaPRO(); + +void setup() +{ + // Initialize ESPMega + espmega.begin(); + Serial.println("ESPMega initialized"); +} + +void loop() +{ + // Read all the inputs and print them + for (int i = 0; i < 16; i++) + { + Serial.print("Input "); + Serial.print(i); + Serial.print(": "); + Serial.print(espmega.inputs.digitalRead(i)); + Serial.println(); + } + // Turn on all the outputs + for (int i = 0; i < 16; i++) + { + espmega.outputs.digitalWrite(i, true); + } + // Wait 1 second + delay(1000); + // Turn off all the outputs + for (int i = 0; i < 16; i++) + { + espmega.outputs.digitalWrite(i, false); + } + // Wait 1 second + delay(1000); +} \ No newline at end of file