|
| 1 | +# SPDX-FileCopyrightText: 2023 Trevor Beaton for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +import time |
| 5 | +import ssl |
| 6 | +import math |
| 7 | +import board |
| 8 | +import microcontroller |
| 9 | +import wifi |
| 10 | +import socketpool |
| 11 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 12 | +from adafruit_io.adafruit_io import IO_MQTT |
| 13 | +from adafruit_adxl34x import ADXL345 |
| 14 | +from adafruit_lc709203f import LC709203F, PackSize |
| 15 | + |
| 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"] |
| 25 | + |
| 26 | +# Wi-Fi |
| 27 | +try: |
| 28 | + print("Connecting to %s" % secrets["ssid"]) |
| 29 | + wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 30 | + print("Connected to %s!" % secrets["ssid"]) |
| 31 | +# Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad. |
| 32 | +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) |
| 35 | + microcontroller.reset() |
| 36 | + |
| 37 | +# Create a socket pool |
| 38 | +pool = socketpool.SocketPool(wifi.radio) |
| 39 | + |
| 40 | +# Initialize a new MQTT Client object |
| 41 | +mqtt_client = MQTT.MQTT( |
| 42 | + broker="io.adafruit.com", |
| 43 | + username=secrets["aio_username"], |
| 44 | + password=secrets["aio_key"], |
| 45 | + socket_pool=pool, |
| 46 | + ssl_context=ssl.create_default_context(), |
| 47 | +) |
| 48 | + |
| 49 | +# Initialize Adafruit IO MQTT "helper" |
| 50 | +io = IO_MQTT(mqtt_client) |
| 51 | + |
| 52 | +try: |
| 53 | + # If Adafruit IO is not connected... |
| 54 | + if not io.is_connected: |
| 55 | + # Connect the client to the MQTT broker. |
| 56 | + print("Connecting to Adafruit IO...") |
| 57 | + io.connect() |
| 58 | + print("Connected to Adafruit IO!") |
| 59 | +except Exception as e: # pylint: disable=broad-except |
| 60 | + print("Failed to get or send data, or connect. Error:", e, |
| 61 | + "\nBoard will hard reset in 30 seconds.") |
| 62 | + time.sleep(30) |
| 63 | + microcontroller.reset() |
| 64 | + |
| 65 | +threshold = 25 # set threshold value here |
| 66 | +time_interval = 0.5 # set the time interval in seconds |
| 67 | + |
| 68 | +# create the I2C bus object |
| 69 | +i2c = board.STEMMA_I2C() |
| 70 | +# For ADXL345 |
| 71 | +accelerometer = ADXL345(i2c) |
| 72 | + |
| 73 | +battery_monitor = LC709203F(i2c) |
| 74 | +battery_monitor.pack_size = PackSize.MAH400 |
| 75 | + |
| 76 | +t0 = time.monotonic() |
| 77 | + |
| 78 | +while True: |
| 79 | + x, y, z = accelerometer.acceleration |
| 80 | + t1 = time.monotonic() |
| 81 | + dt = t1 - t0 |
| 82 | + |
| 83 | + total_acceleration = math.sqrt(x**2 + y**2 + z**2) |
| 84 | + if total_acceleration >= threshold: |
| 85 | + print("Battery Percent: {:.2f} %".format(battery_monitor.cell_percent)) |
| 86 | + print("Collision strength: %.2f" % total_acceleration) |
| 87 | + io.publish("punch-strength", total_acceleration) |
| 88 | + |
| 89 | + # add code here to trigger an event or alert the user |
| 90 | + t0 = t1 |
| 91 | + time.sleep(time_interval) |
0 commit comments