From 908c3f9c0ef626a4342da8fa6d0aa7a08c7495b0 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 28 Dec 2023 11:41:20 +0700 Subject: [PATCH] iot base changes --- .../lib/ESPMegaPRO/AnalogCard.cpp | 5 +++ .../lib/ESPMegaPRO/AnalogCard.hpp | 3 ++ .../lib/ESPMegaPRO/DigitalInputCard.cpp | 5 +++ .../lib/ESPMegaPRO/DigitalInputCard.hpp | 4 +++ .../lib/ESPMegaPRO/DigitalOutputCard.cpp | 5 +++ .../lib/ESPMegaPRO/DigitalOutputCard.hpp | 5 +++ .../lib/ESPMegaPRO/DigitalOutputIoT.cpp | 0 .../lib/ESPMegaPRO/DigitalOutputIoT.hpp | 4 +++ .../lib/ESPMegaPRO/ESPMegaIoT.cpp | 34 +++++++++++++++++++ .../lib/ESPMegaPRO/ESPMegaIoT.hpp | 9 ++++- .../lib/ESPMegaPRO/ExpansionCard.hpp | 2 ++ .../lib/ESPMegaPRO/IoTComponent.hpp | 2 ++ Template Project/platformio.ini | 4 ++- 13 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 Template Project/lib/ESPMegaPRO/DigitalOutputIoT.cpp create mode 100644 Template Project/lib/ESPMegaPRO/DigitalOutputIoT.hpp diff --git a/Template Project/lib/ESPMegaPRO/AnalogCard.cpp b/Template Project/lib/ESPMegaPRO/AnalogCard.cpp index 739fcd0..286a08c 100644 --- a/Template Project/lib/ESPMegaPRO/AnalogCard.cpp +++ b/Template Project/lib/ESPMegaPRO/AnalogCard.cpp @@ -79,4 +79,9 @@ bool AnalogCard::begin() void AnalogCard::loop() { +} + +uint8_t AnalogCard::getType() +{ + return CARD_TYPE_ANALOG; } \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/AnalogCard.hpp b/Template Project/lib/ESPMegaPRO/AnalogCard.hpp index c5115cf..d913326 100644 --- a/Template Project/lib/ESPMegaPRO/AnalogCard.hpp +++ b/Template Project/lib/ESPMegaPRO/AnalogCard.hpp @@ -3,6 +3,8 @@ #include #include +#define CARD_TYPE_ANALOG 0x02 + #define ANALOG_INPUT_BANK_A_ADDRESS 0x48 #define ANALOG_INPUT_BANK_B_ADDRESS 0x49 #define DAC0_ADDRESS 0x60 @@ -17,6 +19,7 @@ class AnalogCard : public ExpansionCard { uint16_t analogRead(uint8_t pin); bool begin(); void loop(); + uint8_t getType(); private: MCP4725 dac0; MCP4725 dac1; diff --git a/Template Project/lib/ESPMegaPRO/DigitalInputCard.cpp b/Template Project/lib/ESPMegaPRO/DigitalInputCard.cpp index cebb198..8ebdec5 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalInputCard.cpp +++ b/Template Project/lib/ESPMegaPRO/DigitalInputCard.cpp @@ -192,4 +192,9 @@ void DigitalInputCard::loadPinMap(uint8_t pinMap[16]) // Load the virtual pin map (virtual pin to physical pin) this->virtualPinMap[pinMap[i]] = i; } +} + +uint8_t DigitalInputCard::getType() +{ + return CARD_TYPE_DIGITAL_INPUT; } \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/DigitalInputCard.hpp b/Template Project/lib/ESPMegaPRO/DigitalInputCard.hpp index 63aa85c..86b6be1 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalInputCard.hpp +++ b/Template Project/lib/ESPMegaPRO/DigitalInputCard.hpp @@ -2,6 +2,8 @@ #include #include +#define CARD_TYPE_DIGITAL_INPUT 0x01 + class DigitalInputCard : public ExpansionCard { public: // Instantiate the card with the specified address @@ -28,6 +30,8 @@ class DigitalInputCard : public ExpansionCard { void unregisterCallback(); // Load a new pin map void loadPinMap(uint8_t pinMap[16]); + // Get type of card + uint8_t getType(); private: PCF8574 inputBankA; PCF8574 inputBankB; diff --git a/Template Project/lib/ESPMegaPRO/DigitalOutputCard.cpp b/Template Project/lib/ESPMegaPRO/DigitalOutputCard.cpp index 89a2562..fba7868 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalOutputCard.cpp +++ b/Template Project/lib/ESPMegaPRO/DigitalOutputCard.cpp @@ -38,4 +38,9 @@ void DigitalOutputCard::analogWrite(uint8_t pin, uint16_t value) { // Dummy loop function void DigitalOutputCard::loop() { +} + +// Get type of card +uint8_t DigitalOutputCard::getType() { + return CARD_TYPE_DIGITAL_OUTPUT; } \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/DigitalOutputCard.hpp b/Template Project/lib/ESPMegaPRO/DigitalOutputCard.hpp index fc2b14b..0ef1912 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalOutputCard.hpp +++ b/Template Project/lib/ESPMegaPRO/DigitalOutputCard.hpp @@ -1,6 +1,9 @@ #pragma once #include #include + +#define CARD_TYPE_DIGITAL_OUTPUT 0x00 + class DigitalOutputCard : public ExpansionCard { public: @@ -16,6 +19,8 @@ public: void digitalWrite(uint8_t pin, bool state); // Set the output to the specified pwm value void analogWrite(uint8_t pin, uint16_t value); + // Get type of card + uint8_t getType(); private: Adafruit_PWMServoDriver pwm; uint8_t address; diff --git a/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.cpp b/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.hpp b/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.hpp new file mode 100644 index 0000000..adc2658 --- /dev/null +++ b/Template Project/lib/ESPMegaPRO/DigitalOutputIoT.hpp @@ -0,0 +1,4 @@ +#pragma once +#include + +class DigitalOutputIoT : p \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp index e69de29..ce24553 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -0,0 +1,34 @@ +#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 (expansionCards[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; + } +} + diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp index 3a1c51c..1237fb6 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp @@ -1,7 +1,10 @@ #include #include +#include #include +#include #include +#include #include #include @@ -25,12 +28,16 @@ class ESPMegaIoT void disconnectFromMqtt(); void publishToTopic(char *topic, char *payload); void registerMqttCallback(void (*callback)(char *, char *)); + void checkCardType(uint8_t card_id); private: void sessionKeepAlive(); void mqttReconnect(); void wifiReconnect(); + void mqttCallback(char *topic, byte *payload, unsigned int length); PubSubClient mqtt; ExpansionCard *expansionCards[255]; bool card_publish_enabled[255]; -} \ No newline at end of file + char payload_buffer[200]; + char base_topic[100]; +}; \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp b/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp index 2f3a6bc..cad282e 100644 --- a/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp +++ b/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp @@ -8,4 +8,6 @@ class ExpansionCard { virtual bool begin() {} // Preform a loop to refresh the input buffers virtual void loop() {} + // Get the card type + virtual uint8_t getType() {} }; \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/IoTComponent.hpp b/Template Project/lib/ESPMegaPRO/IoTComponent.hpp index e69de29..e7310ac 100644 --- a/Template Project/lib/ESPMegaPRO/IoTComponent.hpp +++ b/Template Project/lib/ESPMegaPRO/IoTComponent.hpp @@ -0,0 +1,2 @@ + +class \ No newline at end of file diff --git a/Template Project/platformio.ini b/Template Project/platformio.ini index fc695a6..4ae42e7 100644 --- a/Template Project/platformio.ini +++ b/Template Project/platformio.ini @@ -23,4 +23,6 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1 robtillaart/FRAM_I2C@^0.6.1 paulstoffregen/Time@^1.6.1 paulstoffregen/DS1307RTC@0.0.0-alpha+sha.c2590c0033 -monitor_speed = 115200 \ No newline at end of file + knolleary/pubsubclient@^2.8.0 + +monitor_speed = 115200