Last active
February 17, 2016 10:01
-
-
Save WayneKeenan/b8976f42e87c3b531eff to your computer and use it in GitHub Desktop.
MeArm Bluetooth LE Python example (low level)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Description: Low level example of sending BLE messages to a MeArm Brain Board fitted with the HM-11 Bluetooth LE module | |
# | |
# 17/02/2016 Wayne Keenan ([email protected]) | |
# | |
# Deps: | |
# 1) MeArm should have the MeArm_BLE sketch programmed, found here: https://codebender.cc/sketch:244546 | |
# 2) Adafruit Python BLE libray (see below) | |
# | |
# Python BLE Library Setup (for Linux and Mac): | |
# | |
# git clone https://github.com/adafruit/Adafruit_Python_BluefruitLE.git | |
# cd Adafruit_Python_BluefruitLE/ | |
# python setup.py install --user | |
# | |
# On Mac this will work out of the box for the system default Python that is installed by default in MacOS X. | |
# On Linux please see the "Linux & Raspberry Pi Requirements" section in the Adafruit GitHub link above | |
#----------------------------------------------------------------------------------------------------------------------- | |
import array | |
import uuid | |
import time | |
import Adafruit_BluefruitLE | |
#import logging | |
#logging.basicConfig(level=logging.DEBUG) | |
# Define service and characteristic UUIDs used by the HM-11 module. | |
HM11_SERVICE_UUID = uuid.UUID('0000ffe0-0000-1000-8000-00805f9b34fb') | |
HM11_CHAR_UUID = uuid.UUID('0000ffe1-0000-1000-8000-00805f9b34fb') | |
ble = Adafruit_BluefruitLE.get_provider() | |
def main(): | |
ble.clear_cached_data() | |
adapter = ble.get_default_adapter() | |
adapter.power_on() | |
print('Using adapter: {0}'.format(adapter.name)) | |
ble.disconnect_devices([HM11_SERVICE_UUID]) | |
print('Searching for MeArm (HM-11) device...') | |
try: | |
adapter.start_scan() | |
device = ble.find_device(service_uuids=[HM11_SERVICE_UUID]) | |
if device is None: | |
raise RuntimeError('Failed to find HM11 device!') | |
finally: | |
adapter.stop_scan() | |
print('Connecting to device...') | |
device.connect() # Will time out after 60 seconds, specify timeout_sec parameter | |
try: | |
print('Discovering services...') | |
device.discover([HM11_SERVICE_UUID], [HM11_CHAR_UUID]) | |
hm11 = device.find_service(HM11_SERVICE_UUID) | |
tx = hm11.find_characteristic(HM11_CHAR_UUID) | |
print('Sending messages to device...') | |
# Everything above this line is boilerplate. | |
#----------------------------------------------------------------------------------------------------------------------- | |
# The meat... | |
# Valid array values are specified by the MeARM_BLE Arduino sketch source code (see the deps at the top) | |
# | |
# In Summary: [Magic, FunctionCode, BaseServoAngle, LeftServoAngle, RightServoAngle, ClawServoAngle] | |
# | |
# Magic is always 0x00 | |
# FunctionCode 0x02 for 'SetServoAngles' mode | |
# *ServoAngle is in the range 0 - 179 | |
print('Setting all Servo Angles to 90') | |
tx.write_value(array.array('B', [0x00, 0x02, 90, 90, 90, 90])) | |
time.sleep(2) | |
print('Setting Base Servo Angle to 50') | |
tx.write_value(array.array('B', [0x00, 0x02, 50, 90, 90, 90])) | |
time.sleep(2) | |
print('Setting all Servo Angles to 90') | |
tx.write_value(array.array('B', [0x00, 0x02, 90, 90, 90, 90])) | |
time.sleep(2) | |
print('Here endith the lesson') | |
#----------------------------------------------------------------------------------------------------------------------- | |
# Everything below this line is boilerplate. | |
finally: | |
device.disconnect() | |
ble.initialize() | |
ble.run_mainloop_with(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment