Display NOAA Buoy data on a MagTag?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Display NOAA Buoy data on a MagTag?

Post by ghulse »

Has anyone ever tried to use the MagTag (or PyPortal?) to display buoy data from NOAA. I can go to this page on NOAA's web site and glean this information for a buoy located near Cambridge, MD. It would be really neat to display this same info on a MagTag (or another Adafruit display). Advice?

NDBC - Station CAMM2
8571892 - Cambridge, MD
38.574N 76.069W

9:18 am EST
1418 GMT 12/12/2022
Wind: N (10°), 13.0 kt
Gust: 15.9 kt
Pres: 30.18 inHg
Air Temp: 43.0 °F
Water Temp: 45.5 °F

There's a NOAA web guide here:
https://www.ndbc.noaa.gov/docs/ndbc_web_data_guide.pdf

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: Display NOAA Buoy data on a MagTag?

Post by danhalbert »

Interesting idea. We have several similar projects, for instance, this:
https://learn.adafruit.com/magtag-tides-viewer
https://learn.adafruit.com/spacex-next- ... uit-magtag.

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Display NOAA Buoy data on a MagTag?

Post by ghulse »

danhalbert wrote: Thu Dec 15, 2022 10:54 am Interesting idea. We have several similar projects, for instance, this:
https://learn.adafruit.com/magtag-tides-viewer
https://learn.adafruit.com/spacex-next- ... uit-magtag.
Thanks, yes, I've been studying these. The "Shower Thoughts" project is another good example.

All these projects import data in json format. As far as I can tell, the NOAA buoy program doesn't offer a json API. (I have reached out to them with this question, and haven't heard back yet). But there's a web widget that that you can enter the Buoy ID # and comes up with all the information I need.

https://www.ndbc.noaa.gov/widgets/stati ... tion=CAMM2

So just open up this URL in the MagTag and parse some of the text displayed. I think I would update about every fifteen minutes. Is there a MagTag project that does something like this?

Thanks, as always, for your help.

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: Display NOAA Buoy data on a MagTag?

Post by danhalbert »

Most of the examples use JSON, but you can also use a set of regexps to extract information. See `regexp_path` here: https://github.com/adafruit/Adafruit_Ci ... _init__.py. This project, https://learn.adafruit.com/pyportal-dis ... rd-display, uses regexps.

You can also bypass the MagTag library entirely. It's just a bunch of convenience functions.

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Display NOAA Buoy data on a MagTag?

Post by ghulse »

danhalbert wrote: Thu Dec 15, 2022 12:13 pmYou can also bypass the MagTag library entirely. It's just a bunch of convenience functions.
Unfortunately, I'm not familiar with regexp. Does Beautiful Soup or Element Tree work with Adafruit devices? Otherwise, I think I need to scrape the NOAA buoy site using no external libraries. I've got it working in Python on my Mac, but it uses Beautiful Soup.
Last edited by ghulse on Wed Dec 28, 2022 4:38 pm, edited 1 time in total.

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: Display NOAA Buoy data on a MagTag?

Post by danhalbert »

Beautiful soup is too large to fit, almost certainly. Given the small size of the output, just parsing it "by hand" or with a regular expression would work.

Here's one of many introductions to regular expressions: https://docs.python.org/3/howto/regex.html

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Display NOAA Buoy data on a MagTag?

Post by ghulse »

I was able to fetch NOAA buoy data on my Mac using Python. But in transcoding this code to the PyPortal, I can't figure out step one, which is to open a URL. I get the error: no module named 'urllib'

Here's the first few lines of code in Python. Is urllib.request possible on the PyPortal?

Code: Select all

#-- Fetch the contents of the URL
url = "https://www.ndbc.noaa.gov/rss/ndbc_obs_search.php?lat=38.574N&lon=76.069W"
with urllib.request.urlopen(url) as response:
    html = response.read().decode()

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: Display NOAA Buoy data on a MagTag?

Post by danhalbert »

You would use `adafruit_requests` instead. There is no `urllib` for CircuitPython. `adafruit_requests` is a subset of the regular Python`requests` library. You can search for "adafruit_requests" in the Learn Guides. Some very simple examples of fetching text and json are in the "Internet Connect" example: https://learn.adafruit.com/adafruit-mag ... et-connect.

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Display NOAA Buoy data on a MagTag?

Post by ghulse »

In my Python code, I'm scraping the NOAA buoy RSS Feed to create an index of strings.

Like so:

Code: Select all

