forked from nickoala/sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindoor.py
93 lines (72 loc) · 2.17 KB
/
indoor.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
92
93
# coding=utf8
import sys
import time
import traceback
import telepot
from telepot.loop import MessageLoop
from sensor.DS18B20 import DS18B20
from sensor.SHT20 import SHT20
"""
$ python3 indoor.py <token> <user_id>
An indoor climate monitor with 2 sensors.
It also comes with a Telegram bot that can report data periodically.
To know more about Telegram Bot and telepot, go to:
https://github.com/nickoala/telepot
"""
ds = DS18B20('28-00000736781c')
sht = SHT20(1, 0x40)
def read_all():
return ds.temperature(), sht.humidity()
# Read sensors and send to user
def read_send(chat_id):
t, h = read_all()
msg = '{:.1f}°C {:.1f}%'.format(t.C, h.RH)
bot.sendMessage(chat_id, msg)
def handle(msg):
global last_report, report_interval
msg_type, chat_type, chat_id = telepot.glance(msg)
# ignore non-text message
if msg_type != 'text':
print('Non-text message. Ignore.')
return
# only respond to one user
if chat_id != USER_ID:
print('Message from unknown user. Ignore.')
return
command = msg['text'].strip().lower()
if command == '/now':
read_send(chat_id)
elif command == '/1m':
read_send(chat_id)
last_report = time.time()
report_interval = 60 # report every 60 seconds
elif command == '/1h':
read_send(chat_id)
last_report = time.time()
report_interval = 3600 # report every 3600 seconds
elif command == '/cancel':
last_report = None
report_interval = None # clear periodic reporting
bot.sendMessage(chat_id, "OK")
else:
bot.sendMessage(chat_id, "I don't understand")
TOKEN = sys.argv[1]
USER_ID = int(sys.argv[2])
bot = telepot.Bot(TOKEN)
MessageLoop(bot, handle).run_as_thread()
print('Listening ...')
# variables for periodic reporting
last_report = None
report_interval = None
while 1:
# Is it time to report again?
now = time.time()
if (report_interval is not None
and last_report is not None
and now - last_report >= report_interval):
try:
read_send(USER_ID)
last_report = now
except:
traceback.print_exc()
time.sleep(1)