234 lines
7.2 KiB
Python
234 lines
7.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
#import socket
|
|
import sys
|
|
import time
|
|
import serial
|
|
import threading
|
|
import re
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
#hote = "192.168.67.101"
|
|
hote = "127.0.0.1"
|
|
port = 3020
|
|
|
|
lux=0
|
|
|
|
dimstate=False
|
|
|
|
arduino = serial.Serial('/dev/ttyUSB0')
|
|
|
|
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
#sock.connect((hote, port))
|
|
#print("Connection on {}".format(port))
|
|
|
|
#arduino.write("d 0\n".encode())
|
|
|
|
def stop_chacon():
|
|
print("Switch Off")
|
|
arduino.write("s0\n".encode())
|
|
#os.system('codesend 1381716')
|
|
client.publish("home-assistant/switch/chacon/state", "OFF")
|
|
|
|
def start_chacon():
|
|
print("Switch On")
|
|
arduino.write("s1\n".encode())
|
|
#os.system('codesend 1381717')
|
|
client.publish("home-assistant/switch/chacon/state", "ON")
|
|
|
|
def stop_alarm():
|
|
for i in range(1,8):
|
|
arduino.write("f650\n".encode())
|
|
time.sleep(10)
|
|
|
|
def start_dim():
|
|
global lux
|
|
global dimstate
|
|
if dimstate==False:
|
|
dimstate=True
|
|
luxorig=lux
|
|
print("Dim On")
|
|
arduino.write("d1\n".encode())
|
|
time.sleep(2)
|
|
i=0
|
|
while i<10 and lux<=luxorig:
|
|
print("Dim On")
|
|
arduino.write("d1\n".encode())
|
|
time.sleep(2)
|
|
i=i+1
|
|
#os.system('codesend 1394005')
|
|
|
|
def start_dim2():
|
|
print("Dim2 On")
|
|
arduino.write("d1\n".encode())
|
|
|
|
def stop_dim():
|
|
global lux
|
|
global dimstate
|
|
if dimstate==True:
|
|
dimstate=False
|
|
luxorig=lux
|
|
print("Dim Off")
|
|
arduino.write("d0\n".encode())
|
|
time.sleep(2)
|
|
i=0
|
|
while i<10 and lux>=luxorig:
|
|
print("Dim Off")
|
|
arduino.write("d0\n".encode())
|
|
time.sleep(2)
|
|
i=i+1
|
|
#os.system('codesend 1394004')
|
|
|
|
def stop_dim2():
|
|
print("Dim2 Off")
|
|
arduino.write("d0\n".encode())
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
print("Connected with result code "+str(rc))
|
|
|
|
client.subscribe("home-assistant/salon/lumiereold/manual")
|
|
client.subscribe("home-assistant/salon/lumiereold/set")
|
|
client.subscribe("home-assistant/switch/chacon/set")
|
|
client.subscribe("home-assistant/switch/chacon/timer")
|
|
client.subscribe("home-assistant/switch/chacon/cancel")
|
|
client.subscribe("home-assistant/alarm/set")
|
|
client.subscribe("home-assistant/alarm/cancel")
|
|
client.subscribe("arduino/display/intensity")
|
|
client.subscribe("arduino/display/settext")
|
|
client.subscribe("arduino/display/disableptext")
|
|
client.subscribe("arduino/display/setptext")
|
|
client.subscribe("arduino/display/set")
|
|
client.subscribe("arduino/buzzer/melody")
|
|
client.subscribe("arduino/buzzer/melody2")
|
|
client.subscribe("arduino/buzzer/frequency")
|
|
client.subscribe("arduino/buzzer/callring")
|
|
|
|
def on_message(client, userdata, msg):
|
|
if msg.topic == 'home-assistant/salon/lumiereold/manual' and msg.payload == b'ON':
|
|
start_dim2()
|
|
elif msg.topic == 'home-assistant/salon/lumiereold/set' and msg.payload == b'ON':
|
|
client.publish("home-assistant/salon/lumiereold/state", "ON")
|
|
client.loop(timeout=1.0)
|
|
start_dim2()
|
|
elif msg.topic == 'home-assistant/salon/lumiereold/manual' and msg.payload == b'OFF':
|
|
client.publish("home-assistant/salon/lumiereold/state", "OFF")
|
|
client.loop(timeout=1.0)
|
|
stop_dim2()
|
|
elif msg.topic == 'home-assistant/salon/lumiereold/set' and msg.payload == b'OFF':
|
|
client.publish("home-assistant/salon/lumiereold/state", "OFF")
|
|
client.loop(timeout=1.0)
|
|
stop_dim2()
|
|
elif msg.topic == 'home-assistant/switch/chacon/set' and msg.payload == b'ON':
|
|
start_chacon()
|
|
client.publish("home-assistant/switch/chacon/state", "ON")
|
|
elif msg.topic == 'home-assistant/switch/chacon/set' and msg.payload == b'OFF':
|
|
stop_chacon()
|
|
elif msg.topic == 'home-assistant/switch/chacon/timer':
|
|
start_chacon()
|
|
timerchacon=threading.Timer(int(msg.payload)*60, stop_chacon)
|
|
timerchacon.start()
|
|
elif msg.topic == 'home-assistant/switch/chacon/cancel':
|
|
timerchacon.cancel()
|
|
stop_chacon()
|
|
elif msg.topic == 'home-assistant/alarm/set':
|
|
timeralarm=threading.Timer(int(msg.payload)*60, stop_alarm)
|
|
timeralarm.start()
|
|
elif msg.topic == 'home-assistant/alarm/cancel':
|
|
timeralarm.cancel()
|
|
elif msg.topic == 'arduino/display/intensity':
|
|
msgtosend="i"+msg.payload.decode()+"\n"
|
|
arduino.write(msgtosend.encode())
|
|
elif msg.topic == 'arduino/display/set':
|
|
if msg.payload == b'ON':
|
|
msgtosend="m1\n"
|
|
elif msg.payload == b'OFF':
|
|
msgtosend="m0\n"
|
|
client.publish("arduino/display/state", msg.payload.decode(), 0, True)
|
|
arduino.write(msgtosend.encode())
|
|
elif msg.topic == 'arduino/display/settext':
|
|
msgtosend="t"+msg.payload.decode()+"\n"
|
|
arduino.write(msgtosend.encode())
|
|
elif msg.topic == 'arduino/display/setptext':
|
|
msgtosend="p"+msg.payload.decode()+"\n"
|
|
arduino.write(msgtosend.encode())
|
|
elif msg.topic == 'arduino/display/disableptext':
|
|
arduino.write("p\n".encode())
|
|
elif msg.topic == 'arduino/buzzer/melody':
|
|
if msg.payload.decode()=="1":
|
|
arduino.write("f450\n".encode())
|
|
time.sleep(0.3)
|
|
arduino.write("f450\n".encode())
|
|
elif msg.payload.decode()=="2":
|
|
arduino.write("f450\n".encode())
|
|
time.sleep(0.3)
|
|
arduino.write("f550\n".encode())
|
|
elif msg.payload.decode()=="3":
|
|
arduino.write("f450\n".encode())
|
|
time.sleep(0.3)
|
|
# msgtosend="b"+msg.payload.decode()+"\n"
|
|
# arduino.write(msgtosend.encode())
|
|
arduino.write("f350\n".encode())
|
|
elif msg.topic == 'arduino/buzzer/melody2':
|
|
msgtosend="c"+msg.payload.decode()+"\n"
|
|
arduino.write(msgtosend.encode())
|
|
elif msg.topic == 'arduino/buzzer/frequency':
|
|
msgtosend="f"+msg.payload.decode()+"\n"
|
|
arduino.write(msgtosend.encode())
|
|
elif msg.topic == 'arduino/buzzer/callring' and msg.payload == b'ON':
|
|
arduino.write("b4\n".encode())
|
|
elif msg.topic == 'arduino/buzzer/callring' and msg.payload == b'OFF':
|
|
arduino.write("b5\n".encode())
|
|
|
|
client = mqtt.Client()
|
|
client.on_connect = on_connect
|
|
client.on_message = on_message
|
|
|
|
client.connect("127.0.0.1", 1883, 60)
|
|
client.loop_start()
|
|
|
|
def readlines(arduino, recv_buffer=4096, delim='\n'):
|
|
buffer = ''
|
|
data = True
|
|
while data:
|
|
data = arduino.read(recv_buffer)
|
|
buffer += data.decode()
|
|
|
|
while buffer.find(delim) != -1:
|
|
line, buffer = buffer.split('\n', 1)
|
|
yield line
|
|
return
|
|
|
|
#for line in readlines(arduino):
|
|
while True:
|
|
|
|
line=arduino.readline().decode()
|
|
line2=line.rstrip()
|
|
#line2=line
|
|
#print("Received : "+line2)
|
|
m=re.match(r"^([a-z]) ([0-9.]+)\r?\n?$",line2)
|
|
if m != None:
|
|
cmd=m.group(1)
|
|
val=m.group(2)
|
|
# cmd, val1=line.split(' ',1)
|
|
# val=val1.rstrip()
|
|
curtime=time.time()
|
|
if cmd=='t':
|
|
client.publish("home-assistant/salon/temperature", str(round(float(val),1)), 0, True)
|
|
client.publish("home-assistant/salon/temperature-influx", "dht22 temperature="+str(round(float(val),1))+" "+str(int(curtime))+"000000000", 0, True)
|
|
elif cmd=='h':
|
|
client.publish("home-assistant/salon/humidity", val, 0, True)
|
|
client.publish("home-assistant/salon/humidity-influx", "dht22 humidity="+val+" "+str(int(curtime))+"000000000", 0, True)
|
|
elif cmd=='x':
|
|
if int(val)-lux > 1 and int(val)-lux < -1:
|
|
client.publish("home-assistant/salon/lux", val, 0, True)
|
|
lux=int(val)
|
|
elif cmd=='p':
|
|
if int(val)==0:
|
|
client.publish("home-assistant/salon/pir", "OFF", 0, True)
|
|
elif int(val)==1:
|
|
client.publish("home-assistant/salon/pir", "ON", 0, True)
|
|
|
|
print("Close")
|
|
#sock.close()
|