,

Motivation: One Text at a Time

Using a Trusty Raspberry Pi to Send Periodic Pick-Me-Ups “Character cannot be developed in ease and quiet. Only through experience of trial and suffering can the soul be strengthened, vision cleared, ambition inspired, and success achieved.“ Hellen Keller That’s the text message I received – somewhat unexpectedly – from my cron job running on my…


Using a Trusty Raspberry Pi to Send Periodic Pick-Me-Ups

Character cannot be developed in ease and quiet. Only through experience of trial and suffering can the soul be strengthened, vision cleared, ambition inspired, and success achieved.

Hellen Keller

That’s the text message I received – somewhat unexpectedly – from my cron job running on my Raspberry Pi.

The first time I bridged the gap between scripts and text messages was when I was in college. Due to some financial aid mishaps, I was unable to register for classes in my final semester. Only two days before the semester began, was everything finally cleared up, allowing me to register.

However, every class that I absolutely needed in order to graduate was already full. The only way to get a seat was to hope that someone dropped and quickly register before the position was filled. No amount of begging my advisor would help me – all signs pointed to not being able to take any classes and subsequently not graduate.

The process of checking for seats was entirely manual, there was no concept of a waitlist and no way to be notified if a new seat opened up. If you wanted to register for a class you were glued to a screen and constantly refreshing a webpage. Early on in Computer Science, you are taught how to create algorithms: very specific steps that instruct a computer on how to solve a problem. I remember one of my first professors saying, “If you can do it once, you can teach a computer to do it a million times”. Which is exactly what I needed to do.

I wish I could find the script; from memory I recall it being an absolute monstrosity. It ran not on a raspberry pi, but an old laptop that I just left at my apartment. It used the XDoTool package to simulate me logging into my student portal (and yes you better believe I hard coded in my login credentials in plain text) and scraping the webpages for my classes to see if there were any open seats. Upon finding an opening, it would send me a text message notifying me to act quick and register.

Within three days, I was registered for all of my classes. The script worked for 3 of the four classes I needed and for the final one my professor was so impressed with my scripting that they added an additional seat to let me attend.

I told that story a lot – it was my favorite example of leveraging what I learned in class and applying it to real-world problems. The further I got into my software career, it seemed the further I got from solving actual problems. There’s just not quite as much satisfaction in centering a element with CSS even though the complexity is much higher.

So, I wanted to get back to my roots: short, hacky scripts that only exist to make my life better.

Enter my motivational script

I currently live in an extremely remote part of Alaska. So remote, that a significant highlight of my life recently has been getting a grocery store in town. No more do I need to take a 2-hour drive to get groceries, now I can have somewhat fresh produce more than once a month. I haven’t seen the grass since I moved here in October, and my only friend is my dog. To summarize: it’s isolated and there’s not much to do.

What I needed was some motivation and a project that was small enough to be finished before my attention span left me. I decided to flex those scripting muscles in a small, but fun way. Let’s make something that will send me two text messages, twice a day, consisting of random inspirational quotes. For some reason, coding feels much “bigger” when the meium breaks your personal computer screen.

How it Works

I wanted to limit this to the smallest, most achievable project that I could. So I restricted the features to being:

  • Use a library or API to generate messages
  • Send text messages to just my phone
  • Time between text messages is random
  • Only send during normal hours (no midnight messages)

I toyed with the idea of being able to ‘subscribe’ but set it as a stretch goal and then omitted it entirely. I knew that I wanted this to be a bash script, and something that I could run on my Raspberry Pi. I had planned for it to be a cron job – something about having a task sleep for several hours and always running just feels icky.

Breaking this into components, there are the following:

  • The text messaging library / API
  • The quote generator library / API
  • The script itself
  • Scheduling and running the script

Ideally, I would use only APIs which would simplify everything, but I had a feeling that there would not be any free texting APIs. For obvious reasons…

TextBelt: the Text Messaging Library

In a completely unplanned effort I tried to use the same test messaging API that I had used for my college project, Twilio. Unfortunately, my free trial had long expired. Then I came upon an open source library, TextBelt. They offer a paid version to their API or you can self-host the server instead. Frugality will never let me pay for an API, so I cloned the library on my Raspberry Pi which already had the prerequisites: Node and Redis installed.

I ran the server and a quick Postman test let me know that everything was indeed, not working.

Seeing the letters ‘smtp’ immediately brought me back to my Geek Squad Days of using advanced options in email wizards to get antiquated domains working with Windows mail. It was not something I wanted to see while I was trying to send myself text messages. So, I dug into the config – it used nodemailer which I’ve used before, but never really understood.

I still don’t understand nodemailer, but thanks to a helpful comment, I was able to configure my transport to use Gmail’s smtp servers. After a quick update to my gmail account to add an app password, the configuration was complete and I received my first text message. Next, was the quote API.

Quotable: the quote API

CoPilot gave me a couple of free APIs to choose from, but I settled on Quotable because the quote object included categories, and you could filter on these categories to get more specific, tailored quotes.

The API was free and super easy to call. I added the call and object parsing into the script, easy-peasy.

The Script: only a little hacky

Clocking in at just 25 lines including comments, this is one of the cleaner and smaller scripts that I’ve written. I compiled all of the pieces together: the correctly configured TextBelt library running as a service and using that service to send my phone my motivational message. Because running the service was a blocking call, I ran it in the background and added some sleeps for good measure. A helpful google search gave me the kill $! command which would terminate the service after the total elapsed time.

The small script can be found here – note paths will need to be changed in order to run this locally.

Scheduling the Job to Run

I love me some cron. However, I’ve never made anything that is scheduled to run randomly – seems oxymoronic. However, someone else had the solution, so I slightly modified it to be:

0 8-21 * * * sleep $((RANDOM % 780))m ; /home/pi/bin/motiv.sh

This will send at least two messages through the day. Even though I tried to get out of the “sleeping for an extended period of time” I think this slight hack will do for now.

Now I just wait and watch the motivational messages scroll in.


Related Posts