Fix for Bluetooth

601 views
Skip to first unread message

RandyR

unread,
Feb 5, 2022, 12:56:11 PM2/5/22
to PicoChess
Finally, after much hair-pulling and head scratching, I think I've solved the bluetooth issue, which was the inability of PicoChess to reconnect to the bluetooth board after the initial boot (which did connect). 

The problem was the way the BlueZ 5.50 works (specifically bluetoothctl) which is slightly different from the older v5.43. PicoChess uses a bluetoothctl and rfcomm subprocesses to connect to the bluetooth board by looking for either 'DGT_BT_' or 'PCS-REVII' in the name of the device when scanning for bluetooth devices. In v5.43, bluetoothctl actually displays already-paired devices when it is run, including the name of the device. PicoChess sees this and carries on with the connection. But in BlueZ v5.50, bluetoothctl doesn't display the paired devices, and when the scan is started, previously paired devices do not show their name - only the Received Signal Strength Indication (RSSI). So PicoChess never sees the board and never establishes a connection. If the board was never previously paired, which is the case in a new image, the scan by bluetoothctl (both v5.43 and v5.50) detects NEW devices and displays their BT MAC address and (most importantly) Name. Then PicoChess connects as it should.

The code is not very pretty as I'm not a python dev so if anyone wants to clean it up, feel free.

Another issue previously discussed here was the requirement for a delay in starting the picochess.service. I did some exprimenting and a delay of 8 seconds on boot would work most of the time, but not always. So it is currently set to 10 seconds in my images. I will update them and advise when complete.

For those who don't want to download the new images, I have attached the file to this post. Extract it, stop the picochess service ('sudo service picochess stop'), back up your /opt/picochess/dgt/board.py ('sudo mv /opt/picochess/dgt/board.py /opt/picochess/dgt/board.py.bak') and replace with the one in from the archive. Then set the ownership to root and permissions to 755:

sudo chown root:root /opt/picochess/dgt/board.py
sudo chmod 755 /opt/picochess/dgt/board.py


To disable Eric Singer's cron job (if you haven't already):

sudo crontab -e

Add a hash mark (#) before the fix_bluetooth line:

#@reboot /opt/picochess/scripts/fix_bluetooth_4b.sh 1>/opt/picochess/logs/fix_bluetooth_4b.log 2>&1

To add a delay in the picochess service:

sudo nano /etc/systemd/system/picochess.service

And add an 'ExecStartPre' line just above the 'ExecStart' line in the [Service] section:

.
.
.
ExecStartPre=/bin/sleep 10
ExecStart=/usr/bin/python3 /opt/picochess/picochess.py
ExecStop=/usr/bin/pkill -f picochess.py
.
.
.


Then reboot ('sudo reboot').

Let me know if you run into issues.

Randy
board.py.zip

Dirk

unread,
Feb 5, 2022, 1:38:22 PM2/5/22
to PicoChess

How cool is this!?

Thank you so much Randy for taking care of this issue (my solution was to no longer update the OS ;-) by even getting a bluetooth eBoard - really unbelievable.

I will try this solution when I am back from traveling…

Thanks
Dirk

Randy Reade

unread,
Feb 5, 2022, 2:03:48 PM2/5/22
to pico...@googlegroups.com
Safe travels, Dirk.

Randy

--
You received this message because you are subscribed to the Google Groups "PicoChess" group.
To unsubscribe from this group and stop receiving emails from it, send an email to picochess+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/picochess/b9f616eb-528c-4523-a85b-08a13acb6323n%40googlegroups.com.

Marc Hamilton

unread,
Feb 6, 2022, 2:30:48 PM2/6/22
to PicoChess
Made the change to a fresh copy of your Lite (Debian 10)  - No issues, connects well - thanx

Randy Reade

unread,
Feb 6, 2022, 2:34:52 PM2/6/22
to pico...@googlegroups.com
Thanks for letting me know, Mark. I'll re-tackle Bullseye next and maybe have a look at 64-bit.

Randy

You received this message because you are subscribed to a topic in the Google Groups "PicoChess" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/picochess/0kgxQLVGA8g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to picochess+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/picochess/be4ea0cc-940e-403d-9b7d-9744e6b9d597n%40googlegroups.com.

Wilhelm

unread,
Feb 8, 2022, 7:02:09 AM2/8/22
to PicoChess
Hallo Randy

Danke für deine professionelle Hilfe bei dem Bluetooth-Problem.

Bei deinem neuen Images ist mir bei folgendem Szenario etwas aufgefallen:

1) DGT-Brett EIN
2) Raspberry EIN

