Using the PSoC 6 Pioneer Board with the Pioneer IoT Add-on Shield

Pages
Contributors: SFUptownMaker
Favorited Favorite 0

Example : BLE to Raspberry Pi Using the PSoC 6 Pioneer Kit

This example shows how to send a simple signal via BLE to the Raspberry Pi. It uses only the Cortex-M0+ core, running a Cypress provided code example.

Programming the PSoC 6 Pioneer Kit

If you followed our first example, you should be familiar with how to program the PSoC 6 Pioneer Kit board with a new firmware. Repeat this process with the workspace named "BLE_To_RPi". This is just a renamed copy of Cypress code example CE218134.

Programming the Raspberry Pi

Programming the Raspberry Pi is fairly easy. You don't need to install any additional software, just download the repository from GitHub with the following command:

git clone https://github.com/sparkfun/Pioneer_Kit_Shield

Then, run the python program which will listen for BLE communications by entering this command:

sudo python Pioneer_Kit_Shield/Software/BLE/scanble.py

That should be all it takes. You'll now be able to toggle the LED on the Raspberry Pi using the two CapSense buttons on the PSoC6 Pioneer Kit board.

What's Going On Here Pt. 1: PSoC 6

I'm not going to delve too deeply into the PSoC 6 code, as it's very well documented in the example. I'll call attention to a few details, however.

First, this code runs entirely on the Cortex-M0+ processor core. The other core is never activated.

Second, CapSense events are sent as notifications, which means they are sent from the server (the PSoC 6 board) to the client (the Raspberry Pi). The Raspberry Pi doesn't need to request data from the server as it will be automatically updated on change.

Third, the Raspberry Pi app is hardcoded to attach to the PSoC 6 app. If you're in an environment where there are multiple PSoC 6 boards, you'll have to change the public address of the BLE app as well as the address being looked for in the Python app.

What's Going On Here Pt. 2: Python and Raspberry Pi

The Python code uses the Bluepy module, which should be installed on your Raspberry Pi by default.

We're going to take this one a little out of order, so the code makes more sense. We start by creating a Peripheral object, passing it a string of hexadecimal values corresponding to the public address of the BLE device we're looking for.

language:python
device = Peripheral("00:A0:50:21:81:34")

We then create and assign the ScanDelegate class object to that peripheral device. This lets the module know that this is the object to which incoming BLE messages should be assigned. This will block for approximately 30 seconds waiting for a connection to the Peripheral object we assigned it to.

language:python
device.withDelegate(ScanDelegate())

We now need to write two characteristics: one to start messages flowing from the CapSense buttons and one to turn the onboard LED white as a sign that the connection was successful.

language:python
device.writeCharacteristic(29, b"\x01\x00", withResponse=True)
device.writeCharacteristic(23, b"\xff\xff\xff\xff", withResponse=True)

Now let's go back up in the code and look at the ScanDelegate class. I've removed the trivial code which senses the button state and makes sure that only one button press at a time is recorded no matter how long the buttons are held down.

language:python
class ScanDelegate(DefaultDelegate):
    def __init__(self):
    DefaultDelegate.__init__(self)

    def handleNotification(self, cHandle, data):
        ## Do something with the data object

data will be an array containing all of the bytes sent by the Bluetooth server. In this case, it's two bytes, the second of which tells us which button is pressed.

Finally, at the bottom of the code, we have this section, which waits for notifications in one second blocks and is interrupted by a call to handleNotification() when one is received:

language:Python
while True:
    if device.waitForNotifications(1.0):
        continue

And that's how easy it is to receive data from a BLE device via Python on a Raspberry Pi. All told, less than 50 lines of code.