I am currently on the learning curve for using Adafruit IO to get simple information from the standard topics. For the project I'm working on I would like to get unix time format from a reliable source. (My dad will be the end user and I will need to convince him the data location is safe!) It will be used to set an RTC upon start and will also call on the IO every week or so to update the time. I am doing this as I would like to user to not have to set the time when the "clock" is turned off and on and keep the right time even when time changes due to daylight savings etc
I have been playing around with the following sample code, it currently works by simply printing the time in the REPL . I have two main questions:
1. Is it possible to simply call for the time once so I can pass it to the RTC and then disconnect from the IO (and the internet)? I have tried play around with the functions in the readthedocs section but I could not work out how to simply print one value.
2. Can I somehow pass a location so I can get the standard time where I am?
Note: I am using an ItsyBitsy M4 with CircuitPython 6.0 connected to an ESP AirLift breakout
Code: Select all
import time
import board
import busio
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_io.adafruit_io import IO_MQTT
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
esp32_cs = DigitalInOut(board.D10)
esp32_ready = DigitalInOut(board.D9)
esp32_reset = DigitalInOut(board.D7)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets,)
def connected(client):
print("Connected to Adafruit IO!")
io.subscribe_to_time("seconds")
# pylint: disable=unused-argument
def disconnected(client):
# Disconnected function will be called when the client disconnects.
print("Disconnected from Adafruit IO!")
def message(client, feed_id, payload):
# Message function will be called when a subscribed feed has a new value.
# The feed_id parameter identifies the feed, and the payload parameter has
# the new value.
print(payload)
# Connect to WiFi
print("Connecting to WiFi...")
wifi.connect()
print("Connected!")
# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)
# Initialize a new MQTT Client object
mqtt_client = MQTT.MQTT(
broker="io.adafruit.com",
username=secrets["aio_username"],
password=secrets["aio_key"],
)
# Initialize an Adafruit IO MQTT Client
io = IO_MQTT(mqtt_client)
# Connect the callback methods defined above to Adafruit IO
io.on_connect = connected
io.on_message = message
io.on_disconnect = disconnected
# Connect to Adafruit IO
io.connect()
# Start a blocking message loop...
# NOTE: NO code below this loop will execute
# NOTE: Network reconnection is handled within this loop
while True:
try:
io.loop()
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
io.reconnect()
continue
time.sleep(1)