Loop keeps populating old data - Raspberry Pi Forums
i posed question on reddit got no response. have python script gets data via serial rs232 , prints out. code works well, updating @ rapid pace.
when put google script populate data read on first pass, on , on again. doing wrong?
code: select all
import serial port = serial.serial("/dev/ttyusb0", baudrate=9600, timeout=3.0) def filewrite(rcv): logfile = open("templog.txt", "a") logfile.write(rcv) logfile.close while true: rcv = port.readline() print("received: " + repr(rcv))
code: select all
#!/usr/bin/python # software. import sys import time import datetime import serial import adafruit_dht import gspread port = serial.serial("/dev/ttyusb0", baudrate=9600, timeout=3.0) # google docs account email, password, , spreadsheet name. gdocs_email = '@gmail.com' gdocs_password = 'password' gdocs_spreadsheet_name = 'weight' # how long wait (in seconds) between measurements. frequency_seconds = 15 def filewrite(rcv): logfile = open("templog.txt", "a") logfile.write(rcv) logfile.close def login_open_sheet(email, password, spreadsheet): """connect google docs spreadsheet , return first worksheet.""" try: gc = gspread.login(email, password) worksheet = gc.open(spreadsheet).sheet1 return worksheet except: print 'unable login , spreadsheet. check email, password, spreadsheet name.' sys.exit(1) print 'logging sensor measurements {0} every {1} seconds.'.format(gdocs_spreadsheet_name, frequency_seconds) print 'press ctrl-c quit.' worksheet = none while true: # login if necessary. if worksheet none: worksheet = login_open_sheet(gdocs_email, gdocs_password, gdocs_spreadsheet_name) # attempt sensor reading. rcv = port.readline() # skip next reading if valid measurement couldn't taken. # might happen if cpu under lot of load , sensor # can't reliably read (timing critical read sensor). print("received: " + repr(rcv)) str = (rcv) lb = (str[7:13]) print(lb) # append data in spreadsheet, including timestamp try: worksheet.append_row((datetime.datetime.now(), lb)) except: # error appending data, because credentials stale. # null out worksheet login performed @ top of loop. print 'append error, logging in again' worksheet = none time.sleep(frequency_seconds) continue # wait 30 seconds before continuing print 'wrote row {0}'.format(gdocs_spreadsheet_name) time.sleep(frequency_seconds)
does ``print(bl)`` line expect? timestamp in spreadsheet correct?
filewrite() anything? debugging?
finally, don't know if it's relevant here it's bad idea use reserved words such str variable names , in python putting things inside brackets standard way create tuple it's potentially confusing put expressions inside brackets no reason.
filewrite() anything? debugging?
finally, don't know if it's relevant here it's bad idea use reserved words such str variable names , in python putting things inside brackets standard way create tuple it's potentially confusing put expressions inside brackets no reason.
raspberrypi
Comments
Post a Comment