misc/wifi-cycle-counter#1: System Design/Overview



Issue Information

Issue Type: issue
Status: closed
Reported By: btasker
Assigned To: btasker

Created: 31-Aug-24 10:31



Description

I've tried to get in the habit of counting steps (collected via my Bip and written into InfluxDB).

However, a few months back, I bought and "under desk bike" - basically a set of pedals attached to a resistance wheel.

This has a fidget-spinner like quality and I'm quite often pedalling to some extent.

However, it bugs me that this activity isn't tracked at all.

The bike has a little cycle computer built in, so can display calories burnt, RPM "distance" etc, but it only exists on that device.

In theory it should be linked up to a reed switch and magnet, with the computer applying a small voltage and counting pulses.

What I'd like to do with this project is

  • Disconnect the computer and connect to the GPIO pins of a raspberry Pi instead
  • Write a script to count pulses (in principle it should be near identical to this)

Ultimately, I'd like to use a Raspberry Pi Zero for this (it doesn't make sense to use a full-sized Pi), but it probably makes sense to start with a Pi that I've got knocking around.



Toggle State Changes

Activity


assigned to @btasker

IMG_20240831_113550

IMG_20240831_113226

IMG_20240831_113344

IMG_20240831_114158IMG_20240831_114224

OK, so I've plugged my test pi (camille) into the network. Has come up with IP 192.168.3.175 (username is ben)

IMG_20240831_121646

IMG_20240831_140634

IMG_20240831_140639

I'm using GPIO pins 1 (3.3v) and 7 (GPIO 4).

I've not used a pull-down resistor - my intention is to enable pull-down mode in the code and use the Pi's built in resistor.

import RPi.GPIO as GPIO
import datetime

GPIO_NUM=4

def detected_change(channel):
    if GPIO.input(channel) == GPIO.HIGH:
        print('\n1  at ' + str(datetime.datetime.now()))

try:
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(GPIO_NUM, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    GPIO.add_event_detect(GPIO_NUM, GPIO.BOTH, callback=detected_change)

    message = input('Press enter to exit')

finally:
    GPIO.cleanup()

There are 2 blips per cycle

quick script output

I think what happens is the switch closes as the magnet approaches, opens when it's directly alongside and then closes again as it goes out the other side, ultimately opening once it's back out of range.

One of the things we need to know is what each blip (or pair of blips) is worth.

I've connected it's display back up and will slowly turn the peddles to see how things increase.

One thing I noticed: the counter counts both blips, so it's actually reporting double. It looks like double blips only happen when moving very very slowly.

7 turns per calorie.

It doesn't do a distance

verified

mentioned in commit 5b5255d35fbc011787f61a456103e65af27da2f4

Commit: 5b5255d35fbc011787f61a456103e65af27da2f4 
Author: B Tasker                            
                            
Date: 2024-08-31T14:20:40.000+01:00 

Message

feat: commit initial PoC script (misc/wifi-cycle-counter#1)

+25 -0 (25 lines changed)

With a bit of fiddling about and swearing, I've been able to work out that the radius of the wheel inside is about 5.6cm.

We can use that if we wanted to calculate distance + speed.

In practice, they're fairly meaningless numbers (all it really tells us is how far the magnet has travelled) but might be interesting as a general indicator

verified

mentioned in commit bbfaa67bce98d4369cc155b51ebd49017b3b96dd

Commit: bbfaa67bce98d4369cc155b51ebd49017b3b96dd 
Author: B Tasker                            
                            
Date: 2024-08-31T14:44:01.000+01:00 

Message

feat: only emit stats periodically (misc/wifi-cycle-counter#1)

+37 -6 (43 lines changed)
verified

mentioned in commit 2121940bb6ebc96646543c304a30ccfeb01aa6fd

Commit: 2121940bb6ebc96646543c304a30ccfeb01aa6fd 
Author: B Tasker                            
                            
Date: 2024-08-31T15:43:30.000+01:00 

Message

feat: add option to calculate speed and distance (misc/wifi-cycle-counter#1)

+33 -1 (34 lines changed)

Decided this should be a telegraf execd plugin rather than a standalone script.

Installing telegraf:

curl --silent --location -O \
https://repos.influxdata.com/influxdata-archive.key \
&& echo "943666881a1b8d9b849b74caebf02d3465d6beb716510d86a39f6c8e8dac7515  influxdata-archive.key" \
| sha256sum -c - && cat influxdata-archive.key \
| gpg --dearmor \
| sudo tee /etc/apt/trusted.gpg.d/influxdata-archive.gpg > /dev/null \
&& echo 'deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive.gpg] https://repos.influxdata.com/debian stable main' \
| sudo tee /etc/apt/sources.list.d/influxdata.list
sudo apt-get update && sudo apt-get install telegraf

The first attempt at running via Telegraf failed with

RuntimeError: Not running on a RPi!"

Fixed by doing

adduser telegraf gpio

and then restarting Telegraf

verified

mentioned in commit f405716a7ce495fece68a7b55ba6df37eebea196

Commit: f405716a7ce495fece68a7b55ba6df37eebea196 
Author: B Tasker                            
                            
Date: 2024-08-31T16:37:07.000+01:00 

Message

feat: add example telegraf configs (misc/wifi-cycle-counter#1)

+65 -0 (65 lines changed)

So, it turns out that Grafana doesn't support centimetres as a unit...

So, in my dashboard I've had to multiple values by 10 to get millimetres (which they do support)

There's a writeup of the project: Building a Raspberry Pi Cycling Computer