From a02ed0fe086310136b1d7b1d382d62e886190771 Mon Sep 17 00:00:00 2001 From: yaba-source <12479yp@gmx.de> Date: Fri, 9 May 2025 09:08:43 +0000 Subject: [PATCH] =?UTF-8?q?Connect=20mit=20Ip=20=C3=BCbertragung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aaaSonstiges/micropython/main.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/aaaSonstiges/micropython/main.py b/aaaSonstiges/micropython/main.py index eef60f2..5e6f3e8 100644 --- a/aaaSonstiges/micropython/main.py +++ b/aaaSonstiges/micropython/main.py @@ -25,14 +25,20 @@ vorbereitung = 0 kaffee_fertig = 0 def mqtt_callback(topic, msg): - print('Empfangen:', topic, msg) + print('-------------------------') + print('MQTT Nachricht empfangen:') + print(f'Topic: {topic.decode()}') + print(f'Payload: {msg.decode()}') + print('-------------------------') try: command = json.loads(msg.decode()) if topic == MQTT_TOPIC_COMMAND: if 'einschalten' in command: einschalten(command['einschalten']) + print(f"Kommando 'einschalten' mit Wert {command['einschalten']} ausgeführt") if 'starten' in command: starten(command['starten']) + print(f"Kommando 'starten' mit Wert {command['starten']} ausgeführt") except Exception as e: print('Fehler bei Kommando-Verarbeitung:', e) @@ -44,6 +50,18 @@ def connect_mqtt(): client.subscribe(MQTT_TOPIC_COMMAND) return client +def send_online_status(): + try: + ip = network.WLAN(network.STA_IF).ifconfig() + payload = json.dumps({"ip": ip}) + headers = {'Content-Type': 'application/json'} + response = urequests.post(SERVER_URL, data=payload, headers=headers) + print("Antwort vom Server:", response.text) + response.close() + except Exception as e: + print("Fehler beim Senden:", e) + + # MQTT-Verbindung herstellen try: client = connect_mqtt()