title: Controlling a Lego WeDo 2.0 Motor using Python in Ubuntu
body: <p>I am trying to control the motor of a Lego Wedo 2.0 set via <code>Python</code> from an ubuntu distribution.</p>
  
  <p>The Lego WeDo 2.0 "hub" uses a bluetooth low energy technology, and to it, a motor is connected via cable, that I want to spin.</p>
  
  <p>I read a lot about the specific BLED112 USB dongle, which is <a href="https://education.lego.com/de-de/support/wedo-2/bluetooth-driver" rel="nofollow noreferrer">also recommended by Lego</a>, but I currently just have a generic Bluetooth 4.0 USB dongle. <code>hci0</code> is the dongle, and  <code>hci1</code> the internal one.</p>
  
  <p>The command <code>sudo hcitool -i hci0 lescan</code> shows, when the motor is in pairing mode, the output <code>A0:E6:F8:49:E2:93 LPF2 Smart Hub 2 I</code>. Then, </p>
  
  <p><code>gatttool -I
  [                 ][LE]&gt; connect A0:E6:F8:49:E2:93</code> throws "connection successful".</p>
  
  <p>My Python code is now:</p>
  
  <pre><code>from gattlib import GATTRequester
  from time import sleep
  
  address = 'A0:E6:F8:49:E2:93'
  
  req = GATTRequester(address,True,"hci0")
  req.write_by_handle(0x3d, "\x06\x04\x01\x09")
  sleep(2)
  req.write_by_handle(0x3d, "\x01\x01\x01\x64")
  sleep(2)
  req.write_by_handle(0x3d, "\x01\x01\x01\x9C")
  sleep(2)
  req.write_by_handle(0x3d, "\x01\x01\x01\x00")
  </code></pre>
  
  <p>which should change the color of the <a href="https://ofalcao.pt/blog/2016/wedo-2-0-colors-with-python" rel="nofollow noreferrer">engine's LED</a>, and <a href="https://www.ev3dev.org/docs/tutorials/controlling-wedo2-motor/" rel="nofollow noreferrer">spin it a bit</a>, then stop.</p>
  
  <p>However, the only output I receive is: </p>
  
  <p><code>RuntimeError: connect: Device or resource busy (16)</code></p>
  
  <p>when using <code>hci1</code> instead, the dongle flashes quickly for a few seconds, then the error appears:</p>
  
  <p><code>Segmentation fault (core dumped)</code></p>
  
  <p>Finally, when I disconnect the dongle and type <code>hci0</code> in the above code, the error is: <code>RuntimeError: Invalid device!</code>, as I would expect, given that the dongle has been removed.</p>
  
  <p>During all of this, the WeDo engine just shows a constant blue light, indicating that it is connected and waiting for orders.</p>
  
  <p>In the code <code>req.write_by_handle(0x3d, "\x01\x01\x01\x64")</code>, the initial <code>\x01</code> should dictate which port of the WeDo-Hub the motor is connected to, and it is indeed connected to the first port. (Anyway, the first command would address the hub itself, to change its colors, so even if the motor was connected to the wrong port, the colors should change nonetheless.)</p>
  
  <p>What am I missing?</p>
  
  <p>I do have access to a Windows PC with Lego's children-friendly software on it, which successfully pairs with and controls the motor.</p>
  
  <p>Are there any other models for electric motors that can be perhaps more easily controlled via Python? Both USB or Bluetooth would be fine. </p>
  
  <hr>
  
  <p>Running</p>
  
  <pre><code>from gattlib import GATTRequester, GATTResponse, DiscoveryService
  from time import sleep
  
  service = DiscoveryService("hci0")
  devices = service.discover(2)
  
  for address, name in devices.items():
      print("name: {}, address: {}".format(name, address))
  </code></pre>
  
  <p>gives as output:</p>
  
  <pre><code>name: LPF2 Smart Hub 2 I, address: A0:E6:F8:49:E2:93
  </code></pre>
  
  <p>Next,</p>
  
  <pre><code>from gattlib import GATTRequester
  
  req = GATTRequester("A0:E6:F8:49:E2:93")
  name = req.read_by_uuid("00002a00-0000-1000-8000-00805f9b34fb")[0]
  steps = req.read_by_handle(0x15)[0]
  </code></pre>
  
  <p>outputs: </p>
  
  <pre><code>Segmentation fault (core dumped)
  </code></pre>