# Python 3 program to access OpenWeatherMap.org (OWM) via custom API # Store time/temperature data to Google Sheets via custom APIs # Read statistical summary cell data from Google Sheets # Blink LEDs as needed # ref.: https://pyowm.readthedocs.io/en/latest/v3/code-recipes.html # ref.: OWM data at http://bulk.openweathermap.org/sample/ # ref.: https://chem.libretexts.org/Courses/Intercollegiate_Courses/... # ...Internet_of_Science_Things_(2020)/1%3A_IOST_Modules/ # ...1.6%3A_Writing_to_Google_Sheets # Mr. C. 9-19-2020 # ***************************************************************** # city.list.json.gz (see ref. above) file: 05-Aug-2020 07:57 # "id": 4081671, "name": "Oneonta", "state": "AL", "country": "US", # "coord": {"lon": -86.472763,"lat": 33.948151} # "id": 4830707, "name": "Ashville", "state": "AL", "country": "US", # "coord": {"lon": -86.254417, "lat": 33.83704} # ***************************************************************** # JSON file w/Google Credentials in local folder: rpiowmdata.json # ***************************************************************** # Libraries from gpiozero import LED from pyowm.owm import OWM #OpenWeatherMap Library import datetime, time import gspread #Google Sheets Library from oauth2client.service_account import ServiceAccountCredentials owm = OWM('xxxyour api herexxx') # Mr. C.'s OWM API Key mgr = owm.weather_manager() observation = mgr.weather_at_place('Oneonta,AL,US') #observation has all data weather = observation.weather temp_dict_fahrenheit = weather.temperature('fahrenheit') temp_now_oneonta = temp_dict_fahrenheit['temp'] scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'] # Use custom JSON file downloaded from Google API Manager in next line creds = ServiceAccountCredentials.from_json_keyfile_name('rpiowmdata.json', scope) client = gspread.authorize(creds) # Use name of Google Sheets file used to store data in next line sheet = client.open("RPi_OWM_GS").sheet1 print('initial conditions at Oneonta, AL') print(weather.status) print(weather.detailed_status) print(temp_dict_fahrenheit) print(temp_dict_fahrenheit['temp']) rled = LED(25) # red led yled = LED(24) # yellow led gled = LED(23) # green led # function used to append temperature data to Google Sheets file def append_sheet(data): time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') print("*"*80) values =[time,data] print(values) sheet.append_row(values) std_dev_val = sheet.acell('D24').value mean_val = sheet.acell('E24').value med_val = sheet.acell('F24').value q1_val = sheet.acell('G24').value q3_val = sheet.acell('H24').value min_val = sheet.acell('I24').value max_val = sheet.acell('J24').value print("Google Sheet Updated in Function") print("*"*80) print('Standard Deviation') print(std_dev_val) print('Mean') print(mean_val) print('Median') print(med_val) print('Quartile 1') print(q1_val) print('Quartile 3') print(q3_val) print('Minimum') print(min_val) print('Maximum') print(max_val) while True: if temp_now_oneonta >= 90.00: yled.off() gled.off() rled.on() print('temp high ', temp_dict_fahrenheit['temp'], ' deg. F') time.sleep(15) elif temp_now_oneonta >= 70.00 and temp_now_oneonta < 90.00: rled.off() gled.off() yled.on() print('temp medium ', temp_dict_fahrenheit['temp'], ' deg. F') time.sleep(15) elif temp_now_oneonta < 70.00: rled.off() yled.off() gled.on() print('temp low ', temp_dict_fahrenheit['temp'], ' deg. F') time.sleep(15) owm = OWM('xxxyour api herexxx') # Mr. C.'s OWM API Key mgr = owm.weather_manager() observation = mgr.weather_at_place('Oneonta,AL,US') #observation has all data weather = observation.weather temp_dict_fahrenheit = weather.temperature('fahrenheit') temp_now_oneonta = temp_dict_fahrenheit['temp'] time.sleep(5) client.login() # login to Google Sheets append_sheet(temp_now_oneonta) # append data to Google Sheets print("Google Sheet Updated") print('updated conditions at Oneonta, AL') print(weather.status) print(weather.detailed_status) print(temp_dict_fahrenheit) print(temp_dict_fahrenheit['temp']) continue