Header background

Building a hamster fitness tracker using Dynatrace API

Hamsters are nocturnal rodents and, therefore, are mostly active during the night. My boyfriend and I do have a hamster, and curiosity led us to start thinking about how we could track some of his activities, specifically how far he is able to run within a night. So we decided to turn the hamster wheel into a fitness tracker using the Dynatrace API. Basically, we wanted to capture all the rotations of it. And, there was not much additional “hardware” needed for this goal to be achieved.

We already had the hamster…

…and the hamster wheel.

Required hardware:

  • Hamster
  • Hamster wheel – with a large enough diameter eg “Silent Runner” ~30cm
  • Microcontroller with an integrated wifi chip (ESP8266) – eg a NodeMCU
  • Infrared obstacle avoidance sensor – eg KY-032
  • Small reflector stripe (eg aluminum foil)

Software setup:

  • MQTT broker – eg Mosquitto
  • Node-Red
  • Dynatrace SaaS

Modifying the hamster wheel

It was mandatory that we do not install any wires inside the terrarium. All wires had to be installed on the outside only, out of the hamster’s reach. We decided to use an infrared obstacle avoidance sensor — sensitive to light reflections — which could be placed on top of the terrarium. Next we applied an aluminum foil reflector stripe to the wheel.

Implementing the fitness tracker

The simplest way for us to process the sensor’s data was using a micro controller with an integrated wifi-chip. As a start we wanted to capture some metrics per rotation and some lap and night summary statistics like speed and distance. For all of these metrics we registered a custom time series identifier via the Dynatrace API and, additionally, the hamster wheel was registered as a custom device.

The microcontroller was programmed to calculate and send all the needed metrics to an MQTT broker used as a sort of relay station. MQTT is an extremely lightweight messaging protocol for devices with limited processing power and/or network bandwidth – thus, a perfect fit for us. As the Dynatrace API expects the metrics in JSON format we needed to add some conversion of the sensor’s raw data. This could easily be achieved by implementing a Node-RED flow.

Our flow subscribes to the MQTT broker filtering the incoming wheel’s sensor data. The data is then enriched with the current time stamp and converted to a JSON payload which is sent via HTTP to the Dynatrace REST API.

<h3Fitness Results

I created a custom dashboard showing all the different hamster wheel’s metrics.

It turned out that our hamster is very busy every night. Usually he runs about 5 to 7 kilometers with an average speed of 3 kilometers per hour. So far his personal record was a total distance of roughly 8.5 kilometers per night which equal to approximately 8,900 rotations. He always runs several laps — mostly about 50 to 150 — whereby a lap’s distance adds up to 0.1 to 0.9 kilometers.

Here are the charts showing our hamster’s current “high scores”:

The night run’s total distance…

The total distance of a night's run -- constantly increasing to a total of ~8.5 km within a time of roughly 4 hours.
The total distance of a night’s run — constantly increasing to a total of ~8.5 km within a time of roughly 4 hours.

…and total number of rotations.

The total number of rotations for a night's run -- constantly increasing to a total of ~9,000 rotations within a time of roughly 4 hours
The total number of rotations for a night’s run — constantly increasing to a total of ~9,000 rotations within a time of roughly 4 hours

As well as the average distances per lap…

A night's run always consists of several laps, whereby a single one usually adds up to 0.1 to 0.9 kilometers.
A night’s run always consists of several laps, whereby a single one usually adds up to 0.1 to 0.9 kilometers.

And, of course, speed!

Typically, the average speed is around 3 kilometers per hour -- sometimes also a bit higher.
Typically, the average speed is around 3 kilometers per hour — sometimes also a bit higher.

Conclusion

We learned that the reason our hamster sleeps so much during the day is because he is exhausted from his nighttime workouts! 8.5 kilometers — or a little more than 5 miles — is a significant run for a human! But for such a tiny creature which such short legs, 5 miles is a major accomplishment. We can’t wait to see if he takes a recovery day (or night) like most runners do! In the meantime his terrarium will continue to be located in another room far from the bedroom!

He also gets an incredible amount of mileage for the relatively small amount of fuel he consumes. He’s a very efficient little creature!

However, the primary reason I embarked on this project is to demonstrate, in a humorous way, the versatility of our APM solution for nearly any application you can imagine — large, small, or somewhere in between.

Also, I wanted to show you, the reader, a little bit of the Dynatrace culture and how our commitment to providing the best possible APM solution extends well beyond our workday. I hope you found this posting to be entertaining and informative.

***

Interested in joining Dynatrace on the journey to redefine monitoring? Check out the job opportunities here.