树莓派录音并发送到特定Telegram Group(二)

目的:

在Raspberrypi上录音,然后把录音发送到特定Telegram group, 之前做了通过Telegram Bot 把录音发送到Raspberry pi 并播放.

操作说明:

Raspberry pi GPIO 版本: 0.6.3(这个和最新的0.10.0有方法名有区别)

手指触摸开关,指示灯亮起,此时开始录音,当想停止录音时,手指再次触摸开关, 指示灯关闭, 停止录音(TTP223, 也可以切换到另一模式,手指触摸长亮,手指离开关闭)。将音频文件移动到指定文件夹,然后通过Telegram Bot 把音频文件发送到指定的Telegram Group.

引用:

http://www.jianshu.com/p/3763957519a4

http://www.jianshu.com/p/008339095fd6

http://www.jianshu.com/p/ace71c44ec67

Github 项目:

https://github.com/i-sync/raspberry-record-audio

准备材料

  • Raspberry PI 2 Model B+
  • USB麦克风(CM108) Raspberrypi 免驱(淘宝20元)
  • 发光二极管
  • 电阻(500欧)
  • 杜邦线
  • 面包板
  • TTP223 (触摸开关) (淘宝1元左右)

Raspberry PI GPIO

电路图

使用上图针脚 17(3.3v) 18(输出), 19(输入), 20(GND)

实物图

https://i.loli.net/2017/09/10/59b50cc46993c.jpg
https://i.loli.net/2017/09/10/59b50ce5e9deb.jpg
https://i.loli.net/2017/09/10/59b50cfe87dcf.jpg

代码片段

设置GPIO

GPIO.setmode(GPIO.BOARD) #按物理位置找GPIO头
GPIO.setup(18, GPIO.OUT, initial = GPIO.LOW) # set pin18 output 
GPIO.setup(19, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)  # set pin19 input

在Raspberry pi 录音命令, Ctrl+C 停止录音

arecord -D "plughw:1,0" -r 16000  -f S16_LE test.wav

停止录音,需要先找到arecord进程ID, 然后Kill进程

def get_arecord_pid():
    command = "ps aux | grep arecord | grep -v grep | awk '{print $2}'"
    pid = subprocess.check_output(command, shell = True)
    pid = pid.decode('utf-8').strip('\n')
    return pid

停止录音,移动音频文件到监控文件夹

def start_process(channel):
    global start_time
    global file_name
    global led_flag
    led_flag = not led_flag
    if led_flag:
        start_time = time.time()
        GPIO.output(18, GPIO.HIGH)
        current_time = datetime.today().strftime("%Y%m%d-%X")
        file_name = current_time + '.{}'
        logging.info('start record: {}'.format(file_name))
        #command = 'arecord -D plughw:1,0 -f cd -t wav > /home/pi/telegram/tmp/{}.wav &'.format(current_time)
        command = 'arecord -D plughw:1,0 -f S16_LE -t wav > /home/pi/telegram/tmp/{}.wav &'.format(file_name)
        logging.info(command)
        subprocess.call(command, stdout = subprocess.PIPE, shell = True)
    else:
        pid = get_arecord_pid()
        if pid:
            duration = int(time.time() - start_time)
            command = 'kill {}'.format(pid)
            subprocess.call(command, shell = True)
            mv = 'mv /home/pi/telegram/tmp/{}.wav /home/pi/telegram/files/{}.wav'.format(file_name, file_name.format(duration))
            subprocess.call(mv, shell = True)

        GPIO.output(18, GPIO.LOW)
        logging.info('stop record')

GPIO.add_event_detect(19, GPIO.BOTH, callback = start_process)

当监测到有新音频时,发送音频文件到Telegram Group

def send_message(real_path):
    if not os.path.exists(real_path):
        logging.info('{} file not found, return...'.format(real_path))
        return
    _, duration, _ = real_path.split('.')
    bot.send_voice(chat_id = group_info['id'], voice = open(real_path, 'rb'), duration = duration, timeout = 120)
    logging.info('{}, send file finish...'.format(real_path))

评论

还没有人评论,抢个沙发吧...

Viagle Blog

欢迎来到我的个人博客网站