WouterJD / FortiusANT

FortiusANT enables a pre-smart Tacx trainer (usb- or ANT-connected) to communicate with TrainerRoad, Rouvy or Zwift through ANT or Bluetooth LE.
GNU General Public License v3.0
146 stars 78 forks source link

Implement BLE, using bleak and bless #366

Closed WouterJD closed 2 years ago

WouterJD commented 2 years ago

Issue is currently ready for integration in master

By using bless, native python -> BLE libraries are used and the node.js construction can be kept as an alternative.

ToDo

Advantage

Risks

Remark for windows users

Advertisement On you smartphone you will see "A smart trainer" and select it...so you might not even notice. The name of the trainer should be "FortiusAnt Trainer".

No solution yet available from bless; note that such naming differences also exist using -b node.js


It's under investigation; see bless and bleak for currently open issues. bless - the library for a new FortiusAnt BLE interface (BLE server) bleak - the library to debug FortiusAnt (BLE client, simulating TrainerRoad or Zwift) https://github.com/kevincar/bless/issues/60 (No access on raspberry pi, bypass available) https://github.com/kevincar/bless/issues/74 (sample FTMS, under discussion) https://github.com/kevincar/bless/discussions/76 (How to set Generic Access Profile, not blocking, previously issue#75) not an issue anymore: https://github.com/hbldh/bleak/issues/688 (timeout issue, wait loop implemented) https://github.com/hbldh/bleak/issues/775 (Indications not received on Windows 10, no answer, does not occur now) closed: https://github.com/kevincar/bless/issues/61 https://github.com/kevincar/bless/issues/63 https://github.com/kevincar/bless/issues/66 https://github.com/kevincar/bless/issues/67 (should be solved) https://github.com/kevincar/bless/issues/72 https://github.com/kevincar/bless/issues/73; answered https://github.com/hbldh/bleak/issues/739 https://github.com/hbldh/bleak/issues/782; DeviceName and attributes https://github.com/hbldh/bleak/discussions/772

WouterJD commented 2 years ago

I'm working on this issue for a native python implementation for BLE; to remove the nodejs complexity AND to resolve issues (the structure is too complex).

First version is in https://github.com/WouterJD/FortiusANT/tree/Implement-BLE-using-bless

cleCollector.py is a client to any FitnessMachine (trainer, FortiusAnt) like TrainerRoad and/or Zwift with purpose to show data and make some simple simulations.

Although at 98% I do not get it work and have raised an issue with the bleak developer https://github.com/hbldh/bleak/discussions/772#discussioncomment-2239103

Who can tell me what I'm doing wrong 🙂

decodeais commented 2 years ago

I tried to understand what you are doing. I have not the background and the knowledge to help you. But why can't you you use a ready BLE Analyser like the one from Keuwlsoft. It reads all messages and had the possibility send something? If the question to stupid please erase this text.

Toetta commented 2 years ago

Im happy to try on my installation if you can harvest any info from it... Otherwise I think everything is way beyond my level

WouterJD commented 2 years ago

@decodeais not a stupid remark at all. When I learn an interface, I tend to build something useful. In doing it, I understand the issues, also for other programmers. So it's not so much BLE-analysis but more end2end interface building. This time for bleak.

MeanHat commented 2 years ago

@WouterJD - I remember you saying that you wanted to simplify the BLE installation - that's great work to remove node.js. I've no expertise in this area but would be very happy to lend a hand with RPi testing (0W, 02W, 3B+) and the manual if you want any assistance. Keith

WouterJD commented 2 years ago

@MeanHat thanks. First client version will be available this week. This client can be used to "see" (and Analyse) what is transmitted by FortiusAnt.

MeanHat commented 2 years ago

Great work @WouterJD !

Toetta commented 2 years ago

👍🏻🥂🤘🏻🔥

WouterJD commented 2 years ago

https://github.com/WouterJD/FortiusANT/blob/Implement-BLE-using-bless/pythoncode/bleClient.py is now available. You can use this program to validate the transmission of FortiusAnt (BLE). The program works on raspberry rpi0W raspbian (10) buster It does not work well on Windows 10 (indications not received), reason unknown

See also: https://github.com/hbldh/bleak/discussions/772#discussioncomment-2243434

WouterJD commented 2 years ago

Good news, prototype for bleServer.py is working and pairing with Trainer Road and Rouvy. To be cleaned for testable version... Work for next week :-)

MeanHat commented 2 years ago

Good news, prototype for bleServer.py is working and pairing with Trainer Road and Rouvy. To be cleaned for testable version... Work for next week :-)

That's really great news @WouterJD - great work! That is a gamechanger! I'd be delighted to test with Zwift and Fortius when ready. On standby...

decodeais commented 2 years ago

Good news, prototype for bleServer.py is working and pairing with Trainer Road and Rouvy. To be cleaned for testable version... Work for next week :-)

I would like to test pre alpha state to. It would be interesting to get a chance for bug hunting.

WouterJD commented 2 years ago

It's like a cliffhanger... To be continued in next episode😀

MeanHat commented 2 years ago

