3
3
# SPDX-License-Identifier: MIT
4
4
import time
5
5
import ssl
6
- import board
7
6
import math
7
+ import board
8
+ import microcontroller
8
9
import wifi
9
10
import socketpool
10
11
import adafruit_minimqtt .adafruit_minimqtt as MQTT
22
23
aio_username = secrets ["aio_username" ]
23
24
aio_key = secrets ["aio_key" ]
24
25
25
-
26
26
# Wi-Fi
27
27
try :
28
28
print ("Connecting to %s" % secrets ["ssid" ])
34
34
time .sleep (5 )
35
35
microcontroller .reset ()
36
36
37
-
38
-
39
37
# Create a socket pool
40
38
pool = socketpool .SocketPool (wifi .radio )
41
39
51
49
# Initialize Adafruit IO MQTT "helper"
52
50
io = IO_MQTT (mqtt_client )
53
51
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
52
try :
62
53
# If Adafruit IO is not connected...
63
54
if not io .is_connected :
64
55
# Connect the client to the MQTT broker.
65
56
print ("Connecting to Adafruit IO..." )
66
57
io .connect ()
67
-
58
+ print ( "Connected to Adafruit IO!" )
68
59
except 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 ()
73
64
74
65
threshold = 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
76
67
77
68
# create the I2C bus object
78
69
i2c = board .STEMMA_I2C ()
@@ -82,29 +73,18 @@ def connected(client):
82
73
battery_monitor = LC709203F (i2c )
83
74
battery_monitor .pack_size = PackSize .MAH400
84
75
85
-
86
- # initialize velocity variables to zero
87
- velocity_x = 0
88
- velocity_y = 0
89
- velocity_z = 0
90
-
91
76
t0 = time .monotonic ()
92
77
93
78
while True :
94
79
x , y , z = accelerometer .acceleration
95
80
t1 = time .monotonic ()
96
81
dt = t1 - t0
97
- velocity_x += x * dt
98
- velocity_y += y * dt
99
- velocity_z += z * dt
100
82
101
83
total_acceleration = math .sqrt (x ** 2 + y ** 2 + z ** 2 )
102
84
if total_acceleration >= threshold :
103
85
print ("Battery Percent: {:.2f} %" .format (battery_monitor .cell_percent ))
104
86
print ("Collision strength: %.2f" % total_acceleration )
105
- print ("Velocity X: %.2f Y: %.2f Z: %.2f \n " % (velocity_x , velocity_y , velocity_z ))
106
87
io .publish ("punch-strength" , total_acceleration )
107
- io .publish ("punch-velocity" , velocity_x )
108
88
109
89
# add code here to trigger an event or alert the user
110
90
t0 = t1
0 commit comments