From 1163f2e30f09276107f90f2564958dbc1921029d Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 28 Dec 2023 15:18:37 +0700 Subject: [PATCH] mqtt connection implementation --- .../lib/ESPMegaPRO/ESPMegaIoT.cpp | 31 ++++++------------- .../lib/ESPMegaPRO/ESPMegaIoT.hpp | 7 ++--- .../lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp | 6 +--- ...egaTouch.cpp => ESPMegaTouch.cpp.disabled} | 0 ...egaTouch.hpp => ESPMegaTouch.hpp.disabled} | 0 .../lib/ESPMegaPRO/ExpansionCard.hpp | 2 +- Template Project/src/iotdemo.cpp | 25 +++++++++++---- 7 files changed, 33 insertions(+), 38 deletions(-) rename Template Project/lib/ESPMegaPRO/{ESPMegaTouch.cpp => ESPMegaTouch.cpp.disabled} (100%) rename Template Project/lib/ESPMegaPRO/{ESPMegaTouch.hpp => ESPMegaTouch.hpp.disabled} (100%) diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp index c0bf4d0..b09235c 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -1,6 +1,7 @@ #include -ESPMegaIoT::ESPMegaIoT() { +ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient) { + tcpClient.setTimeout(1); // Initialize the components array for (int i = 0; i < 255; i++) { components[i] = NULL; @@ -41,7 +42,6 @@ void ESPMegaIoT::setBaseTopic(char *base_topic) { void ESPMegaIoT::intr_begin(ExpansionCard *cards[]) { this->cards = cards; - ETH.begin(); active = true; } void ESPMegaIoT::loop() { @@ -106,12 +106,6 @@ void ESPMegaIoT::subscribeToTopic(char *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); } @@ -146,19 +140,27 @@ bool ESPMegaIoT::connectToMqtt(char*client_id, char *mqtt_server, uint16_t mqtt_ } bool ESPMegaIoT::connectToMqtt(char* client_id, char *mqtt_server, uint16_t mqtt_port) { // Store mqtt connection parameters + Serial.println("Storing mqtt connection parameters"); this->mqtt_server = mqtt_server; this->mqtt_port = mqtt_port; this->mqtt_useauth = false; this->client_id = client_id; + Serial.println("Setting mqtt server"); mqtt.setServer(mqtt_server, mqtt_port); + Serial.println("Setting mqtt callback"); auto boundCallback = std::bind(&ESPMegaIoT::mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); mqtt.setCallback(boundCallback); + Serial.println("Connecting to mqtt"); if(mqtt.connect(client_id)) { + Serial.println("Connected to mqtt"); + Serial.println("Calling session keep alive"); sessionKeepAlive(); + Serial.println("Subscribing to topics"); mqttSubscribe(); mqtt_connected = true; return true; } + Serial.println("Failed to connect to mqtt"); mqtt_connected = false; return false; @@ -187,19 +189,6 @@ void ESPMegaIoT::publishRelative(uint8_t card_id, char *topic, char *payload) { mqtt.publish(absolute_topic, payload); } -void ESPMegaIoT::setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) { - ETH.config(ip, gateway, subnet, dns1, dns2); -} -void ESPMegaIoT::setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1) { - ETH.config(ip, gateway, subnet, dns1); -} -void ESPMegaIoT::setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet) { - ETH.config(ip, gateway, subnet); -} -IPAddress ESPMegaIoT::getETHIp() { - return ETH.localIP(); -} - bool ESPMegaIoT::mqttReconnect() { if (mqtt_useauth) { return this->connectToMqtt(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_password); diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp index baf7779..c0e33fd 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp @@ -12,6 +12,7 @@ class ESPMegaIoT { public: + ESPMegaIoT(); void intr_begin(ExpansionCard *cards[]); void loop(); void registerCard(uint8_t card_id); @@ -23,8 +24,6 @@ public: void subscribeRelative(char *topic); void subscribeToTopic(char *topic); void unsubscribeFromTopic(char *topic); - void connectToEthernet(); - bool ethernetConnected(); void connectToWifi(char *ssid, char *password); void connectToWifi(char *ssid); void disconnectFromWifi(); @@ -36,12 +35,10 @@ public: void registerMqttCallback(void (*callback)(char *, char *)); void registerRelativeMqttCallback(void (*callback)(char *, char *)); void setBaseTopic(char *base_topic); - void setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2); - void setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1); - void setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet); IPAddress getETHIp(); private: + WiFiClient tcpClient; void sessionKeepAlive(); bool mqttReconnect(); void wifiReconnect(); diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp index 5e4a4c4..fbe0bd2 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp @@ -11,11 +11,7 @@ bool ESPMegaPRO::begin() { return false; } this->installCard(1, &outputs); - if(!fram.begin(FRAM_ADDRESS)) { - Serial.println("Failed to initialize FRAM"); - Serial.println("Is this an ESPMegaPRO device?"); - return false; - } + fram.begin(FRAM_ADDRESS); uint8_t pinMap[16] = {0, 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8}; inputs.loadPinMap(pinMap); return true; diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaTouch.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaTouch.cpp.disabled similarity index 100% rename from Template Project/lib/ESPMegaPRO/ESPMegaTouch.cpp rename to Template Project/lib/ESPMegaPRO/ESPMegaTouch.cpp.disabled diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaTouch.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaTouch.hpp.disabled similarity index 100% rename from Template Project/lib/ESPMegaPRO/ESPMegaTouch.hpp rename to Template Project/lib/ESPMegaPRO/ESPMegaTouch.hpp.disabled diff --git a/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp b/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp index cad282e..9217c42 100644 --- a/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp +++ b/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp @@ -9,5 +9,5 @@ class ExpansionCard { // Preform a loop to refresh the input buffers virtual void loop() {} // Get the card type - virtual uint8_t getType() {} + virtual uint8_t getType() {return 255;} }; \ No newline at end of file diff --git a/Template Project/src/iotdemo.cpp b/Template Project/src/iotdemo.cpp index 7c6851c..1c562c8 100644 --- a/Template Project/src/iotdemo.cpp +++ b/Template Project/src/iotdemo.cpp @@ -1,5 +1,5 @@ #include - +#include // Instantiate ESPMega ESPMegaPRO espmega = ESPMegaPRO(); @@ -11,21 +11,34 @@ void mqtt_callback(char *topic, char *payload) Serial.println(payload); } +IPAddress ip(192, 168, 0, 11); +IPAddress gateway(192, 168, 0, 1); +IPAddress subnet(255, 255, 255, 0); + void setup() { // Initialize ESPMega espmega.begin(); Serial.println("ESPMega initialized"); + Serial.println("Initializing ESPMega IoT"); espmega.enableIotModule(); - espmega.iot.connectToEthernet(); - IPAddress ip(192, 168, 0, 11); - IPAddress gateway(192, 168, 0, 1); - IPAddress subnet(255, 255, 255, 0); - espmega.iot.setETHStaticIp(ip, gateway, subnet); + Serial.println("ESPMega IoT module enabled"); + Serial.println("Setting static IP"); + Serial.println("Connecting to Ethernet"); + ETH.begin(); + delay(1000); + ETH.config(ip, gateway, subnet); + Serial.println("Static IP set"); + Serial.println("Begin MQTT Initialization Routine"); + Serial.println("Setting MQTT Base Topic"); espmega.iot.setBaseTopic("/testmegaoop"); + Serial.println("Initializing MQTT"); espmega.iot.connectToMqtt("espmega", "192.168.0.26", 1883); + Serial.println("Publishing a test message"); espmega.iot.publishRelative("test", "test"); + Serial.println("Subscribing to test topic"); espmega.iot.subscribeRelative("test"); + Serial.println("Registering MQTT Callback"); espmega.iot.registerMqttCallback(mqtt_callback); Serial.println("ESPMega IoT initialized"); }