-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
86 lines (73 loc) · 2.28 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import time
import os
import json
import board
import adafruit_connection_manager
import wifi
import adafruit_mpu6050
import busio
from board import *
import adafruit_requests
SCL = board.IO1
SDA = board.IO0
# Wi-Fi details
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
# Supabase credentials
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_API_KEY = os.getenv("SUPABASE_API_KEY")
TABLE_URL = f"{SUPABASE_URL}/rest/v1/528"
# Initialize MPU6050
i2c = busio.I2C(SCL, SDA)
mpu = adafruit_mpu6050.MPU6050(i2c)
# Initialize Adafruit requests session
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
requests = adafruit_requests.Session(pool, ssl_context)
# Number of readings
readings_count = 4
duration = 6
try:
wifi.radio.connect(ssid, password)
except OSError as e:
print(f"❌ OSError: {e}")
print("✅ Wifi connected!")
for i in range(readings_count):
print("Starting Reading", i + 1)
start_time = time.monotonic()
data = []
while time.monotonic() - start_time < duration:
elapsed_time = time.monotonic() - start_time
accel_x, accel_y, accel_z = mpu.acceleration
gyro_x, gyro_y, gyro_z = mpu.gyro
reading = {
"id": i + 1, # Incrementing ID for each set of readings
"t(s)": elapsed_time,
"aX": accel_x,
"aY": accel_y,
"aZ": accel_z,
"gX": gyro_x,
"gY": gyro_y,
"gZ": gyro_z
}
data.append(reading)
print("%.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f" % (
elapsed_time, accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z))
time.sleep(0.01)
print("End of Reading", i + 1)
# Send readings to Supabase
headers = {
"apikey": SUPABASE_API_KEY,
"Content-Type": "application/json",
}
response = requests.post(TABLE_URL, headers=headers, json=data)
if response.status_code == 201:
print("Data sent successfully!")
else:
print("Failed to send data:", response.text)
response.close()
if i < readings_count - 1:
print("Preparing for the next reading...")
time.sleep(2)
# Close the requests session
# requests.close()