|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# Google Spreadsheet BMP Sensor Data-logging Example |
| 4 | + |
| 5 | +# Depends on the 'gspread' package being installed. If you have pip installed |
| 6 | +# execute: |
| 7 | +# sudo pip install gspread |
| 8 | + |
| 9 | +# Copyright (c) 2014 Adafruit Industries |
| 10 | +# Author: Tony DiCola |
| 11 | + |
| 12 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 | +# of this software and associated documentation files (the "Software"), to deal |
| 14 | +# in the Software without restriction, including without limitation the rights |
| 15 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 16 | +# copies of the Software, and to permit persons to whom the Software is |
| 17 | +# furnished to do so, subject to the following conditions: |
| 18 | + |
| 19 | +# The above copyright notice and this permission notice shall be included in all |
| 20 | +# copies or substantial portions of the Software. |
| 21 | + |
| 22 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 28 | +# SOFTWARE. |
| 29 | +import sys |
| 30 | +import time |
| 31 | +import datetime |
| 32 | + |
| 33 | +import Adafruit_BMP.BMP085 as BMP085 |
| 34 | +import gspread |
| 35 | + |
| 36 | +# Google Docs account email, password, and spreadsheet name. |
| 37 | +GDOCS_EMAIL = 'your google docs account email address' |
| 38 | +GDOCS_PASSWORD = 'your google docs account password' |
| 39 | +GDOCS_SPREADSHEET_NAME = 'your google docs spreadsheet name' |
| 40 | + |
| 41 | +# How long to wait (in seconds) between measurements. |
| 42 | +FREQUENCY_SECONDS = 30 |
| 43 | + |
| 44 | + |
| 45 | +def login_open_sheet(email, password, spreadsheet): |
| 46 | + """Connect to Google Docs spreadsheet and return the first worksheet.""" |
| 47 | + try: |
| 48 | + gc = gspread.login(email, password) |
| 49 | + worksheet = gc.open(spreadsheet).sheet1 |
| 50 | + return worksheet |
| 51 | + except: |
| 52 | + print 'Unable to login and get spreadsheet. Check email, password, spreadsheet name.' |
| 53 | + sys.exit(1) |
| 54 | + |
| 55 | + |
| 56 | +# Create sensor instance with default I2C bus (On Raspberry Pi either 0 or |
| 57 | +# 1 based on the revision, on Beaglebone Black default to 1). |
| 58 | +bmp = BMP085.BMP085() |
| 59 | + |
| 60 | +print 'Logging sensor measurements to {0} every {1} seconds.'.format(GDOCS_SPREADSHEET_NAME, FREQUENCY_SECONDS) |
| 61 | +print 'Press Ctrl-C to quit.' |
| 62 | +worksheet = None |
| 63 | +while True: |
| 64 | + # Login if necessary. |
| 65 | + if worksheet is None: |
| 66 | + worksheet = login_open_sheet(GDOCS_EMAIL, GDOCS_PASSWORD, GDOCS_SPREADSHEET_NAME) |
| 67 | + |
| 68 | + # Attempt to get sensor readings. |
| 69 | + temp = bmp.read_temperature() |
| 70 | + pressure = bmp.read_pressure() |
| 71 | + altitude = bmp.read_altitude() |
| 72 | + |
| 73 | + print 'Temperature: {0:0.1f} C'.format(temp) |
| 74 | + print 'Pressure: {0:0.1f} Pa'.format(pressure) |
| 75 | + print 'Altitude: {0:0.1f} m'.format(altitude) |
| 76 | + |
| 77 | + # Append the data in the spreadsheet, including a timestamp |
| 78 | + try: |
| 79 | + worksheet.append_row((datetime.datetime.now(), temp, pressure, altitude)) |
| 80 | + except: |
| 81 | + # Error appending data, most likely because credentials are stale. |
| 82 | + # Null out the worksheet so a login is performed at the top of the loop. |
| 83 | + print 'Append error, logging in again' |
| 84 | + worksheet = None |
| 85 | + time.sleep(FREQUENCY_SECONDS) |
| 86 | + continue |
| 87 | + |
| 88 | + # Wait 30 seconds before continuing |
| 89 | + print 'Wrote a row to {0}'.format(GDOCS_SPREADSHEET_NAME) |
| 90 | + time.sleep(FREQUENCY_SECONDS) |
0 commit comments