AWS IoT+樹莓派 打造動態監控(3)

副標題: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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容