Showing AIO data on a Pyportal

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.
User avatar
axelmagnus
 
Posts: 24
Joined: Thu Oct 20, 2016 5:20 pm

Showing AIO data on a Pyportal

Post by axelmagnus »

Hi there.

Circuitpython/Pyportal is great at fetching data and displaying them.
For months i have tried to find a solution to fetch and display my own AIO data on the pyportal. I fail. It seems like a natural request? what have i missed?

/Axel

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

Re: Showing AIO data on a Pyportal

Post by brubell »

Hi Axelmagnus,

Could you include the code you're attempting to use for fetching/displaying your Adafruit IO Data on the PyPortal. I'll take a look at it :)

- Brent R.

User avatar
axelmagnus
 
Posts: 24
Joined: Thu Oct 20, 2016 5:20 pm

Re: Showing AIO data on a Pyportal

Post by axelmagnus »

brubell wrote:Hi Axelmagnus,

Could you include the code you're attempting to use for fetching/displaying your Adafruit IO Data on the PyPortal. I'll take a look at it :)

- Brent R.
Well, no, since i dont know what to use (total noob). But i figure its a request of some sort, like here:

https://io.adafruit.com/api/docs/?pytho ... -feed-data

or here:

https://io.adafruit.com/api/docs/?python#get-feed-data

User avatar
axelmagnus
 
Posts: 24
Joined: Thu Oct 20, 2016 5:20 pm

Re: Showing AIO data on a Pyportal

Post by axelmagnus »

that is, some kind of JSON request that fetches the data.

User avatar
axelmagnus
 
Posts: 24
Joined: Thu Oct 20, 2016 5:20 pm

Re: Showing AIO data on a Pyportal

Post by axelmagnus »

OK, so here i have some code that i tried. I found it at

https://circuitpython.readthedocs.io/pr ... e_all_data

which has you as author:). Now, i use receive_all_data

Code: Select all

#receive_all_data
"""
Example of interacting with Adafruit IO feeds
"""
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 HTTP Client
from adafruit_io.adafruit_io import IO_HTTP

# 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

# ESP32 Setup
try:
    esp32_cs = DigitalInOut(board.ESP_CS)
    esp32_ready = DigitalInOut(board.ESP_BUSY)
    esp32_reset = DigitalInOut(board.ESP_RESET)
except AttributeError:
    esp32_cs = DigitalInOut(board.D9)
    esp32_ready = DigitalInOut(board.D10)
    esp32_reset = DigitalInOut(board.D5)

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
)  # Uncomment for Most Boards
"""Uncomment below for ItsyBitsy M4"""
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 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.)
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]

# Create an instance of the Adafruit IO HTTP client
io = IO_HTTP(aio_username, aio_key, wifi)

# List a specified feed
print("Retrieving new Adafruit IO feed...")
specified_feed = io.receive_all_data("lora_batteryvoltage")
print(specified_feed)
Well, i ran this and it hung after

Code: Select all

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Retrieving new Adafruit IO feed...
Socket missing recv_into. Using more memory to be compatible
I did something else for a while and then after a long long time i got a response!

Code: Select all

