add mqtt bridge

This commit is contained in:
Philipp Kramer 2020-11-24 14:58:35 +01:00
parent 0a931362ca
commit 050d6b854e
3 changed files with 133 additions and 1 deletions

55
mqttwuerfel.py Normal file
View file

@ -0,0 +1,55 @@
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()

65
test/test.pde Normal file
View file

@ -0,0 +1,65 @@
import processing.serial.*;
static int FRAMERATE=20;
int fadetime=1000/FRAMERATE /10 *4; //fadetime in centiseconds SMOOOOTH
Serial sPort;
//int boxnum = 1+2+3+4+5+6+7;
int boxnum=50;
color box[] = new color[boxnum];
int boxid[] = new int[boxnum];
float testcolorhue=0;
float time=0;
void setup() {
size(200, 200);
frameRate(FRAMERATE);
for (int i=0;i<boxnum;i++) {
boxid[i]=i;
//box[i] = color(100,255,255);
}
String portName = Serial.list()[0];
print(portName);
sPort = new Serial(this, portName, 115200);
colorMode(HSB, 255);
}
void draw() {
delay(200);
/*
for (int i=0;i<boxnum;i++) {
boxid[i]=i;
box[i] = color(100,255,255);
}*/
testcolorhue+=4;
testcolorhue%=256;
for (int i=0;i<boxnum;i++) {
box[i] = color(int(testcolorhue),255,255);
}
sendToWuerfels();
}
void sendToWuerfels() {
for (int i=0;i<boxnum;i++) {
//print(str(i)+"="+str(brightness(box[i]))+", ");
sPort.write("B,"+str(boxid[i])+","+str(fadetime)+","+int(red(box[i]))+","+int(green(box[i]))+","+int(blue(box[i]))+"\n");
print("B,"+str(boxid[i])+","+str(fadetime)+","+int(red(box[i]))+","+int(green(box[i]))+","+int(blue(box[i]))+"\n");
}
println();
}

View file

@ -67,6 +67,8 @@ void draw() {
//effect_rainbow();
effect_tree();
//effect_black();
sendToTree();
}
@ -78,6 +80,16 @@ void effect_rainbow() {
}
}
void effect_black() {
colorMode(RGB, 255);
for (int y=0;y<MAXTREEHEIGHT;y++) {
for (int x=0;x<MAXTREEWIDTH;x++) {
colorTree[x][y] = color(0,0,0); //base color green
}
}
}
void effect_tree() {
time+=0.1;
float candleflickerscale=80;
@ -85,7 +97,7 @@ void effect_tree() {
for (int y=0;y<MAXTREEHEIGHT;y++) {
for (int x=0;x<MAXTREEWIDTH;x++) {
colorTree[x][y] = color(15,200,30); //base color green
colorTree[x][y] = color(15,170,20); //base color green
}
}