Skip to content

Instantly share code, notes, and snippets.

@askpatrickw
Last active March 6, 2021 02:50
Show Gist options
  • Save askpatrickw/8fffff8f00efb33e7f486e5b7b023ad5 to your computer and use it in GitHub Desktop.
Save askpatrickw/8fffff8f00efb33e7f486e5b7b023ad5 to your computer and use it in GitHub Desktop.
My network init which I use for all the CircuitPython things...
import rtc
import socketpool
import ssl
import time
import wifi
import adafruit_requests
def set_system_time():
if time.localtime().tm_year < 2020:
print("Setting System Time in UTC")
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
response = requests.get("https://io.adafruit.com/api/v2/time/seconds")
if response:
if response.status_code == 200:
r = rtc.RTC()
r.datetime = time.localtime(int(response.text))
print(f"System Time: {r.datetime}")
else:
print("Setting time failed")
else:
print("Year seems good, skipping set time.")
def wifi_join(ssid, password):
print("Configuring Network")
wifi.radio.connect(ssid, password)
print(f"ip: {wifi.radio.ipv4_address}")
print(f"hostname: {wifi.radio.hostname}")
# Get wifi details and more from a config.py file
try:
import config
except ImportError:
print("WiFi secrets are kept in config.py, please add them there!")
raise
wifi_join(config.SSID, config.WIFI_PASSWORD)
set_system_time()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment