brianpeiris / switchmate

A python-based command line utility for controlling Switchmate switches
MIT License
35 stars 12 forks source link

Cannot find original TSM001W on raspberry pi 3B+ #15

Closed shanestep closed 6 years ago

shanestep commented 6 years ago

Does this script work with the Original TSM001W on raspberry pi 3 (b+) using the internal Bluetooth chip?

I bought a raspberry pi to use this script but have been unsuccessful after 3 days of attempts.

I am using a raspberry pi 3 B+ with updated raspbian stretch and switchmate TSM001W with firmware 2.99.18 and app version 3.3.0 (on nexus 6p). I have wifi disabled to prevent any interference with bluetooth.

What I have tried (in no particular order):

Result: Scanning...... No switchmate devices found

Other information: the switchmate works reasonably well on my phone but not on other (older) phones or tablets that I have tried.

I believe the problem may be related to the raspberry pi and the limitations of its internal bluetooth chip and not necessarily the switchmate script.

I would like to know if anyone has had success with the raspberry pi 3 internal bluetooth and any other troubleshooting that may be useful.

brianpeiris commented 6 years ago

I have a Raspberry Pi 3 B (not a B+) that I use with my Original switch with firmware version 2.99.18.

Not sure if the B+ may be different enough to cause an issue.

I would suggest trying these steps:

shanestep commented 6 years ago

I tried the above but no go. Only seeing the same 4 devices (tv etc). Interestingly I searched using my Windows laptop and can see the switchmate fine and view all (most) of its services and info. But the RPI seems to ignore it while others working fine.

I understand the wireless chip is different on the B+ which 4.2 (as opposed to 4.1 I believe). Could 4.2 bluetooth security enhancements be the issue?

brianpeiris commented 6 years ago

A comment on this issue hints that you may need to update your B+ operating system https://github.com/noble/bleno/issues/372#issuecomment-377405362

shanestep commented 6 years ago

I got it working after many hours of troubleshooting. I remove all bluetooth packages and then updated bluez to the latest source but still same problems.

Then I discovered that the BT chip uses a different firmware than the RPI 3B that is non-free licensed and not easy to update.

The fix was to replace the BT firmware files and attach them with btattach. Instructions can be found here - https://wiki.gentoo.org/wiki/Raspberry_Pi

brianpeiris commented 6 years ago

Glad to hear you figured it out in the end. I would hope that updating Raspbian would have the same effect, but it's good to know the root cause as well. I'm going to call this one closed. Thanks for reporting and for investigating!

joshuamooree commented 6 years ago

I'm having the same issue. Running Raspbian 9.4,, latest updates, on a RPi 3B+, also firmware 2.99.18. Also ran the rpi-update tool to update to the latest firmware. Rebooted many times, tried everything mentioned on this page (the btattach thing doesn't work for me at all and just causes the scan tools to fail saying they can't find the bluetooth adapter). Nothing - can't see the device with any of the scan tools mentioned. Any hints?

shanestep commented 6 years ago

The Btattach gave me errors as well. What worked for me was changing the the firmware for the bluetooth card - instructions found at https://wiki.gentoo.org/wiki/Raspberry_Pi. You need the updated BCM4345C0.hcd file and place it in /lib/firmware/brcm directory. (the firmware can be found on the raspbian bluez-firmware repository to in the above wiki link) then using hciattach /dev/ttyAMA0 bcm43xx 3000000 flow -

I cannot remember if hciattach worked without errors but I do remember the bluetooth was not working at all until after I rebooted and then it worked fine.

joshuamooree commented 6 years ago

Tried that already - no go. Also, the rpi-update command gets that same file. I've read on the raspberry pi the firmware files have to go in a special place, in addition to the /lib/firmware/brcm directory, to work. That tool is supposed to do what's necessary for the firmware to work. Anyway, so I already had the same firmware as what that link had. Tried using the command you mentioned. It failed (sort of, maybe?) saying bcm43xx_init Failed to write reset command Can't initialize device: success

After that hcitool stopped working at all, as did your tool. After a reboot, everything works again, but still fails to find the switchmate. Also, before you ask, yes bluetooth is enabled with rfkill. Also, one weird thing is that hcitool lescan does find a device, but that device doesn't have the right MAC and that MAC changes periodically. One of the tools said it was Microsoft something, but I'm not really sure what it is (hcitool doesn't say anything, just the MAC and, to my knowledge, I don't have any other BLE devices).

shanestep commented 6 years ago

Sounds like you have the same issue I did. What I said above is what worked for me but I also uninstalled the bluetooth drivers/packages (rpi-bluetooth, bluez, bluman etc) a few times and reinstalled. I cannot remember if it was uninstalled prior to changing the firmware and reinstalled after but it is possible.

Also at one point I installed a newer version of bluez from compiling the source which is suppose to bring updates with respect to Bluetooth LE. So possibly you can try that.

With respect to the Mac that you find but changes, I observed that as well despite not knowing I had any Bluetooth Low energy devices but turns out is was a Samsung TV, (my neighbour's) Apple TV box , I believe my laptop and something else by Google (maybe home mini).

I am reasonably certain it is a firmware (or possibly bluetooth driver) problem. I would advise you play around with the firmware/hciattach potentially by detaching/uninstalling firmware/drivers first and then installing and attaching from fresh (and preferably not with rpi-update for the firmware and bluez).

The site above says "Both commands will create a HCI device (e.g. hci0) in /sys/class/bluetooth and load the required firmware." This suggests that it may be a conflict if you already have a hci0 for your bluetooth. Also there was discussion at https://forums.gentoo.org/viewtopic-p-8220298.html#8220298 dealing with the need to use specific setting for the 3B+ board i.e. flow 3000000 (However this should be already present in Raspbian but if you are reinstalling manually from ground up it may be useful).

Also ensure that if you have the lastest version of the switchmate python script, you need the latest version of bluepy (hcitool lescan should still see it regardless, so this is likely not your problem).

I hope any of the above information works for you. If not what type of switchmate model, firmware etc are you using?

brianpeiris commented 6 years ago

(thanks for helping each other out 🙂. Seems like I'll have to get myself a RPi 3B+ at some point. It's unfortunate to hear the bluetooth stack is so different/buggy)