-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt-receive-voice-command.py
91 lines (69 loc) · 2.5 KB
/
mqtt-receive-voice-command.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import glob
import logging
import time
import requests
import paho.mqtt.client as mqtt
# log to a file
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s',
filename='/tmp/mqtt-receive-voice-command.log',
filemode='w')
# # log to std output
# logging.basicConfig(level=logging.DEBUG,
# format='%(asctime)s %(levelname)s %(message)s')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
topic_prefix = "xx@qq.com"
command_topic = topic_prefix + "/command"
status_topic = topic_prefix + "/status"
URL = "https://free.currencyconverterapi.com/api/v5/convert"
def read_temperature_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temperature():
lines = read_temperature_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temperature_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos + 2:]
temp_c = float(temp_string) / 1000.0
return temp_c
# This is the Subscriber
def on_connect(client, userdata, flags, rc):
logging.debug("Connected with result code " + str(rc))
client.subscribe(command_topic)
logging.debug("subscribed to " + command_topic)
def on_message(client, userdata, msg):
text = msg.payload.decode().lower()
logging.debug("received text: " + text)
logging.debug("recognized command: ")
if "temperature" in text and "room" in text:
logging.debug("temperature")
temp = read_temperature()
logging.debug(temp)
client.publish(status_topic, temp)
# client.disconnect()
elif "euro" in text and ("cny" in text or "renminbi" in text):
logging.debug("convert EUR to CNY")
# sending get request and saving the response as response object
params = {"compact": "y", "q": "EUR_CNY"}
response = requests.get(url=URL, params=params)
# extracting data in json format
data = response.json()
logging.debug(data)
result = data['EUR_CNY']['val']
logging.debug(result)
client.publish(status_topic, result)
else:
logging.debug("unknown command")
client = mqtt.Client()
client.username_pw_set("xxx", "yyy")
client.connect("zzz.net", 1883, 60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()