""" Goal: Open a U3 and log ADC values to a google spreadsheet Dependencies: Google's python API library: link as of 2/8/2013: http://code.google.com/p/gdata-python-client/ labjackPython: link as of 2/8/2013: http://labjack.com/support/labjackpython General Description: 1. Log's into google docs & opens a spreadsheet 2. Opens a device 3. Collects U3 data using modbus commands 4. Saves information to a google doc's spreadsheet 5. Closes a device Author: Chris Johnson Company: www.LabJack.com """ #general imports from google's spreadsheetExample.py program try: from xml.etree import ElementTree except ImportError: from elementtree import ElementTree import gdata.spreadsheet.service import gdata.service import atom.service import gdata.spreadsheet import atom import getopt import sys import string #LabJack's import's: import u3 #import time to allow for getting & saving a timestamp with the acquired data to google docs import time SPREADSHEET_NAME = "LabJackU3Example" WORKSHEET_NAME = "loggedData" GOOGLE_USER_NAME = "EnterYourUserNameHere" GOOGLE_PASSWORD = "EnterYourPasswordHere" class googleAPIandU3Example(object): def __init__(self, username, password, DEBUG = True): self.gd_client = gdata.spreadsheet.service.SpreadsheetsService() self.gd_client.email = username self.gd_client.password = password self.gd_client.ProgrammaticLogin() self.curr_key = '' self.curr_wksht_id = '' self.list_feed = None self.spreadsheet_feed = None self.DEBUG = DEBUG self.spreadSheetExists = False self.spreadSheetIndex = None self.worksheetExists = False self.worksheetIndex = None self.LabJack = None """ Name: _FindSpreadsheet() Description: Qweries google doc's for a list of spreadsheet's & if the defined spreadsheet exists it saves the spreadsheet key allowing it to be modified later. if the spreadsheet doesn't exist than the program exit's "sys.exit(2)" """ def _FindSpreadsheet(self): # Get the list of spreadsheets feed = self.gd_client.GetSpreadsheetsFeed() #search the feed to see if defined spreadsheet exists self._SearchForSheet(feed) if(self.spreadSheetExists): #if the spreadsheet exists then get its key id_parts = feed.entry[self.spreadSheetIndex].id.text.split('/') self.curr_key = id_parts[len(id_parts)-1] if self.DEBUG: print "your spreadsheet it exists!" print "selected spreadsheet's index in array: " + str(self.spreadSheetIndex) print "selected spreadsheet's key: " + str(self.curr_key) + "\n" else: #exit program if spreadsheet doesn't exist print "spreadsheet doesn't exist, change SPREADSHEET_NAME or create a spreadsheet in google drive named: " + SPREADSHEET_NAME +"\n" sys.exit(2) """ Name: _SearchForSheet() Description: Searches through the feed for a spreadsheet with the name SPREADSHEET_NAME, if it exists it updates the index & true/false status of the spreadsheet's existance """ def _SearchForSheet(self,feed): #step through entire feed list and search for the spreadsheet for i, entry in enumerate(feed.entry): #checks to see if feed is a cellfeed (from google's API example) if isinstance(feed, gdata.spreadsheet.SpreadsheetsCellsFeed): print '%s %s\n' % (entry.title.text, entry.content.text) #checks to see if feed is a list feed (from google's API example) elif isinstance(feed, gdata.spreadsheet.SpreadsheetsListFeed): print '%s %s %s' % (i, entry.title.text, entry.content.text) #what actually happens when function is used properly else: if( entry.title.text == SPREADSHEET_NAME): #if the spreadsheet's name is found than set parameters self.spreadSheetIndex = i self.spreadSheetExists = True """ Name: _FindWorksheet() Description: Qweries google drive for a list of worksheet's in the opened spreadsheet & searches the array for the defined worksheet name. If it exists it saves the worksheet's id if the worksheet doesn't exist than the program exit's "sys.exit(2)" """ def _FindWorksheet(self): # Get the list of worksheets feed = self.gd_client.GetWorksheetsFeed(self.curr_key) #search the feed to see if defined worksheet exists self._SearchForWorksheet(feed) if(self.worksheetExists): #if the worksheet exists then get its key id_parts = feed.entry[self.worksheetIndex].id.text.split('/') self.curr_wksht_id = id_parts[len(id_parts) - 1] if self.DEBUG: print "the worksheet exists!" print "location of selected worksheet in array: " + str(self.worksheetIndex) print "worksheet's id: " +str(self.curr_wksht_id) + "\n" else: print "the worksheet doesn't exist, change WORKSHEET_NAME or create a worksheet in: " + SPREADSHEET_NAME + " titled: " +WORKSHEET_NAME sys.exit(2) """ Name: _SearchForWorksheet() Description: searches the feed generated by google drive qwery for the defined worksheet name it is almost identical to _SearchForSheet but it updates values for worksheet info. """ def _SearchForWorksheet(self,feed): #step through entire feed list and search for the spreadsheet for i, entry in enumerate(feed.entry): #checks to see if feed is a cellfeed (from google's API example) if isinstance(feed, gdata.spreadsheet.SpreadsheetsCellsFeed): print '%s %s\n' % (entry.title.text, entry.content.text) #checks to see if feed is a list feed (from google's API example) elif isinstance(feed, gdata.spreadsheet.SpreadsheetsListFeed): print '%s %s %s' % (i, entry.title.text, entry.content.text) #what actually happens when function is used properly else: if( entry.title.text == WORKSHEET_NAME): #if the spreadsheet's name is found than set parameters self.worksheetIndex = i self.worksheetExists = True """ Name: _ReadAndLogData(channelNumber, rowNumber) Description: read's a labjack device's analog input (channelNumber) and saves the information along with what time the data was sampled at to a google doc's spreadsheet starting at row (rowNumber) """ def _ReadAndLogAINData(self, channelNumber, rowNumber): #collect AIN reading & round it to 4 decimal places value = round(self.LabJack.readRegister(channelNumber * 2),4) #acquire current system time & build string to be saved to spreadsheet currentTime = time.localtime(time.time()) timeStamp = str(currentTime.tm_mon)+"-"+str(currentTime.tm_mday)+"-"+str(currentTime.tm_year)+" "+str(currentTime.tm_hour) + ":" + str(currentTime.tm_min) + ":" + str(currentTime.tm_sec) #save time stamp to spreadsheet at rowNumber, col 1, uses saved spreadsheet & worksheet keys entry = self.gd_client.UpdateCell(row = rowNumber, col = 1, inputValue=timeStamp, key=self.curr_key, wksht_id=self.curr_wksht_id) #check to make sure that it worked properly if isinstance(entry, gdata.spreadsheet.SpreadsheetsCell): print 'Updated time' #save AINx channel info to spreadsheet at rowNumber, col 1, uses saved spreadsheet & worksheet keys entry = self.gd_client.UpdateCell(row = rowNumber, col = 2, inputValue=("AIN"+str(channelNumber)), key=self.curr_key, wksht_id=self.curr_wksht_id) #check to make sure that it worked properly if isinstance(entry, gdata.spreadsheet.SpreadsheetsCell): print 'Updated Channel Info' ##save analog reading to spreadsheet at rowNumber, col 1, uses saved spreadsheet & worksheet keys entry = self.gd_client.UpdateCell(row = rowNumber, col = 3, inputValue=str(value), key=self.curr_key, wksht_id=self.curr_wksht_id) #check to make sure that it worked properly if isinstance(entry, gdata.spreadsheet.SpreadsheetsCell): print 'Updated value' #print out debugging information if enabled if self.DEBUG: print "values logged:" print "channel: " +str(channelNumber) + " sampled at: " + timeStamp + " was: " +str(value) + "\n" """ Name: _OpenLabJack() Description: open's a labjack U3 device """ def _OpenLabJack(self): #opens labjack u3 device self.LabJack = u3.U3() """ Name: _CloseLabJack() Description: closes a labjack U3 device """ def _CloseLabJack(self): #closes the device self.LabJack.close() def Run(self): #finds spreadsheet & exits if it doesn't exist self._FindSpreadsheet() #finds worksheet & exits if it doesn't exist self._FindWorksheet() #open's LabJack self._OpenLabJack() #read & log data (ADC channel number, row in spreadsheet) self._ReadAndLogAINData(1, 1) self._ReadAndLogAINData(2, 2) #close LabJack self._CloseLabJack() def main(): sample = googleAPIandU3Example(GOOGLE_USER_NAME, GOOGLE_PASSWORD, DEBUG = False) sample.Run() if __name__ == '__main__': main()