Das Brett wird sicher gefunden und gekoppelt.
Das Spiel beginnt.

3) DGT Brett AUS
Simulation Bluetooh-Verbindung geht verloren weil z.B. der Akku leer wurde, Entfernung zu groß usw.

4) Picochess zeigt an, dass das Brett gesucht wird

5) DGT-Brett EIN
Simulation Bluetooh-Verbindung wieder herstellen (Akku wird wieder geladen usw.)

6) Picochess sucht weiter, koppelt das Brett jedoch nicht mehr.

Das Spiel kann so nicht fortgeführt werden.

Bei deiner “Bluetooth Board.txt” - Lösung kann das Spiel bei dem gleichen Szenarium fortgeführt werden. Dort wird das Brett wieder gekoppelt.

Gruß
Wilhelm

RandyR schrieb am Samstag, 5. Februar 2022 um 18:56:11 UTC+1:

RandyR

unread,
Feb 8, 2022, 9:33:35 AM2/8/22
to PicoChess

Hi Wilhelm, I think I understand what you are saying but I'm just going by Google Translate. Perhaps someone who understands German can correct me if I'm not translating correctly. Here is what Google thinks you said:

Hallo Wilhelm, ich glaube, ich verstehe, was du sagst, aber ich gehe nur von Google Translate. Vielleicht kann mich jemand korrigieren, der Deutsch versteht, wenn ich nicht richtig übersetze. Hier ist, was Google glaubt, dass Sie gesagt haben:
___________________________________________________________________________________

Hello Randy

Thank you for your professional help with the bluetooth problem.

With your new image, I noticed something in the following scenario:

1) DGT board A
2) Raspberry A

The board will be safely found and paired.
The game begins.

3) DGT Board OFF
Simulation Bluetooh connection is lost because e.g. the battery has run out, the distance is too great, etc.

4) Picochess indicates that the board is being sought

5) DGT Board A
Restore simulation Bluetooth connection (battery is charged again, etc.)

6) Picochess keeps searching but no longer pairs the board.

The game cannot be continued like this.

With your “Bluetooth Board.txt” solution, the game can continue in the same scenario. There the board is coupled again.

___________________________________________________________________________________

Vielen Dank, dass Sie diese Bedingung getestet haben. Ich hatte das gleiche bemerkt, bin aber nie wieder dazu gekommen, es zu lösen. Und ich dachte, es sei wahrscheinlich das gleiche Verhalten wie beim Original v0.9l (das BlueZ 5.43 verwendete), habe das aber nie bestätigt. Ich dachte auch, dass Sie in diesem Fall die Teile an Ort und Stelle lassen und den Pi neu starten könnten (oder den Picochess-Dienst stoppen, Bluetooth neu starten und den Picochess-Dienst neu starten), sobald das Board aufgeladen oder angeschlossen ist. Ich war neugierig, ob Sie das rfcomm verwenden. Service-Methode (aus der Bluetooth Board.txt) würde die Verbindung wieder herstellen (nachdem das Board ein paar Minuten ausgeschaltet war), aber ich habe nicht getestet, ob das Board für längere Zeit ausgeschaltet war. Es IST wahrscheinlich die zuverlässigste Lösung, erfordert aber mehr Aufwand.

Ich werde versuchen, das Szenario „Batterie leer“ zu lösen und board.py zu aktualisieren.

In English:

