From bf6c241c04984738f7d9ae5973ac18783dadce7a Mon Sep 17 00:00:00 2001 From: Daan Meijer Date: Fri, 24 Jan 2025 23:27:10 +0100 Subject: [PATCH] mqtt works --- platformio.ini | 16 ++--------- src/Hanglamp.cpp | 14 ++++------ src/MQTT.cpp | 70 +++++++++++++++++++++++++++++------------------- src/MQTT.h | 4 ++- 4 files changed, 52 insertions(+), 52 deletions(-) diff --git a/platformio.ini b/platformio.ini index 6fd0e2a..bd2d295 100644 --- a/platformio.ini +++ b/platformio.ini @@ -29,6 +29,7 @@ lib_deps = devyte/ESPAsyncDNSServer@^1.0.0 me-no-dev/ESPAsyncUDP makuna/NeoPixelBus @ ^2.8.0 + knolleary/PubSubClient@^2.8 lib_ldf_mode = chain+ monitor_filters = default @@ -37,23 +38,10 @@ board_build.filesystem = littlefs board_build.f_cpu = 80000000L [env:esp32c3_supermini] -; platform = espressif32 -; board = esp32-c3-devkitm-1 - -; platform = https://github.com/Jason2866/platform-espressif32.git#Arduino/IDF5 -; board = lolin_c3_mini - board = dfrobot_beetle_esp32c3 platform = espressif32 framework = arduino -; board_build.f_cpu = 80000000L monitor_speed = 460800 lib_deps = fastled/FastLED@^3.7.0 - -; [env:dfrobot_beetle_esp32c3] -; platform = espressif32 -; board = dfrobot_beetle_esp32c3 -; framework = arduino -; lib_deps = -; fastled/FastLED@^3.7.0 \ No newline at end of file + knolleary/PubSubClient@^2.8 diff --git a/src/Hanglamp.cpp b/src/Hanglamp.cpp index 625bdb4..96b3b4b 100644 --- a/src/Hanglamp.cpp +++ b/src/Hanglamp.cpp @@ -8,7 +8,7 @@ #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass #define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only -// #define ENABLE_MQTT +#define ENABLE_MQTT #define ENABLE_WIFI // #define ENABLE_FS // #define ENABLE_PINFINDER @@ -146,6 +146,9 @@ void newBrightness(){ delay(100); FastLED.setBrightness(newBrightness); FastLED.show(); + #ifdef ENABLE_MQTT + MQTT_publish("hanglamp/brightness", String(newBrightness)); + #endif } void controlsCallback(ControlEvent event){ @@ -265,10 +268,6 @@ void setup() { void loop() { - #ifdef ENABLE_MQTT - MQTT_loop(); - #endif - #ifdef ENABLE_OTA OTA_loop(); #endif @@ -302,10 +301,7 @@ void loop() { #endif delay(1000); - - // Led_loop(); - - + String command; while(Serial.available()) { diff --git a/src/MQTT.cpp b/src/MQTT.cpp index 9ec5902..864f735 100644 --- a/src/MQTT.cpp +++ b/src/MQTT.cpp @@ -1,13 +1,9 @@ -#ifdef ENABLE_MQTT + #include "MQTT.h" #include -#include -#include - - WiFiClient espClient; PubSubClient client(espClient); @@ -17,12 +13,23 @@ std::vector * channels = new std::vector() char mqtt_server[40] = "manus"; char mqtt_port[6] = "1883"; + +uint32_t chipId = 0; + void connect() { + - Serial.print("Attempting MQTT connection..."); + if (WiFi.status() != WL_CONNECTED){ + Serial.println("Wifi not connected"); + return; + } + + Serial.println("Attempting MQTT connection..."); // Create a random client ID - String clientId = "Konijntje-"; - clientId += String(ESP.getChipId()); + String clientId = "Hanglamp-"; + clientId += String(chipId, 16); + Serial.printf("MQTT: Connecting as %s\n", clientId.c_str()); + // Attempt to connect if (client.connect(clientId.c_str())) { Serial.println("connected"); @@ -81,7 +88,11 @@ void MQTT_callback(char* topic, uint8_t * payload, unsigned int length){ void MQTT_setup(){ - Serial.println("MQTT_init"); + for (int i = 0; i < 17; i = i + 8) { + chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i; + } + + Serial.printf("MQTT_init: %06x\n", chipId); client.setServer(mqtt_server, String(mqtt_port).toInt()); client.setCallback(MQTT_callback); @@ -91,29 +102,32 @@ void MQTT_setup(){ // sprintf(buff, "%08x", _pCallback); // Serial.println(buff); - connect(); + TaskHandle_t taskMqtt = NULL; + xTaskCreate(MQTT_loop, "Wifi_loop", 10000, NULL, tskIDLE_PRIORITY, &taskMqtt); } -void MQTT_loop(){ +void MQTT_loop(void* params){ - #if DEBUG_GENERAL - Serial.println("MQTT_loop()"); - #endif - int reconnectCounter = 0; - while(!client.connected() && reconnectCounter < 3) { - Serial.println("MQTT: connecting..."); - reconnectCounter++; - connect(); + while(true){ + #if DEBUG_GENERAL + Serial.println("MQTT_loop()"); + #endif + int reconnectCounter = 0; + while(!client.connected() && reconnectCounter < 3) { + Serial.println("MQTT: connecting..."); + reconnectCounter++; + connect(); - if(client.connected()){ - Serial.println("MQTT: succes!"); - break; - }else{ - Serial.println("MQTT: connection failed, try again in 5 seconds"); - // Wait 5 seconds before retrying - delay(5000); + if(client.connected()){ + Serial.println("MQTT: succes!"); + break; + }else{ + Serial.println("MQTT: connection failed, try again in 5 seconds"); + // Wait 5 seconds before retrying + vTaskDelay(5000/portTICK_PERIOD_MS); + } } + client.loop(); + vTaskDelay(100/portTICK_PERIOD_MS); } - client.loop(); } -#endif \ No newline at end of file diff --git a/src/MQTT.h b/src/MQTT.h index 5ae11af..1c3c740 100644 --- a/src/MQTT.h +++ b/src/MQTT.h @@ -3,6 +3,8 @@ #include "Arduino.h" +#include + using namespace std; @@ -17,5 +19,5 @@ struct SubscribedChannel { void MQTT_publish(const char * topic, String str); void MQTT_publish(const char * topic, const char * msg); -void MQTT_loop(); +void MQTT_loop(void* params); void MQTT_setup();