unix time from Adafruit IO

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
slowjim
 
Posts: 40
Joined: Fri Sep 27, 2019 3:49 am

unix time from Adafruit IO

Post by slowjim »

Hi
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)

User avatar
brubell
Learn User Page
 
Posts: 2017
Joined: Fri Jul 17, 2015 10:33 pm

Re: unix time from Adafruit IO

Post by brubell »

The Adafruit MatrixPortal/PyPortal use a different part of the Adafruit IO Time API to set the RTC, it may be closer to what you're looking for. The source for it is within a "get_local_time" function that takes in a location argument.

You may be able to simply import portalbase's network module into your code. If not, you could split this function out of this library and into your code.
https://github.com/adafruit/Adafruit_Ci ... rk.py#L166

User avatar
slowjim
 
Posts: 40
Joined: Fri Sep 27, 2019 3:49 am

Re: unix time from Adafruit IO

Post by slowjim »

Thanks brubell, I have created a function that works. Great tips.

Cheers, SlowJim

User avatar
brubell
Learn User Page
 
Posts: 2017
Joined: Fri Jul 17, 2015 10:33 pm

Re: unix time from Adafruit IO

Post by brubell »

glad to help!

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”