-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetectpress.py
335 lines (296 loc) · 10.1 KB
/
detectpress.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import threading
import RPi.GPIO as gpio
import time
from time import sleep
from gpiozero import MCP3008, Button
import pygame
from telegram.ext import Updater # 更新者
from telegram.ext import CommandHandler, CallbackQueryHandler # 註冊處理 一般用 回答用
from telegram.ext import MessageHandler, Filters # Filters過濾訊息
from telegram import InlineKeyboardMarkup, InlineKeyboardButton # 互動式按鈕
from picamera import PiCamera,Color
from datetime import datetime
from subprocess import call, Popen, PIPE
# MCP3008 設置
sound = MCP3008(0)
# MCP3008 gigital sound pin
digital_sound = Button(4)
# button pin
btn = 27
# button pin setup
gpio.setmode(gpio.BCM)
gpio.setup(btn, gpio.IN, pull_up_down=gpio.PUD_UP)
# button pin2
btn2 = 25
# button pin setup
gpio.setup(btn2, gpio.IN, pull_up_down=gpio.PUD_UP)
# 循環間隔時間
delayy = 0.3
pygame.mixer.init()
###馬達
#訊號輸出pin(PWM)
MotorPin=18
#訊號輸出pin設置
gpio.setup(MotorPin,gpio.OUT)
#50 : 頻率(Hz)
PWM_FREQ = 50
pwm_motor = gpio.PWM(MotorPin, PWM_FREQ)
# 設定一開始的Duty Cycle為 7.5
pwm_motor.start(7.5)
# global variables
loop = 1
running = True
AvgArr =[]
input_result = True
# camera
camera = PiCamera()
PHOTO_PATH = ''
VIDEO_PATH = ''
VIDEO_PATH_mp4 = ''
isstart = False
isstop = True
isDBZstart = False
isDBZstop = True
# bot start
def start(update, context):
global running, input_result, detecting_thread, previous, isstart, isstop
if isstart == False :
p = Popen("ip route list", shell=True, stdout=PIPE)
data = p.communicate()[0].decode().split()
ip = data[data.index('src')+1]
# = press the Btn1
input_result = False
running = True
chat_id = update.message.chat_id
context.bot.send_message(chat_id=chat_id, text="start ok!, The IP of RPi is : " + ip)
print("=============Start================",chat_id)
detecting_thread = None
detecting_thread = threading.Thread(target=detectDB)
detecting_thread.start()
previous = True
isstart = True
isstop = False
else:
chat_id = update.message.chat_id
context.bot.send_message(chat_id=chat_id, text="Already start!")
# bot stop
def stop(update,context):
global running, detecting_thread, previous, isstop, isstart
if isstop == False:
chat_id = update.message.chat_id
context.bot.send_message(chat_id=chat_id, text="stop ok!")
# print("=============Start================")
print("=============Stop================",chat_id)
running = False
detecting_thread = None
previous = False
isstop = True
isstart = False
else:
chat_id = update.message.chat_id
context.bot.send_message(chat_id=chat_id, text="Already stop!")
# bot play background music
def DBZ(update,context):
global isDBZstart, isDBZstop
if isDBZstart == False:
play('/home/pi/mp3/DBZq.wav')
chat_id = update.message.chat_id
context.bot.send_message(chat_id=chat_id, text="A MI TO FOOl,A MI TO FOOl~~")
print("=============A-MI-TO-FOOL================")
isDBZstart = True
isDBZstop = False
else:
chat_id = update.message.chat_id
context.bot.send_message(chat_id=chat_id, text="Bot is playing!")
# bot take photo
def photo(update,context):
# global Tbot
# Tbot = False
chat_id = update.message.chat_id
# context.bot.send_message(chat_id=chat_id, text="stop ok!")
# TELEGRAM_CHAT_ID = 'update.message.chat_id'
takephoto()
count = time_now(0)
context.bot.send_message(chat_id, text=" Photo From Telegram Bot : %s" %count)
context.bot.send_photo(chat_id, photo=open(PHOTO_PATH, 'rb'))
# print("=============Start================")
print("=============Photo================",chat_id)
# detect()
# bot record video
def video(update,context):
global VIDEO_PATH, VIDEO_PATH_mp4
# Tbot = False
chat_id = update.message.chat_id
# context.bot.send_message(chat_id=chat_id, text="stop ok!")
# TELEGRAM_CHAT_ID = 'update.message.chat_id'
recordvideo()
count = time_now(0)
context.bot.send_message(chat_id, text=(" Video From Telegram Bot : %s" %count))
context.bot.send_video(chat_id, video=open(VIDEO_PATH_mp4, 'rb'))
# print("=============Start================")
print("=============Video================",chat_id)
# detect()
# play stop
def DBZstop(update,context):
global isDBZstart, isDBZstop
if isDBZstop == False:
# 暫停音樂
pygame.mixer.Channel(0).stop()
print('music stop_playing')
chat_id = update.message.chat_id
context.bot.send_message(chat_id=chat_id, text="A MI stop,A MI stop~~")
print("=============A-MI-stop================")
isDBZstop = True
isDBZstart = False
else:
chat_id = update.message.chat_id
context.bot.send_message(chat_id=chat_id, text="Music is stopped!")
# detect voice
def detectDB():
global loop ,running, AvgArr, input_result
# 開始運作bot
print("read stop function")
while running :
print('{}-SoundValue:{:.4f}, ButtonValue:{}'.format(loop, sound.value, digital_sound.value))
AvgArr.append(sound.value)
time.sleep(0.1)
loop += 1
if (loop%5 == 0):
Avg = sum(AvgArr)/5
#如果太大聲
if(Avg > 0.57):
print("【too loud!!!!!】",Avg)
# 拍牆壁
for a in range(5):
#頻寬百分比 +90 = 4
# pwm_motor.ChangeDutyCycle(4.5)
pwm_motor.ChangeDutyCycle(4.5)
time.sleep(0.3)
print ("【To+60】",a)
pwm_motor.ChangeDutyCycle(11)
time.sleep(0.3)
print ("【To-60】")
pwm_motor.ChangeDutyCycle(7.5)
pygame.mixer.music.load('/home/pi/mp3/hanweivoice.mp3')
pygame.mixer.music.play(0,0.6)
time.sleep(1)
print("--------")
print("Average:",Avg)
print("--------")
AvgArr = []
def play(file) :
player = pygame.mixer.Sound(file)
pygame.mixer.Channel(0).play(player)
def detectBtn():
global input_result, running, previous
while True:
# 放開 = 1 = True
# 按下 = 0 = False
#接收到按鈕輸入
input_result = gpio.input(btn)
print("【Btn1_In】",input_result)
####大悲咒按鈕####
input_result2 = gpio.input(btn2)
print("【Btn2_In】",input_result2)
#當Btn2 被按下去時
if input_result2 == False:
# 開始播背景音樂
if pygame.mixer.Channel(0).get_busy() == False:
# 載入、播放音樂
play('/home/pi/mp3/DBZq.wav')
print("Playing",input_result)
else:
# 暫停音樂
pygame.mixer.Channel(0).stop()
print('Isnotplaying')
#################
# when Btn1 pressed
if input_result == False :
# 剛才沒按過
if previous == False :
print("=============Start================")
# start to detect
running = True
previous = True
detecting_thread = None
# add thread to detect voice
detecting_thread = threading.Thread(target=detectDB)
detecting_thread.start()
# 剛才按過
else :
running = False
previous = False
detecting_thread = None
time.sleep(delayy)
# call bot to take photo
def takephoto():
global PHOTO_PATH
camera.start_preview()
camera.annotate_background = Color('red')
camera.annotate_text = "I'm the bad neighbor"
# sleep(0.25)
camera.rotation = 180
PHOTO_PATH = '/home/pi/camera/image.jpg'
camera.capture(PHOTO_PATH)
camera.stop_preview()
print("photo tooken")
# call bot to record video
def recordvideo():
global VIDEO_PATH,VIDEO_PATH_mp4,count
count = time_now(1)
# camera.resolution = (1000, 1000)
camera.start_preview()
camera.annotate_background = Color('red')
camera.annotate_text = "I'm the bad neighbor"
camera.rotation = 180
VIDEO_PATH = ('/home/pi/camera/video%s.h264'% count)
VIDEO_PATH_mp4 = ('/home/pi/camera/video%s.mp4'% count)
camera.start_recording(VIDEO_PATH)
time.sleep(5)
camera.stop_recording()
camera.stop_preview()
convert(VIDEO_PATH, VIDEO_PATH_mp4)
print("video recorded")
# covert video format
def convert(VIDEO_PATH, VIDEO_PATH_mp4):
# Record a 15 seconds video.
print("Rasp_Pi => Video Recorded! \r\n")
# Convert the h264 format to the mp4 format.
command = "MP4Box -add " + VIDEO_PATH + " " + VIDEO_PATH_mp4
call([command], shell=True)
print("\r\nRasp_Pi => Video Converted! \r\n")
def time_now(type):
# 1:檔名
# 0: 時間字串
if(type == 1):
result = datetime.now().strftime("%Y%m%d%H%M%S%p")
else:
result = datetime.now().strftime("%Y-%m-%d %H:%M:%S %p")
print(result)
return result
def main():
# 設定 bot token
token = '5025165228:AAGTD4Lv1kojz0N4YjdEX9A6wUURRmseQK0'
# 初始化bot
updater = Updater(token=token, use_context=True)
dispatcher = updater.dispatcher
# set Command
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('play', DBZ))
dispatcher.add_handler(CommandHandler('stop', stop))
dispatcher.add_handler(CommandHandler('playstop', DBZstop))
dispatcher.add_handler(CommandHandler('photo', photo))
dispatcher.add_handler(CommandHandler('video', video))
updater.start_polling()
# 監測車子上的按鈕
detectBtn()
# global
detecting_thread = None
previous = False
if __name__ == "__main__":
try :
main()
# pass
except KeyboardInterrupt:
detecting_thread = None
gpio.cleanup()