Skip to content

Commit 7675afc

Browse files
authored
Merge pull request #140 from DJDevon3/WorkingBranch
add basic Fitbit API example
2 parents 87ae429 + 2f88f54 commit 7675afc

File tree

3 files changed

+382
-78
lines changed

3 files changed

+382
-78
lines changed

examples/requests_adafruit_discord_active_online.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
1-
# SPDX-FileCopyrightText: 2022 DJDevon3 for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2023 DJDevon3
22
# SPDX-License-Identifier: MIT
3-
# Coded for Circuit Python 8.0
4-
"""DJDevon3 Adafruit Feather ESP32-S2 Adafruit_Discord_Active_Users_Example"""
3+
"""
4+
Coded for Circuit Python 8.2.3
5+
requests_adafruit_discord_active_online
6+
"""
57
import gc
8+
import os
69
import time
710
import ssl
811
import json
912
import wifi
1013
import socketpool
1114
import adafruit_requests
1215

13-
# No user or token required, 100% JSON web scrape from SHIELDS.IO
16+
# Public API. No user or token required
17+
# JSON web scrape from SHIELDS.IO
1418
# Adafruit uses Shields.IO to see online users
1519

1620
# Initialize WiFi Pool (There can be only 1 pool & top of script)
1721
pool = socketpool.SocketPool(wifi.radio)
1822

19-
# Time between API refreshes
20-
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
23+
# Time in seconds between updates (polling)
24+
# 600 = 10 mins, 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
2125
sleep_time = 900
2226

23-
try:
24-
from secrets import secrets
25-
except ImportError:
26-
print("Secrets File Import Error")
27-
raise
27+
# this example uses settings.toml for credentials
28+
ssid = os.getenv("WIFI_SSID")
29+
appw = os.getenv("WIFI_PASSWORD")
30+
31+
32+
# Converts seconds to minutes/hours/days
33+
def time_calc(input_time):
34+
if input_time < 60:
35+
sleep_int = input_time
36+
time_output = f"{sleep_int:.0f} seconds"
37+
elif 60 <= input_time < 3600:
38+
sleep_int = input_time / 60
39+
time_output = f"{sleep_int:.0f} minutes"
40+
elif 3600 <= input_time < 86400:
41+
sleep_int = input_time / 60 / 60
42+
time_output = f"{sleep_int:.0f} hours"
43+
else:
44+
sleep_int = input_time / 60 / 60 / 24
45+
time_output = f"{sleep_int:.1f} days"
46+
return time_output
2847

29-
if sleep_time < 60:
30-
sleep_time_conversion = "seconds"
31-
sleep_int = sleep_time
32-
elif 60 <= sleep_time < 3600:
33-
sleep_int = sleep_time / 60
34-
sleep_time_conversion = "minutes"
35-
elif 3600 <= sleep_time < 86400:
36-
sleep_int = sleep_time / 60 / 60
37-
sleep_time_conversion = "hours"
38-
else:
39-
sleep_int = sleep_time / 60 / 60 / 24
40-
sleep_time_conversion = "days"
4148

4249
# Originally attempted to use SVG. Found JSON exists with same filename.
4350
# https://img.shields.io/discord/327254708534116352.svg
@@ -49,7 +56,7 @@
4956
requests = adafruit_requests.Session(pool, ssl.create_default_context())
5057
while not wifi.radio.ipv4_address:
5158
try:
52-
wifi.radio.connect(secrets["ssid"], secrets["password"])
59+
wifi.radio.connect(ssid, appw)
5360
except ConnectionError as e:
5461
print("Connection Error:", e)
5562
print("Retrying in 10 seconds")
@@ -93,7 +100,7 @@
93100
print("Monotonic: ", time.monotonic())
94101

95102
print("\nFinished!")
96-
print("Next Update in %s %s" % (int(sleep_int), sleep_time_conversion))
103+
print("Next Update: ", time_calc(sleep_time))
97104
print("===============================")
98105
gc.collect()
99106

0 commit comments

Comments
 (0)