From 162b4eb5b6ddcbccc3d7b9d6a3f97316bd0b3d14 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sat, 7 Dec 2024 14:25:04 +0100 Subject: [PATCH] MQTT Communication established --- ReadMe.md | 27 +++++++++++++++++++++- mqttclient/Dockerfile | 8 +++++++ mqttclient/fanATserial2mqtt.py | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 mqttclient/Dockerfile create mode 100644 mqttclient/fanATserial2mqtt.py diff --git a/ReadMe.md b/ReadMe.md index 6202e78..0da37bc 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -12,4 +12,29 @@ Interface: USB-UART ## Source Inspired by the following Project: -https://github.com/stefanthoss/esp8266-fan-control \ No newline at end of file +https://github.com/stefanthoss/esp8266-fan-control + +## MQTT Control + +### Linux based Controller +* Docker +* Python3 + * paho + +### Installation + +Open Terminal in **mqttclient**. + +Docker must be installed +```docker build .``` + +#### Execution + +Set the following parameter: +* USB Device +* MQTT + * Server Name / IP Address + * Base Topic + + + ```docker run -e MQTT_SERVER="192.168.x.y" -e MQTT_TOPIC="testtopic/" sha256:``` diff --git a/mqttclient/Dockerfile b/mqttclient/Dockerfile new file mode 100644 index 0000000..409fa74 --- /dev/null +++ b/mqttclient/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.9 +# Add sourcecode files +ADD fanATserial2mqtt.py /root/ +# handle dependencies +RUN pip install --no-cache-dir --upgrade pip && \ + pip install --no-cache-dir paho-mqtt pyserial +CMD ["python", "/root/fanATserial2mqtt.py"] +# Or enter the name of your unique directory and parameter set. \ No newline at end of file diff --git a/mqttclient/fanATserial2mqtt.py b/mqttclient/fanATserial2mqtt.py new file mode 100644 index 0000000..44b808c --- /dev/null +++ b/mqttclient/fanATserial2mqtt.py @@ -0,0 +1,41 @@ +#! /usr/bin/python3 +import time +import sys +from paho.mqtt import client as mqtt_client +import os +mqtt_server=os.environ['MQTT_SERVER'] +mqtt_topic=os.environ['MQTT_TOPIC'] + +# The callback for when a PUBLISH message is received from the server. +def on_message(client, userdata, msg): + print(msg.topic+" "+str(msg.payload)) + + +# Start mqtt +client_id = f'python-fan-ctrl' +client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2) +client.on_message = on_message + +if (not (mqtt_server)): + print("MQTT_SERVER is not set") + +if (not (mqtt_topic)): + print("MQTT_TOPIC is not set") + +client.connect(mqtt_server, 1883) +val=0 + +client.loop_start() + +# Endless Loop +while True: + try: + client.publish(mqtt_topic + "val", val) + + time.sleep(0.2) + val = val + 1 + + except (KeyboardInterrupt, SystemExit): + cleanAndExit() + +client.loop_stop()