Hello! My name is Shonie, I'm 15 years old from Canada. Working more efficiently can help me get school work finished so I can build projects like this. I want to try to improve the working "a-fish-in-sea" (efficiency) of users, while helping them live a healthier life.
Program detects when the user walks into their office, turns on light and provides the user with predicted working efficiency based on data fetched from the Fitbit API.
I'm using this everyday to collect more data and adjust values to improve the accuracy of the program. Predictions are based on the user's sleep, physical activity and age.
(Nutrition, heartrate, burned calories, and user input will be added in the future to improve accuracy)
Efficiency will also be printed on Oplà MKR Carrier in the future.
Building the RequestThe program works by sending a request that the program crafts, then sends to the Fitbit API
//⑄⑄ Getting date to build string ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
int dd = rtc.getDay(); // Get current date & store as int
int mm = rtc.getMonth(); // Get current month & store as int
int yy = rtc.getYear(); // Get current year & store as int
//⑄⑄ Building parts of string ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
String pt1 = "/1/user/"; // Start of request
String pt2 = "/1.2/user/"; // Start of request (some reqs use different start)
String dash = "-";
String endpt = ".json"; // End part of request
String yypt1 = "20"; // Put in front of 'yy' to make yyyy (Change to '21' in 2100)
//⑄⑄ Specific parts ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
String sleepSTR = "/sleep/date/";
String activitySTR = "/activities/date/";
String ageSTR = "/profile/age";
String stepsSTR = "/activities/steps/date/today/1d/15min";
String calsSTR = "/activities/calories/date/today/1d/15min";
String heartSTR = "/activities/heart/date/today/1d/1min";
String prevsleepSTR = "/sleep/list.json?afterDate=2021-07-27&sort=desc&offset=0&limit=1";
//⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
//⑄⑄ Building Strings ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
//⑄⑄ Sleep data from today ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
String ApiSleep = pt2 + userid + sleepSTR + yypt1 + yy + dash + mm + dash + dd + endpt;
//⑄⑄ Intraday Step data ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
String ApiStep = pt1 + userid + stepsSTR + endpt;
//⑄⑄ Age data ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
String ApiAge = pt1 + userid + ageSTR + endpt;
//⑄⑄ Intraday Calorie data ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
String ApiCal = pt1 + userid + calsSTR + endpt;
//⑄⑄ Intraday Heart Rate data ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
String ApiHeart = pt1 + userid + heartSTR + endpt;
//⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
Sending the RequestAfter making the request, the program sends out multiple requests to get the different values it needs to predict the user's working efficiency.
//⑄⑄ Send Sleep request ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
if (req1 == true) { // To only send once
client.beginRequest();
client.get(ApiSleep);
client.sendHeader("accept", "application/json");
client.sendHeader("authorization", HeaderAccessToken);
client.endRequest();
// Read the status code and body of the response
int statusCode = client.responseStatusCode();
String sleepRES = client.responseBody();
DynamicJsonDocument doc(6144);
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, sleepRES);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
JsonObject sleepdoc = doc["sleep"][0];
// Sleep duration in seconds
long sleepDuration = sleepdoc["duration"];
// Sleep efficiency out of 100
int sleepEfficiency = sleepdoc["efficiency"];
// Serial.println(sleepEfficiency);
// Serial.print("GET Status code: ");
// Serial.println(statusCode);
// Serial.print("GET Response: ");
// Serial.println(sleepRES);
delay(3000);
// Get out of Sleep Request loop
req1 = false;
// Set 2nd loop Flag to true
req2 = true;
}
//⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
The request you see above is one of six requests the program sends out.
Starting the request buildingA PIR motion sensor detects when the user walks into their office, to get the program to start building the requests to send to Fitbit, as soon as the PIR detects a person, it will begin building the request.
//⑄⑄ Light On ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
if (k == 1) {
//Turn Light on
servo.write(130);
delay(500);
//Start requesting sequence
req1 = true;
//Turn back to middle position
servo.write(90);
delay(500);
k = k + 1;
Flagsreq1 is a flag to start the building and sending process, as soon as one request is made, the program starts making the next one, this is done by this code:
if (req1 == true) {
// [SENDING SLEEP REQUEST]
// Get out of Sleep Request loop
req1 = false;
// Set 2nd loop Flag to true
req2 = true;
}
//⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
//⑄⑄ Send Step request ⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄⑄♡
if (req2 == true) { // To only send once
// [SENDING STEP REQUEST]
Comments