From 822eb662858880307013c4990db97379e16ecdee Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 28 Dec 2023 03:09:41 +0700 Subject: [PATCH] IoT Structure --- Template Project/lib/ESPMegaPRO/AnalogIoT.hpp | 17 +++++++++ .../lib/ESPMegaPRO/DigitalInputIoT.hpp | 15 ++++++++ .../lib/ESPMegaPRO/ESPMegaIoT.cpp | 0 .../lib/ESPMegaPRO/ESPMegaIoT.hpp | 36 +++++++++++++++++++ .../lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp | 4 +-- .../lib/ESPMegaPRO/ESPMegaTouch.cpp | 1 + .../lib/ESPMegaPRO/ESPMegaTouch.hpp | 1 + .../lib/ESPMegaPRO/IoTComponent.hpp | 0 8 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 Template Project/lib/ESPMegaPRO/AnalogIoT.hpp create mode 100644 Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp create mode 100644 Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp create mode 100644 Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp create mode 100644 Template Project/lib/ESPMegaPRO/ESPMegaTouch.cpp create mode 100644 Template Project/lib/ESPMegaPRO/ESPMegaTouch.hpp create mode 100644 Template Project/lib/ESPMegaPRO/IoTComponent.hpp diff --git a/Template Project/lib/ESPMegaPRO/AnalogIoT.hpp b/Template Project/lib/ESPMegaPRO/AnalogIoT.hpp new file mode 100644 index 0000000..bd7ae09 --- /dev/null +++ b/Template Project/lib/ESPMegaPRO/AnalogIoT.hpp @@ -0,0 +1,17 @@ +#pragma once +#include +class AnalogIoT { + public: + bool begin(AnalogCard *card); + void handleMqttMessage(char *topic, char *payload); + void publishADCs(); + void setADCsPublishInterval(uint32_t interval); + void setADCsPublishEnabled(bool enabled); + private: + char *adc_topic; + char *dac_topic; + uint32_t adc_publish_interval = 1000; + uint32_t last_adc_publish = 0; + bool adc_publish_enabled = false; + AnalogCard *card; +}; \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp new file mode 100644 index 0000000..46ee16d --- /dev/null +++ b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp @@ -0,0 +1,15 @@ + +#include +class DigitalInputIoT { + public: + bool begin(DigitalInputCard *card); + void handleMqttMessage(char *topic, char *payload); + void publishDigitalInputs(); + void setDigitalInputsPublishEnabled(bool enabled); + void handleValueChange(uint8_t pin, uint8_t value); + void registerValueChangeCallback(void (*callback)(uint8_t, uint8_t)); + private: + char *digital_inputs_topic; + bool digital_inputs_publish_enabled = false; + DigitalInputCard *card; +}; \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp new file mode 100644 index 0000000..3a1c51c --- /dev/null +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include + +class ESPMegaIoT +{ + public: + void begin(); + void loop(); + void registerCard(uint8_t card_id); + void deregisterCard(uint8_t card_id); + void publishCard(uint8_t card_id); + void subscribeToTopic(char *topic); + void unsubscribeFromTopic(char *topic); + void connectToEthernet(); + bool ethernetConnected(); + void connectToWifi(char *ssid, char *password); + void connectToWifi(char *ssid); + void disconnectFromWifi(); + bool wifiConnected(); + void connectToMqtt(char *mqtt_server, uint16_t mqtt_port, char *mqtt_user, char *mqtt_password); + void disconnectFromMqtt(); + void publishToTopic(char *topic, char *payload); + void registerMqttCallback(void (*callback)(char *, char *)); + + private: + void sessionKeepAlive(); + void mqttReconnect(); + void wifiReconnect(); + PubSubClient mqtt; + ExpansionCard *expansionCards[255]; + bool card_publish_enabled[255]; +} \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp index 724f0dc..f800c1d 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp @@ -5,12 +5,12 @@ ESPMegaPRO::ESPMegaPRO() { bool ESPMegaPRO::begin() { Wire.begin(14, 33); Serial.begin(115200); - if(!inputs.begin()) { + if(!this->installCard(0, &inputs)) { Serial.println("Failed to initialize inputs"); Serial.println("Is this an ESPMegaPRO device?"); return false; } - outputs.begin(); + this->installCard(1, &outputs); if(!fram.begin(FRAM_ADDRESS)) { Serial.println("Failed to initialize FRAM"); Serial.println("Is this an ESPMegaPRO device?"); diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaTouch.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaTouch.cpp new file mode 100644 index 0000000..d8a80b2 --- /dev/null +++ b/Template Project/lib/ESPMegaPRO/ESPMegaTouch.cpp @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaTouch.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaTouch.hpp new file mode 100644 index 0000000..78a19ea --- /dev/null +++ b/Template Project/lib/ESPMegaPRO/ESPMegaTouch.hpp @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/IoTComponent.hpp b/Template Project/lib/ESPMegaPRO/IoTComponent.hpp new file mode 100644 index 0000000..e69de29