Thank you for testing that condition. I had noticed the same thing but never got back to solving it. And I thought it was probably the same behavior as the original v0.9l (which used BlueZ 5.43) but never confirmed that. I also thought that if it happened you could leave the pieces in place and reboot the Pi (or stop the picochess service, restart bluetooth and restart the picochess service) once the board was charged or plugged in. I was curious if using the rfcomm.service method (from the Bluetooth Board.txt) would reconnect (after a couple minutes of the board being OFF) but I didn't test having the board OFF for a longer time. It IS probably the most reliable fix but requires more effort.

I will try to solve the 'dead battery' scenario and update board.py.

Randy

Wilhelm

unread,
Feb 8, 2022, 12:06:16 PM2/8/22
to PicoChess

Hello Randy,
Sorry I wrote in German.

You're right.
If the Bluetooth connection is lost during the game (for whatever reason) and then becomes possible again, your "Bluetooth Board.txt" solution will pair the board again. But not with your new image.

If you find a solution here too, until you are the greatest.

With Picochess you can also reload and continue the game, but the other solution is nicer.

 Wilhelm

RandyR

unread,
Feb 8, 2022, 5:26:03 PM2/8/22
to PicoChess
Wilhelm,

I just tested v0.9l and it DOES reconnect to bluetooth after losing the connection for an extended period of time. Interestingly, when it loses the connection, shortly afterwards (~15 sec) it removes the 'lost' board (it no longer shows as paired) and starts scanning again. Then it finds the board as a NEW device.

Now to apply that to BlueZ 5.5x...

Randy

Marc Hamilton

unread,
Feb 9, 2022, 6:17:26 PM2/9/22
to PicoChess
You must have updated your images... I download the Desktop expecting to see Debian 10 and was surprised to see Debian 11 pop up ... and the Bluetooth connect works! - Excellent.

Randy Reade

unread,
Feb 9, 2022, 6:26:18 PM2/9/22
to pico...@googlegroups.com
Not yet. It should be Buster, with the new board.py. But I think I have a better version now.

--
You received this message because you are subscribed to a topic in the Google Groups "PicoChess" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/picochess/0kgxQLVGA8g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to picochess+...@googlegroups.com.

m371...@gmail.com

unread,
Feb 9, 2022, 7:02:27 PM2/9/22
to pico...@googlegroups.com

Great!  Post the board.py when you’re happy with it.  I wanted to install the desktop to use the Mu app (python editor) it’s simple but has a nice editing IDE.   Thonny was too basic, PyCharm would not run in the 1GB memory of the 3b+ (and barely on the 4b) and Mu was just a better option. However the 64 bit of Debian 11 seems really good at this point. Certainly a lot of the lag I was seeing is gone.  

A YouTube video will just start playing immediately without any buffering issues.  Question is, will the 64 bit version make Picochess more efficient or will the speed increase actually cause issues?

 

 

 

Sent from Mail for Windows

Randy Reade

unread,
Feb 9, 2022, 7:11:34 PM2/9/22
to pico...@googlegroups.com
I find picochess works great (with minor code changes - any ref to armv7l needs to be changed to aarch64) in 64-bit, but there is an issue with communication in the DGTPi between the Pi and the clock. I thought it was the dgtpicom program but maybe it's the picochess code. Also, Dirk's PGN Explorer would need to be converted to 64-bit to be usable, in addition to the engines and mess.

You received this message because you are subscribed to the Google Groups "PicoChess" group.
To unsubscribe from this group and stop receiving emails from it, send an email to picochess+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/picochess/8C9F216D-5A5A-4D31-8813-8B757A5A7C5D%40hxcore.ol.

RandyR

unread,
Feb 9, 2022, 8:43:40 PM2/9/22
to PicoChess
Here's the updated board.py file. It should connect more consistently. As was previously stated on this forum, bluetooth sometimes takes a while to initialize. If using a bluetooth board with the desktop GUI images, I would suggest adding a 15 second delay to picochess.service. USB connections don't need the delay.

Wilhelm, this should reconnect if your batteries die. :^)

Any issues, let me know. I'll update my images tomorrow.

Randy
board.py

Wilhelm