Can't wait!

On Fri, 25 Feb 2022, 17:13 Wouter Dubbeldam, @.***> wrote:

It's like a cliffhanger... To be continued in next episode😀

— Reply to this email directly, view it on GitHub https://github.com/WouterJD/FortiusANT/issues/366#issuecomment-1051037436, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARS665FOXR6YLFPGFC27S7LU462CTANCNFSM5MIAN3TA . You are receiving this because you were mentioned.Message ID: @.***>

BikeBeppe64 commented 2 years ago

It's like a cliffhanger... To be continued in next episode😀

All we need is.... your next episode!!

MeanHat commented 2 years ago

Hi @WouterJD - I've been testing today as you requested (using Pi 0 2 W as it is a lot quicker and I can then revert to Pi 0 W). I have done / found the following:

  1. There was an error cloning the branch. I increased the swapfile size from 100 (to 1024) to allow me to clone the branch. sudo dphys swapfileoff; sudo nano /etc/dphys-swapfile changing to CONF_SWAPSIZE = 1024; sudo dphys-swapfile setup; sudo dphys-swapfile swapon
  2. I modified FortiusAnt.sh to -a -bb -D-1 -s -d CEWIDbu
  3. There was an error finding lib_detect_testenv so I installed manually git clone https://github.com/bitranox/lib_detect_testenv.git followed by cd lib_detect_testenv and sudo python setup.py install
  4. There was a service_identity error so I installed manually python3 -m pip install service_identity
  5. I am now getting an error: "org.freedesktop.DBus.Error.AccessDenied: Connection 1.10 not allowed". I ran sudo usermod -G bluetooth -a pi and this resulted in the same error but Connection 1.49

The log file is attached for info: FortiusAnt.2022-03-04 16-25-06.log

I've had to stop at this stage but happy to pick up again at a future date. Hope this is a small help.

WouterJD commented 2 years ago

Hi guys, I'm still engineering the code. It's not trivial. As you see in the headers, some questions and support required from bless & bleak :-)

WouterJD commented 2 years ago

5. I am now getting an error: "org.freedesktop.DBus.Error.AccessDenied: Connection 1.10 not allowed". I ran sudo usermod -G bluetooth -a pi and this resulted in the same error but Connection 1.49

See bless/issue/#60

In /etc/dbus-1/system.d/bluetooth.conf add

  <policy user="pi">
    <allow own="org.bluez.FortiusAntTrainer"/>
    <allow send_destination="org.bluez.FortiusAntTrainer"/>
  </policy> 
MeanHat commented 2 years ago

Great work @WouterJD! I should have some time for more testing to support you next week and will run the addition you suggest.

WouterJD commented 2 years ago

Current version is 99% final. There is one open issue; the DeviceName and Appearance cannot be set. Let's hope @kevincar can answer how to do that. It's not blocking for TrainerRoad or Rouvy.

Toetta commented 2 years ago

Fantastic news, great work

WouterJD commented 2 years ago

Fantastic news, great work

Hi Malkus, feel free to test!

WouterJD commented 2 years ago

When FortiusAnt is advertising over BLE, depending on the interface and backends, the CTP (Trainer Road, Rouvy) will see the following devices:

a Windows Raspberry
-b using NodeJs hostname FortiusANT trainer
-bb using Bless hostname BLE-ADDRESS
Useful sources: check here and here It did not help me

With my knowledge and what I found, there is not much I can do about it.

There is no practical difference, since your CTP sees one usable device and you select it. Having "FortiusAnt Trainer" as displayed name would have been more elegant.

For I now, I surrender.

Toetta commented 2 years ago

64bit Bullseye up and running with FortiusANT using bless =)

Screenshot from 2022-03-27 14-21-17

WouterJD commented 2 years ago

Congrats. Does your app recognize it?

Toetta commented 2 years ago

Yes! Have taken a ride with Zwift and worked well!

Possibly dropping cconnection adter some time. I will have to investigate that further whats going on

WouterJD commented 2 years ago

Cheerio! Let me kno😀

MeanHat commented 2 years ago

Hi @WouterJD - I've been trying bless branch on RPi0 2W. Clean reinstall with Debian Bullseye 32bit.

Toetta commented 2 years ago

I had problems getting Zwift to have a stable connection with the 64 bit. The raspberry disappeared from the available devices and got lost signal responce from zwift. Possibly resolved just now by installing the x11-utils package..? Now managed to pedal a couple of km with stable connection :) will continue later

WouterJD commented 2 years ago

