Wednesday, May 23, 2018

Take-Your-Medicine-Reminder 3.0: Now there's an App for That

Version 2.0 of my medication reminder system was fun to build. However, it effectively tied up my micro:bit and meant that if I wanted to continue experimenting with it, I'd have to buy another. Rather than buy more hardware, I decided to develop a software solution to the don't forget my allergy medication problem.

I opened up Tasker and set to work crafting a solution. Like my original solution, I wanted a passive reminder that I'd need to take my medication every 24 hours. And like the original solution, when I took the medication, I wanted an easy way to reset the process so the countdown would start anew.

To solve the second half of the challenge, I busted out an NFC sticker. I stuck it to the bottom of my pill bottle like so:

My plan was to arrange it so that scanning this tag reset the countdown. The sequence would then be: get notified that I have to take the medication, take it, scan the bottom of the bottle, repeat.

Next, I needed to implement a countdown timer. One solution was to use Tasker's Set Alarm Action to register an alarm due in 24 hours. When the relevant NFC tag was read, I'd need to kill the alarm and set a new one. After a bit of futzing with the alarm action, I decided that depending on a system alarm wasn't going to be reliable enough.

I then turned my attention to AutoNotification, a Tasker plugin that allows you to make sophisticated use of Android's notification panel. AutoNotification has a number of interesting features: first, it allows you to show a countdown timer in the notification itself. It also allows you to render a progress bar. Using these features, I realized I could put my medication reminder right in the notification bar. A quick glance would tell me how much time I had left before the medicine needed to be taken.

Here's how the system looks while running:

This screenshot shows that I just kicked off the reminder and that I should take my meds in 23+ hours.

This functionality is powered by two Actions: Med Reminder Init and Med Reminder Refresh. The init task calculates when the reminder is due, sets this in a global variable and creates the first notification.

%deadline is calculated by taking the 24 hour period and adding it to %TIMEMS (which is current time millis). This value is then plugged into the AutoNotifcation's time field. This combined with checking both the Chronometer and Chronometer Countdown force the notification to show a countdown timer and not an absolute time.

I used the Local NFC Plugin to tie scanning the NFC tag to kicking off the above init action:

One Tasker-NFC lesson I'm constantly relearning is this: when scanning a new/empty tag, Android will pop-up a Complete action using dialog:

You can avoid this dialog by writing the custom URI bad://access/development to the tag.

To make the progress bar on the notification work, I implemented a refresh action. This action does a bit of math to figure out what percentage of time is remaining and writes this value to %percent. Apparently the AutoNotification plugin doesn't like using floating point values for the progress bar, so I use a bit of search-and-replace logic to remove the decimal point and everything after it.

When setting up the initial notification I made sure to use the ID MedReminder. When I want to refresh the notification with the updated progress bar value, I use this same ID again.

I used a Time-of-Day profile that runs every 2 minutes, all day, to invoke the Med Reminder Refresh action. This keeps the progress bar reasonably up to date.

Put all of these items together and you have a refresh action:

I'm sorry to see my hardware solution go. Every time I saw the grid of LED's light up, I couldn't help but think: heck yeah, I built that! But this Tasker solution is a wee bit more practical. And getting a chance to experiment with the very powerful AutoNotification is a nice bonus.