wuerfelbaum/mqttwuerfel.py
2020-11-24 14:58:35 +01:00

55 lines
No EOL
1.7 KiB
Python

import paho.mqtt.client as mqtt
import serial
import time
ser = serial.Serial(
port='COM4',
baudrate=115200
#parity=serial.PARITY_ODD,
#stopbits=serial.STOPBITS_TWO,
#bytesize=serial.SEVENBITS
)
ser.isOpen()
FRAMERATE=20
fadetime=1000/FRAMERATE /10 *4 #fadetime in centiseconds SMOOOOTH
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("wuerfel/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
#print(msg.topic+" "+str(msg.payload))
stopic=msg.topic.split('/')
if (stopic[2]=="set"):
color=(msg.payload).decode()
writestring='B,' + stopic[1] +','+str(int(fadetime))+','+color+'\r\n'
#writestring=writestring
print(writestring)
ser.write(writestring.encode())
ser.flush()
time.sleep(0.01)
#ser.reset_input_buffer()
#sPort.write("B,"+str(boxiTree[x][y])+","+str(fadetime)+","+int(red(colorTree[x][y]))+","+int(green(colorTree[x][y]))+","+int(blue(colorTree[x][y]))+"\n");
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("nodered.atd.theater.digital", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
ser.close()