无法将 raspberry pi 传感器数据上传到 Google 电子表格
Unable to upload raspberry pi sensor data to a Google Spreadsheet
我正在做一个 raspberry pi 项目,我想每 30 秒将传感器数据发送到 google 电子表格。但我得到输出 'failed'。似乎数据没有附加到电子表格。我找不到确切的问题。另外,我希望程序能够保存数据 headers,例如日期、时间、温度、湿度等
这是我正在使用的代码。有人可以帮忙吗? :)
import csv
import os
import time
from time import sleep
import datetime
import json
import sys
import gspread
from oauth2client.service_account import ServiceAccountCredentials
GDOCS_OAUTH_JSON = '***.json'
GDOCS_SPREADSHEET_NAME = '***data'
FREQUENCY_SECONDS = 30
def login_open_sheet(oauth_key_file,spreadsheet):
try:
scope =
['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(oauth_key_file,scope)
gc=gspread.authorize(credentials)
worksheet=gc.open(spreadsheet).sheet1
return worksheet
except Exception as ex:
print ('Unable to login and get spreadsheet')
print('Google sheet login failes with error',ex)
sys.exit(1)
def main():
worksheet = None
ipcon = IPConnection() # Create IP connection
t = BrickletTemperatureV2(UID_tem, ipcon) # Create device object
h = BrickletHumidityV2(UID_hum, ipcon)
co2 = BrickletCO2V2(UID_co2, ipcon)
aq = BrickletAirQuality(UID_aq, ipcon)
ptc = BrickletIndustrialPTC(UID_iptc, ipcon)
ipcon.connect(HOST, PORT)
while True:
if worksheet is None:
worksheet=login_open_sheet(GDOCS_OAUTH_JSON,GDOCS_SPREADSHEET_NAME)
daten= datetime.datetime.now().strftime("%Y/%m/%d")
timen= datetime.datetime.now().strftime("%H:%M:%S")
temp = t.get_temperature() / 100.0
hum = h.get_humidity() / 100.0
co2con= co2.get_co2_concentration()
iaq = aq.get_iaq_index()[0]
ptctemp = ptc.get_temperature() / 100.0
try:
worksheet.append_row(daten,timen,temp,strhum,co2con,iaq,ptctemp)
except:
print('failed')
worksheet = None
time.sleep(FREQUENCY_SECONDS)
continue
time.sleep(FREQUENCY_SECONDS)
ipcon.disconnect()
if __name__ == "__main__":
main()
从你I removed try except and getting this error now. TypeError: append_row() takes from 2 to 6 positional arguments but 8 were given
的错误信息来看,错误似乎发生在worksheet.append_row(daten,timen,temp,strhum,co2con,iaq,ptctemp)
。
从你的脚本来看,我认为有2个修改点。
好像append_row
的参数是append_row(values, value_input_option='RAW', insert_data_option=None, table_range=None)
.
在您的脚本中,strhum
未声明。这样,即使 append_row
有效,也会为此发生错误。
当这些点反映到你的脚本中,就变成了下面这样。
发件人:
worksheet.append_row(daten,timen,temp,strhum,co2con,iaq,ptctemp)
收件人:
strhum = "###" # Please declare this variable.
worksheet.append_row([daten, timen, temp, strhum, co2con, iaq, ptctemp], value_input_option="USER_ENTERED")
参考:
我正在做一个 raspberry pi 项目,我想每 30 秒将传感器数据发送到 google 电子表格。但我得到输出 'failed'。似乎数据没有附加到电子表格。我找不到确切的问题。另外,我希望程序能够保存数据 headers,例如日期、时间、温度、湿度等
这是我正在使用的代码。有人可以帮忙吗? :)
import csv
import os
import time
from time import sleep
import datetime
import json
import sys
import gspread
from oauth2client.service_account import ServiceAccountCredentials
GDOCS_OAUTH_JSON = '***.json'
GDOCS_SPREADSHEET_NAME = '***data'
FREQUENCY_SECONDS = 30
def login_open_sheet(oauth_key_file,spreadsheet):
try:
scope =
['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(oauth_key_file,scope)
gc=gspread.authorize(credentials)
worksheet=gc.open(spreadsheet).sheet1
return worksheet
except Exception as ex:
print ('Unable to login and get spreadsheet')
print('Google sheet login failes with error',ex)
sys.exit(1)
def main():
worksheet = None
ipcon = IPConnection() # Create IP connection
t = BrickletTemperatureV2(UID_tem, ipcon) # Create device object
h = BrickletHumidityV2(UID_hum, ipcon)
co2 = BrickletCO2V2(UID_co2, ipcon)
aq = BrickletAirQuality(UID_aq, ipcon)
ptc = BrickletIndustrialPTC(UID_iptc, ipcon)
ipcon.connect(HOST, PORT)
while True:
if worksheet is None:
worksheet=login_open_sheet(GDOCS_OAUTH_JSON,GDOCS_SPREADSHEET_NAME)
daten= datetime.datetime.now().strftime("%Y/%m/%d")
timen= datetime.datetime.now().strftime("%H:%M:%S")
temp = t.get_temperature() / 100.0
hum = h.get_humidity() / 100.0
co2con= co2.get_co2_concentration()
iaq = aq.get_iaq_index()[0]
ptctemp = ptc.get_temperature() / 100.0
try:
worksheet.append_row(daten,timen,temp,strhum,co2con,iaq,ptctemp)
except:
print('failed')
worksheet = None
time.sleep(FREQUENCY_SECONDS)
continue
time.sleep(FREQUENCY_SECONDS)
ipcon.disconnect()
if __name__ == "__main__":
main()
从你I removed try except and getting this error now. TypeError: append_row() takes from 2 to 6 positional arguments but 8 were given
的错误信息来看,错误似乎发生在worksheet.append_row(daten,timen,temp,strhum,co2con,iaq,ptctemp)
。
从你的脚本来看,我认为有2个修改点。
好像
append_row
的参数是append_row(values, value_input_option='RAW', insert_data_option=None, table_range=None)
.在您的脚本中,
strhum
未声明。这样,即使append_row
有效,也会为此发生错误。
当这些点反映到你的脚本中,就变成了下面这样。
发件人:
worksheet.append_row(daten,timen,temp,strhum,co2con,iaq,ptctemp)
收件人:
strhum = "###" # Please declare this variable.
worksheet.append_row([daten, timen, temp, strhum, co2con, iaq, ptctemp], value_input_option="USER_ENTERED")