Skip to content

Commit 44fd33a

Browse files
committed
First commit
1 parent 014c913 commit 44fd33a

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

CPBoxing_Glove_Tracker/code.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# SPDX-FileCopyrightText: 2023 Trevor Beaton for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
import time
5+
import ssl
6+
import board
7+
import math
8+
import wifi
9+
import socketpool
10+
import adafruit_minimqtt.adafruit_minimqtt as MQTT
11+
from adafruit_io.adafruit_io import IO_MQTT
12+
from adafruit_adxl34x import ADXL345
13+
from adafruit_lc709203f import LC709203F, PackSize
14+
15+
16+
try:
17+
from secrets import secrets
18+
except ImportError:
19+
print("WiFi and Adafruit IO credentials are kept in secrets.py - please add them there!")
20+
raise
21+
22+
aio_username = secrets["aio_username"]
23+
aio_key = secrets["aio_key"]
24+
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+
38+
39+
# Create a socket pool
40+
pool = socketpool.SocketPool(wifi.radio)
41+
42+
# Initialize a new MQTT Client object
43+
mqtt_client = MQTT.MQTT(
44+
broker="io.adafruit.com",
45+
username=secrets["aio_username"],
46+
password=secrets["aio_key"],
47+
socket_pool=pool,
48+
ssl_context=ssl.create_default_context(),
49+
)
50+
51+
# Initialize Adafruit IO MQTT "helper"
52+
io = IO_MQTT(mqtt_client)
53+
54+
def connected(client):
55+
print("Connected to Adafruit IO!")
56+
57+
58+
# Set up the callback methods above
59+
io.on_connect = connected
60+
61+
try:
62+
# If Adafruit IO is not connected...
63+
if not io.is_connected:
64+
# Connect the client to the MQTT broker.
65+
print("Connecting to Adafruit IO...")
66+
io.connect()
67+
68+
except Exception as e: # pylint: disable=broad-except
69+
print("Failed to get or send data, or connect. Error:", e,
70+
"\nBoard will hard reset in 30 seconds.")
71+
time.sleep(30)
72+
microcontroller.reset()
73+
74+
threshold = 25 # set threshold value here
75+
time_interval = 0.1 # set the time interval in seconds
76+
77+
# create the I2C bus object
78+
i2c = board.STEMMA_I2C()
79+
# For ADXL345
80+
accelerometer = ADXL345(i2c)
81+
82+
battery_monitor = LC709203F(i2c)
83+
battery_monitor.pack_size = PackSize.MAH400
84+
85+
86+
# initialize velocity variables to zero
87+
velocity_x = 0
88+
velocity_y = 0
89+
velocity_z = 0
90+
91+
t0 = time.monotonic()
92+
93+
while True:
94+
x, y, z = accelerometer.acceleration
95+
t1 = time.monotonic()
96+
dt = t1 - t0
97+
velocity_x += x*dt
98+
velocity_y += y*dt
99+
velocity_z += z*dt
100+
101+
total_acceleration = math.sqrt(x**2 + y**2 + z**2)
102+
if total_acceleration >= threshold:
103+
print("Battery Percent: {:.2f} %".format(battery_monitor.cell_percent))
104+
print("Collision strength: %.2f" % total_acceleration)
105+
print("Velocity X: %.2f Y: %.2f Z: %.2f \n" % (velocity_x, velocity_y, velocity_z))
106+
io.publish("punch-strength", total_acceleration)
107+
io.publish("punch-velocity", velocity_x)
108+
109+
# add code here to trigger an event or alert the user
110+
t0 = t1
111+
time.sleep(time_interval)

0 commit comments

Comments
 (0)