33# SPDX-License-Identifier: MIT
44import time
55import ssl
6- import board
76import math
7+ import board
8+ import microcontroller
89import wifi
910import socketpool
1011import adafruit_minimqtt .adafruit_minimqtt as MQTT
2223aio_username = secrets ["aio_username" ]
2324aio_key = secrets ["aio_key" ]
2425
25-
2626# Wi-Fi
2727try :
2828 print ("Connecting to %s" % secrets ["ssid" ])
3434 time .sleep (5 )
3535 microcontroller .reset ()
3636
37-
38-
3937# Create a socket pool
4038pool = socketpool .SocketPool (wifi .radio )
4139
5149# Initialize Adafruit IO MQTT "helper"
5250io = IO_MQTT (mqtt_client )
5351
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-
6152try :
6253 # If Adafruit IO is not connected...
6354 if not io .is_connected :
6455 # Connect the client to the MQTT broker.
6556 print ("Connecting to Adafruit IO..." )
6657 io .connect ()
67-
58+ print ( "Connected to Adafruit IO!" )
6859except Exception as e : # pylint: disable=broad-except
69- print ("Failed to get or send data, or connect. Error:" , e ,
70- "\n Board will hard reset in 30 seconds." )
71- time .sleep (30 )
72- microcontroller .reset ()
60+ print ("Failed to get or send data, or connect. Error:" , e ,
61+ "\n Board will hard reset in 30 seconds." )
62+ time .sleep (30 )
63+ microcontroller .reset ()
7364
7465threshold = 25 # set threshold value here
75- time_interval = 0.1 # set the time interval in seconds
66+ time_interval = 0.5 # set the time interval in seconds
7667
7768# create the I2C bus object
7869i2c = board .STEMMA_I2C ()
@@ -82,29 +73,18 @@ def connected(client):
8273battery_monitor = LC709203F (i2c )
8374battery_monitor .pack_size = PackSize .MAH400
8475
85-
86- # initialize velocity variables to zero
87- velocity_x = 0
88- velocity_y = 0
89- velocity_z = 0
90-
9176t0 = time .monotonic ()
9277
9378while True :
9479 x , y , z = accelerometer .acceleration
9580 t1 = time .monotonic ()
9681 dt = t1 - t0
97- velocity_x += x * dt
98- velocity_y += y * dt
99- velocity_z += z * dt
10082
10183 total_acceleration = math .sqrt (x ** 2 + y ** 2 + z ** 2 )
10284 if total_acceleration >= threshold :
10385 print ("Battery Percent: {:.2f} %" .format (battery_monitor .cell_percent ))
10486 print ("Collision strength: %.2f" % total_acceleration )
105- print ("Velocity X: %.2f Y: %.2f Z: %.2f \n " % (velocity_x , velocity_y , velocity_z ))
10687 io .publish ("punch-strength" , total_acceleration )
107- io .publish ("punch-velocity" , velocity_x )
10888
10989 # add code here to trigger an event or alert the user
11090 t0 = t1
0 commit comments