Hacking My Health

David R Bayer
4 min readJun 26, 2021

The Story

I have reached that time in my life when docs start taking particular interest in certain aspects of my health. “Look what I found! You have high blood pressure. We should do something about that.” So now I get to be a guinea pig and test out new meds and how effective they are at different levels. To determine effectiveness I have to take my blood pressure twice daily.

I am supposed to take my meds in the evening before bed. However, my bed time varies significantly from day to day, and I occasionally forget to take the meds until the next morning. As you would expect, this affects the blood pressure readings. I thought to myself, “Self, you should record the time that you take your meds so you can show BP readings relative to when the meds were taken.” I also know me — if I have to enter them in a spreadsheet or write them down in a journal right before bed I will either forget or stop doing it after a short period of time.

So how can I leverage technology to make my life easier? My immediate thought was “Wouldn’t it be nice to just press a button and have it record the time? THAT’s something I could be consistent about doing!” I looked at what resources I had readily available to came across my old Raspberry Pi 2b sitting on a shelf. Sure, it might be old and slow and only support a 32-bit OS, but for my purposes that’s all I need.

The Hardware

I got the Raspberry Pi as part of a kit with the intent of learning electronics and controlling them using the RPi. Of course that never happened. I created a little stop light using 3 LEDs that came with the kit and never did anything else. Of course I never got rid of the kit, so out it came — I needed the breadboard, the GPIO breakout board, a push button, a jumper wire, and a resistor. It’s a VERY simple circuit, and yes I had to research how to build it because I never did more than that stop light project previously.

The picture below shows the circuit. The jumper wire connects one pin of the button to GPIO pin 10. The resistor connects the other pin to the 3.3V bus and limits the voltage so I don’t burn anything out.

Breadboard connected to Raspberry Pi breakout board with circuit showing pushbutton, resistor and jumper wire.

The Program

With a little googling (really it was duck-duck-going, but that doesn’t really roll off the tongue now does it?), I found that the Python modules gspread and RPi.GPIO would give me what I need. First I needed to set up a new project in the Google Developers Console.

Once that was done it was a simple matter to write the script. First I learned how to watch the button. All I wanted at first was to get some evidence that it was working as desired. The script below watches for the GPIO.RISING event on pin 10, meaning voltage is going from low to high (the button is getting pressed). When that is detected, it prints “button pressed” to the console so that I know something happened. The input line is just to give me an easy way to exit my test.

Now that works, so I can just replace the callback function with one to record the time that the button was pressed. First I auth against Google, then select the Google Sheets file, then the sheet within the file, then append a row with my date and time. I know I could have recorded a timestamp, but my BP monitor outputs in separate fields so I thought I should follow suit.

Then I just put it all together. Since my goal is for this to run when the RPi boots, I changed it from waiting for user input to a simple sleep loop. One thing I didn’t mention was the bouncetime parameter for GPIO.add_event_detect. I noticed that the button would occasionally output multiple times for a single press, and bouncetime (ms) is supposed to prevent subsequent events during that window. I don’t have it tuned well yet, but it’s better than before.

The System

Now I need to have this run on startup. Surprisingly this took me longer to test than expected. I reimaged the RPi with the latest 32-bit version of Raspberry Pi OS which by default automatically logs in as the pi user, so I run this in that user’s crontab. I don’t like muddying the system Python environment, so I created a Python3 virtualenv which gets activated before the script is launched. Note the & after launching the script — this is required to background the task. Otherwise the bash script will never complete and could interfere with user login.

After that I just run crontab -e when logged in as the pi user and add the following line:

@reboot /home/pi/rpi-pushbutton/start-rpi-pushbutton.sh

This was a fun little evening hack. Now the beastie sits on my bedside table. I take my meds on my way to bed, hit the button, and voila! Later I will have the task of creating graphs showing the correlation, but that’s a task for another day.

--

--