-
Notifications
You must be signed in to change notification settings - Fork 5
Problem with two SBricks #11
Description
Hi,
I am not very experienced with programming and would he very happy for receiving help.
I try to address multiple SBricks. For now two sequentially, but plan to control up to five in parallel to control LEGO trains on a larger layout.
Using **Benson'**s original code with bluepy 1.1.4 on raspbian bullseye I can control two SBricks ("white" and "yellow") by running White first, then Yellow but then White is not running again because it has lost connection (Tiago's see below):
import time
import sys
from lib.sbrick_m2mipc import SbrickIpcClient
SBRICK_MAC_WHI = '00:07:80:2E:2D:C9'
SBRICK_MAC_YEL = '00:07:80:2E:42:99'
SBRICK_CHANNEL = '01'
if __name__ == '__main__':
client = SbrickIpcClient(broker_ip='127.0.0.1', broker_port=1883)
client.connect()
client.publish_drive(sbrick_id=SBRICK_MAC_WHI,
channel=SBRICK_CHANNEL,
direction = '00',
power= 'AF',
exec_time=1)
time.sleep(5)
client.publish_drive(sbrick_id=SBRICK_MAC_YEL,
channel=SBRICK_CHANNEL,
direction = '01',
power= 'AF',
exec_time=1)
time.sleep(5)
client.publish_drive(sbrick_id=SBRICK_MAC_WHI,
channel=SBRICK_CHANNEL,
direction = '00',
power= 'AF',
exec_time=1)
time.sleep(3)
client.disconnect()
sys.exit(0)
2022-01-14 17:59:35,025 [sbrick_m2mipc.py:97(DEBUG)] MainThread - Accept drive() event: {'sbrick_id': '00:07:80:2E:42:99', 'channel': '01', 'direction': '01', 'power': 'AF', 'exec_time': 1}
2022-01-14 17:59:35,026 [sbrick_api.py:104(DEBUG)] channel_01 - Exec command 010101AF
2022-01-14 17:59:35,027 [sbrick_api.py:266(DEBUG)] channel_01 - RCC characteristic writes binary: b'\x01\x01\x01\xaf'
2022-01-14 17:59:36,026 [sbrick_api.py:70(DEBUG)] timer_01 - Drive action times_up 010101AF
2022-01-14 17:59:36,029 [sbrick_api.py:104(DEBUG)] channel_01 - Exec command 0001
2022-01-14 17:59:36,029 [sbrick_api.py:266(DEBUG)] channel_01 - RCC characteristic writes binary: b'\x00\x01'
2022-01-14 17:59:40,030 [sbrick_m2mipc.py:97(DEBUG)] MainThread - Accept drive() event: {'sbrick_id': '00:07:80:2E:2D:C9', 'channel': '01', 'direction': '00', 'power': 'AF', 'exec_time': 1}
2022-01-14 17:59:40,032 [sbrick_api.py:104(DEBUG)] channel_01 - Exec command 010100AF
2022-01-14 17:59:40,033 [sbrick_api.py:266(DEBUG)] channel_01 - RCC characteristic writes binary: b'\x01\x01\x00\xaf'
2022-01-14 17:59:40,036 [sbrick_api.py:282(ERROR)] channel_01 - SBrick (00:07:80:2E:2D:C9): Device disconnected
Exception in thread channel_01:
Traceback (most recent call last):
File "/home/pi/SBrick/lib/sbrick_api.py", line 274, in rcc_char_write_ex
self._rcc_char.write(binary)
File "/usr/local/lib/python3.9/dist-packages/bluepy/btle.py", line 200, in write
return self.peripheral.writeCharacteristic(self.valHandle, val, withResponse)
File "/usr/local/lib/python3.9/dist-packages/bluepy/btle.py", line 543, in writeCharacteristic
return self._getResp('wr')
File "/usr/local/lib/python3.9/dist-packages/bluepy/btle.py", line 407, in _getResp
resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
File "/usr/local/lib/python3.9/dist-packages/bluepy/btle.py", line 362, in _waitResp
raise BTLEDisconnectError("Device disconnected", resp)
bluepy.btle.BTLEDisconnectError: Device disconnected
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
self.run()
File "/home/pi/SBrick/lib/sbrick_api.py", line 58, in run
self.drive()
File "/home/pi/SBrick/lib/sbrick_api.py", line 62, in drive
self.drive_channel()
File "/home/pi/SBrick/lib/sbrick_api.py", line 94, in drive_channel
self.exec_command(drive_hex_string)
File "/home/pi/SBrick/lib/sbrick_api.py", line 106, in exec_command
self._sbrick.rcc_char_write_ex(binary, reconnect_do_again=False)
File "/home/pi/SBrick/lib/sbrick_api.py", line 283, in rcc_char_write_ex
if BTLEException.DISCONNECTED == e.code:
AttributeError: type object 'BTLEException' has no attribute 'DISCONNECTED'
2022-01-14 17:59:41,031 [sbrick_api.py:70(DEBUG)] timer_01 - Drive action times_up 010100AF
With Tiago's code and bluepy 1.3.0 also just White runs, then Yellow, but White does again not run a second time. The output of the server is different here, showing that it seems to re-connect, but then does nothing:
2022-01-14 18:05:48,330 [sbrick_m2mipc.py:97(DEBUG)] MainThread - Accept drive() event: {'sbrick_id': '00:07:80:2E:42:99', 'channel': '01', 'direction': '01', 'power': 'AF', 'exec_time': 1}
2022-01-14 18:05:48,331 [sbrick_api.py:105(DEBUG)] channel_01 - Exec command 010101AF
2022-01-14 18:05:48,332 [sbrick_api.py:266(DEBUG)] channel_01 - RCC characteristic writes binary: b'\x01\x01\x01\xaf'
2022-01-14 18:05:49,331 [sbrick_api.py:71(DEBUG)] timer_01 - Drive action times_up 010101AF
2022-01-14 18:05:49,334 [sbrick_api.py:105(DEBUG)] channel_01 - Exec command 0001
2022-01-14 18:05:49,334 [sbrick_api.py:266(DEBUG)] channel_01 - RCC characteristic writes binary: b'\x00\x01'
2022-01-14 18:05:58,340 [sbrick_m2mipc.py:97(DEBUG)] MainThread - Accept drive() event: {'sbrick_id': '00:07:80:2E:2D:C9', 'channel': '01', 'direction': '00', 'power': 'AF', 'exec_time': 1}
2022-01-14 18:05:58,342 [sbrick_api.py:105(DEBUG)] channel_01 - Exec command 010100AF
2022-01-14 18:05:58,342 [sbrick_api.py:266(DEBUG)] channel_01 - RCC characteristic writes binary: b'\x01\x01\x00\xaf'
2022-01-14 18:05:58,346 [sbrick_api.py:282(ERROR)] channel_01 - SBrick (00:07:80:2E:2D:C9): Device disconnected
2022-01-14 18:05:58,347 [sbrick_api.py:146(INFO)] channel_01 - Construct a new bluetooth object
2022-01-14 18:05:58,347 [sbrick_api.py:226(INFO)] channel_01 - Re-connect to SBrick (00:07:80:2E:2D:C9)
2022-01-14 18:05:58,348 [sbrick_api.py:221(INFO)] channel_01 - Disconnect from SBrick(00:07:80:2E:2D:C9) successfully
2022-01-14 18:05:58,348 [sbrick_api.py:154(INFO)] channel_01 - Try to connect to SBrick (00:07:80:2E:2D:C9)
2022-01-14 18:05:59,124 [sbrick_api.py:173(INFO)] channel_01 - Connect to SBrick (00:07:80:2E:2D:C9) successfully
2022-01-14 18:05:59,125 [sbrick_api.py:177(INFO)] channel_01 - Get rcc characteristic
2022-01-14 18:05:59,342 [sbrick_api.py:71(DEBUG)] timer_01 - Drive action times_up 010100AF
2022-01-14 18:06:01,199 [sbrick_api.py:105(DEBUG)] channel_01 - Exec command 0001
2022-01-14 18:06:01,200 [sbrick_api.py:266(DEBUG)] channel_01 - RCC characteristic writes binary: b'\x00\x01'
Could you help me with this please?
By the way, when each SBrick has to be reconnected after another SBrick has been addressed before, would controlling several SBricks "at once" even work for running multiple trains?
Thanks and regards,
Guido