-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.py
222 lines (178 loc) · 9.49 KB
/
temp.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# -*- coding: utf-8 -*-
import datetime
import os
import sqlite3
import sys
import time
import telepot
from apscheduler.schedulers.blocking import BlockingScheduler
from pyA20.gpio import gpio
from pyA20.gpio import port
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, ReplyKeyboardRemove, \
ForceReply, InlineKeyboardButton, InlineQueryResultArticle, InlineQueryResultPhoto, InputTextMessageContent
conn = sqlite3.connect('/usr/local/src/tempcontrol/temp.sqlite', check_same_thread=False)
db = conn.cursor()
rele = port.PA1
pomp_1 = port.PA0
pomp_2 = port.PA3
a_pomp_1 = 1
a_pomp_2 = 1
gpio.init()
gpio.setcfg(rele, gpio.OUTPUT)
gpio.setcfg(pomp_1, gpio.OUTPUT)
gpio.setcfg(pomp_2, gpio.OUTPUT)
token = 'TOKEN' # TempBot #Токен к Telegram боту
bot = telepot.Bot(token)
hysteresis = 0.5 # Гистерезис температуры комнатного термостата
temp = 24.0 # Начальная температура комнатного термостата
heater = True # Режим нагрева
t_notify = 180 # Интервал уведомлений (мин)
message_with_inline_keyboard = None
s_temp = False
s_notify = False
def get_temperature(idW1):
filepath = '/sys/devices/w1_bus_master1/' + idW1 + '/w1_slave'
f = open(filepath, 'r')
data = f.read()
f.close()
return float(data[data.find('t=') + 2:]) / 1000
def on_chat_message(msg):
global s_temp, temp, s_notify, t_notify, pomp_1, pomp_2, a_pomp_1, a_pomp_2
content_type, chat_type, chat_id = telepot.glance(msg)
if content_type == 'text':
command = msg['text']
if command == '/start' or command == '/start@LeechDacha_bot':
markup = ReplyKeyboardMarkup(keyboard=[
[dict(text='Инфо'), dict(text='Управление'), dict(text='Сигнализация')]
], resize_keyboard=True)
bot.sendMessage(chat_id, 'Привет! Я дачный бот. Выбери раздел', reply_markup=markup)
elif command == 'Главное меню':
markup = ReplyKeyboardMarkup(keyboard=[
[dict(text='Инфо'), dict(text='Управление'), dict(text='Сигнализация')]
], resize_keyboard=True)
bot.sendMessage(chat_id, 'Выбери раздел', reply_markup=markup)
elif command == u'Инфо':
markup = ReplyKeyboardMarkup(keyboard=[
[dict(text='Температура'), dict(text='Уст. температура')],
[dict(text='Режим нагрева'), dict(text='Главное меню')],
], resize_keyboard=True)
bot.sendMessage(chat_id, 'Выбери объект', reply_markup=markup)
elif command == u'Температура':
bot_temp(chat_id)
elif command == u'Уст. температура':
bot.sendMessage(chat_id, 'Установленная температура %s C' % temp)
elif command == u'Режим нагрева':
bot.sendMessage(chat_id, 'Идет нагрев %s' % heater)
elif command == u'Управление':
markup = InlineKeyboardMarkup(inline_keyboard=[[dict(text='Задать температуру', callback_data='temp_set')]])
bot.sendMessage(chat_id, 'Выбери объект', reply_markup=markup)
markup = ReplyKeyboardMarkup(keyboard=[
[dict(text='Насос №2 АВТО'), dict(text='Насос №2 ВКЛ'), dict(text='Насос №2 ВЫКЛ')],
[dict(text='Насос №1 АВТО'), dict(text='Насос №1 ВКЛ'), dict(text='Насос №1 ВЫКЛ')],
[dict(text='Главное меню')],
], resize_keyboard=True)
bot.sendMessage(chat_id, 'Выбери объект', reply_markup=markup)
elif command == u'Насос №1 АВТО':
a_pomp_1 = 1
bot.sendMessage(chat_id, 'Включен авто режим для насоса №1 ')
elif command == u'Насос №2 АВТО':
a_pomp_2 = 1
bot.sendMessage(chat_id, 'Включен авто режим для насоса №2 ')
elif command == u'Насос №1 ВКЛ':
a_pomp_1 = 0
gpio.output(pomp_1, 0)
bot.sendMessage(chat_id, 'Включен насос №1 ')
elif command == u'Насос №2 ВКЛ':
a_pomp_2 = 0
gpio.output(pomp_2, 0)
bot.sendMessage(chat_id, 'Включен насос №2 ')
elif command == u'Насос №1 ВЫКЛ':
a_pomp_1 = 0
gpio.output(pomp_1, 1)
bot.sendMessage(chat_id, 'Выключен насос №1 ')
elif command == u'Насос №2 ВЫКЛ':
a_pomp_2 = 0
gpio.output(pomp_2, 1)
bot.sendMessage(chat_id, 'Выключен насос №2 ')
elif command == u'Сигнализация':
markup = InlineKeyboardMarkup(
inline_keyboard=[[dict(text='Задать период оповещения', callback_data='notify_set')]])
bot.sendMessage(chat_id, 'Выбери объект', reply_markup=markup)
if s_temp:
# если происходит установка температуры
if command.isdigit():
temp = int(command)
markup = ReplyKeyboardMarkup(keyboard=[[KeyboardButton(text='Главное меню')]], resize_keyboard=True)
bot.sendMessage(chat_id, str("Температура установлена в %s градусов.") % command, reply_markup=markup)
s_temp = False
else:
markup = ReplyKeyboardMarkup(keyboard=[[KeyboardButton(text='Главное меню')]], resize_keyboard=True)
bot.sendMessage(chat_id, str(
"%s - это не целое число. При необходимости пройдите настройку заново. Значение не установлено!") % command,
reply_markup=markup)
s_temp = False
if s_notify:
# если происходит установка периода оповещения
if command.isdigit():
job_notify.reschedule('interval', minutes=int(command))
markup = ReplyKeyboardMarkup(keyboard=[[KeyboardButton(text='Главное меню')]], resize_keyboard=True)
bot.sendMessage(chat_id, str("Период оповещения каждые %s минут.") % command, reply_markup=markup)
s_notify = False
else:
markup = ReplyKeyboardMarkup(keyboard=[[KeyboardButton(text='Главное меню')]], resize_keyboard=True)
bot.sendMessage(chat_id, str(
"%s - это не целое число. При необходимости пройдите настройку заново. Значение не установлено!") % command,
reply_markup=markup)
s_notify = False
def on_callback_query(msg):
global s_temp, s_notify
query_id, from_id, data = telepot.glance(msg, flavor='callback_query')
if data == 'temp_set':
s_temp = True
bot.answerCallbackQuery(query_id, text='Установите температуру. Введите целое число в градусах.',
show_alert=True)
elif data == 'notify_set':
s_notify = True
bot.answerCallbackQuery(query_id, text='Установите период оповещения. Введите целое число в минутах.',
show_alert=True)
def timed_send():
bot_temp(111111111) # DachaGroup # You ID or ID group
def bot_temp(chat_id):
bot.sendMessage(chat_id, "Температура выхода котла: %s\n" \
"Температура входа котла: %s\n" \
"Температура в помещении: %s\n" % (
get_temperature('28-0316b2ec9aff'),
get_temperature('28-0316b306ffff'),
get_temperature('28-0516a16bcbff')))
def add_temp():
db.execute("INSERT INTO temp_control (date,temp, temp_s, heater) VALUES ('%s','%s','%s','%s')" % (
datetime.datetime.now(), get_temperature('28-0516a16bcbff'), temp, heater))
conn.commit()
def temp_control():
global heater
r_temp = get_temperature('28-0516a16bcbff')
if 0.0 <= r_temp <= 75:
if r_temp <= (temp - hysteresis):
gpio.output(rele, 0)
if a_pomp_1 == 1:
gpio.output(pomp_1, 0)
if a_pomp_2 == 1:
gpio.output(pomp_2, 0)
heater = True
elif r_temp >= (temp + hysteresis):
gpio.output(rele, 1)
if a_pomp_1 == 1:
gpio.output(pomp_1, 1)
if a_pomp_2 == 1:
gpio.output(pomp_2, 1)
heater = False
add_temp()
bot.message_loop({'chat': on_chat_message,
'callback_query': on_callback_query})
print('Listening ...')
scheduler = BlockingScheduler()
job_notify = scheduler.add_job(timed_send, 'interval', minutes=t_notify)
job_temp = scheduler.add_job(temp_control, 'interval', seconds=5)
scheduler.start()
while 1:
time.sleep(10)