forked from tomtaozhou/ubi_sports-sensing-device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubi_sports-sensing-device.ino
156 lines (130 loc) · 3.99 KB
/
ubi_sports-sensing-device.ino
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <M5StickC.h>
#include <WiFiManager.h>
#include <WiFiClientSecure.h>
#include <base64.h>
const int httpsPort = 443;
const char* host = "example.com";
const char* wpUsername = "your_wordpress_username";
const char* wpAppPassword = "your_generated_app_password";
WiFiManager wifiManager;
WiFiClientSecure client;
float bodyWeight = 70.0;
int stepCount = 0;
int sitUpCount = 0;
float caloriesBurned = 0;
float previousAy = 0;
float previousAz = 0;
bool dataChanged = false;
unsigned long lastCheckTime = 0;
const int CHECK_INTERVAL = 10000;
String lastPostedStatus;
const int MAX_RETRIES = 3;
void showMessage(String msg) {
M5.Lcd.setRotation(1);
M5.Lcd.setCursor(0, 10, 2);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.println(msg);
}
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
Serial.println(myWiFiManager->getConfigPortalSSID());
showMessage("Please connect to this AP\nto config WiFi\nSSID: " + myWiFiManager->getConfigPortalSSID());
}
void setupWiFi() {
wifiManager.setAPCallback(configModeCallback);
bool doManualConfig = false;
showMessage("Push POWER button to enter Wifi config.");
for(int i=0 ; i<200 ; i++) {
M5.update();
if (M5.BtnB.wasPressed()) {
doManualConfig = true;
break;
}
delay(10);
}
if (doManualConfig) {
Serial.println("wifiManager.startConfigPortal()");
if (!wifiManager.startConfigPortal()) {
Serial.println("startConfigPortal() connect failed!");
showMessage("Wi-Fi failed.");
return;
}
} else {
showMessage("Wi-Fi connecting...");
Serial.println("wifiManager.autoConnect()");
if (!wifiManager.autoConnect()) {
Serial.println("autoConnect() connect failed!");
showMessage("Wi-Fi failed.");
return;
}
}
String IPaddress = WiFi.localIP().toString();
showMessage("Wi-Fi connected.\nIP Address: " + IPaddress);
}
void postToWordPress(String status) {
if (!client.connect(host, httpsPort)) {
Serial.println("Connection to WordPress failed!");
return;
}
String postData = "title=M5StickC Activity&content=" + status + "&status=publish";
client.println("POST /wp-json/wp/v2/posts HTTP/1.1");
client.println("Host: " + String(host));
client.println("User-Agent: M5StickC");
String basicAuthCredentials = wpUsername + String(":") + wpAppPassword;
client.println("Authorization: Basic " + base64::encode(basicAuthCredentials));
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(postData.length());
client.println();
client.println(postData);
// Read the full response from WordPress for debugging
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
Serial.println(line);
}
}
void setup() {
M5.begin();
M5.Lcd.begin();
M5.Lcd.setRotation(1);
M5.IMU.Init();
Serial.begin(9600);
client.setInsecure();
setupWiFi();
}
void loop() {
float ax, ay, az, gx, gy, gz;
M5.IMU.getAccelData(&ax, &ay, &az);
M5.IMU.getGyroData(&gx, &gy, &gz);
if (ay > 0.5 && previousAy < 0.5) {
stepCount++;
caloriesBurned += (0.035 * bodyWeight / 30);
dataChanged = true;
}
if (az > 0.5 && previousAz < 0.5 && gy > 0.5) {
sitUpCount++;
caloriesBurned += 0.8;
dataChanged = true;
}
previousAy = ay;
previousAz = az;
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(0, 0);
M5.Lcd.println("Steps: " + String(stepCount));
M5.Lcd.println("Sit-ups: " + String(sitUpCount));
M5.Lcd.println("Calories: " + String(caloriesBurned, 2));
if (millis() - lastCheckTime >= CHECK_INTERVAL) {
lastCheckTime = millis();
String currentStatus = "Steps: " + String(stepCount) + ", Sit-ups: " + String(sitUpCount) + ", Calories: " + String(caloriesBurned, 2);
if (currentStatus != lastPostedStatus) {
postToWordPress(currentStatus);
lastPostedStatus = currentStatus;
}
}
delay(1000);
}