Skip to content

Add new IP-based time endpoint #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 14, 2019

Conversation

brentru
Copy link
Member

@brentru brentru commented Mar 12, 2019

This PR modifies the receive_time method to support the new time endpoint on Adafruit IO: /integrations/time/struct.json. This endpoint will return a time based on the IP Address.

The new receive_time returns the time as a struct_time(https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/__init__.html#time.struct_time)

The previous /time endpoint returned a plaintext response, instead of json. Since the new endpoint returns a JSON response, this PR will remove the return_text kwarg from the HTTP _get method

@brentru brentru added the enhancement New feature or request label Mar 12, 2019
@brentru brentru requested a review from a team March 12, 2019 16:37
@jerryneedell
Copy link

I did not see any example for this so I tried:

import board
import busio
from digitalio import DigitalInOut

# ESP32 SPI
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager

# Import NeoPixel Library
import neopixel

# Import Adafruit IO REST Client
from adafruit_io.adafruit_io import RESTClient

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise


# PyPortal ESP32 Setup
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)


# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
ADAFRUIT_IO_USER = secrets['adafruit_io_user']
ADAFRUIT_IO_KEY = secrets['adafruit_io_key']

# Create an instance of the Adafruit IO REST client
io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)


print('Getting time from IO...')
time = io.receive_time()
print(time)

but it failed on a PyPortal:


Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 4.0.0-beta.3-15-gcfb54f026 on 2019-03-12; Adafruit PyPortal with samd51j20
>>> 
>>> import io_time
Getting time from IO...
Failed to connect, retrying
 ('No such ssid', b'Needell Airport')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "io_time.py", line 43, in <module>
  File "adafruit_io/adafruit_io.py", line 263, in receive_time
  File "adafruit_io/adafruit_io.py", line 118, in _get
  File "adafruit_esp32spi/adafruit_esp32spi_wifimanager.py", line 110, in get
  File "adafruit_esp32spi/adafruit_esp32spi_requests.py", line 235, in get
  File "adafruit_esp32spi/adafruit_esp32spi_requests.py", line 221, in request
  File "adafruit_esp32spi/adafruit_esp32spi_requests.py", line 176, in request
AttributeError: 'bytes' object has no attribute 'encode'

What did I miss?

@jerryneedell
Copy link

jerryneedell commented Mar 12, 2019

this breaks other examples -- like the one I ran for PR #17 same error.

Ah I see why -- When I pull this PR it undid the recently merged #17

How do I merge this with the latest master so I can test it?

@brentru brentru force-pushed the add-new-time-integration branch from bf2e0bd to 88cb011 Compare March 12, 2019 21:34
@brentru
Copy link
Member Author

brentru commented Mar 12, 2019

@jerryneedell I rebased the pull request onto master. It should now contain changes from #17

Copy link

@jerryneedell jerryneedell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested on pyportal with

import board
import busio
from digitalio import DigitalInOut

# ESP32 SPI
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager

# Import NeoPixel Library
import neopixel

# Import Adafruit IO REST Client
from adafruit_io.adafruit_io import RESTClient

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise


# PyPortal ESP32 Setup
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)


# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
ADAFRUIT_IO_USER = secrets['adafruit_io_user']
ADAFRUIT_IO_KEY = secrets['adafruit_io_key']

# Create an instance of the Adafruit IO REST client
io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)


print('Getting time from IO...')
time = io.receive_time()
print(time)

responded ok:

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 4.0.0-beta.3-15-gcfb54f026 on 2019-03-12; Adafruit PyPortal with samd51j20
>>> import io_time
Getting time from IO...
Failed to connect, retrying
 ('No such ssid', b'Needell Airport')
struct_time(tm_year=2019, tm_mon=3, tm_mday=12, tm_hour=21, tm_min=19, tm_sec=46, tm_wday=2, tm_yday=71, tm_isdst=1)

looks good.

@jerryneedell
Copy link

after rebase, both examples (time and weather) ran as expected.

@brentru brentru merged commit 2ae8741 into adafruit:master Mar 14, 2019
@brentru brentru deleted the add-new-time-integration branch March 14, 2019 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants