Let's build a visual aid where we display the number of steps taken in a given day using an LED strip. The LED strip would light up like a progress bar based on the daily physical activity.
- The first step is importing the requisite libraries while building the visual aid. This includes the fitbit and blinkt libraries. We will also import some additional libraries:
import blinkt
import datetime
import fitbit
import time
import schedule
- Make sure that you have the requisite tokens discussed earlier in this section:
CONSUMER_KEY = "INSERT_KEY"
CONSUMER_SECRET = "INSERT_SECRET"
ACCESS_TOKEN = "INSER_TOKEN"
REFRESH_TOKEN = "INSERT_TOKEN"
- A new refresh token is needed every 8 hours. This is a feature of the API's authorization mechanism. Hence, we need a function that gets a new token using the existing token:
def refresh_token():
global REFRESH_TOKEN
oauth = fitbit.FitbitOauth2Client(client_id=CONSUMER_KEY,
client_secret=CONSUMER_SECRET,
refresh_token=REFRESH_TOKEN,
access_token=ACCESS_TOKEN)
REFRESH_TOKEN = oauth.refresh_token()
- In the function refresh_token(), we are making use of the FitbitOauth2Client class to refresh the token. It is important to note that we have made use of the global keyword. The global keyword helps with modifying the REFRESH_TOKEN and enables the use of the new token in other parts of the program. Without the global keyword, the changes made to any variable is restricted to the refresh_token() function.
In general, it is a bad practice to make use of the global keyword. Use it with your best judgement.
- Next, we need a function to retrieve steps using the Fitbit class. We are going to use the same procedure as the previous example. Initialize the fitbit class and retrieve the steps using intraday_time_series:
def get_steps():
num_steps = 0
client = fitbit.Fitbit(CONSUMER_KEY,
CONSUMER_SECRET,
access_token=ACCESS_TOKEN,
refresh_token=REFRESH_TOKEN)
try:
now = datetime.datetime.now()
end_time = now.strftime("%H:%M")
response =
client.intraday_time_series('activities/steps',
detail_level='15min',
start_time="00:00",
end_time=end_time)
except Exception as error:
print(error)
else:
str_steps = response['activities-steps'][0]['value']
print(str_steps)
try:
num_steps = int(str_steps)
except ValueError:
pass
return num_steps
- In the main function, we schedule a timer that refreshes the token every 8 hours using the schedule library (https://pypi.python.org/pypi/schedule):
schedule.every(8).hours.do(refresh_token)
- We check for the steps every 15 minutes and light up the LEDs accordingly. Because the Pimoroni Blinkt consists of eight LEDs, we can light up one LED for every 1250 steps of physical activity:
# update steps every 15 minutes
if (time.time() - current_time) > 900:
current_time = time.time()
steps = get_steps()
num_leds = steps // 1250
if num_leds > 8:
num_leds = 8
for i in range(num_leds):
blinkt.set_pixel(i, 0, 255, 0)
if num_leds <= 7:
blinkt.set_pixel(num_leds, 255, 0, 0)
blinkt.show()
time.sleep(1)
blinkt.set_pixel(num_leds, 0, 0, 0)
blinkt.show()
time.sleep(1)
- For every multiple of 1250 steps, we set an LED's color to green using the blinkt.set_pixel() method. We set the next LED to a blinking red. For example, at the time of writing this section, the total number of steps was 1604. This is (1250 x1) + 354 steps. Hence, we light up one LED in green and the next LED blinks red. This indicates that the steps are in progress.
- The picture here shows the blinking red LED when the progress was less than 1250 steps:

Physical activity progress less than 1250 steps
- After walking around, the progress shifted to the right by one LED:

Physical activity at 1604 steps
- The next step is to set off a buzzer when there is no minimum physical activity. This is achieved by connecting a buzzer to the GPIO pins of the Raspberry Pi Zero. We have demonstrated the use of a buzzer in an earlier chapter.
- The earlier example is available for download along with this chapter as visual_aid.py. We will let you figure out the logic to set off a buzzer when there is no minimum physical activity in a period (for example, an hour).
Install this visual aid somewhere prominent and find out if it motivates you to stay physically active! If you make use of the Pimoroni Rainbow HAT, you can display the steps using the 14-segment display.