From 2ed7394d3a8bf81ffdbbd65b3e8129b31723a7bd Mon Sep 17 00:00:00 2001 From: baschno Date: Thu, 19 Jun 2025 10:43:20 +0200 Subject: [PATCH] mqtt working state --- platformio.ini | 3 +- src/main.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 5 deletions(-) diff --git a/platformio.ini b/platformio.ini index 45669d9..c04e582 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,4 +12,5 @@ platform = espressif8266 board = nodemcuv2 framework = arduino -monitor_speed = 115200 \ No newline at end of file +monitor_speed = 115200 +lib_deps = knolleary/PubSubClient@^2.8 diff --git a/src/main.cpp b/src/main.cpp index 7fdc6e5..e347f59 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,27 +1,120 @@ #include +#include +#include #define LED 2 -// put function declarations here: +#ifndef STASSID +#define STASSID "CurrywurstPommesMajo" +#define STAPSK "1976071219770620" +#endif + +WiFiClient espClient; +PubSubClient client(espClient); + int myFunction(int, int); +void callback(char* topic, byte* payload, unsigned int length); +void wifiSetup(); +void mqttSetup(); + +void initiateAction(int); + +void wifiSetup() { + + WiFi.mode(WIFI_STA); + WiFi.begin(STASSID, STAPSK); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.print("Connected! IP address: "); + Serial.println(WiFi.localIP()); +}; + + +void mqttReconnect() { + while (!client.connected()) { + Serial.println("Connecting to MQTT..."); + + if (client.connect("ESP8266Client", "mosqhd", "ieN2K7YtGgkK" )) { + + Serial.println("connected"); + + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } + client.publish("esp32/test", "Hello from ESP8266"); + client.subscribe("esp32/output"); +} + + +void initiateAction(int act) { + + Serial.print("Action "); + Serial.println(act); + +} + +void callback(char* topic, byte* message, unsigned int length) { + Serial.print("Message arrived on topic: "); + Serial.print(topic); + Serial.print(". Message: "); + String messageTemp; + + for (int i = 0; i < length; i++) { + Serial.print((char)message[i]); + messageTemp += (char)message[i]; + } + Serial.println(); + + // If a message is received on the topic esp32/output, you check if the message is either "on" or "off". + // Changes the output state according to the message + if (String(topic) == "esp32/output") { + Serial.print("Changing output to "); + if(messageTemp == "on"){ + Serial.println("on"); + //digitalWrite(ledPin, HIGH); + } + else if(messageTemp == "off"){ + Serial.println("off"); + //digitalWrite(ledPin, LOW); + } + } +} + void setup() { Serial.begin(115200); pinMode(LED, OUTPUT); + wifiSetup(); + client.setServer("muckibude.fritz.box", 1883); + client.setCallback(callback); } void loop() { - // put your main code here, to run repeatedly: + + if (!client.connected()) { + mqttReconnect(); + } + client.loop(); + myFunction(1,2); } // put function definitions here: int myFunction(int x, int y) { digitalWrite(LED, HIGH); - Serial.println("LED is on"); + // Serial.println("LED is on"); delay(1000); digitalWrite(LED, LOW); - Serial.println("LED is off"); + // Serial.println("LED is off"); delay(1000); return 1; } \ No newline at end of file