-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqserver.py
executable file
·66 lines (58 loc) · 2.25 KB
/
mqserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import paho.mqtt.client as mqtt
from random import randint
import json
from vesync import VesyncApi
from mqclient import VesyncMQ
import logging
import re
#Passwords stored in seperate file
from secrets import *
logging.basicConfig(
filename='vesync.log',
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG)
logging.info("Starting Application")
def on_connect(client, userdata, flags,
rc): # The callback for when the client connects to the broker
logging.info("Connected with result code {0}".format(
str(rc))) # Print result of connection attempt
client.subscribe("home/humidifier/command/#")
def on_message(
client, userdata, msg
): # The callback for when a PUBLISH message is received from the server.
# Trigger the on or off
found = 'skip'
logging.info("Message received-> " + msg.topic + " " +
str(msg.payload)) # Print a received msg
#payload = msg.payload
payload = msg.payload.decode("utf-8")
logging.info(msg.topic + " " + str(payload))
try:
found = re.search('home/humidifier/command/(.*)', msg.topic).group(1)
logging.info(found)
except AttributeError:
pass
if 'on' in payload:
logging.info("Turning ON")
api = VesyncApi(USERNAME, PASSWORD)
api.turn_on(found)
elif 'off' in payload:
logging.info("Turning OFF")
api = VesyncApi(USERNAME, PASSWORD)
api.turn_off(found)
elif int(payload) > 40 and int (payload) < 80:
logging.info("Adjusting Humidity" + str(payload))
api = VesyncApi(USERNAME, PASSWORD)
api.set_target(found, int(payload))
client = VesyncMQ()
client.main()
client = mqtt.Client("Humidifier_%s" % randint(
1000, 9999)) # Create instance of client with client ID “digi_mqtt_test”
client.on_connect = on_connect # Define callback function for successful connection
client.on_message = on_message # Define callback function for receipt of a message
# client.connect("m2m.eclipse.org", 1883, 60) # Connect to (broker, port, keepalive-time)
client.username_pw_set('jddayley', 'java')
client.connect('192.168.0.116', 1883)
client.loop_forever() # Start networking daemon