From 716c42fc8dab1f78032939ca69b037523016c4ab Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 26 Aug 2023 01:15:39 +0700 Subject: [PATCH] finish iot example --- MQTT-IoTController/platformio.ini | 6 ++- MQTT-IoTController/src/main.cpp | 88 +++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/MQTT-IoTController/platformio.ini b/MQTT-IoTController/platformio.ini index 46857e8..03bad5f 100644 --- a/MQTT-IoTController/platformio.ini +++ b/MQTT-IoTController/platformio.ini @@ -12,4 +12,8 @@ platform = espressif32 board = wt32-eth01 framework = arduino -lib_deps = siwats/espmegapror3 \ No newline at end of file +lib_deps = siwats/espmegapror3 + knolleary/PubSubClient@^2.8 + ivanseidel/ArduinoThread@^2.1.1 + arduino-libraries/Arduino_BuiltIn@^1.0.0 + dersimn/PubSubClientTools@^0.6 \ No newline at end of file diff --git a/MQTT-IoTController/src/main.cpp b/MQTT-IoTController/src/main.cpp index 2a1ec63..e821236 100644 --- a/MQTT-IoTController/src/main.cpp +++ b/MQTT-IoTController/src/main.cpp @@ -1,4 +1,24 @@ +// #define USE_INTERRUPT #include +#include +#include +#include +#include +#include + +// Network Connectivity +#define HOSTNAME "espmega-pro-r3" +const IPAddress IP(192, 168, 0, 210); +const IPAddress SUBNET(255, 255, 255, 0); +const IPAddress GATEWAY(192, 168, 0, 1); +const IPAddress DNS(10, 192, 1, 1); +const IPAddress MQTT_SERVER(192, 168, 0, 26); +const int MQTT_PORT = 1883; +// #define MQTT_USE_AUTH +#ifdef MQTT_USE_AUTH +const char MQTT_USERNAME[] = "username"; +const char MQTT_PASSWORD[] = "password"; +#endif // Inputs const int DEBOUNCE_TIME_MS = 50; @@ -14,19 +34,87 @@ int pwm_values[16]; // Forward declaration void virtual_interrupt_loop(); void virtual_interrupt_callback(int pin, int state); +void network_begin(); +void mqtt_connect(); +void mqtt_subscribe(); +void thread_initialization(); +void mqtt_callback(String topic, String message); + +WiFiClient eth; +PubSubClient mqtt_client(MQTT_SERVER, 1883, eth); +PubSubClientTools mqtt(mqtt_client); + +Thread mqtt_reconnector = Thread(); +StaticThreadController<1> thread_controller(&mqtt_reconnector); void setup() { ESPMega_begin(); + Serial.begin(115200); + Serial.println("ESPMega R3 Initializing . . ."); + Serial.println("Initializing Network . . ."); + network_begin(); + Serial.println("Initializing MQTT . . ."); + mqtt_connect(); + Serial.println("Initializing Threads . . ."); + thread_initialization(); } void loop() { + virtual_interrupt_loop(); ESPMega_loop(); } +void network_begin() +{ + ETH.setHostname(HOSTNAME); + ETH.config(IP, GATEWAY, SUBNET, DNS); + ETH.begin(); +} + +void mqtt_connect() +{ + if (!mqtt_client.connected()) + { + Serial.print("MQTT not connected, connecting . . .\n"); +#ifdef MQTT_USE_AUTH + mqtt_client.connect(HOSTNAME, MQTT_USERNAME, MQTT_PASSWORD); +#else + mqtt_client.connect(HOSTNAME); +#endif + if (mqtt_client.connected()) + { + mqtt_subscribe(); + Serial.print("MQTT connected\n"); + } + else + { + Serial.print("MQTT not connected, continuing in standalone mode\n"); + } + } +} + +void mqtt_subscribe() +{ + mqtt.subscribe("/espmega", mqtt_callback); +} + +void thread_initialization() +{ + Serial.print("Initializing MQTT Thread\n"); + mqtt_reconnector.onRun(mqtt_connect); + mqtt_reconnector.setInterval(30000); +} + +void mqtt_callback(String topic, String message) +{ + +} + void virtual_interrupt_callback(int pin, int state) { + } void virtual_interrupt_loop()