|
2 | 2 | #
|
3 | 3 | # SPDX-License-Identifier: MIT
|
4 | 4 |
|
| 5 | +from os import getenv |
5 | 6 | import time
|
6 | 7 | import ssl
|
7 | 8 | import wifi
|
|
14 | 15 | from digitalio import DigitalInOut, Direction, Pull
|
15 | 16 | from adafruit_debouncer import Debouncer
|
16 | 17 |
|
| 18 | +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml |
| 19 | +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) |
| 20 | +ssid = getenv("CIRCUITPY_WIFI_SSID") |
| 21 | +password = getenv("CIRCUITPY_WIFI_PASSWORD") |
| 22 | +aio_username = getenv("ADAFRUIT_AIO_USERNAME") |
| 23 | +aio_key = getenv("ADAFRUIT_AIO_KEY") |
| 24 | + |
| 25 | +if None in [ssid, password, aio_username, aio_key]: |
| 26 | + raise RuntimeError( |
| 27 | + "WiFi and Adafruit IO settings are kept in settings.toml, " |
| 28 | + "please add them there. The settings file must contain " |
| 29 | + "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', " |
| 30 | + "'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum." |
| 31 | + ) |
| 32 | + |
17 | 33 | alarm_out = DigitalInOut(board.A1)
|
18 | 34 | alarm_out.direction = Direction.OUTPUT
|
19 | 35 | alarm_out.value = False
|
|
23 | 39 | button = Debouncer(button_in)
|
24 | 40 |
|
25 | 41 |
|
26 |
| -# Get wifi details and more from a secrets.py file |
27 |
| -try: |
28 |
| - from secrets import secrets |
29 |
| -except ImportError: |
30 |
| - print("WiFi secrets are kept in secrets.py, please add them there!") |
31 |
| - raise |
32 |
| - |
33 | 42 | print("Adafruit Raspberry Pi In Stock Tweet Listener")
|
34 | 43 |
|
35 | 44 | # import your bearer token
|
36 |
| -bear = secrets['bearer_token'] |
| 45 | +bearer_token = getenv('bearer_token') |
37 | 46 |
|
38 | 47 | # query URL for tweets. looking for hashtag partyparrot sent to a specific username
|
39 | 48 | # disabling line-too-long because queries for tweet_query & TIME_URL cannot have line breaks
|
40 | 49 | # pylint: disable=line-too-long
|
41 | 50 | tweet_query = 'https://api.twitter.com/2/tweets/search/recent?query=In Stock at Adafruit from:rpilocator&tweet.fields=created_at'
|
42 | 51 |
|
43 |
| -headers = {'Authorization': 'Bearer ' + bear} |
| 52 | +headers = {'Authorization': 'Bearer ' + bearer_token} |
44 | 53 |
|
45 |
| -print("Connecting to %s"%secrets["ssid"]) |
46 |
| -wifi.radio.connect(secrets["ssid"], secrets["password"]) |
47 |
| -print("Connected to %s!"%secrets["ssid"]) |
48 |
| -print("My IP address is", wifi.radio.ipv4_address) |
| 54 | +print(f"Connecting to {ssid}") |
| 55 | +wifi.radio.connect(ssid, password) |
| 56 | +print(f"Connected to {ssid}!") |
| 57 | +print(f"My IP address is {wifi.radio.ipv4_address}") |
49 | 58 |
|
50 | 59 | pool = socketpool.SocketPool(wifi.radio)
|
51 | 60 | requests = adafruit_requests.Session(pool, ssl.create_default_context())
|
52 | 61 |
|
53 | 62 | # gets and formats time from adafruit.io
|
54 |
| -aio_username = secrets["aio_username"] |
55 |
| -aio_key = secrets["aio_key"] |
56 |
| -location = secrets.get("timezone", None) |
| 63 | +location = getenv("timezone", None) |
57 | 64 | TIME_URL = "https://io.adafruit.com/api/v2/%s/integrations/time/strftime?x-aio-key=%s" % (aio_username, aio_key)
|
58 | 65 | TIME_URL += "&fmt=%25Y-%25m-%25dT%25H%3A%25M%3A%25S.%25L%25j%25u%25z%25Z"
|
59 | 66 |
|
|
132 | 139 |
|
133 | 140 | else:
|
134 | 141 | # if it's not new, then the wait continues
|
135 |
| - no_tweet_text = ("No stock in last hour :( Last stock: %s" % (timestamp)) |
| 142 | + no_tweet_text = "No stock in last hour :( Last stock: %s" % (timestamp) |
136 | 143 | text_area.text = "\n".join(wrap_text_to_lines(no_tweet_text, 21))
|
137 | 144 | print("no new in stock notifications :(")
|
138 | 145 | # updates tweet ID
|
139 | 146 | last_value = value
|
140 | 147 | # if the tweet wasn't today
|
141 | 148 | else:
|
142 | 149 | # if it's not new, then the wait continues
|
143 |
| - no_tweet_text = ("No stock in last hour :( Last stock: %s" % (timestamp)) |
| 150 | + no_tweet_text = "No stock in last hour :( Last stock: %s" % (timestamp) |
144 | 151 | text_area.text = "\n".join(wrap_text_to_lines(no_tweet_text, 21))
|
145 | 152 | print("no new in stock notifications :(")
|
0 commit comments