Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2014/02/21

Using Fitbit to keep me moving

I have a FitBit Ultra (which I have discussed before) that I use to keep track of my daily activity, trying to force myself into higher and higher numbers, toward the AHA-suggested 10,000 steps per day.

In all honesty, I have dropped my personal goal from 10,000 steps to 6000, because 6000 is an amount that I can adjust my day to get, while I have to spend hours of my evening walking around to get myself up to 10,000. With 6000, I have a lower barrier, so I don't beat myself up with disappointment as easily. I still do, because I still have <3000-step days, but with 6000, I feel I have a fighting chance.

I have a FitBit but I don't pay for the upgraded API, but I can check every hour. And now I do. 

  1. #!/usr/bin/env python  
  2.   
  3. from oauth import oauth  
  4. import datetime   
  5. import httplib  
  6. import os  
  7. import pymongo  
  8. import simplejson as json  
  9. import time  
  10.   
  11. import fitbit  
  12. import locked  
  13. import pushover as p  
  14.   
  15. def main():  
  16.     checker = locked.locked()   # determines if screen is locked  
  17.     if not checker.is_locked():  
  18.         from pymongo import MongoClient # connect to mongodb  
  19.         client = MongoClient()          # connect to mongodb  
  20.         db = client.fitbit              # connect to fitbit DB   
  21.         reads = db.daily_reads          # connect to collection   
  22.         now = datetime.datetime.now()       # what time is now?  
  23.         datestr = now.strftime("%Y-%m-%d")  # now in date format  
  24.         isostr  = now.isoformat()           # now in ISO format  
  25.         fb = fitbit.make_class()                    # connect   
  26.         fbr = fb.activities_date_json( datestr )    # get updates  
  27.         summary         = fbr['summary']            # get summary  
  28.         steps  = summary['steps']                   # pull out steps   
  29.         new_read = {}               # create new read  
  30.         new_read["date"] = datestr     # date == datestr  
  31.         new_read["time"] = isostr      # time == ISO  
  32.         new_read["steps"] = steps      # steps is steps to date  
  33.           
  34.         if 0 == reads.find( { "date" : datestr } ).count(): # if nada   
  35.             for read in reads.find():       # clear out   
  36.                 id = read["_id"]  
  37.                 reads.remove( { "_id" : id } )  
  38.             reads.insert( new_read )        # insert   
  39.         else:  
  40.             old_read = reads.find_one()  
  41.             delta_steps = new_read["steps"] - old_read["steps"]  
  42.             if delta_steps < 100:  
  43.                 msg = "You took " + str(delta_steps)   
  44.                 msg = msg + " steps in the last hour. "  
  45.                 msg = msg + "Go take a walk."  
  46.                 pu = p.pushover()  
  47.                 pu.send_message( msg )  
  48.             id = old_read["_id"]  
  49.             reads.update( { '_id' : id } , new_read )  
  50.   
  51. if __name__ == '__main__':  
  52.     main()  

There are three modules that are mine: the fitbit module which comes from my fitbit_tools repository; locked module which uses xscreensaver-command to determine if the screen is locked (if I'm not at my desk, my step counter isn't being updates; and pushover, which uses the Pushover service to keep me informed. I could and maybe should have it do speech with Festival or pop to my screens with Notify or Snarl, which also are desktop-centered tools, but this is what I have.

Similarly, perhaps this task is better suited to a key-value store like Redis than MongoDB's document store. At most I'm having one entry per day and the first entry of a day clears out all previous entries.

But, notwithstanding all the perhaps-suboptimal design decisions, this is an example of how coding and quantifying yourself can help you improve your life.

No comments:

Post a Comment