arduino 1 copilot 1 docker 3 editor-script 1 gcp 2 github 1 grafana 1 htmlhint 1 hugo 2 influxdb 1 jekyll 1 kubernetes 2 minikube 1 npm 2 plantuml 1 rpi 1 semantic-release 1 semver 1 skaffold 2 static-site-generator 3 telegraf 1 unity 1

DIY Realtime hamster monitoring using Arduino and Grafana

Ever so often you stumble on a can-be-done-in-a-day (CBDIAD) Arduino project that is both simple and rewarding. This is one of those. 😄

The overall idea was to measure the hamster wheel to answer the question on everyones minds:

But… how far does the he run each night?

What you need

In this setup I used:

Total cost (excluding laptop, hamster, hamster accessories (wheel) and power supply): Approx 8€, but this very much depends on where you buy the Arduino components.

Setup

Part 1: Assemble the Arduino components

Wire the ESP-01 according to photo below (it’s the shock sensor sticking down into the hamster cage, next picture shows it better).

ESP8266 in action

The metal wire is attached to the shock sensor with a piece of tape. This allowed me to slip it down into the hamster cage without risking to accidentally electrocute the hamster.

Elegant assembly of shock sensor

Part 2: Start the backend services

Start the backend services using the provided docker compose file.

git clone git@github.com:oversizedhat/hamstermonitor.git
cd hamstermonitor
cd server
docker-compose up

Naturally this is just for testing purposes… when you take your hamster monitoring to production it’s a whole different ballgame 😄

Part 3: Connecting the pieces (upload the sketch)

The sketch is hardcoded with wifi settings matching my home network which need to be changed…

  1. In main.ino change network name and ip adress of server to match your network.
const char* ssid = "YOUR_WIFI_NAME";
const char* host = "YOUR_SERVER_IP_ADRESS";
  1. Next to main.ino, create a file named secrets.ino and add you local wifi password in it like this:
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"
  1. In order to upload your updated sketch you need to make a few additional wirings between the ESP-01 and the serial adapter (or Arduino if you are using it).
    1. Connect RXD with TXD
    2. Connect TXD with RXD
    3. Add a wire between GP0 and GRN (this will force the ESP-01 to start in bootloader mode)
    4. Power off/on to start in bootlader mode
    5. Upload sketch from IDE (I use VSCode with Arduino extension)
    6. Set Baud Rate to 9600 in the IDE
    7. When done and verified you can remove these wires.

Open the Serial Monitor, power off/on and it should give you some response:

8�������%�Ready

Connecting to YOUR_NETWORK_NAME
..
WiFi connected
IP address: 
THE_ALLOCATED_LOCAL_IP_ADDRESS_OF_ESP01

The test “run”

Slap on the piece of tape on the wheel, make sure it can spin nicely without touching the ground.

Scientific tape..

Fine tune the positioning of the piece of metal meeting the tape. You want to make sure the metal and tape touch each lap, just enough to register vibration without adding resistance. To make sure the wheel was fixed in place I screwed it onto a wooden board…

Hamster safe insertion

Time to test it out a bit…

  1. Open the Grafana Dashboard: http://localhost:3000 (admin:admin)
  2. Find the dashboard most elegantly named “Poppe Dashboard” unless you renamed it to match your hamster…
  3. Chances are you now need to wake up and motivate your hamster to go for a run. If you dont want to wake up the hamster you can spin the wheel a couple of times and observe the dashboard for changes, note the 5 sec delay between each refresh.

Here is my test run.. such a glorious moment it was:


Results

From a few days of collecting metrics I noticed some trends:

Out of the measured 5 nights his record was a whopping 4019 meters(!).

4019 meters in one night!

Sadly I accidentally deleted the data to prove that but here are some screenshots of the metrics😄

Poppe 3 nights of running:

Grafana stats 1
One nights running divided into two sessions, the second with slightly higher average lap time:
Grafana stats 1

So I had tons of useful ideas where to go from here but as usual when something is working, interest fades quickly…

Some of the mistakes I made

[connection]
wifi.powersave = 2

A final notes on the ESP8266

The ESP8266 ESP-01 is a super neat little module with microcontroller capabilites, i.e. you can upload your Arduino sketches straight to instead of having to use an Arduino board. It also has a couple of general purpose pins. One thing to make note of is that it’s easy to burn these with wrong voltage (needs to be 3.3 V). A tip is to order a couple of them in one batch so you don’t have to be so careful…


Feel free to get in touch if you have any questions.
Click to reveal contact details dude
arduino grafana telegraf influxdb