├── .github
└── workflows
│ └── build.yml
├── .gitignore
├── README.md
├── arduino-cli.yaml
├── art
├── BLELibrary.PNG
├── MyCadence.png
└── cadence.png
├── device.h
├── icons.h
├── icons
├── README.md
├── cadence.png
├── clock.png
└── mountain.png
├── mycadence-arduino.ino
└── power.h
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Build Heltec WiFi Kit 32
2 |
3 | on: [push, pull_request]
4 |
5 | jobs:
6 | build:
7 | runs-on: ubuntu-latest
8 |
9 | steps:
10 | - uses: actions/checkout@v1
11 | - name: Install Arduino CLI
12 | uses: arduino/setup-arduino-cli@v1.1.1
13 | - name: Install Python
14 | uses: actions/setup-python@v2
15 | with:
16 | python-version: '3.x'
17 | - name: Install ESP32 tools
18 | run: |
19 | pip install esptool
20 | - name: Install CLI dependencies
21 | run: |
22 | arduino-cli core update-index
23 | arduino-cli core install Heltec-esp32:esp32
24 | arduino-cli lib update-index
25 | arduino-cli lib install "Heltec ESP32 Dev-Boards"
26 | arduino-cli lib install "ESP32 BLE Arduino"
27 | - name: Build
28 | run: |
29 | arduino-cli compile --fqbn Heltec-esp32:esp32:wifi_kit_32 --export-binaries mycadence-arduino
30 | - name: Upload artifacts
31 | uses: actions/upload-artifact@v2
32 | with:
33 | name: Heltec WiFi Kit 32
34 | path: build/Heltec-esp32.esp32.wifi_kit_32/
35 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Aa][Rr][Mm]/
27 | [Aa][Rr][Mm]64/
28 | bld/
29 | [Bb]in/
30 | [Oo]bj/
31 | [Ll]og/
32 | [Ll]ogs/
33 |
34 | # Visual Studio 2015/2017 cache/options directory
35 | .vs/
36 | # Uncomment if you have tasks that create the project's static files in wwwroot
37 | #wwwroot/
38 |
39 | # Visual Studio 2017 auto generated files
40 | Generated\ Files/
41 |
42 | # MSTest test Results
43 | [Tt]est[Rr]esult*/
44 | [Bb]uild[Ll]og.*
45 |
46 | # NUnit
47 | *.VisualState.xml
48 | TestResult.xml
49 | nunit-*.xml
50 |
51 | # Build Results of an ATL Project
52 | [Dd]ebugPS/
53 | [Rr]eleasePS/
54 | dlldata.c
55 |
56 | # Benchmark Results
57 | BenchmarkDotNet.Artifacts/
58 |
59 | # .NET Core
60 | project.lock.json
61 | project.fragment.lock.json
62 | artifacts/
63 |
64 | # StyleCop
65 | StyleCopReport.xml
66 |
67 | # Files built by Visual Studio
68 | *_i.c
69 | *_p.c
70 | *_h.h
71 | *.ilk
72 | *.meta
73 | *.obj
74 | *.iobj
75 | *.pch
76 | *.pdb
77 | *.ipdb
78 | *.pgc
79 | *.pgd
80 | *.rsp
81 | *.sbr
82 | *.tlb
83 | *.tli
84 | *.tlh
85 | *.tmp
86 | *.tmp_proj
87 | *_wpftmp.csproj
88 | *.log
89 | *.vspscc
90 | *.vssscc
91 | .builds
92 | *.pidb
93 | *.svclog
94 | *.scc
95 |
96 | # Chutzpah Test files
97 | _Chutzpah*
98 |
99 | # Visual C++ cache files
100 | ipch/
101 | *.aps
102 | *.ncb
103 | *.opendb
104 | *.opensdf
105 | *.sdf
106 | *.cachefile
107 | *.VC.db
108 | *.VC.VC.opendb
109 |
110 | # Visual Studio profiler
111 | *.psess
112 | *.vsp
113 | *.vspx
114 | *.sap
115 |
116 | # Visual Studio Trace Files
117 | *.e2e
118 |
119 | # TFS 2012 Local Workspace
120 | $tf/
121 |
122 | # Guidance Automation Toolkit
123 | *.gpState
124 |
125 | # ReSharper is a .NET coding add-in
126 | _ReSharper*/
127 | *.[Rr]e[Ss]harper
128 | *.DotSettings.user
129 |
130 | # TeamCity is a build add-in
131 | _TeamCity*
132 |
133 | # DotCover is a Code Coverage Tool
134 | *.dotCover
135 |
136 | # AxoCover is a Code Coverage Tool
137 | .axoCover/*
138 | !.axoCover/settings.json
139 |
140 | # Visual Studio code coverage results
141 | *.coverage
142 | *.coveragexml
143 |
144 | # NCrunch
145 | _NCrunch_*
146 | .*crunch*.local.xml
147 | nCrunchTemp_*
148 |
149 | # MightyMoose
150 | *.mm.*
151 | AutoTest.Net/
152 |
153 | # Web workbench (sass)
154 | .sass-cache/
155 |
156 | # Installshield output folder
157 | [Ee]xpress/
158 |
159 | # DocProject is a documentation generator add-in
160 | DocProject/buildhelp/
161 | DocProject/Help/*.HxT
162 | DocProject/Help/*.HxC
163 | DocProject/Help/*.hhc
164 | DocProject/Help/*.hhk
165 | DocProject/Help/*.hhp
166 | DocProject/Help/Html2
167 | DocProject/Help/html
168 |
169 | # Click-Once directory
170 | publish/
171 |
172 | # Publish Web Output
173 | *.[Pp]ublish.xml
174 | *.azurePubxml
175 | # Note: Comment the next line if you want to checkin your web deploy settings,
176 | # but database connection strings (with potential passwords) will be unencrypted
177 | *.pubxml
178 | *.publishproj
179 |
180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
181 | # checkin your Azure Web App publish settings, but sensitive information contained
182 | # in these scripts will be unencrypted
183 | PublishScripts/
184 |
185 | # NuGet Packages
186 | *.nupkg
187 | # NuGet Symbol Packages
188 | *.snupkg
189 | # The packages folder can be ignored because of Package Restore
190 | **/[Pp]ackages/*
191 | # except build/, which is used as an MSBuild target.
192 | !**/[Pp]ackages/build/
193 | # Uncomment if necessary however generally it will be regenerated when needed
194 | #!**/[Pp]ackages/repositories.config
195 | # NuGet v3's project.json files produces more ignorable files
196 | *.nuget.props
197 | *.nuget.targets
198 |
199 | # Microsoft Azure Build Output
200 | csx/
201 | *.build.csdef
202 |
203 | # Microsoft Azure Emulator
204 | ecf/
205 | rcf/
206 |
207 | # Windows Store app package directories and files
208 | AppPackages/
209 | BundleArtifacts/
210 | Package.StoreAssociation.xml
211 | _pkginfo.txt
212 | *.appx
213 | *.appxbundle
214 | *.appxupload
215 |
216 | # Visual Studio cache files
217 | # files ending in .cache can be ignored
218 | *.[Cc]ache
219 | # but keep track of directories ending in .cache
220 | !?*.[Cc]ache/
221 |
222 | # Others
223 | ClientBin/
224 | ~$*
225 | *~
226 | *.dbmdl
227 | *.dbproj.schemaview
228 | *.jfm
229 | *.pfx
230 | *.publishsettings
231 | orleans.codegen.cs
232 |
233 | # Including strong name files can present a security risk
234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
235 | #*.snk
236 |
237 | # Since there are multiple workflows, uncomment next line to ignore bower_components
238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
239 | #bower_components/
240 |
241 | # RIA/Silverlight projects
242 | Generated_Code/
243 |
244 | # Backup & report files from converting an old project file
245 | # to a newer Visual Studio version. Backup files are not needed,
246 | # because we have git ;-)
247 | _UpgradeReport_Files/
248 | Backup*/
249 | UpgradeLog*.XML
250 | UpgradeLog*.htm
251 | ServiceFabricBackup/
252 | *.rptproj.bak
253 |
254 | # SQL Server files
255 | *.mdf
256 | *.ldf
257 | *.ndf
258 |
259 | # Business Intelligence projects
260 | *.rdl.data
261 | *.bim.layout
262 | *.bim_*.settings
263 | *.rptproj.rsuser
264 | *- [Bb]ackup.rdl
265 | *- [Bb]ackup ([0-9]).rdl
266 | *- [Bb]ackup ([0-9][0-9]).rdl
267 |
268 | # Microsoft Fakes
269 | FakesAssemblies/
270 |
271 | # GhostDoc plugin setting file
272 | *.GhostDoc.xml
273 |
274 | # Node.js Tools for Visual Studio
275 | .ntvs_analysis.dat
276 | node_modules/
277 |
278 | # Visual Studio 6 build log
279 | *.plg
280 |
281 | # Visual Studio 6 workspace options file
282 | *.opt
283 |
284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
285 | *.vbw
286 |
287 | # Visual Studio LightSwitch build output
288 | **/*.HTMLClient/GeneratedArtifacts
289 | **/*.DesktopClient/GeneratedArtifacts
290 | **/*.DesktopClient/ModelManifest.xml
291 | **/*.Server/GeneratedArtifacts
292 | **/*.Server/ModelManifest.xml
293 | _Pvt_Extensions
294 |
295 | # Paket dependency manager
296 | .paket/paket.exe
297 | paket-files/
298 |
299 | # FAKE - F# Make
300 | .fake/
301 |
302 | # CodeRush personal settings
303 | .cr/personal
304 |
305 | # Python Tools for Visual Studio (PTVS)
306 | __pycache__/
307 | *.pyc
308 |
309 | # Cake - Uncomment if you are using it
310 | # tools/**
311 | # !tools/packages.config
312 |
313 | # Tabs Studio
314 | *.tss
315 |
316 | # Telerik's JustMock configuration file
317 | *.jmconfig
318 |
319 | # BizTalk build output
320 | *.btp.cs
321 | *.btm.cs
322 | *.odx.cs
323 | *.xsd.cs
324 |
325 | # OpenCover UI analysis results
326 | OpenCover/
327 |
328 | # Azure Stream Analytics local run output
329 | ASALocalRun/
330 |
331 | # MSBuild Binary and Structured Log
332 | *.binlog
333 |
334 | # NVidia Nsight GPU debugger configuration file
335 | *.nvuser
336 |
337 | # MFractors (Xamarin productivity tool) working folder
338 | .mfractor/
339 |
340 | # Local History for Visual Studio
341 | .localhistory/
342 |
343 | # BeatPulse healthcheck temp database
344 | healthchecksdb
345 |
346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
347 | MigrationBackup/
348 |
349 | # Ionide (cross platform F# VS Code tools) working folder
350 | .ionide/
351 |
352 | # ESP32 build folder
353 | build/
354 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # My Cadence for Arduino
2 |
3 | This is an open source project to connect to popular cadence sensors for indoor cycling and display the current cadence in realtime. Looking for a free cadence display for iOS & Android? Checkout [My Cadence for iOS & Android](https://mycadence.app).
4 |
5 | Inspired & code forked from https://github.com/snowzach/echbt, which is an awesome way of dislaying data from an Echelon bike.
6 |
7 | 
8 |
9 | * Build Status: 
10 |
11 | ## Hardware Required
12 |
13 | * [MakerFocus ESP32](https://amzn.to/2LAmqt4)
14 | * [ESP32 Batteries(optional)](https://amzn.to/2KGfJWA)
15 | * [USB Micro Cable](https://amzn.to/2KSxstH)
16 |
17 | ## Compatible Sensors
18 | * [Moofit Cadence](https://amzn.to/349DpZM)
19 | * [Wahoo](https://amzn.to/3h2Lyo1)
20 | * [Magene](https://amzn.to/3raJ0Jd)
21 | * [Garmin](https://amzn.to/2WtSgdl)
22 | * Other cadence sensors that broadcast Speed & Cadence specification
23 |
24 | ### 3D Printable Case
25 | This is a really nice case that you can print from thingiverse.
26 | * [caseESP32_OLED on Thingiverse](https://www.thingiverse.com/thing:2670713)
27 |
28 | ## Setup
29 |
30 | 1. Install [Arduino Studio](https://www.arduino.cc/en/software)
31 | 2. Open .ino in Arduino Studio
32 | 3. Add ESP32 Device - [Follow these steps](https://heltec-automation-docs.readthedocs.io/en/latest/esp32/quick_start.html)
33 | 4. Select Wifi 32 from Tools -> Board -> Heltec
34 | 5. Add ESP32 Library - [Follow these steps](https://github.com/HelTecAutomation/Heltec_ESP32)
35 | 6. Add ESP32 BLE Arduino Library - https://github.com/nkolban/ESP32_BLE_Arduino
36 | 7. Deploy to device!
37 |
38 | The app will automatically scan for cadence sensors for 10 seconds. If it finds a single sensor it will automatically connect and start reporting data! If there are multiple sensors detected you can pick on from the list.
39 |
40 | ## Get My Cadence for iOS & Android
41 |
42 | Looking for an app for your phone or tablet to display the cadence? Checkout [My Cadence](https://mycadence.app), a simple display for your cadence sensor when you are indoor cycling. Simply connect to your cadence sensor via Bluetooth and my cadence will update your cadence in realtime with a simple to read display.
43 |
44 | 
45 |
46 | ## License information
47 |
48 | LICENSE: 
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
49 |
--------------------------------------------------------------------------------
/arduino-cli.yaml:
--------------------------------------------------------------------------------
1 | # arduino-cli.yaml
2 | board_manager:
3 | additional_urls:
4 | - https://resource.heltec.cn/download/package_heltec_esp32_index.json
--------------------------------------------------------------------------------
/art/BLELibrary.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jamesmontemagno/mycadence-arduino/7b84538374d3e4329700fc49541df3da42e8f5cd/art/BLELibrary.PNG
--------------------------------------------------------------------------------
/art/MyCadence.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jamesmontemagno/mycadence-arduino/7b84538374d3e4329700fc49541df3da42e8f5cd/art/MyCadence.png
--------------------------------------------------------------------------------
/art/cadence.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jamesmontemagno/mycadence-arduino/7b84538374d3e4329700fc49541df3da42e8f5cd/art/cadence.png
--------------------------------------------------------------------------------
/device.h:
--------------------------------------------------------------------------------
1 | //This is standard speed & cadence bluetooth UUID
2 | static BLEUUID serviceUUID("00001816-0000-1000-8000-00805f9b34fb");
3 | // This is the standard notify characteristic UUID
4 | static BLEUUID notifyUUID("00002a5b-0000-1000-8000-00805f9b34fb");
5 |
6 | static BLEAdvertisedDevice * devices[20];
7 | static int device_count = 0;
8 |
9 | // button handling
10 | #define BUTTON_DEBOUNCE_TIME 50
11 | #define BUTTON_SHORT_PRESS_TIME 400
12 |
13 | // Add a device to our device list (deduplicate in the process)
14 | void addDevice(BLEAdvertisedDevice * device) {
15 | for (uint8_t i = 0; i < device_count; i++) {
16 | if(device->getName() == devices[i]->getName()){
17 | return;
18 | }
19 | }
20 | if(!device->haveServiceUUID() || !device->isAdvertisingService(serviceUUID)) {
21 | return;
22 | }
23 | devices[device_count] = device;
24 | device_count++;
25 | }
26 |
27 | // Select a device and return it
28 | BLEAdvertisedDevice * selectDevice(void) {
29 | Heltec.display->setFont(ArialMT_Plain_10);
30 |
31 | if(device_count == 0) return nullptr;
32 | if(device_count == 1) return devices[0];
33 |
34 | int selected = 0;
35 |
36 | // Select a device
37 | while(true) {
38 | if(selected > device_count-1) selected = 0;
39 | int start = selected < 2 ? 0 : selected - 1;
40 |
41 | Heltec.display->clear();
42 | Heltec.display->setLogBuffer(10, 50);
43 | // Print to the screen
44 | for (int i = start; i < device_count; i++) {
45 | if(i == selected) {
46 | Heltec.display->print("->");
47 | } else {
48 | Heltec.display->print(" ");
49 | }
50 | Heltec.display->println(devices[i]->getName().c_str());
51 | }
52 | Heltec.display->drawLogBuffer(0, 0);
53 | Heltec.display->display();
54 |
55 | int lastState = LOW; // the previous state from the input pin
56 | int currentState; // the current reading from the input pin
57 | unsigned long pressedTime = 0;
58 |
59 | while(true) {
60 | // read the state of the switch/button:
61 | currentState = digitalRead(KEY_BUILTIN);
62 | if(lastState == HIGH && currentState == LOW) { // button is pressed
63 | pressedTime = millis();
64 | Serial.println("DOWN");
65 | } else if(pressedTime != 0 && lastState == LOW && currentState == HIGH) { // button is released
66 | long pressDuration = millis() - pressedTime;
67 | Serial.println("UP");
68 | if(pressDuration < BUTTON_DEBOUNCE_TIME) {
69 | pressedTime = 0;
70 | } else if(pressDuration < BUTTON_SHORT_PRESS_TIME ) {
71 | Serial.println("A short press is detected");
72 | pressedTime = 0;
73 | selected++;
74 | break;
75 | } else {
76 | Serial.println("A long press is detected");
77 | pressedTime = 0;
78 | return devices[selected];
79 | break;
80 | }
81 | }
82 |
83 |
84 | // save the the last state
85 | lastState = currentState;
86 | }
87 | }
88 | return nullptr;
89 | }
90 |
--------------------------------------------------------------------------------
/icons.h:
--------------------------------------------------------------------------------
1 | // convert clock.png clock.mono
2 | // xxd -i clock.mono
3 |
4 | const unsigned char clock_icon[] PROGMEM = {
5 | 0xc0, 0x3f, 0x00, 0xf0, 0xff, 0x00, 0x78, 0xe0, 0x01, 0x1c, 0x80, 0x03,
6 | 0x0e, 0x06, 0x07, 0x06, 0x06, 0x06, 0x07, 0x06, 0x0e, 0x03, 0x06, 0x0c,
7 | 0x03, 0x06, 0x0c, 0x03, 0x06, 0x0c, 0x03, 0x0e, 0x0c, 0x03, 0x1c, 0x0c,
8 | 0x03, 0x38, 0x0c, 0x07, 0x30, 0x0e, 0x06, 0x00, 0x06, 0x0e, 0x00, 0x07,
9 | 0x1c, 0x80, 0x03, 0x78, 0xe0, 0x01, 0xf0, 0xff, 0x00, 0xc0, 0x3f, 0x00
10 | };
11 | #define clock_icon_width 20
12 | #define clock_icon_height 20
13 |
14 |
15 | const unsigned char cadence_icon[] PROGMEM = {
16 | 0x00, 0x00, 0x0f, 0xc0, 0x9f, 0x09, 0xe0, 0xff, 0x08, 0x70, 0x70, 0x0c,
17 | 0x38, 0x30, 0x06, 0x18, 0x18, 0x03, 0x0c, 0x8c, 0x03, 0x0e, 0xc6, 0x07,
18 | 0x06, 0x63, 0x06, 0x06, 0x33, 0x06, 0x06, 0x1f, 0x06, 0x0e, 0x0e, 0x07,
19 | 0x0c, 0x00, 0x03, 0x1c, 0x80, 0x01, 0x3c, 0xc0, 0x01, 0x7e, 0xe0, 0x00,
20 | 0xf3, 0x7f, 0x00, 0xf1, 0x3f, 0x00, 0x19, 0x00, 0x00, 0x0f, 0x00, 0x00
21 | };
22 | #define cadence_icon_width 20
23 | #define cadence_icon_height 20
24 |
25 | unsigned char mountain_icon[] = {
26 | 0x00, 0x00, 0x00, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff,
27 | 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x1f, 0x00, 0x00,
28 | 0x00, 0x00, 0xff, 0x03, 0xe0, 0x7f, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00,
29 | 0x00, 0xfe, 0x01, 0x00, 0x00, 0xe0, 0x0f, 0x00, 0x00, 0xf8, 0x03, 0x00,
30 | 0x00, 0xf0, 0x03, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xf8, 0x00, 0x00,
31 | 0x00, 0x80, 0x1f, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00,
32 | 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x0f, 0x00, 0x00,
33 | 0x00, 0x00, 0xf8, 0x00, 0x80, 0x07, 0x60, 0x00, 0x00, 0x00, 0xf0, 0x00,
34 | 0xc0, 0x03, 0x60, 0x00, 0x00, 0x0e, 0xe0, 0x01, 0xe0, 0x01, 0x60, 0x00,
35 | 0x00, 0x0e, 0xc0, 0x03, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x04, 0xc0, 0x07,
36 | 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x07, 0x78, 0x00, 0x00, 0x80,
37 | 0x01, 0x00, 0x00, 0x0f, 0x78, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x0f,
38 | 0x3c, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x1e, 0x3c, 0x00, 0x00, 0x00,
39 | 0x00, 0x00, 0x00, 0x1e, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
40 | 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x0e, 0x00, 0x00, 0x00,
41 | 0x00, 0x00, 0x00, 0x38, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38,
42 | 0x0f, 0x80, 0x03, 0x80, 0x01, 0x00, 0x00, 0x78, 0x0f, 0x80, 0x03, 0xc0,
43 | 0x03, 0x1c, 0x00, 0x70, 0x07, 0x00, 0x01, 0xe0, 0x07, 0x1c, 0x00, 0x70,
44 | 0x07, 0x00, 0x00, 0xf0, 0x0f, 0x00, 0x00, 0x70, 0x07, 0x00, 0x00, 0x78,
45 | 0x1e, 0x00, 0x00, 0x70, 0x07, 0x00, 0x00, 0x3c, 0x3c, 0x00, 0x00, 0xf0,
46 | 0x07, 0x00, 0x00, 0x1e, 0x78, 0x00, 0x00, 0xf0, 0x07, 0x00, 0x00, 0x0f,
47 | 0xf0, 0x00, 0x00, 0xf0, 0x07, 0x00, 0x80, 0x07, 0xe0, 0x01, 0x00, 0xf0,
48 | 0x07, 0x00, 0xc0, 0x03, 0xc0, 0x03, 0x00, 0xf0, 0x07, 0x07, 0xe0, 0x01,
49 | 0x80, 0x07, 0x70, 0x70, 0x87, 0x0f, 0xf0, 0xc0, 0x03, 0x0f, 0xf8, 0x70,
50 | 0xc7, 0x1f, 0xf8, 0xe0, 0x07, 0x1e, 0xfc, 0x71, 0xef, 0x3f, 0xfc, 0xf1,
51 | 0x0f, 0x3f, 0xfe, 0x73, 0xff, 0x7d, 0xfe, 0xfb, 0x9f, 0x7f, 0xdf, 0x7f,
52 | 0xfe, 0xf8, 0xcf, 0x7f, 0xfe, 0xf3, 0x8f, 0x7f, 0x7e, 0xf0, 0x87, 0x3f,
53 | 0xfc, 0xe1, 0x07, 0x3f, 0x3e, 0xe0, 0x03, 0x1f, 0xf8, 0xc0, 0x03, 0x3e,
54 | 0x1c, 0xe0, 0x01, 0x0e, 0x70, 0x80, 0x07, 0x3c, 0x3c, 0xf0, 0x00, 0x04,
55 | 0x00, 0x00, 0x0f, 0x1c, 0x3c, 0x78, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x1e,
56 | 0x78, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x0f, 0x78, 0x1e, 0x00, 0x00,
57 | 0x00, 0x00, 0x78, 0x0f, 0xf0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07,
58 | 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x07, 0xe0, 0x03, 0x00, 0x00,
59 | 0x00, 0x00, 0xc0, 0x03, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x01,
60 | 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x01, 0x80, 0x0f, 0x00, 0x00,
61 | 0x00, 0x00, 0xf8, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00,
62 | 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0xfc, 0x00, 0x00,
63 | 0x00, 0x80, 0x1f, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xe0, 0x0f, 0x00,
64 | 0x00, 0xe0, 0x0f, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xc0, 0x3f, 0x00,
65 | 0x00, 0xfe, 0x01, 0x00, 0x00, 0x00, 0xff, 0x01, 0xc0, 0x7f, 0x00, 0x00,
66 | 0x00, 0x00, 0xfc, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff,
67 | 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00,
68 | 0x00, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x00, 0x00
69 | };
70 | #define mountain_icon_width 64
71 | #define mountain_icon_height 64
72 |
--------------------------------------------------------------------------------
/icons/README.md:
--------------------------------------------------------------------------------
1 | To convert to OLED format
2 | ```
3 | convery clock.png clock.mono
4 | xxd -i clock.mono
5 | ```
6 |
7 |
--------------------------------------------------------------------------------
/icons/cadence.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jamesmontemagno/mycadence-arduino/7b84538374d3e4329700fc49541df3da42e8f5cd/icons/cadence.png
--------------------------------------------------------------------------------
/icons/clock.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jamesmontemagno/mycadence-arduino/7b84538374d3e4329700fc49541df3da42e8f5cd/icons/clock.png
--------------------------------------------------------------------------------
/icons/mountain.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jamesmontemagno/mycadence-arduino/7b84538374d3e4329700fc49541df3da42e8f5cd/icons/mountain.png
--------------------------------------------------------------------------------
/mycadence-arduino.ino:
--------------------------------------------------------------------------------
1 | #include "Arduino.h"
2 | #include "heltec.h"
3 | #include "BLEDevice.h"
4 | #include "icons.h"
5 | #include "device.h"
6 | #include "power.h"
7 |
8 | static boolean connected = false;
9 |
10 | static BLERemoteCharacteristic* sensorCharacteristic;
11 | static BLEAdvertisedDevice* device;
12 | static BLEClient* client;
13 | static BLEScan* scanner;
14 |
15 | static int cadence = 0;
16 | static unsigned long runtime = 0;
17 | static unsigned long last_millis = 0;
18 |
19 | static int prevCumulativeCrankRev = 0;
20 | static int prevCrankTime = 0;
21 | static double rpm = 0;
22 | static double prevRPM = 0;
23 | static int prevCrankStaleness = 0;
24 | static int stalenessLimit = 4;
25 | static int scanCount = 0;
26 |
27 | #define debug 0
28 | #define maxCadence 120
29 |
30 |
31 |
32 | static bool is_bit_set(unsigned value, unsigned bitindex)
33 | {
34 | return (value & (1 << bitindex)) != 0;
35 | }
36 |
37 | // Called when device sends update notification
38 | static void notifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* data, size_t length, bool isNotify) {
39 |
40 |
41 | bool hasWheel = is_bit_set(data[0], 0);
42 | bool hasCrank = is_bit_set(data[0], 1);
43 |
44 | int crankRevIndex = 1;
45 | int crankTimeIndex = 3;
46 | if(hasWheel)
47 | {
48 | crankRevIndex = 7;
49 | crankTimeIndex = 9;
50 | }
51 |
52 | int cumulativeCrankRev = int((data[crankRevIndex + 1] << 8) + data[crankRevIndex]);
53 | int lastCrankTime = int((data[crankTimeIndex + 1] << 8) + data[crankTimeIndex]);
54 |
55 | if(debug)
56 | {
57 | Serial.println("Notify callback for characteristic");
58 | Serial.print("cumulativeCrankRev: ");
59 | Serial.println(cumulativeCrankRev);
60 | Serial.print("lastCrankTime: ");
61 | Serial.println(lastCrankTime);
62 | }
63 |
64 | int deltaRotations = cumulativeCrankRev - prevCumulativeCrankRev;
65 | if (deltaRotations < 0)
66 | {
67 | deltaRotations += 65535;
68 | }
69 |
70 | int timeDelta = lastCrankTime - prevCrankTime;
71 | if (timeDelta < 0)
72 | {
73 | timeDelta += 65535;
74 | }
75 |
76 | if(debug)
77 | {
78 | Serial.print("deltaRotations: ");
79 | Serial.println(deltaRotations);
80 | Serial.print("timeDelta: ");
81 | Serial.println(timeDelta);
82 | }
83 |
84 | // In Case Cad Drops, we use PrevRPM
85 | // to substitute (up to 4 seconds before reporting 0)
86 | if (timeDelta != 0)
87 | {
88 | prevCrankStaleness = 0;
89 | double timeMins = ((double)timeDelta) / 1024.0 / 60.0;
90 | rpm = ((double)deltaRotations) / timeMins;
91 | prevRPM = rpm;
92 |
93 | if(debug)
94 | {
95 | Serial.print("timeMins: ");
96 | Serial.println(timeMins);
97 | Serial.print("timeDelta != 0: rpm - ");
98 | Serial.println(rpm);
99 | }
100 | }
101 | else if (timeDelta == 0 && prevCrankStaleness < stalenessLimit)
102 | {
103 | rpm = prevRPM;
104 | prevCrankStaleness += 1;
105 | if(debug)
106 | {
107 | Serial.print("timeDelta == 0 and not stale yet, rpm -");
108 | Serial.println(rpm);
109 | }
110 | }
111 | else if (prevCrankStaleness >= stalenessLimit)
112 | {
113 | rpm = 0.0;
114 | if(debug)
115 | {
116 | Serial.print("stale");
117 | Serial.println(rpm);
118 | }
119 | }
120 |
121 | prevCumulativeCrankRev = cumulativeCrankRev;
122 | prevCrankTime = lastCrankTime;
123 |
124 | if(debug)
125 | {
126 | Serial.print("prevCumulativeCrankRev: ");
127 | Serial.println(prevCumulativeCrankRev);
128 | Serial.print("prevCrankTime: ");
129 | Serial.println(prevCrankTime);
130 | }
131 |
132 | cadence = (int)rpm;
133 | //Test
134 | //cadence = cadence + 2;
135 |
136 |
137 | if(debug) {
138 | Serial.print("CALLBACK(");
139 | Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
140 | Serial.print(":");
141 | Serial.print(length);
142 | Serial.print("):");
143 | for(int x = 0; x < length; x++) {
144 | if(data[x] < 16) {
145 | Serial.print("0");
146 | }
147 | Serial.print(data[x], HEX);
148 | }
149 | Serial.println();
150 | }
151 |
152 | if(digitalRead(KEY_BUILTIN) == LOW)
153 | {
154 | esp_deep_sleep_start();
155 | }
156 | }
157 |
158 | // Called on connect or disconnect
159 | class ClientCallback : public BLEClientCallbacks {
160 | void onConnect(BLEClient* pclient) {
161 | digitalWrite(LED,HIGH);
162 | Serial.println("Connected!");
163 | }
164 | void onDisconnect(BLEClient* pclient) {
165 | connected = false;
166 | delete(client);
167 | client = nullptr;
168 | digitalWrite(LED,LOW);
169 | Serial.println("Disconnected!");
170 | }
171 | };
172 |
173 | class AdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
174 | void onResult(BLEAdvertisedDevice advertisedDevice) {
175 | Serial.print("BLE Advertised Device found: ");
176 | Serial.println(advertisedDevice.toString().c_str());
177 | if(advertisedDevice.getName().size() > 0) {
178 | BLEAdvertisedDevice * d = new BLEAdvertisedDevice;
179 | *d = advertisedDevice;
180 | addDevice(d);
181 | }
182 | }
183 | };
184 |
185 | void updateDisplay() {
186 | Heltec.display->clear();
187 |
188 | // Runtime
189 | Heltec.display->setFont(ArialMT_Plain_24);
190 | char buf[5];
191 | const int minutes = int(runtime / 60000);
192 | itoa(minutes, buf, 10);
193 | Heltec.display->setTextAlignment(TEXT_ALIGN_RIGHT);
194 | Heltec.display->drawString(48, 0, buf);
195 | Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
196 | Heltec.display->drawXbm(0, 4, clock_icon_width, clock_icon_height, clock_icon);
197 | Heltec.display->drawString(48, 0, ":");
198 | const int seconds = int(runtime % 60000)/1000;
199 | if(seconds < 10) {
200 | buf[0] = '0';
201 | itoa(seconds, &buf[1], 10);
202 | } else {
203 | itoa(seconds, buf, 10);
204 | }
205 | Heltec.display->drawString(54, 0, buf);
206 |
207 | // Cadence
208 | Heltec.display->drawXbm(0, 26, cadence_icon_width, cadence_icon_height, cadence_icon);
209 | itoa(cadence, buf, 10);
210 | Heltec.display->drawString(22, 22, buf);
211 |
212 | uint8_t progress = 0;
213 | if(cadence >= maxCadence)
214 | progress = 100;
215 | else
216 | progress = uint8_t((100 * cadence) / maxCadence);
217 | Heltec.display->drawProgressBar(0, 49, 126, 14, progress);
218 |
219 | Heltec.display->display();
220 | }
221 |
222 | bool connectToServer() {
223 | Serial.print("Connecting to ");
224 | Serial.println(device->getName().c_str());
225 |
226 | Heltec.display->clear();
227 | Heltec.display->setLogBuffer(10, 50);
228 | Heltec.display->setFont(ArialMT_Plain_10);
229 | Heltec.display->println("Connecting to:");
230 | Heltec.display->println(device->getName().c_str());
231 | Heltec.display->drawLogBuffer(0, 0);
232 | Heltec.display->display();
233 |
234 | client = BLEDevice::createClient();
235 | client->setClientCallbacks(new ClientCallback());
236 | client->connect(device);
237 |
238 | // Sometimes it immediately disconnects - client will be null if so
239 | delay(200);
240 | if(client == nullptr) {
241 | return false;
242 | }
243 |
244 | BLERemoteService* remoteService = client->getService(serviceUUID);
245 | if (remoteService == nullptr) {
246 | Serial.print("Failed to find service UUID: ");
247 | Serial.println(serviceUUID.toString().c_str());
248 | client->disconnect();
249 | return false;
250 | }
251 | Serial.println("Found device.");
252 |
253 | // Look for the sensor
254 | sensorCharacteristic = remoteService->getCharacteristic(notifyUUID);
255 | if (sensorCharacteristic == nullptr) {
256 | Serial.print("Failed to find sensor characteristic UUID: ");
257 | Serial.println(notifyUUID.toString().c_str());
258 | client->disconnect();
259 | return false;
260 | }
261 | sensorCharacteristic->registerForNotify(notifyCallback);
262 | Serial.println("Enabled sensor notifications.");
263 |
264 |
265 | Serial.println("Activated status callbacks.");
266 |
267 | return true;
268 | }
269 |
270 | void setup() {
271 | Serial.begin(115200);
272 | Serial.flush();
273 | delay(50);
274 |
275 | Heltec.display->init();
276 | Heltec.display->flipScreenVertically();
277 |
278 | pinMode(LED_BUILTIN, OUTPUT);
279 | digitalWrite(LED_BUILTIN, LOW);
280 |
281 | pinMode(KEY_BUILTIN, OUTPUT);
282 | digitalWrite(KEY_BUILTIN, HIGH);
283 |
284 | BLEDevice::init("");
285 | scanner = BLEDevice::getScan();
286 | scanner->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallbacks());
287 | scanner->setInterval(1349);
288 | scanner->setWindow(449);
289 | scanner->setActiveScan(true);
290 | }
291 |
292 | void loop() {
293 | // Start scan
294 | if(!connected){
295 | Serial.println("Start Scan!");
296 | Heltec.display->clear();
297 | Heltec.display->drawXbm(64, 0, mountain_icon_width, mountain_icon_height, mountain_icon);
298 | Heltec.display->setLogBuffer(10, 50);
299 | Heltec.display->setFont(ArialMT_Plain_10);
300 | Heltec.display->println("Starting Scan..");
301 | Heltec.display->drawLogBuffer(0, 0);
302 | Heltec.display->display();
303 | scanner->start(11, false); // Scan for 10 seconds
304 | BLEDevice::getScan()->stop();
305 |
306 | device = selectDevice(); // Pick a device
307 | if(device != nullptr) {
308 | connected = connectToServer();
309 | if(!connected) {
310 | Serial.println("Failed to connect...");
311 | Heltec.display->println("Failed to");
312 | Heltec.display->println("connect...");
313 | Heltec.display->drawLogBuffer(0, 0);
314 | Heltec.display->display();
315 | delay(3100);
316 | return;
317 | }
318 | scanCount = 0;
319 | } else {
320 | Serial.println("No device found...");
321 | Heltec.display->println("No device");
322 | Heltec.display->println("found...");
323 | Heltec.display->drawLogBuffer(0, 0);
324 | Heltec.display->display();
325 | scanCount++;
326 | if(scanCount > 5)
327 | {
328 | esp_deep_sleep_start();
329 | return;
330 | }
331 | delay(3100);
332 | return;
333 | }
334 | }
335 |
336 | // Update timer if cadence is rolling
337 | if(cadence > 0) {
338 | unsigned long now = millis();
339 | runtime += now - last_millis;
340 | last_millis = now;
341 | } else {
342 | last_millis = millis();
343 | }
344 |
345 | delay(200); // Delay 200ms between loops.
346 | updateDisplay();
347 | }
348 |
--------------------------------------------------------------------------------
/power.h:
--------------------------------------------------------------------------------
1 | /*
2 | https://rextester.com/l/r_online_compiler
3 |
4 | f <- function(x1,x2,a,b1,b2) {a * (b1^x1) * (b2^x2) }
5 |
6 | # generate some data
7 | x1 <- c(11,15,18,21,23,11,15,18,22,24)
8 | x2 <- c(80,80,80,80,80,100,100,100,100,100)
9 | y <- c(60,84,113,145,187,90,122,162,216,262)
10 | dat <- data.frame(x1,x2, y)
11 |
12 | # fit a nonlinear model
13 | fm <- nls(y ~ f(x1,x2,a,b1,b2), data = dat, start = c(a=3, b1=1.02,b2=1.09))
14 |
15 | # get estimates of a, b
16 | co <- coef(fm)
17 |
18 | print(co)
19 |
20 | OUTPUT:
21 | a b1 b2
22 | 7.228959 1.090112 1.015343
23 |
24 | power = pow(1.090112, resisstance) * pow(1.015343, cadence) * 7.228958
25 |
26 | fmt.Println(math.Pow(1.090112, 18) * math.Pow(1.015343, 30.0) * 7.228958)
27 |
28 | Resistance
29 | https://www.desmos.com/calculator/j5fmz904ps
30 |
31 | Echelon Values
32 | 70,10,50
33 | 70,7,40
34 | 50,7,22
35 | 50,10,30
36 | 50,13,38
37 | 50,17,53
38 | 50,24,100
39 | 49,7,22
40 | 54,26,151
41 | 74,26,225
42 | 60,20,96
43 | a b1 b2
44 | 3.737140 1.023652 1.095726
45 |
46 | Peloton Values
47 | 80,30,60
48 | 80,35,84
49 | 80,40,113
50 | 80,45,145
51 | 80,50,187
52 | 100,30,90
53 | 100,35,122
54 | 100,40,162
55 | 100,45,216
56 | 100,50,262
57 | a b1 b2
58 | 3.165689 1.018363 1.053851
59 |
60 | Equation
61 | 1.023652^C * 1.095726^R * 3.737140 = 3.165689 * 1.018363^C * 1.053851^T
62 | Peleton R = -19.0654(-0.00518019*C - 0.0914172*R - 0.16595)
63 |
64 | */
65 |
66 | // Calculate the power
67 | int getPower(int cadence, int resistance) {
68 | if(cadence == 0 || resistance == 0) return 0;
69 | return int(pow(1.090112, resistance) * pow(1.015343, cadence) * 7.228958);
70 | }
71 |
72 | int getPeletonResistance(int resistance) {
73 | int pr = int((0.0116058 * pow(resistance, 3)) + (-0.568462 * pow(resistance, 2)) + (10.4126 * resistance) - 31.4807);
74 | return pr < 0 ? 0 : pr;
75 | }
76 |
--------------------------------------------------------------------------------