Fixed errors

This commit is contained in:
TrevKnows 2023-02-08 10:13:38 -05:00
parent 44fd33a944
commit cf6f706392

View file

@ -3,8 +3,9 @@
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
import time import time
import ssl import ssl
import board
import math import math
import board
import microcontroller
import wifi import wifi
import socketpool import socketpool
import adafruit_minimqtt.adafruit_minimqtt as MQTT import adafruit_minimqtt.adafruit_minimqtt as MQTT
@ -22,7 +23,6 @@ except ImportError:
aio_username = secrets["aio_username"] aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"] aio_key = secrets["aio_key"]
# Wi-Fi # Wi-Fi
try: try:
print("Connecting to %s" % secrets["ssid"]) print("Connecting to %s" % secrets["ssid"])
@ -34,8 +34,6 @@ except Exception as e: # pylint: disable=broad-except
time.sleep(5) time.sleep(5)
microcontroller.reset() microcontroller.reset()
# Create a socket pool # Create a socket pool
pool = socketpool.SocketPool(wifi.radio) pool = socketpool.SocketPool(wifi.radio)
@ -51,20 +49,13 @@ mqtt_client = MQTT.MQTT(
# Initialize Adafruit IO MQTT "helper" # Initialize Adafruit IO MQTT "helper"
io = IO_MQTT(mqtt_client) io = IO_MQTT(mqtt_client)
def connected(client):
print("Connected to Adafruit IO!")
# Set up the callback methods above
io.on_connect = connected
try: try:
# If Adafruit IO is not connected... # If Adafruit IO is not connected...
if not io.is_connected: if not io.is_connected:
# Connect the client to the MQTT broker. # Connect the client to the MQTT broker.
print("Connecting to Adafruit IO...") print("Connecting to Adafruit IO...")
io.connect() io.connect()
print("Connected to Adafruit IO!")
except Exception as e: # pylint: disable=broad-except except Exception as e: # pylint: disable=broad-except
print("Failed to get or send data, or connect. Error:", e, print("Failed to get or send data, or connect. Error:", e,
"\nBoard will hard reset in 30 seconds.") "\nBoard will hard reset in 30 seconds.")
@ -72,7 +63,7 @@ except Exception as e: # pylint: disable=broad-except
microcontroller.reset() microcontroller.reset()
threshold = 25 # set threshold value here threshold = 25 # set threshold value here
time_interval = 0.1 # set the time interval in seconds time_interval = 0.5 # set the time interval in seconds
# create the I2C bus object # create the I2C bus object
i2c = board.STEMMA_I2C() i2c = board.STEMMA_I2C()
@ -82,29 +73,18 @@ accelerometer = ADXL345(i2c)
battery_monitor = LC709203F(i2c) battery_monitor = LC709203F(i2c)
battery_monitor.pack_size = PackSize.MAH400 battery_monitor.pack_size = PackSize.MAH400
# initialize velocity variables to zero
velocity_x = 0
velocity_y = 0
velocity_z = 0
t0 = time.monotonic() t0 = time.monotonic()
while True: while True:
x, y, z = accelerometer.acceleration x, y, z = accelerometer.acceleration
t1 = time.monotonic() t1 = time.monotonic()
dt = t1 - t0 dt = t1 - t0
velocity_x += x*dt
velocity_y += y*dt
velocity_z += z*dt
total_acceleration = math.sqrt(x**2 + y**2 + z**2) total_acceleration = math.sqrt(x**2 + y**2 + z**2)
if total_acceleration >= threshold: if total_acceleration >= threshold:
print("Battery Percent: {:.2f} %".format(battery_monitor.cell_percent)) print("Battery Percent: {:.2f} %".format(battery_monitor.cell_percent))
print("Collision strength: %.2f" % total_acceleration) print("Collision strength: %.2f" % total_acceleration)
print("Velocity X: %.2f Y: %.2f Z: %.2f \n" % (velocity_x, velocity_y, velocity_z))
io.publish("punch-strength", total_acceleration) io.publish("punch-strength", total_acceleration)
io.publish("punch-velocity", velocity_x)
# add code here to trigger an event or alert the user # add code here to trigger an event or alert the user
t0 = t1 t0 = t1