unread,
Feb 10, 2022, 2:04:26 AM2/10/22
to PicoChess
great Randy,
the board connects again.

thank you
Wilhelm

Mark HL

unread,
Feb 10, 2022, 10:34:46 AM2/10/22
to pico...@googlegroups.com
Initially, I though, I'd just keep using the original brute force pairing fix, but this one is better.  After the first pairing it will connect in about the same length of time thereafter. No more having to manually re-pair the BT connection after exchanging out a Micro SD card.

Without your work, I'd be connecting with a legacy USB 2.0 cable or running .9l/V2 of Pico. 
... and it would have change how I felt about "upgrading" ro a BT board. 

Many thanks...

Marc

Sent from my Samsung Tab S7

From: pico...@googlegroups.com <pico...@googlegroups.com> on behalf of Wilhelm <elo.di...@gmail.com>
Sent: Wednesday, February 9, 2022 11:04:26 PM
To: PicoChess <pico...@googlegroups.com>

RandyR

unread,
Aug 16, 2022, 4:45:58 PM8/16/22
to PicoChess
A slight improvement for bluetooth with the attached board.py. I'll add it to my images the next time I update.

Also, for anyone running picochess in a stand-alone setup (no DGTPi or wiring mod), you will still need to run picochess via sudo (or use the picochess.service). If you attempt to run picochess from the command line (python3 picochess.py) you will see that it won't find the BT board as non-root users cannot create the rfcomm device. There is probably a way to get it working but I couldn't - even with a udev rule and adding the pi user to the dialout group. You could get around it by editing board.py and adding sudo before rfcomm in lines 541 and 675.

Let me know if you run into issue. Maybe someone could test on a Rev II. :^)

Randy

board.py.zip

Dirk

unread,
Aug 17, 2022, 12:11:22 PM8/17/22
to PicoChess
Hi Randy,

your new board.py works perfectly with my

- standalone pi4 and the revelation ii
- MolliPI (Wilhelms brilliant solution for an upgraded DGTPI)
- PicochessWeb (an PI4 inside a touchscreen case with soundboard)

Thanks again for your ongoing Picochess service!!!!!!

If we now can find a solution for a local game and opening database Picochess for me would be perfect and  the new "Mephisto Phönix" will most likely not an option for me....

Thanks again
Dirk 

Randy Reade

unread,
Aug 17, 2022, 1:16:39 PM8/17/22
to pico...@googlegroups.com
Thanks for testing, Dirk! Yes, hopefully Shiv can find the time to integrate the database locally so we won't need to rely on his server.

Randy

--
You received this message because you are subscribed to the Google Groups "PicoChess" group.
To unsubscribe from this group and stop receiving emails from it, send an email to picochess+...@googlegroups.com.

FrankE

unread,
Aug 18, 2022, 10:55:22 AM8/18/22
to PicoChess
Hi,

I managed to get the latest version from Shivs chessui running on my dgtpi. I can connect to the webinterface (port 9998) and I am able to see the bookinformation. Unfortunately I couldn't find a way how to integrate the chessclock-part in the new chessui and because this doesn't I tried to integrate the bookinformation into the already installed webinterface of Scally's latest image, which doesn't work, too.

Maybe someone can use this information to get a step further.

For the german readers and if my english isn't good enough:

ich habe die aktuellste Version von Shivs chessui auf meinem dgtpi ans Laufen gebracht. Ich kann mich mit dem Webinterface (auf Port 9998) verbinden und sehe auch die Eröffnungsbuchinformationen (usw..). Leider habe ich nicht herausgefunden, wie ich die Informationen von der Schachuhr in die neue Version von chessui einbinden kann. Ebenfalls habe ich keinen Weg gefunden (als Alternative) die Eröffnungsbuchinformationen in die bereits installierte Version des Webinterfaces von Scallys aktuellstem Image zu integrieren.

Eventuell kann jemand auf diese Informationen aufbauen?

Greetings/Grüße
Frank

RandyR

unread,
Nov 4, 2022, 2:40:08 PM11/4/22
to PicoChess
Hi everyone,

