MQTT Communication established
This commit is contained in:
8
mqttclient/Dockerfile
Normal file
8
mqttclient/Dockerfile
Normal file
@@ -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.
|
41
mqttclient/fanATserial2mqtt.py
Normal file
41
mqttclient/fanATserial2mqtt.py
Normal file
@@ -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()
|
Reference in New Issue
Block a user