[{'created_at': '2020-10-01T14:24:11Z', 'id': '0EJ3PQV7QPZX60NRVACWKGV1S6', 'expiration': '2020-11-30T15:24:11Z', 'created_epoch': 1601562251, 'feed_id': 1010762, 'value': '3.58', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T14:08:32Z', 'id': '0EJ3PEWQJEFB2JT9A1T71WT6CX', 'expiration': '2020-11-30T15:08:32Z', 'created_epoch': 1601561312, 'feed_id': 1010762, 'value': '3.58', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:49:58Z', 'id': '0EJ3P48S3DV9AFGET3YMPT27PN', 'expiration': '2020-11-30T14:49:58Z', 'created_epoch': 1601560198, 'feed_id': 1010762, 'value': '3.58', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:34:18Z', 'id': '0EJ3NV9TAG5TRV7Z2CXX4Q0HRB', 'expiration': '2020-11-30T14:34:18Z', 'created_epoch': 1601559258, 'feed_id': 1010762, 'value': '3.58', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:18:39Z', 'id': '0EJ3NJBBA957GQFXXFQKF521VT', 'expiration': '2020-11-30T14:18:39Z', 'created_epoch': 1601558319, 'feed_id': 1010762, 'value': '3.58', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:02:58Z', 'id': '0EJ3N9C71C5RKETJHYSD7T3435', 'expiration': '2020-11-30T14:02:58Z', 'created_epoch': 1601557378, 'feed_id': 1010762, 'value': '3.58', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T12:47:18Z', 'id': '0EJ3N0D7SG1109RK5FWP23FKSR', 'expiration': '2020-11-30T13:47:18Z', 'created_epoch': 1601556438, 'feed_id': 1010762, 'value': '3.58', 'feed_key': 'lora-batteryvoltage'}
but now im hung again. How is this possible? I just had to copy it in here, because i still cant believe it.

Sometimes i get this error:

Code: Select all

Traceback (most recent call last):
  File "code.py", line 56, in <module>
  File "adafruit_io/adafruit_io.py", line 583, in receive_all_data
  File "adafruit_io/adafruit_io.py", line 539, in _get
  File "adafruit_requests.py", line 343, in json
  File "adafruit_requests.py", line 339, in json
ValueError: syntax error in JSON
Line 56 is

Code: Select all

specified_feed = io.receive_all_data("lora-batteryvoltage")
Im using 6.0.0 for adafruit_io and _requests, 200929

regards,
Axel

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

Re: Showing AIO data on a Pyportal

Post by brubell »

We did recently change the socket recv implementation in the Adafruit CircuitPython Requests library to use recv_into instead of recv. What you are seeing may be a result of an error within the updated Adafruit Requests library attempting to retrieve chunked JSON data.

Could you please file an issue on https://github.com/adafruit/Adafruit_Ci ... sts/issues including as much as you can, as well as a link to this topic?

User avatar
axelmagnus
 
Posts: 24
Joined: Thu Oct 20, 2016 5:20 pm

Re: Showing AIO data on a Pyportal

Post by axelmagnus »


User avatar
axelmagnus
 
Posts: 24
Joined: Thu Oct 20, 2016 5:20 pm

Re: Showing AIO data on a Pyportal

Post by axelmagnus »

Ok small update.
If i use receive_data, it works:

Code: Select all

print("Retrieving new Adafruit IO feed...")
specified_feed = io.receive_data("lora-batteryvoltage")
print(specified_feed)

Code: Select all

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Retrieving new Adafruit IO feed...
Socket missing recv_into. Using more memory to be compatible
{'id': '0EJ55B84Q414124WFDJMMJWJMZ', 'feed_id': 1010762, 'location': None, 'lat': None, 'created_at': '2020-10-03T09:50:36Z', 'lon': None, 'expiration': '1606906236.0', 'ele': None, 'updated_at': '2020-10-03T09:50:36Z', 'value': '3.59'}
But it takes a long time, over a minute to get the one latest datapoint.

User avatar
Buddahaid
 
Posts: 10
Joined: Tue Aug 27, 2019 9:10 pm

Re: Showing AIO data on a Pyportal

Post by Buddahaid »

I've just run into this trying to get any of the Pyportal Pynt examples that use get_local_time to run. They all error out starting with socket missing recv_into.

User avatar
axelmagnus
 
Posts: 24
Joined: Thu Oct 20, 2016 5:20 pm

Re: Showing AIO data on a Pyportal

Post by axelmagnus »

Buddahaid wrote:I've just run into this trying to get any of the Pyportal Pynt examples that use get_local_time to run. They all error out starting with socket missing recv_into.
Have you seen this:
viewtopic.php?f=60&t=169654

User avatar
axelmagnus
 
Posts: 24
Joined: Thu Oct 20, 2016 5:20 pm

Re: Showing AIO data on a Pyportal

Post by axelmagnus »

So, i keep investigating and leaving my experiences here.

request_all_data hasnt worked again
i try to get the last data only, and this works. Once

Then it throws this error (Failed to send??):

Code: Select all

2020-10-01 14 24 3.78
2020-10-01 14 08 4.58
2020-10-01 13 49 3.28
2020-10-01 13 34 3.88
2020-10-01 13 18 5.58
2020-10-01 13 02 1.68
2020-10-01 12 47 3.64
2020-10-01 14 24 3.78
2020-10-01 14 08 4.58
2020-10-01 13 49 3.28
2020-10-01 13 34 3.88
2020-10-01 13 18 5.58
2020-10-01 13 02 1.68
2020-10-01 12 47 3.64
Socket missing recv_into. Using more memory to be compatible
3.62
Traceback (most recent call last):
  File "code.py", line 86, in <module>
  File "adafruit_io/adafruit_io.py", line 591, in receive_data
  File "adafruit_io/adafruit_io.py", line 536, in _get
  File "adafruit_esp32spi/adafruit_esp32spi_wifimanager.py", line 225, in get
  File "adafruit_requests.py", line 632, in get
  File "adafruit_requests.py", line 540, in request
  File "adafruit_requests.py", line 537, in request
  File "adafruit_requests.py", line 458, in _send_request
  File "adafruit_requests.py", line 449, in _send
  File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 82, in send
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 710, in socket_write
RuntimeError: Failed to send 3 bytes (sent 0)
I wonder if there is some throttling or something. I see nothing on the monitor page of my AIO+

Code: Select all

#receive_all_data worked once, latest request
#receive_dataa works once, then "adafruit_esp32spi/adafruit_esp32spi.py", line 710, in socket_write
#RuntimeError: Failed to send 3 bytes (sent 0)
#original adafruit_io_feeds
"""
Example of interacting with Adafruit IO feeds
"""
import board
import busio
import time
from digitalio import DigitalInOut
import displayio
from adafruit_display_shapes.sparkline import Sparkline

# ESP32 SPI
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager

# Import NeoPixel Library
import neopixel

# Import Adafruit IO HTTP Client
from adafruit_io.adafruit_io import IO_HTTP

# Get wifi details and more from a secrets.py file

from secrets import secrets

# ESP32 Setup
try:
    esp32_cs = DigitalInOut(board.ESP_CS)
    esp32_ready = DigitalInOut(board.ESP_BUSY)
    esp32_reset = DigitalInOut(board.ESP_RESET)
except AttributeError:
    esp32_cs = DigitalInOut(board.D9)
    esp32_ready = DigitalInOut(board.D10)
    esp32_reset = DigitalInOut(board.D5)

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
)  # Uncomment for Most Boards
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(
    esp, secrets, status_light)

aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]

# Create an instance of the Adafruit IO HTTP client
io = IO_HTTP(aio_username, aio_key, wifi)

display = board.DISPLAY
DISPLAY_WIDTH = board.DISPLAY.width
chart_width = 200
chart_height = 100

sparkline = Sparkline(
    width=DISPLAY_WIDTH - 30,
    height=chart_height ,
    max_items=10,
    x=0,
    y=120,
    color=0xFFFFFF
)

my_group = displayio.Group(max_size=20)
my_group.append(sparkline)
#display.show(my_group)

recv_all=[{'created_at': '2020-10-01T14:24:11Z', 'id': '0EJ3PQV7QPZX60NRVACWKGV1S6', 'expiration': '2020-11-30T15:24:11Z', 'created_epoch': 1601562251, 'feed_id': 1010762, 'value': '3.78', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T14:08:32Z', 'id': '0EJ3PEWQJEFB2JT9A1T71WT6CX', 'expiration': '2020-11-30T15:08:32Z', 'created_epoch': 1601561312, 'feed_id': 1010762, 'value': '4.58', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:49:58Z', 'id': '0EJ3P48S3DV9AFGET3YMPT27PN', 'expiration': '2020-11-30T14:49:58Z', 'created_epoch': 1601560198, 'feed_id': 1010762, 'value': '3.28', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:34:18Z', 'id': '0EJ3NV9TAG5TRV7Z2CXX4Q0HRB', 'expiration': '2020-11-30T14:34:18Z', 'created_epoch': 1601559258, 'feed_id': 1010762, 'value': '3.88', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:18:39Z', 'id': '0EJ3NJBBA957GQFXXFQKF521VT', 'expiration': '2020-11-30T14:18:39Z', 'created_epoch': 1601558319, 'feed_id': 1010762, 'value': '5.58', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:02:58Z', 'id': '0EJ3N9C71C5RKETJHYSD7T3435', 'expiration': '2020-11-30T14:02:58Z', 'created_epoch': 1601557378, 'feed_id': 1010762, 'value': '1.68', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T12:47:18Z', 'id': '0EJ3N0D7SG1109RK5FWP23FKSR', 'expiration': '2020-11-30T13:47:18Z', 'created_epoch': 1601556438, 'feed_id': 1010762, 'value': '3.64', 'feed_key': 'lora-batteryvoltage'},{'created_at': '2020-10-01T14:24:11Z', 'id': '0EJ3PQV7QPZX60NRVACWKGV1S6', 'expiration': '2020-11-30T15:24:11Z', 'created_epoch': 1601562251, 'feed_id': 1010762, 'value': '3.78', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T14:08:32Z', 'id': '0EJ3PEWQJEFB2JT9A1T71WT6CX', 'expiration': '2020-11-30T15:08:32Z', 'created_epoch': 1601561312, 'feed_id': 1010762, 'value': '4.58', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:49:58Z', 'id': '0EJ3P48S3DV9AFGET3YMPT27PN', 'expiration': '2020-11-30T14:49:58Z', 'created_epoch': 1601560198, 'feed_id': 1010762, 'value': '3.28', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:34:18Z', 'id': '0EJ3NV9TAG5TRV7Z2CXX4Q0HRB', 'expiration': '2020-11-30T14:34:18Z', 'created_epoch': 1601559258, 'feed_id': 1010762, 'value': '3.88', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:18:39Z', 'id': '0EJ3NJBBA957GQFXXFQKF521VT', 'expiration': '2020-11-30T14:18:39Z', 'created_epoch': 1601558319, 'feed_id': 1010762, 'value': '5.58', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T13:02:58Z', 'id': '0EJ3N9C71C5RKETJHYSD7T3435', 'expiration': '2020-11-30T14:02:58Z', 'created_epoch': 1601557378, 'feed_id': 1010762, 'value': '1.68', 'feed_key': 'lora-batteryvoltage'}, {'created_at': '2020-10-01T12:47:18Z', 'id': '0EJ3N0D7SG1109RK5FWP23FKSR', 'expiration': '2020-11-30T13:47:18Z', 'created_epoch': 1601556438, 'feed_id': 1010762, 'value': '3.84', 'feed_key': 'lora-batteryvoltage'}]
print("Retrieving new Adafruit IO feed...")
print(recv_all[0]['value'])

for index, item in enumerate(recv_all):
    date, t = item['created_at'].split("T") #date and time
    h, m , s= t.split(":")         #hours and minutes
    value=item['value']
    sparkline.add_value(float(value))
    print(date,h,m,float(value))

display.show(my_group)

while True:

    lastdata = io.receive_data("lora-batteryvoltage")
    print(lastdata['value'])
    sparkline.add_value(float(lastdata['value']))
    display.auto_refresh = True
    time.sleep(60)

User avatar
Buddahaid
 
Posts: 10
Joined: Tue Aug 27, 2019 9:10 pm

Re: Showing AIO data on a Pyportal

Post by Buddahaid »

axelmagnus wrote:
Buddahaid wrote:I've just run into this trying to get any of the Pyportal Pynt examples that use get_local_time to run. They all error out starting with socket missing recv_into.
Have you seen this:
viewtopic.php?f=60&t=169654
No I haven't. I followed that and I no longer have the recv_into error but I still have a problem getting the local time to load. Errors out stating invalid API key provided. Too bad as I thought it would be solved.

Thanks for the link.

User avatar
Buddahaid
 
Posts: 10
Joined: Tue Aug 27, 2019 9:10 pm

Re: Showing AIO data on a Pyportal

Post by Buddahaid »

I forgot to mention that error is generated from the adafruit_pyportal.mpy library file.

User avatar
CGrover
 
Posts: 188
Joined: Mon Dec 07, 2015 2:52 am

Re: Showing AIO data on a Pyportal

Post by CGrover »

I'm also seeing this error when using the PyPortal library's push_to_io() function.


adafruit-circuitpython-bundle-6.x-mpy-20201009
Adafruit CircuitPython 6.0.0-beta.2 on 2020-10-05; Adafruit PyPortal with samd51j20

User avatar
CGrover
 
Posts: 188
Joined: Mon Dec 07, 2015 2:52 am

Re: Showing AIO data on a Pyportal

Post by CGrover »

... and adafruit-circuitpython-bundle-6.x-mpy-20201010

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”