I have made a further upgrade to the Bluetooth detection code in /opt/picochess/dgt/board.py which should work better, especially for those who use a Bluetooth speaker. I will incorporate it into my images in the next release. I have also attached it here. If someone with a Revelation II board could test it for me I would be grateful. See the first post in this thread for brief instructions on how to add it (for those who have some Linux experience).

Randy


board.py.zip

Dirk

unread,
Nov 4, 2022, 2:45:55 PM11/4/22
to PicoChess
Hi Randy,

bluetooth improvements are always welcome if I think about all my Bluetooth devices for Picochess :-)

I will check on my revelation II and my PicoChessWeb system when I am back at home (unfortunately in about 2 weeks) 

Thanks
Dirk 

Shivkumar Shivaji

unread,
Nov 4, 2022, 2:48:22 PM11/4/22
to pico...@googlegroups.com
My confusion now is what exactly is needed. People need a way to build and run books on their own for picochess web server or do we just have to build one book and put up an image with it working locally?

Shiv

--
You received this message because you are subscribed to the Google Groups "PicoChess" group.
To unsubscribe from this group and stop receiving emails from it, send an email to picochess+...@googlegroups.com.

RandyR

unread,
Nov 4, 2022, 2:52:22 PM11/4/22
to PicoChess
Thanks, Dirk.

Randy

RandyR

unread,
Nov 4, 2022, 3:02:54 PM11/4/22
to PicoChess
Hi Shiv,

I would be happy to have the server code which could be run on the local (home) network (on a separate Linux PC) or could also be run on the same RPi as PicoChess is running on, with just an edit of the current app.js file to point to where the server is. Having the ability to update the book & games database(s) would be great as well. I am OK with the current layout of the web server provided by PicoChess. I just thought this would be the easiest route to take rather than incorporating your newer ChessUI code, but you know better than I as to what is involved.

Randy

Shivkumar Shivaji

unread,
Nov 4, 2022, 6:33:01 PM11/4/22
to pico...@googlegroups.com
Ah, 

That's not too hard to do. Looks like Gerhard created another format, which was not needed, but any effort is appreciated! I can support the older picochessweb format or the newer chessUI format. I will support a local version for this. I somehow assumed this was already done. 

Shiv

--
You received this message because you are subscribed to the Google Groups "PicoChess" group.
To unsubscribe from this group and stop receiving emails from it, send an email to picochess+...@googlegroups.com.

Randy Reade

unread,
Nov 4, 2022, 6:56:27 PM11/4/22
to pico...@googlegroups.com
Thanks, Shiv. Whatever you think is best.

Randy

You received this message because you are subscribed to a topic in the Google Groups "PicoChess" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/picochess/0kgxQLVGA8g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to picochess+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/picochess/CACRTmB5fTBqh_pNe%2Byk9Z-_vYhXvzrKjUCqi1LhwCEBX13vLeA%40mail.gmail.com.

RandyR

unread,
Nov 12, 2022, 4:50:01 PM11/12/22
to PicoChess
While doing some troubleshooting today, I had the same issue that others have noticed with Bluetooth not connecting on first boot after flashing the SD card. I was using my Lite image in the DGTPi. First of all, contrary to how I thought the image was set up, after resizing the root partition, the system does NOT reboot. So the first boot time is longer only due to the resize script running. There is no subsequent reboot.

Anyway, my Bluetooth board would not connect. And PicoChess just kept saying 'no e-board'. I looked at the system logs and noticed this:

