The next project that I wanted to work on was actually a very old project – but with one important change. I wanted to use a Pi with a Sense Hat attached to track the temperature in my shed, as well as other features of the environment. The Sense Hat is perfect for this kind of application, as much of it is built into the hat, and it also has a great visual representation via the 8 x 8 panel too, for in person inspection.
In the past, I’d had no trouble setting up a monitoring system, using simply Python scripts to read the temperature etc from the sense hat and printing it to the terminal. However, I wanted to be a little more ambitious, and not just print the results to the screen, I also wanted to make sure that they were
Current Script:
import gspread from oauth2client.service_account import ServiceAccountCredentials from pprint import pprint import datetime import time from sense_hat import SenseHat sense = SenseHat() sense.clear() while True: temp = sense.get_temperature() print(temp) humidity = sense.get_humidity() print(humidity) pressure = sense.get_pressure() print(pressure) scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/sp$ creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope) client = gspread.authorize(creds) sheet = client.open("MiniWeatherStation Log").sheet1 # Open the spreadhseet data = sheet.get_all_records() # Get a list of all records # next row id number is length of data plus 1 nextId = len(data) + 1 # next row to insert that data is 1 more than the id due to the header nextInsertRowNum = nextId + 1 # current timestamp now = datetime.datetime.now() current_time = now.strftime("%H:%M:%S") current_date = now.strftime("%Y:%m:%d") # json data prep for insert insertRowData = [nextId, current_time, current_date, temp, humidity, pressure] # insert the json into the nextInsertRowNum sheet.insert_row(insertRowData, nextInsertRowNum) time.sleep(60)