##-- Find the substring between the start and end strings
start_string = '<description>'
end_string = "</description>"
start_index = html.find(start_string)
start_index = html.find(start_string, start_index+ len(start_string))
end_index = html.find(end_string, start_index + len(start_string))

desc = html[start_index: end_index+start_index]
rows = desc.split('\n')
CircuitPython gives me an AttributeError: 'Response' object has no attribute 'find'

Is this where I need to use regexp? Or is there another way to grab these substrings? Sorry if this is too basic.

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: Display NOAA Buoy data on a MagTag?

Post by danhalbert »

It looks like you haven't fetched the text from the Response object. If `html` is the response object, you need to do

Code: Select all

html_text = html.text
and then operate on `html_text`. (Rename things as you wish for convenience.)

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Display NOAA Buoy data on a MagTag?

Post by ghulse »

Wow, it's actually printing the NOAA buoy info to the screen. Now comes the time to create some graphics and fine-tune how the text gets displayed. Thank you for your help.

A problem remains though. At the top of every hour, the NOAA RSS feed adds a new variable (barometric pressure trend) to the data. This new variable changes the position of two strings: air-temp and water_temp. As such, in Python what I did was convert the date string to a datetime object, so that I can check to see if the minute is at the top of the hour. Like so:

Code: Select all

datetime_str = datetime.datetime.strptime(input_str, input_time_format)

if datetime_str.minute == 0:
    trending = rows [7]
    trending = trending.strip()
    trending = trending.replace("<strong>Pressure Tendency:</strong> ", "")
    trending = trending.replace("<br />", "")
    air_temp = rows[8]
    water_temp = rows[9]

else:
    air_temp = rows[7]
    water_temp = rows[8]
Can we use datetime on the MagTag? Or is there another way to accomplish this?

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: Display NOAA Buoy data on a MagTag?

Post by danhalbert »

Can you just check the HTML for the presence of that string ("Barometric Pressure Trend") or whatever, rather than checking the time? That seems easier.

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Display NOAA Buoy data on a MagTag?

Post by ghulse »

danhalbert wrote: Sat Jan 14, 2023 1:01 pm Can you just check the HTML for the presence of that string ("Barometric Pressure Trend") or whatever, rather than checking the time? That seems easier.
I'll try that and post back here. Thanks.

User avatar
ghulse
 
Posts: 97
Joined: Tue Nov 30, 2021 10:49 am

Re: Display NOAA Buoy data on a MagTag?

Post by ghulse »

I've reworked the code to avoid the datetime stuff. And so far it does work, printing the NOAA buoy info to the screen.

My next step is to have the code continuously run with an occasional refresh of the URL. I believe I will need to import the following libraries to do this?

import time
import random
from adafruit_magtag.magtag import MagTag

However, right away, I get the following:

ImportError: no module named 'adafruit_magtag'

Likewise, using the Shower Thoughts project as a model, I'll include the following lines of code as well?

Code: Select all

# in seconds, we can refresh about 100 times on a battery
TIME_BETWEEN_REFRESHES = 1 * 60 * 60  # one hour delay

magtag = MagTag(
    url=DATA_SOURCE,
    json_path=(QUOTE_LOCATION, AUTHOR_LOCATION),
)
magtag.graphics.set_background(bitmap image path)
Only I would delete the json_path, right? Also, need to have this code too?

Code: Select all

try:
    magtag.network.connect()
    value = magtag.fetch()
    print("Response is", value)
except (ValueError, RuntimeError, ConnectionError, OSError) as e:
    magtag.set_text(e)
    print("Some error occured, retrying! -", e)

# wait 2 seconds for display to complete
time.sleep(2)
magtag.exit_and_deep_sleep(TIME_BETWEEN_REFRESHES)

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: Display NOAA Buoy data on a MagTag?

Post by danhalbert »

You can get the MagTag library from the library bundle (see https://circuitpython.org/libraries) or use `circup` to install the library (https://learn.adafruit.com/keep-your-ci ... ith-circup). More about libraries: https://learn.adafruit.com/welcome-to-c ... -libraries.

But you have a good working program now, and you don't really need the magtag library to continue. It's designed for a fairly specific use case.

You can wrap you current code in an unending loop, and just sleep in between:

Code: Select all

import time

while True:
    # do all the work you're doing now.
    time.sleep(5*60)   # sleep for 5 minutes
    # loop around and go back
If this is a battery-operated device, you can use "deep sleep" to not drain the battery when you're sleeping. This guide talks about that: https://learn.adafruit.com/deep-sleep-w ... cuitpython.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”