Using Amazon Machine Learning to Predict the Best Time of Day for Exercise – Pt 3: Automating the Model with Alexa

Integration with Alexa allows a user to obtain a workout recommendation (and create a machine learning model) all by voice command.

002[su_note note_color=”#d3d3d3″]Note: This is the third post about using Amazon Machine Learning to predict workout intensity. Check out Part 1 (Overview) and Part 2 (Building the Machine Learning Model) for background. A working model is available via web and Alexa. Code can be found/downloaded from my Hackster site.[/su_note]

After I was able to build a working model, I needed to come up a way to automate the model. I originally planned to allow access through my website, but decided to use Alexa in addition to the website link.

Note: The process of creating an Alexa skill isn’t too complicated (if you have experience building lambda functions). That being said, I suggest you start by building a sample skill – such as the Fact Skill example. Also, be sure to read and follow the certification requirements.

Alexa, AWS, and the exposed Fitbit APIs provided a mechanism to build a model and return results for a specific user – all initiated by voice.

Step 1 – Linking the user’s Fitbit account to the skill

A user has to link his/her Fitbit account to the skill before s/he can (a) build a specific machine learning model based on their history and (b) get a workout recommendation. Step 1 covers the logic for this functionality.

Click image to enlarge

Slide7

Continue reading “Using Amazon Machine Learning to Predict the Best Time of Day for Exercise – Pt 3: Automating the Model with Alexa”

Using Amazon Machine Learning to Predict the Best Time of Day for Exercise – Pt 1: Overview

A simple application that uses your Fitbit data to recommend the best time of day to workout.

Best Time of The Day Workout Recommendation[su_note note_color=”#d3d3d3″]Note: This is the first post about using Amazon Machine Learning to predict workout intensity. Part 2 will cover the design of the Machine Learning Model. The third post will cover the Lambda and Alexa code used to automate the model. A working model is available via web and Alexa. Code can be found/downloaded from my Hackster site.[/su_note]

A few months ago, one of my former co-workers1 published a brilliant post on Linked about using machine learning to predict his mood. It got me thinking – with all the data at my fingertips, what could – or should – I attempt to predict?

I ultimately decided to focus my attention on fitness. Specifically, I wanted to determine the most optimal time to workout on a given day.

Why? Well, when it comes to fitness, we live in a world of paradoxes.

We have the best fitness information available, the most knowledgeable doctors, and affordable devices to track our health.

We have the best workout options: gyms in every neighborhood; gyms at our offices; online and DVD courses to support fitness at home.

We even have more flexibility in our schedules. Many people work from home at least once a week. Our gyms open earlier and close later. Our employers support and encourage an active lifestyle.

Yet despite this, people struggle to find time to work out. And when we do get to the gym, we are either rushed or distracted – so our workouts become less impactful to our overall health.

texting-in-the-gym-300x300[1]

I built my app to help myself, and others, find the most optimal time of the day to workout. Are my workouts more efficient in the morning? Or do I get better results by working out at noon?

Each user is different, so I had to build the app so that each user’s prediction model was customized to their activities. This was done by building a custom model for each individual user of the application.

I’ll talk in detail about how I designed the machine learning model and how the app is architected in parts 2 and 3 of this topic. In the meantime, feel free to use the web based version of the app or the Alexa skill.

A Fitbit Low Battery SMS Notification using AWS – Part 3

In Part 1, I introduced an app that sends an SMS notification when your Fitbit battery is low. Part 2 covered the Authentication Flow of the process. This post describes how the device status is checked and communicated to the user.

Fitbit-SMS-Sched2The flow is as follows:

  1. The CloudWatch Scheduler kicks off a Lambda job that pulls registered users from the DynamoDB table. This information is based to a SNS topic, which in turn…
  2. …kicks off the Lambda function that obtains the Fitbit device info. The Fitbit api is called, and the resulting message is processed by the Lambda function. The device information is stored in and retrieved from the DynamoDB table as needed for processing and history tracking.
  3. If the result was a success then the a message is sent to the SNS topic for the subscribed user – assuming that the battery status is low AND the user has not already been notified.
  4. If a “Token Expired” error is returned, then the Lambda function passes the required information to the Refresh User SNS topic. The Lambda function again calls a Fitbit API; this time to refresh the token.
  5. If the token is refreshed, then the information is saved to the DynamoDB table and passed to the Get Device Info Topic (starting the process all over again)
  6. If a failure occurs, then a message is sent to the user, directing them to re-authorize the application to access their Fitbit account.

Continue reading “A Fitbit Low Battery SMS Notification using AWS – Part 3”

A Fitbit Low Battery SMS Notification using AWS – Part 2

In Part 1, I introduced an app that sends an SMS notification when your Fitbit battery is low. Part 2 covers the Authentication Flow of the process.

Fitbit-SMS-Auth

The flow is as follows:

  1. User accesses the sign-up page (and by default, provides authorization via Fitbit.com). The request is passed through an API Gateway to a Lambda function. The Lambda function calls a Fitbit api to authenticate the user.
  2. The Fitbit authentication returns to the Lambda code. From there: (3) the token is saved to a DynamoDB database, (4) a message is placed on a SNS topic, and (5) a success response is sent back to the webpage
  3. The user information (access_token, refresh_token, and mobile number) is saved to a DynamoDB table
  4. An SNe1079abb478cc4e04d13615bc10aa4ca[1]S topic receives the mobile number as a message
  5. A success message is sent back to the end user.
  6. The SNS topic (step 4) calls a second Lambda function.
  7. The Create_User_Topic lambda function creates a sns topic for the newly authenticated user.
  8. Once the topic is created, a notice is sent to the user’s mobile device – asking to confirm subscription.
  9. The user confirms the subscription.

Continue reading “A Fitbit Low Battery SMS Notification using AWS – Part 2”

A Fitbit Low Battery SMS Notification using AWS – Part 1

765a3f73cd8c5b0c0bc96bc3cd094740[1]

A few weeks ago, my Fitbit (one of my new favorite devices) died.

Not died, as in bricked; my battery died.

That was a bit surprising, as I am signed up for low battery notifications. Turns out, I did get a notice – in an email. Problem was, I wasn’t checking my email that day.1

fitbit_notificationThe next day, I went back to the Fitbit website to sign-up for SMS notifications; unfortunately, Fitbit doesn’t provide a low battery notification via SMS.

So, I built my own, using a handful of AWS services (Lambda, SNS, API Gateway, and DynamoDB) and exposed Fitbit APIs.

So far, it works well. After subscribing to the service, I got an introductory note telling my my battery status. A few hours later, I received a second notice – letting me know that my battery was low. Continue reading “A Fitbit Low Battery SMS Notification using AWS – Part 1”