|
1 | 1 | # SPDX-FileCopyrightText: 2023 Trevor Beaton for Adafruit Industries
|
2 | 2 | #
|
3 | 3 | # SPDX-License-Identifier: MIT
|
| 4 | +import os |
4 | 5 | import time
|
5 | 6 | import ssl
|
6 | 7 | import math
|
|
13 | 14 | from adafruit_adxl34x import ADXL345
|
14 | 15 | from adafruit_lc709203f import LC709203F, PackSize
|
15 | 16 |
|
16 |
| - |
17 |
| -try: |
18 |
| - from secrets import secrets |
19 |
| -except ImportError: |
20 |
| - print("WiFi and Adafruit IO credentials are kept in secrets.py - please add them there!") |
21 |
| - raise |
22 |
| - |
23 |
| -aio_username = secrets["aio_username"] |
24 |
| -aio_key = secrets["aio_key"] |
| 17 | +aio_username = os.getenv('aio_username') |
| 18 | +aio_key = os.getenv('aio_key') |
25 | 19 |
|
26 | 20 | # Wi-Fi
|
27 | 21 | try:
|
28 |
| - print("Connecting to %s" % secrets["ssid"]) |
29 |
| - wifi.radio.connect(secrets["ssid"], secrets["password"]) |
30 |
| - print("Connected to %s!" % secrets["ssid"]) |
| 22 | + print("Connecting to %s" % os.getenv('CIRCUITPY_WIFI_SSID')) |
| 23 | + wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) |
| 24 | + print("Connected to %s!" % os.getenv('CIRCUITPY_WIFI_SSID')) |
31 | 25 | # Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad.
|
32 | 26 | except Exception as e: # pylint: disable=broad-except
|
33 |
| - print("Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 5 seconds.") |
34 |
| - time.sleep(5) |
| 27 | + print("Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 30 seconds.") |
| 28 | + time.sleep(30) |
35 | 29 | microcontroller.reset()
|
36 | 30 |
|
37 | 31 | # Create a socket pool
|
|
40 | 34 | # Initialize a new MQTT Client object
|
41 | 35 | mqtt_client = MQTT.MQTT(
|
42 | 36 | broker="io.adafruit.com",
|
43 |
| - username=secrets["aio_username"], |
44 |
| - password=secrets["aio_key"], |
| 37 | + username= aio_username, |
| 38 | + password= aio_key, |
45 | 39 | socket_pool=pool,
|
46 | 40 | ssl_context=ssl.create_default_context(),
|
47 | 41 | )
|
|
50 | 44 | io = IO_MQTT(mqtt_client)
|
51 | 45 |
|
52 | 46 | try:
|
53 |
| - # If Adafruit IO is not connected... |
54 | 47 | if not io.is_connected:
|
55 | 48 | # Connect the client to the MQTT broker.
|
56 | 49 | print("Connecting to Adafruit IO...")
|
57 | 50 | io.connect()
|
58 | 51 | print("Connected to Adafruit IO!")
|
59 | 52 | except Exception as e: # pylint: disable=broad-except
|
60 | 53 | print("Failed to get or send data, or connect. Error:", e,
|
61 |
| - "\nBoard will hard reset in 30 seconds.") |
| 54 | + "\nBoard will hard reset in 30 seconds./n") |
62 | 55 | time.sleep(30)
|
63 | 56 | microcontroller.reset()
|
64 | 57 |
|
65 |
| -threshold = 25 # set threshold value here |
| 58 | +threshold = 20 # set threshold value here |
66 | 59 | time_interval = 0.5 # set the time interval in seconds
|
67 | 60 |
|
68 | 61 | # create the I2C bus object
|
69 | 62 | i2c = board.STEMMA_I2C()
|
| 63 | + |
70 | 64 | # For ADXL345
|
71 | 65 | accelerometer = ADXL345(i2c)
|
72 | 66 |
|
| 67 | +# To monitor the battery |
73 | 68 | battery_monitor = LC709203F(i2c)
|
74 | 69 | battery_monitor.pack_size = PackSize.MAH400
|
75 | 70 |
|
|
0 commit comments