element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Bluetooth Unleashed Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Bluetooth Unleashed Design Challenge
  • More
  • Cancel
Bluetooth Unleashed Design Challenge
Blog Universal LED Animator #5 - Better BLE connectivity
  • Blog
  • Forum
  • Documents
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: mgal
  • Date Created: 5 Jul 2018 7:24 PM Date Created
  • Views 113 views
  • Likes 7 likes
  • Comments 1 comment
Related
Recommended

Universal LED Animator #5 - Better BLE connectivity

mgal
mgal
5 Jul 2018

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!

Anonymous

Top Comments

  • genebren
    genebren over 4 years ago +1

    Any progress is good progress.  Keep up the good work!
    Gene

  • genebren
    genebren over 4 years ago

    Any progress is good progress.  Keep up the good work!
    Gene

    • Cancel
    • Vote Up +1 Vote Down
    • Reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube