If you’ve built a robot or two, chances are your robot has probably crashed at one point or another. And that can be quite a headache—especially when you’re relying on that robot to perform an action or provide data to inform your wider fleet.
Just last week, my robot crashed because my Raspberry Pi didn’t have enough RAM to account for my robot’s systems and processes. But I didn’t find out about it until it had been out of commission for a few hours. Thankfully, my robot was not handling anything urgent, but I still would have preferred it to be functioning rather than not. So, I did what any builder would do, and set out to fix it.
The solution: a text notification sent to my mobile phone to alert me when my robot was using a high amount of memory, but before it would crash, so that I could intervene.
Here are the steps I took to make that text message alert system a reality. Feel free to use it to do the same when you need to know if something is amiss with your robot!
Step 1: Create a modular sensor
My robot is fairly simple, and had no way to check the current RAM usage of the Raspberry Pi. However, Viam offers a simple way to add customized modular resources to any robot, so I was able to create a modular sensor as part of the robot just for this purpose.
Creating a modular sensor to check my Pi’s RAM with Viam is fairly straightforward. Viam's module system is underpinned by resource APIs that make adding necessary components and services very easy. We even provide examples that can be used by developers with slight modifications.
All I had to do was copy the simple module example from this repo, change the readings to look at my RAM statistics at “/proc/meminfo”, and pull out the information I cared about.
Here is the get_readings() snippet that pulls and cleans up the memory data:
Step 2: Set up remote monitoring
Modularly adding a service that any developer might want their robot to leverage is as easy with Viam as adding a component like the sensor in step 1.
To remotely monitor this Pi, I decided I’d just integrate a 3rd-party service that takes care of the hard part of sending a text. I chose Twilio for this purpose, since it only takes a few minutes to set up a quick account and import the library. An example of integrating Twilio into Viam as a modular resource is hosted in this repo.
I then added an if/then statement to the code, so when the RAM usage exceeded 70%, the robot posted Twilio, which sent me a text.
Here is the code snippet showing how I use Python to post a request for a text to Twilio if the value exceeds my configured threshold:
Combining the modular sensor and the modular Twilio service is as simple as calling that function before returning my sensor reading - a single line of code (highlighted below):
Step 3: Data capture to configure how often to check the RAM
Viam has a data capture service which helps to gather information from robots where it would otherwise be difficult or time-consuming to accomplish. Using this data capture service, I can configure how often to check the RAM, which in my case I have set to checking hourly. I can change my threshold or the frequency at which I’m checking at any time.
Before posting this code, I moved a few sensitive attributes like the api_key, account ID, and phone numbers so they weren’t hardcoded. This lets me (or you) keep information secure and replicate this implementation across any robot. I can even change the recipient simply by using the configuration tab. Your configuration will look something like this:
The importance of being modular
As you can see from this example, being able to quickly add new components and features can be the difference between a functioning robot and a very expensive paperweight. Viam enabled me to add key functionality and data collection so that I could monitor my robot and keep it running even if issues arose, a process that would have been much more complex and taken a lot longer otherwise.
If you’d like to follow in my footsteps and see how I did all this, you can peruse my final project code, which is hosted here. Learn more about Viam’s unique approach to modularity in smart machine components and services in our detailed documentation repository here.