第一部分:用跳繩測試儀解決運動不足問題!
第二部分:用SensorMedal檢測跳躍次數并在顯示器上顯示
大家好,我是吉田!
本連載旨在制作一款用來解決運動不足問題的跳繩設備。本文是第三部分,前面已經實現了通過加速度傳感器對跳躍次數進行計數,本文將介紹將其數據保存在Google Sheets中并進行處理的步驟。如果能夠了解自己的跳躍次數、時間、消耗的總熱量等,可能會增加今后堅持鍛煉的動力。現在就動手開始做吧!
本部分所需部件
跳繩
ROHM SensorMedal(SensorMedal-EVK-002)
Raspberry Pi zero(Raspberry Pi Zero W)
本部分的流程
1.設置
2.從Raspberry Pi上傳到Google
3.保存并顯示跳繩結果
4.總結
1.設置Google Sheets
當我們實現了跳繩計數后,就很希望能將次數等數據保存下來,以便隨時查看以往的跳躍次數。
為此,讓我們將跳躍次數等數據保存在Google云端的電子表格中吧。Google的云服務可以在一定程度內免費使用,因此可以隨時開始使用。
首先,需要訪問下面的Google控制臺。單擊下面的鏈接可以注冊您的Google帳戶并登錄開始使用:
https://console.developers.google.com/
您Google帳戶的初始設置完成后,可以在控制臺中搜索名為“Sheets”的API,就會出來Google Sheets API,請選擇它。
現在,點擊左側的藍色按鈕“啟用”,啟用Sheets API。
從認證頁面頂部的“創建認證信息”中選擇“服務帳戶”。
在服務帳戶創建頁面上,輸入適當的帳戶名稱和ID,然后點擊“創建”。
即使“允許訪問”,也需要授予項目所有者等權限并點擊“繼續”。
這樣,就創建了服務帳戶。現在,單擊如下所示的創建秘鑰按鈕,下載JSON格式的文件。
下載好JSON文件后打開,保存郵件信息“xxx@yyy.iam.gserviceaccount.com”的地址。另外,將這個JSON文件發送給Raspberry Pi。
這些設置完成后,轉到 Google Sheets并嘗試創建電子表格。可以訪問Google Drive或使用下面的鏈接:
https://docs.google.com/spreadsheets/create
新建的Google Sheets表格打開后,在左上角的名稱位置輸入適當的表格名稱。(我設置的是“Jump_Count”)
然后點擊右側的“分享”按鈕,輸入之前保存的賬號信息(xxx@yyy.iam.gserviceaccount.com),完成。
2. 從Raspberry Pi上傳到Google
現在,Google這邊的設置已經完成,讓我們從Raspberry Pi開始使用吧。啟動Raspberry Pi并打開終端。
要想使用Google Sheets,需要各種認證和使用名為“gspread”的庫,因此請按如下方式安裝。
pi@raspizero:~/Programs $ sudo pip3 install oauth2client pi@raspizero:~/Programs $ sudo pip3 install httplib2 Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting httplib2 Downloading 100% |████████████████████████████████| 102kB Installing collected packages: httplib2 pi@raspizero:~/Programs $ sudo pip3 install gspread
現在可以將數據保存在Sheets中了。在這里,我創建了一個程序,用來從Raspberry Pi輸入數據,程序如下:
pi@raspizero:~/Programs $ sudo vi spreadsheet.py --------- #!/usr/bin/python # -*- coding: utf-8 -*- import gspread from oauth2client.service_account import ServiceAccountCredentials key_name = ‘xxx.json’ # Use your own information sheet_name = 'jump_count' # Use sheets API scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'] credentials = ServiceAccountCredentials.from_json_keyfile_name(key_name, scope) gc = gspread.authorize(credentials) # Enter TEST to Sell A1 cell_number = 'A1' input_value = 'Total Jump' wks = gc.open(sheet_name).sheet1 wks.update_acell(cell_number, input_value)
當運行這個程序時,“Jump Data”的數據會從Raspberry Pi被插入到云表格中。
3. 跳繩&保存、顯示!
現在讓我們修改跳繩程序,以讓跳躍次數可以上傳到Google。
首先需要記錄下您的表格的sheet_id(下面網址中紅框內的部分)。
在電子表格中,日期和時間保存在A列和B列中,跳躍次數保存在C列中,跳繩時間(秒)保存在D列中,卡路里消耗量保存在E列中。當在表格的C1、D1和E1輸入求和命令時,就會自動計算跳躍的總次數和消耗的卡路里總量等數據。
接下來,通過在ble_jump_4d.py程序中添加的方式,創建ble_jump_4d_sheets.py程序。為了將跳躍次數保存在Google Sheets中,需要添加以下部分:
第9?42行
第49?50行
第57?69行
第76?77行
pi@raspizero:~/Programs $ sudo cp ble_jump_4d.py ble_jump_4d_sheets.py --- 下面的更改或添加部分用黃色表示 --- #!/usr/bin/env python3 # coding: utf-8 --- import gspread from apiclient import discovery from oauth2client.service_account import ServiceAccountCredentials import httplib2 import numpy as np from datetime import datetime key_name = "xxx.json" # Use your JSON file name sheet_name= “jump_count” # Sheet name sheet_id = “zzz” # Sheet Id class SpreadSheet(object): def __init__(self, sheet_id): self.sheetId = sheet_id scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'] credentials = ServiceAccountCredentials.from_json_keyfile_name(key_name, scope) http_auth = credentials.authorize(httplib2.Http()) discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?''version=v4') self.service = discovery.build('sheets', 'v4', http=http_auth, discoveryServiceUrl=discoveryUrl) def append(self, values): # Append data in Google Sheets assert np.array(values).shape==(5,) , "The shape of value %s must be 5" % (np.array(values).shape) value_range_body = {'values':[values]} result = self.service.spreadsheets().values().append(spreadsheetId=self.sheetId, range=APPEND_RANGE, valueInputOption='USER_ENTERED', body=value_range_body).execute() print(result) spread_sheet = SpreadSheet(sheet_id) APPEND_RANGE = 'sheet1!A1:E1' start_time= datetime.now() last_time = datetime.now() last_cnt = 0 last_cal = 0 last_dur = 0 scanner = btle.Scanner() while True: --- fourletterphat.print_str("JUMP") fourletterphat.show() --- SEQ = sensors['SEQ'] jump_cnt = sensors['Steps'] if SEQ in [255,0,1] and jump_cnt == 0: start_seq+= 1 start_time= datetime.now() print(start_time.strftime('%H:%M:%S')+" Start Jumping!") if last_cnt!=0: spread_sheet.append([last_time.strftime('%Y/%m/%d'), last_time.strftime('%H:%M:%S'), last_cnt, last_dur, last_cal]) else: last_time= start_time last_cnt = jump_cnt cur_time= datetime.now() if start_seq >= 1: dur_time= cur_time - start_time dur_seconds = dur_time.seconds cur_cnt = jump_cnt cur_cal = round(cur_cnt*0.1,1) jump_text= str(cur_cnt)+" Jump "+str(cur_cal)+" Cal "+str(dur_seconds)+" s" print(jump_text) fourletterphat.print_number_str(cur_cnt) fourletterphat.show() last_dur = dur_seconds last_cal = cur_cal ''' for key, value in sorted(sensors.items(), key=lambda x:x[0]): print(' ',key,'=',value) '''
讓我們運行這個程序,按下SensorMedal上的按鈕后跳幾下吧。
在有網絡的地方,每次按下按鈕并跳繩,都會將次數保存下來。 我們設計的這種方式,可以成功地將跳躍次數保存在表格中。
4. 總結
在本連載中,我們使用Raspberry Pi和SensorMedal實現了連接物聯網的數字化跳繩。
本文是第三部分,我們不僅實現了次數計數,而且還成功地將每次跳繩的次數等數據上傳到Google Sheets以保存結果。Google Sheets很容易獲得合計值和繪制圖表,因此非常適合隨時查看跳繩結果。
在下一部分中,也就是最后一部分,我們將使用跳躍次數和保存的數據來創建一種機制,讓跳繩變得更加有趣也更加有動力,敬請期待!
本系列連載一覽
第一部分:用跳繩測試儀解決運動不足問題!
第二部分:用SensorMedal檢測跳躍次數并在顯示器上顯示
第三部分:連接Google Drive,保存并查看跳繩結果(本章)
第四部分:在設備上安裝顯示器以增加動力!
審核編輯黃宇
-
傳感器
+關注
關注
2553文章
51503瀏覽量
757139 -
測試儀
+關注
關注
6文章
3794瀏覽量
55167 -
Raspberry Pi
+關注
關注
2文章
559瀏覽量
22350
發布評論請先 登錄
相關推薦
霍爾元件YS253在計數跳繩中的應用案例
涂鴉智能跳繩解決方案 BK3432單藍牙驅動LCD屏,可芯片或PCBA
智能藍牙跳繩方案 深圳鼎盛合提供智能藍牙跳繩方案開發設計
單片機開發的智能跳繩解決方案
自動計數用霍爾?跳繩機、電機等智能計數霍爾開關
用Raspberry Pi和Arduino Micro制作的虛擬窺視孔
![<b class='flag-5'>用</b><b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>和Arduino Micro<b class='flag-5'>制作</b>的虛擬窺視孔](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
用Raspberry Pi和SensorMedal制作IoT跳繩設備:用SensorMedal檢測跳躍次數并在顯示器上顯示
![<b class='flag-5'>用</b><b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>和<b class='flag-5'>SensorMedal</b><b class='flag-5'>制作</b><b class='flag-5'>IoT</b><b class='flag-5'>跳繩</b><b class='flag-5'>設備</b>:<b class='flag-5'>用</b><b class='flag-5'>SensorMedal</b>檢測跳躍次數并在顯示器上顯示](https://file.elecfans.com/web2/M00/92/E1/pYYBAGPyzcWAQW-wAAGTBZAJzlg845.jpg)
用Raspberry Pi和SensorMedal制作IoT跳繩設備:用跳繩測試儀解決運動不足問題!
![<b class='flag-5'>用</b><b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>和<b class='flag-5'>SensorMedal</b><b class='flag-5'>制作</b><b class='flag-5'>IoT</b><b class='flag-5'>跳繩</b><b class='flag-5'>設備</b>:<b class='flag-5'>用</b><b class='flag-5'>跳繩</b>測試儀解決運動不足問題!](https://file.elecfans.com/web2/M00/92/5E/poYBAGPyzgqAHZOEAAK_jthUfdg108.jpg)
用Raspberry Pi和SensorMedal制作IoT跳繩設備 第四部分:在設備上安裝顯示器以增加動力
![<b class='flag-5'>用</b><b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>和<b class='flag-5'>SensorMedal</b><b class='flag-5'>制作</b><b class='flag-5'>IoT</b><b class='flag-5'>跳繩</b><b class='flag-5'>設備</b> 第四部分:在<b class='flag-5'>設備</b>上安裝顯示器以增加動力](https://file.elecfans.com/web2/M00/92/E0/pYYBAGPyzHGALBgoAAJ3btmzOFY183.jpg)
霍爾開關在跳繩中如何應用
![霍爾開關在<b class='flag-5'>跳繩</b>中如何應用](https://file1.elecfans.com/web2/M00/8C/35/wKgZomSnt6KAM3LrAAAfFWKBlBY783.png)
智能跳繩藍牙芯片MS1656解決方案
![智能<b class='flag-5'>跳繩</b>藍牙芯片MS1656解決方案](https://file.elecfans.com/web2/M00/0F/39/poYBAGESJy-ADDmLAAAsPLLd5bg785.jpg)
智能跳繩的產品體驗與思考(一)
![智能<b class='flag-5'>跳繩</b>的產品體驗與思考(一)](https://file1.elecfans.com/web2/M00/EA/0F/wKgZomZWm-uASNW-AABkctMjakw864.png)
智能跳繩的產品體驗與思考(二) 離線智能跳繩是如何設計的
![智能<b class='flag-5'>跳繩</b>的產品體驗與思考(二) 離線智能<b class='flag-5'>跳繩</b>是如何設計的](https://file1.elecfans.com/web2/M00/EA/0F/wKgZomZWm-uASNW-AABkctMjakw864.png)
評論