-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_definitivo_V1.py
230 lines (200 loc) · 6.61 KB
/
bot_definitivo_V1.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
223
224
225
226
227
228
229
import sys
import time
import random
import datetime
import telegram
import logging
from telegram.ext import MessageHandler, Updater, Filters
import RPi.GPIO as GPIO
import cv2
from skimage.measure import compare_ssim as ssim
# Set Gpio
GPIO.setmode(GPIO.BCM)
# Input
PB_DX = 23 # PushButtonDX
PB_SX = 24 # PushButtonSX
GPIO.setup([PB_SX, PB_DX], GPIO.IN)
# Output
POWER = 12 # Power relay
HBED = 16 # Heated bed relay
LIGHT = 25 # Light relay
INIT = 8 # Init relay
LED = 18 # LED
GPIO.setup([POWER, HBED, LIGHT, INIT, LED], GPIO.OUT)
GPIO.output([POWER, HBED, LIGHT, INIT, LED], GPIO.LOW)
# Global variable
state = [False, False, False, False, False, False, False] # State of printer: ON/OFF, PRINTING, SHUTTING DOWN, MANUAL MODE, CAMERA BUSY, HEATED BED, LIGHT
chat_id = 25188214 # chat ID Ale Torri
control_var = 0 # How many controll()==True before
last_control = time.time()
last_msg = last_control
control_time = 30 #Determine the time for controll()
# On-off functions
def on(pin):
GPIO.output(pin, GPIO.HIGH)
return
def off(pin):
GPIO.output(pin, GPIO.LOW)
return
# Photo function
def photo(save):
global state
while state[4]:
time.sleep(0.001)
state[4] = True
cam = cv2.VideoCapture(0)
if cam.isOpened():
ret, frame = cam.read()
if ret:
if save:
cv2.imwrite("temp_image.jpg", frame)
cam.release()
state[4] = False
return frame
else:
state[4] = False
exit(1)
else:
state[4] = False
exit(1)
# Controllin function
def control():
global control_var
frame_1 = photo(False)
cv2.waitKey(700)
frame_2 = photo(False)
sim = ssim(frame_1[0, :, :], frame_2[0, :, :], multichannel=True)
if sim >= 0.85:
control_var += 1
print('Control passed True: %s' % control_var)
return True
else:
control_var = 0
return False
# Main loop for message income
def messageLoop(bot, update):
global chat_id
command = update.message.text
print('Got command: %s' % command)
global state
if state[2]:
if command == 'Y':
off(HBED)
time.sleep(0.01)
off(POWER)
bot.send_message(chat_id, text="Printer OFF")
print('Printer OFF')
state = [False, False, False, False, False, False, False]
elif command == 'N':
bot.send_message(chat_id, text="Keep printer ON")
state[2] = False
else:
bot.send_message(chat_id, text="Input Y/N")
return
if command == 'On' and not state[0]:
on(POWER)
bot.send_message(chat_id, text='Printer ON')
print('Printer ON')
state[0] = True
if command == 'Off' and state[0]:
frame = photo(True)
bot.send_photo(chat_id, open("temp_image.jpg", "rb"), '%s' %datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) #Or better send GIF
bot.send_message (chat_id, text = 'Do you want to shut down the printer?? (Y/N)')
state[2] = True
if command == 'Start' and state[0]:
bot.send_message(chat_id, text='Start')
state[1] = True
if command == 'Stop' and state[0]:
bot.send_message(chat_id, text = 'Stop')
state[1] = False
if command == 'Bed' and state[0]:
if state[5]:
bot.send_message(chat_id, 'Heated bed OFF')
off(HBED)
state[5] = False
else:
bot.send_message(chat_id, 'Heated bed ON')
on(HBED)
state[5] = True
if command == 'Photo':
frame = photo(True)
bot.send_photo(chat_id, open("temp_image.jpg", "rb"), '%s' %datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
if command == 'State':
bot.send_message(chat_id, 'State is: %s' % state)
if command == 'Light':
if state[6]:
bot.send_message(chat_id, 'Light OFF')
off(LIGHT)
state[6] = False
else:
bot.send_message(chat_id, 'Light ON')
on(LIGHT)
state[6] = True
bot = telegram.Bot('660828097:AAH3n-5T7jMX2CbHAVSkuSKCY9i0wN80Lc4')
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
updater = Updater('660828097:AAH3n-5T7jMX2CbHAVSkuSKCY9i0wN80Lc4')
updater.dispatcher.add_handler(MessageHandler(Filters.text, messageLoop))
updater.start_polling()
print('I am listening...')
# Main loop
while 1:
if (GPIO.input(PB_SX)): # ON-OFF Button
while(GPIO.input(PB_SX)): # Debounce
time.sleep(0.1)
if (not state[0]):
on(POWER)
bot.send_message(chat_id, text='Printer ON')
print('Printer ON')
state[0] = True
else:
off(HBED)
time.sleep(0.01)
off(POWER)
bot.send_message(chat_id, text="Printer OFF")
print('Printer OFF')
state = [False, False, False, False, False, False, False]
control_var = 0
if (GPIO.input(PB_DX)): # Manual Mode Button
while(GPIO.input(PB_DX)): # Debounce
time.sleep(0.1)
if (not state[3]):
on(LED)
bot.send_message(chat_id, text='Manual Mode ON')
print('Manual Mode ON')
state[3] = True
else:
off(LED)
bot.send_message(chat_id, text="Manual Mode OFF")
print('Manual Mode OFF')
state[3] = False
if state[0] and state [1]: # Prinetr ON and Print ONGOING
current_time = time.time()
if current_time >= last_msg+1200:
last_msg = current_time
bot.send_message(chat_id, 'The print is going, OK!')
control_time_1 = control_time
if control_var > 1:
control_time_1 = 30
if current_time >= (last_control+control_time_1):
last_control = current_time
print('Controlling: %s' % last_control)
flag = control()
print('%s' % flag)
if flag and control_var > 5:
##shtu down request
print('Print DONE!')
bot.send_message(chat_id, 'Great, the printer is finished!!!')
control_var = 0
time.sleep(0.02)
time.sleep(0.02)
while 1:
try:
time.sleep(0.5)
except KeyboardInterrupt:
print('\n Program interrupted')
GPIO.cleanup()
exit()
except:
print('Other error or exception occured!')
GPIO.cleanup()
exit()