美文网首页
AWS IoT+树莓派 打造动态监控(3)

AWS IoT+树莓派 打造动态监控(3)

作者: 今後次 | 来源:发表于2017-06-22 13:28 被阅读760次

    副标题:RaspberryPi图片上传到GoogleDrive

    上篇:AWS IoT+树莓派 打造动态监控(2)

    总体流程

    这篇文章中实现的是PyDrive→GoogleDrive的部分

    取得GoogleAPI认证信息

    首先得需要一个google帐号,自己另外申请(注意:国内需要翻墙)

    • 取得OAuth信息


    选择最后一项【その他】


    サービス名输入【imageupload】


    下面就能看到Client ID和Client secret,请拷贝保存在本地。以后设定文件中会用。

    • API有效化


    到此为止,GoogleDrive的API环境已经准备好,接下来是在树莓派上编码阶段。

    创建认证用配置文件

    • 在程序同一文件夹下(我的是aws-iot)使用vim(安装手顺见最下)创建settings.yaml
    client_config_backend: settings
    client_config:
    client_id: 上图Google API发行的Client ID
    client_secret: 上图Google API发行的Client secret
    save_credentials: True
    save_credentials_backend: file
    save_credentials_file: credentials.json
    
    get_refresh_token: True
    
    oauth_scope:
    - https://www.googleapis.com/auth/drive.file
    - https://www.googleapis.com/auth/drive.install
    

    此处有坑
    【save_credentials_file:credentials.json】的位置是个半角空格,一定要有!!!否则会报client_secrets.json文件买不到的error。
    调试半个小时的血泪教训。

    安装PyDrive

    python的第三方包,可以使用python代码
    逐次执行以下命令

    $ sudo apt-get update
    $ sudo apt-get install python3-pip(python3自带pip3,没有的话,执行这条命令)
    $ pip3 install --upgrade google-api-python-client
    $ pip3 install PyDrive

    创建上传python3代码

    imageupload2Gdrive.py

    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    import time
    import os
    gauth = GoogleAuth()
    gauth.CommandLineAuth()
    drive = GoogleDrive(gauth)
    IMGPATH ="/home/pi/motion-images/"
    # time.sleep(1)
    folder_id = '0Bx1-M6qTTmagdWxFNlh1d0JEVFk'
    
    assert os.path.exists(IMGPATH)
    assert os.path.isdir(IMGPATH)
    imageList = os.listdir(IMGPATH)
    lenList= len(imageList)
    
    f = drive.CreateFile({'title': imageList[lenList-1],
                          'mimeType': 'image/jpeg',
                          'parents': [{'kind': 'drive#fileLink', 'id':folder_id}]})
    
    f.SetContentFile(IMGPATH+imageList[lenList-1])
    f.Upload()
    os.remove(IMGPATH+imageList[lenList-1])
    

    修改motion.conf

    在motion.conf中有一下配置

    on_picture_save … 保留截图时,同时执行的脚本。我的脚本路径/home/pi/aws-iot/mosquitto_pub.sh

    现在需要把上面的imageupload2Gdrive.py执行,加进mosquitto_pub.sh

    sh脚本内容如下

    #!/bin/sh
    mosquitto_pub --cafile /home/pi/aws-iot/rootCA.pem \
      --cert /home/pi/aws-iot/cede542bce-certificate.pem.crt \
      --key /home/pi/aws-iot/cede542bce-private.pem.key \
      -h a9s8e7v6urbcc.iot.us-west-2.amazonaws.com \
      -p 8883 -q 1 -d \
      -t topic/sns \
      -m '{"message":"Alter!! https://drive.google.com/drive/folders/0Bx1-M6qTTm    agdWxFNlh1d0JEVFk?usp=sharing"}'
    #以上m中追加了GoogleDrive的地址,在收到的邮件中点击就可查看。
    #以下追加uploadImage2Gdrive.py执行,上传截图到GoogleDrive
    cd /home/pi/aws-iot/
    python3 /home/pi/aws-iot/uploadImage2Gdrive.py
    

    重启motion

    killall -TERM motion
    sudo motion

    效果

    • 这时只要在摄像头前挥舞一下手臂,就能看到保存下来的图片。


    • Gmail收到报警邮件


    • 点开邮件里面的地址,能看到GoogleDrive中图片


    补充

    树莓派上默认是vi,感觉比vim难用多了,所以我就安装了vim。
    安装步骤:

    • 执行安装命令

    sudo apt-get install -y vim

    • 代码高亮设置
      在~目录下面新建.vimrc
    pi@raspberrypi ~ $ cd ~
    pi@raspberrypi ~ $ vim .vimrc
    
    #显示格式
    set number
    syntax on
    set tabstop=4
    #中文乱码问题解决
    set encoding=utf-8
    set fileencodings=iso-2022-jp,euc-jp,sjis,utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
    set fileformats=unix,dos,mac

    相关文章

      网友评论

          本文标题:AWS IoT+树莓派 打造动态监控(3)

          本文链接:https://www.haomeiwen.com/subject/eskmqxtx.html