pi@picochess:~ $ systemctl status hciuart
● hciuart.service - Configure Bluetooth Modems connected by UART
     Loaded: loaded (/lib/systemd/system/hciuart.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Sat 2022-11-12 14:03:33 CST; 36min ago
        CPU: 235ms

Nov 08 14:26:14 picochess systemd[1]: Starting Configure Bluetooth Modems connected by UART...
Nov 12 14:03:33 picochess btuart[429]: Initialization timed out.
Nov 12 14:03:33 picochess btuart[429]: bcm43xx_init
Nov 12 14:03:33 picochess btuart[429]: Flash firmware /lib/firmware/brcm/BCM43430A1.hcd
Nov 12 14:03:33 picochess btuart[429]: Set BDADDR UART: xx:xx:xx:xx:xx:xx
Nov 12 14:03:33 picochess btuart[429]: Set Controller UART speed to 921600 bit/s
Nov 12 14:03:33 picochess systemd[1]: hciuart.service: Control process exited, code=exited, status=1/FAILURE
Nov 12 14:03:33 picochess systemd[1]: hciuart.service: Failed with result 'exit-code'.
Nov 12 14:03:33 picochess systemd[1]: Failed to start Configure Bluetooth Modems connected by UART.

This will prevent the bluetooth.service from starting:

pi@picochess:~ $ systemctl status bluetooth
● bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: man:bluetoothd(8)

From what I have read online this might be intermittent (race?) condition which is normally solved by a reboot. I wonder if maybe it's the fake hw clock getting updated just after the hciuart.service initializes (see the time stamp for the 'Initialization timed out' line above).

I was able to get it working by restarting the hciuart.service, bluetooth.service and picochess.service (commands in bold below):

pi@picochess:~ $ sudo systemctl restart hciuart.service
pi@picochess:~ $ systemctl status hciuart
● hciuart.service - Configure Bluetooth Modems connected by UART
     Loaded: loaded (/lib/systemd/system/hciuart.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-11-12 15:32:04 CST; 22s ago
    Process: 20609 ExecStart=/usr/bin/btuart (code=exited, status=0/SUCCESS)
   Main PID: 20645 (hciattach)
      Tasks: 1 (limit: 1596)
        CPU: 124ms
     CGroup: /system.slice/hciuart.service
             └─20645 /usr/bin/hciattach /dev/serial1 bcm43xx 921600 noflow - xx:xx:xx:xx:xx:xx

Nov 12 15:32:00 picochess systemd[1]: Starting Configure Bluetooth Modems connected by UART...
Nov 12 15:32:04 picochess btuart[20632]: bcm43xx_init
Nov 12 15:32:04 picochess btuart[20632]: Flash firmware /lib/firmware/brcm/BCM43430A1.hcd
Nov 12 15:32:04 picochess btuart[20632]: Set BDADDR UART: xx:xx:xx:xx:xx:xx
Nov 12 15:32:04 picochess btuart[20632]: Set Controller UART speed to 921600 bit/s
Nov 12 15:32:04 picochess btuart[20632]: Device setup complete
Nov 12 15:32:04 picochess systemd[1]: Started Configure Bluetooth Modems connected by UART.
pi@picochess:~ $ sudo systemctl restart bluetooth
pi@picochess:~ $ systemctl status bluetooth
● bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-11-12 15:33:09 CST; 37s ago
       Docs: man:bluetoothd(8)
   Main PID: 20951 (bluetoothd)
     Status: "Running"
      Tasks: 1 (limit: 1596)
        CPU: 91ms
     CGroup: /system.slice/bluetooth.service
             └─20951 /usr/libexec/bluetooth/bluetoothd

Nov 12 15:33:09 picochess systemd[1]: Starting Bluetooth service...
Nov 12 15:33:09 picochess bluetoothd[20951]: Bluetooth daemon 5.55
Nov 12 15:33:09 picochess systemd[1]: Started Bluetooth service.
Nov 12 15:33:09 picochess bluetoothd[20951]: Starting SDP server
Nov 12 15:33:09 picochess bluetoothd[20951]: Bluetooth management interface 1.21 initialized
Nov 12 15:33:09 picochess bluetoothd[20951]: profiles/sap/server.c:sap_server_register() Sap driver initialization fail>
Nov 12 15:33:09 picochess bluetoothd[20951]: sap-server: Operation not permitted (1)
Nov 12 15:33:09 picochess bluetoothd[20951]: Failed to set privacy: Rejected (0x0b)
pi@picochess:~ $ sudo systemctl restart picochess

Randy
Reply all
Reply to author
Forward
Message has been deleted
0 new messages