I believe I was able to solve the answer to this issue by using the
matrixportal class to make all the requests on my behalf.
I didn't realize that by CircuitPython v7.x, the matrixportal library included a
matrixportal.push-to-io(FEED-NAME, VALUE) method that could be used to push the data AND using
matrixportal.network.fetch_data(URL) could allow for fetching weather data as well.
I like this solution because it involved passing the
esp class directly into the MatrixPortal class and since it is already the mediator between so much on the board - having it all in a super class like this works better than having to instantiate more requests classes.
Here's ultimately what I ended up with:
I think it's important to note that I didn't pass any of the credentials for the Adafruit IO to the
matrixportal class whatsoever, but it is picking up the tokens necessary to post the data. I believe that somewhere it is picking this up internally by checking the
secrets.py and looking for the specific keywords as defined here:
https://learn.adafruit.com/electronic-h ... secrets-py
Code: Select all
#!/usr/bin/env python3
# Get temperature from sensor and temperate from OpenWeather and display it on the MatrixPortal
import time
import board
import terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal
from busio import I2C
import adafruit_bme680
try:
from secrets import secrets
LATITUDE = secrets['latitude']
LONGITUDE = secrets['longitude']
OPENWEATHER_TOKEN = secrets['openweather_token']
OPENWEATHER_UNITS = secrets['openweather_units']
except ImportError:
print("WiFi secrets, Openweather API Tokens, and Latitude/Longitude values are kept in secrets.py, please add them!")
raise
# --- Display setup ---
matrixportal = MatrixPortal(
status_neopixel=board.NEOPIXEL
)
NEXT_SYNC = 0
# Create a static label
# "IN"
matrixportal.add_text(
text_font=terminalio.FONT,
text_position=(2, 4),
text_scale=1
)
# "OUT"
matrixportal.add_text(
text_font=terminalio.FONT,
text_position=(35, 4),
text_scale=1
)
# "F" for degrees for INDOORS
matrixportal.add_text(
text_font=terminalio.FONT,
text_position=(26, 16),
text_scale=1
)
# "F" for degrees for OUTDOORS
matrixportal.add_text(
text_font=terminalio.FONT,
text_position=(59, 16),
text_scale=1
)
# Dynamic labels
# Temperature INDOORS
matrixportal.add_text(
text_font=terminalio.FONT,
text_position=(2, 18),
text_scale=2
)
# Temperature OUTDOORS
matrixportal.add_text(
text_font=terminalio.FONT,
text_position=(35, 18),
text_scale=2
)
i2c = I2C(board.SCL, board.SDA)
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c)
def sensor_data_stringified(bme680):
temperature = "%0.0f" % ((bme680.temperature * 1.8) + 32)
return "{}".format(temperature)
def callWeatherAPI(token, lat, lng, units):
DATA_SOURCE = 'https://api.openweathermap.org/data/2.5/onecall?units={}&lat={}&lon={}&appid={}&exclude=minutely,hourly'.format(
units,
lat,
lng,
token
)
print(DATA_SOURCE)
value = matrixportal.network.fetch_data(DATA_SOURCE, json_path=['current', 'temp'])
return value
def parseForTemperature(weatherData):
return int(weatherData[0])
outdoor_temp = '??'
indoor_temp = '??'
while True:
NOW = time.time() # Current epoch time in seconds
matrixportal.set_text("IN", 0)
matrixportal.set_text("OUT", 1)
matrixportal.set_text("F", 2)
matrixportal.set_text("F", 3)
matrixportal.set_text(indoor_temp, 4)
matrixportal.set_text(outdoor_temp, 5)
if NOW > NEXT_SYNC:
NEXT_SYNC = NOW + (60 * 60) # Network call every hour
value = callWeatherAPI(OPENWEATHER_TOKEN, LATITUDE, LONGITUDE, OPENWEATHER_UNITS)
outdoor_temp = parseForTemperature(value)
indoor_temp = sensor_data_stringified(bme680)
matrixportal.push_to_io("outdoor-temp", outdoor_temp)
matrixportal.push_to_io("indoor-temp-sensor", indoor_temp)
time.sleep(60)