カラーセンサーS11059-02DT

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smbus
import time
from datetime import datetime
from time import sleep
import RPi.GPIO as GPIO

def sheetwrite(p_distance):
import json
import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope=['https://spreadsheets.google.com/feeds']
credentials=ServiceAccountCredentials.from_json_keyfile_name('project-7766a0463656.json',scope)
gc=gspread.authorize(credentials)
wks=gc.open_by_key('1GqimZ714elIPRg_QHXSeWiObzr9W-f5g0BMv0Ts43cM').sheet1

#各セルにアクセス
#print (wks.acell('A1'))
#print (wks.acell('B2'))

#書き換え
wks.update_acell('B3',p_distance)
print (wks.acell('B3'))



def S11059_Sensor():
bus = smbus.SMBus(1)
addr = 0x2a
param=[0x0b,0x0a,0x09,0x08,0x03,0x02,0x01,0x00]

bus.write_byte_data(addr , 0x00, 0x80)
bus.write_byte_data(addr , 0x00, param[0] )
time.sleep(1)
data = bus.read_i2c_block_data(addr , 0x03, 8)
R = data[0] *256 + data[1]
return R

try:
while True:
p_light_R=S11059_Sensor()
if p_light_R >= 10000:
sheetwrite(datetime.now().strftime("%Y/%m/%d %H:%N:%S"))

print p_light_R
sleep(10)
except KeyboardInterrupt:
pass