Library suggestion for #CircuitPython2022
Re: Library suggestion for #CircuitPython2022
Re: Library suggestion for #CircuitPython2022
Re: Library suggestion for #CircuitPython2022
kipd wrote:Did you look at the "secrets.py" file (located at the root of your CircuitPython drive)? In the code examples I am working on (https://learn.adafruit.com/magtag-weather)
the WiFi settings are set there ('ssid' and 'password') and your code just needs to add ("from secrets import secrets").
secrets = {
'ssid' : 'Example_SSID',
'password' : 'Example_PASSWORD',
}
Re: Library suggestion for #CircuitPython2022
"""Gets and displays JSON spreadsheet from Google."""
import time
import rtc
import terminalio
from adafruit_display_shapes.rect import Rect
from adafruit_magtag.magtag import MagTag
from secrets import secrets #for openweather, sheets, time.
# CONFIGURABLE SETTINGS and ONE-TIME INITIALIZATION ------------------------
TESTING = False #for debug. Otherwise sleep until 3:15AM
MsgList = " "
NEED_TO_CHARGE = 3.4 #battery voltage below this indicates time to charge
# MagTag will stop operating below ~ 3.0 volts.
JSON_URL = secrets["json_url"] #link to published Google sheet.
MAGTAG = MagTag(rotation=270) # Portrait (vertical) display
# GRAPHICS INITIALIZATION --------------------------------------------------
MAGTAG.add_text(
text_font= terminalio.FONT,
text_position=(0, 0),
line_spacing=1.0,
text_color=0x000000,
text_anchor_point=(0, 0), # Top left
is_data=False,
)
# Black bar at bottom of display is a distinct layer not just background
MAGTAG.graphics.splash.append(Rect(0, MAGTAG.graphics.display.height - 13,
MAGTAG.graphics.display.width,
MAGTAG.graphics.display.height, fill=0x0))
# Center white text over black bar. show last update time & battery info.
MAGTAG.add_text(
text_font='/fonts/helvB12.pcf',
text_position=(MAGTAG.graphics.display.width // 2,
MAGTAG.graphics.display.height - 5),
text_color=0xFFFFFF,
text_anchor_point=(0.5, 0.7), # Center bottom
is_data=False, # Text will be set manually
)
#Battery voltage and admonition to charge if voltage < NEED_TO_CHARGE
BATT = "{:.1f}".format(MAGTAG.peripherals.battery) + " "
if MAGTAG.peripherals.battery < NEED_TO_CHARGE :
BATT += "Charge me today. "
# WEATHER --------------------------------------------------------------------|
def get_data_source_url(api="onecall", location=None):
"""Build and return the URL for the OpenWeather API."""
if api.upper() == "FORECAST5":
URL = "https://api.openweathermap.org/data/2.5/forecast?"
URL += "q=" + location
elif api.upper() == "ONECALL":
URL = "https://api.openweathermap.org/data/2.5/onecall?exclude=minutely,hourly,alerts"
URL += "&lat={}".format(location[0])
URL += "&lon={}".format(location[1])
else:
raise ValueError("Unknown API type: " + api)
return URL + "&appid=" + secrets["openweather_token"]
def get_latlon():
"""Use the Forecast5 API to determine lat/lon for given city."""
MAGTAG.url = get_data_source_url(api="forecast5", location=secrets["openweather_location"])
MAGTAG.json_path = ["city"]
raw_data = MAGTAG.fetch()
return raw_data["coord"]["lat"], raw_data["coord"]["lon"]
def get_forecast(location):
"""Use OneCall API to fetch forecast and timezone data."""
resp = MAGTAG.network.fetch(get_data_source_url(api="onecall", location=location))
json_data = resp.json()
return json_data["daily"], json_data["current"]["dt"], json_data["timezone_offset"]
def temperature_text(tempK):
return "{:3.0f}F".format(32.0 + 1.8 * (tempK - 273.15))
# MAIN ----------------------------------------------------------------
try:
MAGTAG.network.connect() # Do this last, as WiFi uses power
print('Updating time')
MAGTAG.get_local_time()
NOW = rtc.RTC().datetime
print(NOW)
print('-'*20)
print("Time is", str(NOW.tm_hour) + ':' + str(NOW.tm_min))
print('-'*20)
print('Getting Latitude & Longitude')
latlon = get_latlon()
print(secrets["openweather_location"])
print(latlon)
print("Fetching forecast...")
forecast_data, utc_time, local_tz_offset = get_forecast(latlon)
max = forecast_data[0]['temp']['max']
max_F = "High today" + temperature_text(max)
print (max_F)
print('Spreadsheet content')
RESPONSE = MAGTAG.network.fetch(JSON_URL)
if RESPONSE.status_code == 200:
JSON_DATA = RESPONSE.json()
print('OK response')
print('-'*40)
print (JSON_DATA)
# Show battery voltage, ask to charge if needed, and Time of update.
time_string = str(NOW.tm_hour % 12)
if NOW.tm_hour == 0 : time_string = '12'
SUFFIX = 'am'
if NOW.tm_hour > 12 : SUFFIX = 'pm'
time_string += ':' + "{:02d}".format(NOW.tm_min) + SUFFIX
MAGTAG.set_text('%s %s %s %s' % (BATT, 'Updated at', time_string, max_F)
, 1, auto_refresh=False)
# Place spreadsheet data on display
ENTRIES = JSON_DATA['values'] # List of cell data
for entry in ENTRIES:
MsgList += str(*entry) + "\n "
print ('='*40)
MAGTAG.set_text(MsgList) # Update list on the display
print (MsgList)
# Allow refresh to finish before deep sleep. Compute to wake at 3:15am.
time.sleep(2)
seconds_since_midnight = 60 * (NOW.tm_hour * 60 + NOW.tm_min) + NOW.tm_sec
three_fifteen = (3 * 60 + 15) * 60
seconds_to_sleep = (24 * 60 * 60 - seconds_since_midnight) + three_fifteen
if TESTING == True : seconds_to_sleep = 121
print("Sleep for {} hours, {} minutes".format(
seconds_to_sleep // 3600, (seconds_to_sleep // 60) % 60
)
)
MAGTAG.exit_and_deep_sleep(seconds_to_sleep)
except ConnectionError as my_error:
MAGTAG.set_text(" Unable to connect to WiFi.\n Will try again hourly.")
MAGTAG.exit_and_deep_sleep(60 * 60) # 60 minute deep sleep
except RuntimeError as error:
# If there's an error above, no harm, just try again in ~15 minutes.
# Usually it's a common network issue or time server hiccup.
print('Retry in 15 min - ', error)
MAGTAG.set_text("error")
MAGTAG.exit_and_deep_sleep(15 * 60) # 15 minute deep sleep
Re: Library suggestion for #CircuitPython2022
# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it
secrets = {
'ssid' : 'WIFI_SSID',
'password' : 'PASSWORD,
'ssid2' : 'WIFI_SSID2',
'password2' : 'PASSWORD2,
'ssid3' : 'WIFI_SSID3',
'password3' : 'PASSWORD3,
'openweather_token' : 'xyzxyzxyz',
'aio_username' : "butter",
'aio_key' : 'abcabcabcabc',
'location' : 'Baltimore, US'
}
Re: Library suggestion for #CircuitPython2022
blakebr wrote:This is what I would like to see:
- Code: Select all | TOGGLE FULL SIZE
# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it
secrets = {
'ssid' : 'WIFI_SSID',
'password' : 'PASSWORD,
'ssid2' : 'WIFI_SSID2',
'password2' : 'PASSWORD2,
'ssid3' : 'WIFI_SSID3',
'password3' : 'PASSWORD3,
'openweather_token' : 'xyzxyzxyz',
'aio_username' : "butter",
'aio_key' : 'abcabcabcabc',
'location' : 'Baltimore, US'
}
The SSID & PASSWORD pairs would be cycled through before failing
Bruce
P.S. I learned something new today.
Apparently using a string of x characters to block out sensitive information is a banned word.
Re: Library suggestion for #CircuitPython2022
try:
for network in wifi.radio.start_scanning_networks():
# print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
# network.rssi, network.channel))
if network.ssid == secrets["ssidhome"]:
print("found home network")
wifi.radio.connect(secrets["ssidhome"], secrets["passwordhome"])
# print("Connecting to %s" % secrets["ssidhome"])
wifi.radio.stop_scanning_networks()
break
if network.ssid == secrets["ssid2"]:
wifi.radio.connect(secrets["ssidkelly"], secrets["password2"])
# print("Connecting to %s" % secrets["ssid2"])
# print("My IP address is", wifi.radio.ipv4_address)
wifi.radio.stop_scanning_networks()
break
if network.ssid == secrets["ssid3"]:
wifi.radio.connect(secrets["ssid3"], secrets["password3"])
# print("Connecting to %s" % secrets["ssid3"])
wifi.radio.stop_scanning_networks()
break
wifi.radio.stop_scanning_networks()
except OSError:
print("OS error")
pass
except RuntimeError:
print("Runtime error")
pass
else:
print("some error")
pass
finally:
print("finally")
pass
Re: Library suggestion for #CircuitPython2022
Re: Library suggestion for #CircuitPython2022
def htm_connect(self):
attemptnumber = 1
print ("htm_connect in network.py attempt=",attemptnumber)
self._wifi.neo_status(STATUS_CONNECTING)
while not self._wifi.is_connected:
if attemptnumber % 2 == 0 :
htm_ssid = secrets['ssid']
htm_password = secrets ['password']
else :
htm_ssid = secrets['ssid1']
htm_password = secrets['password1']
print("Connecting to AP", htm_ssid,attemptnumber)
self._wifi.neo_status(STATUS_NO_CONNECTION) # red = not connected
try:
self._wifi.connect(htm_ssid, htm_password)
self.requests = self._wifi.requests
except ConnectionError as error:
print("connect error with",htm_ssid,"attempt",attemptnumber)
attemptnumber += 1
if attemptnumber >= 10 :
return ("failed after 10 attempts.")
gc.collect()
Re: Library suggestion for #CircuitPython2022
import time
import board
import busio
import displayio
from digitalio import DigitalInOut, Direction, Pull
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import PWMOut
from adafruit_esp32spi import *
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
#################### Figure out what board we have ####################
Pico, Itsy, QTPy, Fthr, ANRC, Chal = False, False, False, False, False, False
brd = board.board_id # Ask the AdaFruit Circuit Python OS what board we are using
print(brd, end=" - ")
# "12345678901234567890123456789
if (brd == "raspberry_pi_pico"): Pico = True # 17
elif(brd == "adafruit_qtpy_rp2040"): QTPy = True # 20
elif(brd == "adafruit_feather_rp2040"): Fthr = True # 23
elif(brd == "adafruit_itsybitsy_rp2040"): Itsy = True # 25
elif(brd == "challenger_rp2040_wifi"): Chal = True # 22
elif(brd == "challenger_nb_rp2040_wifi"): Chal = True # 25
elif(brd == "arduino_nano_rp2040_connect"): ANRC = True # 27
else:
while(True):
print("Board Not Supported.")
time.sleep(15)
pass # raise
print("Board Supported.")
#if board.board_id not in ["feather_m0_express", "trinket_m0"]:
# raise ValueError(f"unsupported board: {board.board_id}")
# If you are using a board with pre-defined ESP32 Pins:
if(ANRC or Chal):
esp32_cs = DigitalInOut(board.ESP_CS1)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
else:
# If you have an externally connected ESP32: AirLift etc.
esp32_cs = DigitalInOut(board.D13) # Change to match your build
esp32_ready = DigitalInOut(board.D6 )
esp32_reset = DigitalInOut(board.D12)
esp32_GPIO0 = DigitalInOut(board.D10) # ?
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
wifi = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
if wifi.status == adafruit_esp32spi.WL_IDLE_STATUS:
print("ESP32 found and in idle mode")
print("Firmware version : ", str(wifi.firmware_version, 'utf-8'))
print("MAC addr reversed: ", [hex(i) for i in wifi.MAC_address])
print("MAC addr actual : ", [hex(i) for i in wifi.MAC_address_actual])
##########################################################
ca = True
try:
for network in wifi.scan_networks():
# Connect to WiFi Router print(network)
# {'rssi': -39, 'ssid': bytearray(b'aristotle'), 'bssid': bytearray(b'\x9bv\xabh\xefX'), 'encryption': 4, 'channel': 6}
print("\nConnecting to AP...", end='')
print("\tSSID: %s\tRSSI: %d\tChannel: %d\tEncryption: %d" % (str(network['ssid'], 'utf-8'), network['rssi'], network['channel'], network['encryption']))
if network['ssid'] == secrets["ssid"]:
print("found home network")
wifi.connect_AP(secrets["ssid"], secrets["password"])
print("Connecting to %s" % secrets["ssid"])
print("My IP address is ", wifi.ip_address)
print("Pretty IP Address", wifi.pretty_ip(wifi.ip_address))
print("Connected to:", str(wifi.ssid, "utf-8"), "\tRSSI:", wifi.rssi)
if(ca): break
if network['ssid'] == secrets["ssid2"]:
print("found secondary network")
wifi.connect_AP(secrets["ssid2"], secrets["password2"])
print("Connecting to %s" % secrets["ssid2"])
# print("My IP address is", wifi.ip_address)
print("Pretty IP Address", wifi.pretty_ip(wifi.ip_address))
print("Connected to:", str(wifi.ssid, "utf-8"), "\tRSSI:", wifi.rssi)
if(ca): break
if network['ssid'] == secrets["ssid3"]:
print("found tertiary network")
wifi.connect_AP(secrets["ssid3"], secrets["password3"])
print("Connecting to %s" % secrets["ssid3"])
print("My IP address is", wifi.ip_address)
print("Pretty IP Address", wifi.pretty_ip(wifi.ip_address))
print("Connected to:", str(wifi.ssid, "utf-8"), "\tRSSI:", wifi.rssi)
if(ca): break
print("Disconnecting: %s" % wifi.disconnect())
except OSError:
print("OS Error")
pass
except RuntimeError:
print("Runtime Error")
pass
else:
print("Some Error")
pass
finally:
pass
#print(network)
print("Finally")
pass
print(wifi.disconnect())