The documentation for the Fitbit API is available at http://python-fitbit.readthedocs.org/.Let's write a simple example to get today's physical activity:
- The first step is import the fitbit module:
import fitbit
- We have to initialize the fitbit client using the client key, client secret, access_token, and refresh_token earlier in this section:
fbit_client = fitbit.Fitbit(CONSUMER_KEY,
CONSUMER_SECRET,
access_token=ACCESS_TOKEN,
refresh_token=REFRESH_TOKEN)
- According to the Fitbit API documentation, the current day's physical activity can be retrieved using the intraday_time_series() method.
- The required arguments to retrieve the physical activity include the resource that needs to be retrieved; that is, steps, detail_level, that is, the smallest time interval for which the given information needs to be retrieved, start times and the end times.
- The start time is 12:00 a.m. of the current day, and the end time is the current time. We will be making use of the datetime module to get the current time. There is a function named strftime that gives us the current time in the hour:minute format.
Make sure that your Raspberry Pi Zero's OS time is correctly configured with the local time zone settings.
now = datetime.datetime.now()
end_time = now.strftime("%H:%M")
response = fbit_client.intraday_time_series('activities/steps',
detail_level='15min',
start_time="00:00",
end_time=end_time)
- The fitbit client returns a dictionary containing the current day's physical activity and intraday activity in 15-minute intervals:
print(response['activities-steps'][0]['value'])
- This example is available for download along with this chapter as fitbit_client.py. If you have a Fitbit tracker, register an application and test this example for yourself.