I've adapted a Magtag CircuitPython example (https://learn.adafruit.com/pyportal-ada ... cuitpython) to an api for a temperature sensor. An example of the response from my API call is;
Code: Select all
[{"id":"648012da3a121945321959","temperatureF":73,"date":1680020164000,"dateString":"2023-03-28T16:16:04.000Z","temperatureTrend":6,"direction":"Down","unixTS":1680020164000}]
Code: Select all
# Set up where we'll be fetching data from
DATA_SOURCE = "https://www.mytemperaturesensor.com" //Obviously not the real URL
temperatureF_Reading = [0, 'temperatureF']
tread_Reading = [0, 'temperatureTrend']
unixTS_Reading = [0, 'unixTS']
I would appreciate help in defining these parsed elements of my CircuitPython code as values (Int, Float).
Here is the full code that works for displaying the three key parameters but I want to be able to define those three as values that can be mathmatically operated on and/or subjected to conditional logic. >, ==, <
Code: Select all
# SPDX-FileCopyrightText: 2020 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# Be sure to put WiFi access point info in secrets.py file to connect
import time
import random
from adafruit_magtag.magtag import MagTag
# Set up where we'll be fetching data from
DATA_SOURCE = "https://www.mytemperaturesensor.com" #Obviously not the real URL
temperatureF_Reading = [0, 'temperatureF']
tread_Reading = [0, 'temperatureTrend']
unixTS_Reading = [0, 'unixTS']
# in seconds, we can refresh about 100 times on a battery
TIME_BETWEEN_REFRESHES = 1 * 60 * 5 # 5 minute delay
magtag = MagTag(
url=DATA_SOURCE,
json_path=(temperatureF_Reading, tread_Reading, unixTS_Reading),
)
magtag.graphics.set_background("/bmps/Blank.bmp")
# temperatureF_Reading
magtag.add_text(
text_font="/fonts/Impact-30.pcf",
text_wrap=28,
text_maxlen=120,
text_position=(
(magtag.graphics.display.width // 2),
(magtag.graphics.display.height // 2) - 10,
),
line_spacing=0.75,
text_anchor_point=(0.5, 0.5), # center the text on x & y
)
# tread_Reading
magtag.add_text(
text_font="/fonts/Arial-Italic-12.bdf",
text_position=(magtag.graphics.display.width // 6, 118),
text_anchor_point=(0.5, 0.5), # left justify this line
)
# unixTS_Reading
magtag.add_text(
text_font="/fonts/Arial-Italic-12.bdf",
text_position=(magtag.graphics.display.width // 2, 118),
text_anchor_point=(0.5, 0.5), # left justify this line
)
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)
Thank you.