Hello and welcome to my fifth blog post. Today's update is a very short one, only here to present some LED strip effects and explain a change of approach in the BLE communications. In the last few days I found myself struggling with OpenCV and decided not to post my results yet (as basically very little is ready) and instead went on to improve the suboptimal Bluetooth connection script that I presented some time ago.
As you may or may not remember, previously I've been using GATT SDK for Python and the default Redbear Blend libraries. Any attempts to upgrade the old code on the Beagle Bone side and extend its functionality only exposed its weaknesses - long connection times, difficulties in counting the Blends properly and overall poor desing. It wasn't that bad on the Arduino side, but working with bytes coming from the BLE RX characteristic was still quite a nuisance. Therefore I decided to look for a new, better way, and it came in the form of a mix of two solutions - the Adafruit Bluefruit LE library for the BBW and Sandeep Mistry's BLEPeripheral Arduino library for interfacing with the nRF8001 chipset on the Blend. BLEPeripheral was written with Bluefruit compatibility in mind, so integration was quite easy. The only problem I had was that Bluez, the Linux BT stack, only worked with Bluefruit (or maybe it was the Bluefruit+nRF8001 combo) after I downgraded it to a recompiled 5.37 version - wasn't quick, but worked exactly as shown in the Bluefruit docs. The most important difference is that both libraries implement decent, full-featured methods of Serial-like communication, with BLEPeripheral's BLESerial object implementing most of the built-in Arduino Serial methods. They will be a good solution for my project's needs.
With the basic examples interfacing seamlessly on both devices (uart_service.py on the BBW and serial.ino from BLEPeripheral on the Blend), I went on to modify the Python script to transmit some basic configuration info for the LED strips, and replaced my previous BLE communication methods in the Arduino sketch with the BLEPeripheral's BLESerial. As outlined in my previous post on BBW-Blend communication, I'm using an extremely simple protocol utilizing a single letter followed by integer parameters. Here is the new Python code that creates a connection and sends five messages to make the LED strip change the pattern displayed (the letter 'm' followed by mode identifier):
import Adafruit_BluefruitLE from Adafruit_BluefruitLE.services import UART import time # Get the BLE provider for the current platform. ble = Adafruit_BluefruitLE.get_provider() #Main function to run all BLE-related logic def main(): #Get a clean start ble.clear_cached_data() adapter = ble.get_default_adapter() adapter.power_on() print('Using adapter: {0}'.format(adapter.name)) print('Disconnecting any connected UART devices...') UART.disconnect_devices() # Scan and connect to the first UART device you can find: print('Searching for UART device...') try: adapter.start_scan() device = UART.find_device() if device is None: raise RuntimeError('Failed to find UART device!') finally: adapter.stop_scan() print('Connecting to device...') device.connect() try: # Discover services print('Discovering services...') UART.discover(device) uart = UART(device) # Send all commands sequentially uart.write('m9') print("Sent config mode command to the device.") time.sleep(5) uart.write('m5') print("Sent new mode command to the device.") time.sleep(5) uart.write('m3') print("Sent new mode command to the device.") time.sleep(5) uart.write('m1') print("Sent new mode command to the device.") time.sleep(5) uart.write('m2') print("Sent new mode command to the device.") finally: device.disconnect() # Initialize BLE ble.initialize() # Run BLE loop ble.run_mainloop_with(main)
The example is based on the uart_service.py example from the Bluefruit library. I'll post a video of the LED strips patterns over the weekend when the code will - hopefully - be better developed. So long!
Top Comments