Gdata spreadsheet on beaglebone

The target is to use gdata service to upload data from beaglebone to a existing google drive spreadsheet...
since beaglebone has already comes with python 2.7, this will be easier to do it.
AlL my reference comes from:
eduardo simioni

first following the installation guide here: https://developers.google.com/gdata/articles/python_client_lib

down load the gdata here : https://code.google.com/p/gdata-python-client/downloads/list

Find the path of down load file, then use wget, example:
wget https://gdata-python-client.googlecode.com/files/gdata-2.0.18.tar.gz

you can also down load to PC and the transfer to beaglebone with SCP.
untar the file with :
tar -zxvf yourfile.tar.gz

go into the gdata fold:
cd gdata-2.0.18

then run :
sudo python setup.py install

wait for the installation.

for testing, change to /tests/ and try:
python run_data_tests.py

it should process a serie of testing to see if there is any fault..

The other example for testing is to go to /samples/docs/
and run python docs_example.py
the program will as you to input gmail account(ex: name@gmail.com) and password, then come out with 7 options, you can try to list all docs in your account to see if the access are correct.

Now we do a test to see if things can be written into it:The following example is writing date, time and a constant weight

#!/usr/bin/python
# writing stuff to target file

import time
import gdata.spreadsheet.service

email = 'beaglelds@gmail.com'
password = 'clotechxxx'
weight = '256'
# Find this value in the url with 'key=XXX' and copy XXX below
spreadsheet_key = '0AsJuzOWZPoeKdFNUUFk0aUxMUGtHWWd2TXlfdmVLR3c'
# All spreadsheets have worksheets. I think worksheet #1 by default always
# has a value of 'od6'
worksheet_id = 'od6'

spr_client = gdata.spreadsheet.service.SpreadsheetsService()
spr_client.email = email
spr_client.password = password
spr_client.source = 'Example Spreadsheet Writing Application'
spr_client.ProgrammaticLogin()

# Prepare the dictionary to write
dict = {}
dict['date'] = time.strftime('%m/%d/%Y')
dict['time'] = time.strftime('%H:%M:%S')
dict['weight'] = weight
print dict

entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
print "Insert row succeeded."
else:
print "Insert row failed."


few things to note:
1. the email and password are the one you use to logging google drive(normally the ones you use for gmail)
2. every spreadsheet on google drive has a key, when you open the doc, on the web address, everything between "="and "#" is the key.I'm not quite sure why the page ID is "od6"....
3. spr_client.source seems to be the name of the spreadsheet
4. the writing is based on dict, and the first row in the spreadsheet should bear the dict, in this case date, time ,weight

once you execute this with python writing2.py it would write the dict in the spread sheet and return the result in terminal


Update 2014 05 20
sometimes network is not good, it could have trouble to access gdata server and cause the service to stoped.
Like this:
socket.error: [Errno 101] Network is unreachable



Mr. Trevor Johns provide a wat to avoid it.

my example:

originally I do this:
entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
            if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
                print "Insert row succeeded."
            else:
                print "Insert row failed."

then I added:

try:
    entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
            if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
                print "Insert row succeeded."
            else:
                print "Insert row failed."

it comes out as error, then I added except

       try:
            entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
            if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
                print "Insert row succeeded."
            else:
                print "Insert row failed."
        except:
            print "error network"

then it works...even I unplug the ethernet, it still fill up all lag when network is back.

留言

  1. the data type need to be char, so if you have int value, need to use str(value) to convert to string

    回覆刪除

張貼留言

這個網誌中的熱門文章

Heltec ESP32+OLED+Lora, hardware testing

micro SD card for ESP32, on lolin32 with OLED and heltec 32 lora oled

Install Network Time Protocol(NTP) on BeagleBone with Angstrom linux and set local time zone