Hi @WouterJD - I've been trying bless branch on RPi0 2W. Clean reinstall with Debian Bullseye 32bit.

  • I had to change the permissions on some of the new raspberry *.sh files Thanks

  • I had to install lib_detect_testenv using the install I described above https://github.com/WouterJD/FortiusANT/issues/366#issuecomment-1059339128 and also increase the swapfile size to 1024 to prevent error when using git clone to get the new branch "Implement-BLE-using-bless". Is this bullseye specific, since I use the exact same Raspberry installation as before and not required

  • I used options -a -bb -s -D-1 -d CEWIDbpjmuafA in FortiusAntMain.sh There is no need to modify FortiusAntMain, usually parameters would be set in FortiusAnt.sh, or is that what you mean?

  • FortiusANT ran yet while using bluetooth on CTP=Zwift running on either Win11 or Android devices I could not detect any bluetooth devices Did you try bleBleak.py to act as a testing client?

  • I have attached the logfile: FortiusAnt.2022-03-30 16-24-32.log I see no special issues; other than that there is not very much of logging by bless

  • I have no doubt made a mistake somewhere. Do you have any suggestions? Hmmm... not right now

WouterJD commented 2 years ago

I have uploaded some error-handling improvements and access rights settings

WouterJD commented 2 years ago
  • I used options -a -bb -s -D-1 -d CEWIDbpjmuafA in FortiusAntMain.sh

To avoid this, you can use -d all or -d log; the difference is that log will not create a json-file.

Note that in the standard python logging system, the messges are hierarchical:

-d CEWID is therefore identical to -d D

As opposed to the FortiusAnt flag-approach, where -d DW would produce Debug and Warning but not the others.

MeanHat commented 2 years ago

In answer to your points above https://github.com/WouterJD/FortiusANT/issues/366#issuecomment-1090532681:

Testing again...

WouterJD commented 2 years ago

I tried running bleBleak.py but without success

Please explain, copy output...

MeanHat commented 2 years ago

Here's the bleBLEAK.sh output: bleBLEAK_output-22-04-14.txt

MeanHat commented 2 years ago

Hi @WouterJD - I'm testing again, this time using Debian Buster rather than Bullseye on an RPi0 2 W and here is an update of the results:

I will do a fresh install of Buster and try Node.js to check "original" setup is still functioning and problem is not related to RPi0 2 W

MeanHat commented 2 years ago

ps - I've been using RPi0 2 W rather than RPi0 as it is a lot faster to set up etc.

MeanHat commented 2 years ago

RPi 0 2 W worked with Node.js (though needs fix in issue #57

Will now test RPi 0 with Bullseye and bless

WouterJD commented 2 years ago

Special. I've updated 3_InstallBless_Temporarily.sh (to be published) Will communicated what device I have, I think it's a new type because booting much faster than I was used to.

MeanHat commented 2 years ago
Here's an update on the RPi testing: OS Buster 10.12 Buster 10.12 Bullseye 11.3 Bullseye 11.3
BLE Mode Node.js bless Node.js bless
RPi0 OK (other than disconnect - see issue #57) NO (see log) OK (other than disconnect - see issue #57) (see log) NO (see log)
RPi0 2 OK (other than disconnect - see issue #57) NO (see log posted previously) NO NO (see previous post)

I have focussed on RPi0 as RPi0 2 is fairly different (ARMv7-l vs ARMv6-l, Bluetooth 4.2 vs 4.1 etc). The closest I came was RPi0 running Buster and bless linking to Zwift as the CTP. Zwift recognised FortiusANT but could not achieve a stable connection in simulation mode. Hopefully the log file will help. During the install (running shell scripts 1, 2, 3_InstallBless_Temporarily.sh, 5, 9_GrantAccessToBluetoothForBless.sh and 9_GrantAccessToUSB_withReboot.sh the main errors were (a) in shell 3 I had to change line 4 from python to python3 and (b) there was a prompt to add /home/pi/.local/bin to PATH.

Hope this helps a little. I'm keen to carry on the work to get RPi0 running Buster working with bless. (Next steps could then be to extend the solution further to use Bullseye and then RPi0 2?)

RPi0_bullseye_bless.log RPi0_bullseye_nodejs.log RPi0_buster_bless.log

MeanHat commented 2 years ago

Special. I've updated 3_InstallBless_Temporarily.sh (to be published) Will communicated what device I have, I think it's a new type because booting much faster than I was used to.

Thank you - I used this in the testing reported above

WouterJD commented 2 years ago

Implemented and released

WouterJD commented 2 years ago

Here's an update on the RPi testing:

Hi @MeanHat I have released the BLE/bless version today, and I come across this test-work in my Inbox-to-be-handled. Please share your thoughts.

I'm trying to get clean for the tacx-season :-)

MeanHat commented 2 years ago

Hi @WouterJD - I am sorry for the slow reply. I've been working on some other projects and got distracted! I have not tested bleak & bless since my last trials (I submitted feedback and comments). If you'd like me to test anything again on RPi3, RPi0 or RPi0 2 please let me know.

WouterJD commented 2 years ago

Hi @MeanHat I did not do a full regression test, so such a test would be very welcome. If there are any issues, we can quickly resolve.

I wanted to proceed with steering, using the BLE base, without keeping branches open - although technically possible

MeanHat commented 2 years ago

Ok @WouterJD - leave it with me for a few days and I'll do a full test.

WouterJD commented 2 years ago

👍

WouterJD commented 1 year ago

Have been quite busy lately; just stepped in again and did some small stuff. RPi testing will come later.

Now trying to get TrainerRoad back up again.