The reason behind this example was that I needed to have a mechanism for reminding myself to regularly get some steps in every hour. While this feature is supported by newer Fitbit devices, my Fitbit Charge was missing the Reminders to Move function. My search for an approach to support this service without the need to manage servers etc. lead me to investigating AWS Lambda. AWS Lambda is a part of the AWS compute offering that lets one run code in response to events without having to provision or manage servers.
The process for getting the movenow notification occur through two related flows; see diagram below.
The first flow pertains to capturing steps data from the Fitbit device. This part occurs automatically when the device is paired with a smartphone running the Fitbit app. The device will sync periodically (approx. every 15 minutes) with the app and as long as the smartphone is Internet-connected it will upload the latest step data to the Fitbit server.
The second flow is what this example tackles. A scheduled rule is created in AWS CloudWatch to invoke an AWS Lambda function on an hourly basis. The AWS Lambda function retrieves step data from Fitbit servers and determines based on the step threshold value (i.e the minimum number of steps to take within an hour) if the user needs to be notified. If a notification needs to be sent, then the AWS Lambda function will use AWS Simple Notification Service (SNS) to send an SMS text through a predefined topic in SNS.
These two flows are depicted in the diagram below:
The source code is based on a NodeJS package structure and uses NPM to import libraries that facilitate certain core functions. The following are the key files needed:
- src/lambda.js: The lambda function to transform the payload
- package.json: Defines the packaging tasks and the npm modules in use
- Need to create an AWS account
- Need to setup AWS CLI with the credentials of your AWS account
- NodeJS and NPM need to be installed
- Need to have an app registered with Fitbit in order to use the Web API.
- Obtain an access token from Fitbit to invoke the API. You will want to obtain this token ahead of time (providing it with a large expiration period) as it will be needed to invoke the Fitbit API to obtain step data.
- Define the AWS Lambda function
- Create Amazon Simple Notification Service (SNS) Topic through the console
- Create AWS CloudWatch event rule. The rule should define the frequency of execution through a schedule (fixed rate or Cron expression). The target should be the Lambda function created in Step 1 with the Configure Input having a JSON object containing the configuration object (event input values) of the function. The following is an example configuration:
{
"accessToken": "eyJhbGciOiJ",
"minHourlySteps": 250,
"snsTopicArn": "arn:aws:sns:us-east-1:999999999:movenow",
"snsTopicRegion": "us-east-1"
}- Clone or fork this repository
- Ensure that you've followed the Requirements section above
- Run
npm run buildto install dependencies, package the Lambda function and node modules in a zip and finally deploys the Lambda function to AWS using the AWS CLI.
###License
See LICENSE for further details.
