# Bluetooth on the CircuitPlayground Bluefruit (CPB Only)

**Parts List**

* Smart Phone
* Adafruit BLE Connect App ([Play Store](https://play.google.com/store/apps/details?id=com.adafruit.bluefruit.le.connect)/[App Store](https://apps.apple.com/us/app/bluefruit-connect/id830125974))
* CircuitPlayground Bluefruit
* USB Cable

**Learning Objectives**

* Understand the bluetooth module on the CircuitPlayground Bluefruit
* Learn how to send data via the Bluefruit to your smart phone
* Understand how to plot data sent via UART

**Extra Help**

You might find plotting data via bluetooth to be rather difficult and it was pretty difficult for me until I learned that you can export data as a txt file rather than a csv file. Before I learned how to do that I put together a [4 part series](https://www.youtube.com/playlist?list=PL_D7_GvGz-v0-U3JACRMgldvqQqn2mje9) describing everything in this module. Worst case you can just watch the [third video in the series](https://www.youtube.com/watch?v=9EHNFdVX9O8\&list=PL_D7_GvGz-v0-U3JACRMgldvqQqn2mje9\&index=3). The video is 30 minutes but the first 8 minutes goes through setting up the bluetooth module and the rest of the video is just on plotting the exported csv data which took me some time. Note that exporting data as a txt file is the preferred&#x20;

**Project**

In this module we're going to go over how to get the bluetooth module on the Circuitplayground Bluefruit (CPB). There is a lot you can do with bluetooth but the bottom line is that you can send data from your phone to your CPB and you can also send data to your smart phone. All code required for this module is on my [Github](https://github.com/cmontalvo251/Microcontrollers/tree/master/Circuit_Playground/CircuitPython/CPB_DataLogger).

First we're going to run the [bluetooth\_uart\_send.py](https://github.com/cmontalvo251/Microcontrollers/blob/master/Circuit_Playground/CircuitPython/CPB_DataLogger/bluefruit_uart_send.py) script which sends data to your smart phone via something called UART which is a type of serial communication. It's beyond the scope of this lesson but serial is digital as opposed to analog like we will do in future videos.&#x20;

![](https://617870685-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZEVs4nuNxgLEzikFcx%2Fuploads%2F2EdoTEPRQvX4BRfrGeXs%2FScreenshot%20from%202021-12-20%2017-48-26.png?alt=media\&token=6e10e077-2fb1-4400-8a1a-e220cf639797)

Lines 4-14 import a ton of modules. You'll recognize many of them like analogio, thermistor, and lis3dh but the new ones are the ones that say ble. These are the bluetooth modules required for the CPB. Lines 22-24 setup the thermistor and lines 26-31 setup the accelerometer as we've done in another lesson. Lines 16-20 kick of the BLERadio object and the UARTService() to send data. That's the only required setup for this lesson. **Note:** The image below is from an old version o the code. On line 35 a new line of code has been added **print('Look for',ble.name).** ble.name is the name of your CPB which is unique to your device.

![](https://617870685-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZEVs4nuNxgLEzikFcx%2Fuploads%2FEi6AnlFJ9mYxy8NxGhhg%2FScreenshot%20from%202021-12-20%2017-52-28.png?alt=media\&token=cc9b4ae5-d819-49a0-bde1-d680689b5d69)

The code above is the infinite while loop which actually contains 2 while loops. Lines 33-42 check to see if bluetooth is connected. If bluetooth is not connected it will start advertising to any devices that are listening. It will then enter a while loop from 38-39 until bluetooth is connected. Once bluetooth is connected it will enter into the second while loop from line 45-60. In those lines 47-54 is responsible for taking all the necessary measurements and printing them to the serial monitor in Mu. Line 57 send the data over bluetooth using the UART server. You'll notice in this case I'm sending x,y,z by using the format variable an the 3 empty {} brackets. If you want to send more data you need to add more empty brackets and more variables to the format function. When you first save this script your CPB will not be connected and enter into an infinite while loop where it waits for your smart phone to connect. If you open your smart phone and open the Bluefruit Connect App the following screen will pop up.

![](https://617870685-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZEVs4nuNxgLEzikFcx%2Fuploads%2Fbt0gkYVmh1v8qB3nkrrz%2FScreenshot_20211220-175913.png?alt=media\&token=992a5124-c130-4069-8229-3321a21fd7f7)

In this case there are numerous different bluetooth modules can be seen but the one you need to click is the one that says CIRCUITPYf8e8. You will have a different code after CIRCUITPY and you can figure out what your 4 digit code is by making sure you have the **print('Look for',ble.name)** in your code. Once you do that the CPB will begin sending x,y,z accelerometer data to the smart phone.&#x20;

![](https://617870685-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZEVs4nuNxgLEzikFcx%2Fuploads%2FAX0Gxz4riH0PCVUyhYAx%2FScreenshot_20211220-181145.png?alt=media\&token=745de721-48b1-4ec5-9ee5-f10903aadf8a)

There are numerous items you can click. The Controller is very fun for creating a remote control robot but we're only going to go over the UART and Plotter tabs. If you click the plotter tab you will be greeted with a live screen of the data being sent.

![](https://617870685-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZEVs4nuNxgLEzikFcx%2Fuploads%2F10pDZZicHBIljZoCzLNJ%2FScreenshot_20211220-181118.png?alt=media\&token=9a946b2d-4adf-4575-8972-eacdc6684d22)

In the photo above you can see three data streams that is coming directly from the CPB. This is great for live demonstrations and for debugging if you need to see data from an experiment and you don't have access to a laptop with Mu. If you hit the back arrow and then click UART you will see the raw data come in as text.

![](https://617870685-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZEVs4nuNxgLEzikFcx%2Fuploads%2FFZLTBQY868oLXExZxILs%2FScreenshot_20211220-181131.png?alt=media\&token=d7c78b28-f9bd-4c5b-b38a-91d2f194ac2c)

Again here you can see the 3 data streams separated by commas. The very neat thing with the UART tab is that you can click the three vertical dots in the upper right hand corner and click export to TXT. The easiest thing for me was to export the data to google drive and then download the data to my computer. Before I downloaded the data though I went back and changed the following line of code

*uart\_server.write('{},{},{}\n'.format(x,y,z))*

to&#x20;

*uart\_server.write('{},{},{},{},{}\n'.format(t,x,y,z,T))*

In this case I added more empty brackets and also added the time and the temperature. When I opened the UART tab I was greeted with 6 data streams separated by commas. Once I downloaded the TXT file to my computer and opened it the data file looked like this.

![](https://617870685-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZEVs4nuNxgLEzikFcx%2Fuploads%2FRjVqWmTHipeFDUP62HjW%2FScreenshot%20from%202022-03-01%2009-00-16.png?alt=media\&token=dc9a4938-4d16-4799-9e48-c4107e458021)

If you export the file as a CSV the data file will look completely different and it's much more complicated to plot. If you export the data as a TXT file you just need to use the *np.loadtxt* command to read in the data. Note you might have commas in your data file. If there are commas just use the CTRL+H command and replace all commas with spaces or use the *np.loadtxt('file.txt',delimiter=',')* command. Here's a simple piece of code to plot temperature and the accel data. I have not uploaded this code to Github simply because the code is very simple to create on your own.

![](https://617870685-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZEVs4nuNxgLEzikFcx%2Fuploads%2FL1xeRDIPbLtYv1DxtGaE%2FScreenshot%20from%202022-03-01%2009-08-30.png?alt=media\&token=67e219c0-c83a-4c81-88b7-10590b00afa6)

The code reads in the data with the *np.loadtxt* command and then extracts each column. Once that's done the code plots all the data. In my case this was the result.

![](https://617870685-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MZEVs4nuNxgLEzikFcx%2Fuploads%2FF0XCDpYNUDtlTjtklXqV%2FScreenshot%20from%202021-12-20%2018-33-09.png?alt=media\&token=511ea0cb-720e-40a4-b6c0-f116d6f6733b)

**Turning in this Assignment**

Once you've done that upload a PDF with all of the photos and text below included. My recommendation is for you to create a Word document and insert all the photos and text into the document. Then export the Word document to a PDF. For videos I suggest uploading the videos to Google Drive, turn on link sharing and include a link in your PDF.

The main goal for this assignment is to do exactly what was presented to you in this lesson.&#x20;

1. First get the bluetooth code on your CPB and connect your smart phone to it and video tape yourself connecting the CPB to your smart phone (20%)
2. Take a screenshot of the Plotter on your phone (15%)
3. Take a screenshot of the UART raw data on your phone (15%)
4. Export time, accelerometer and temperature data to a TXT file and plot it on your computer. Include your raw data in an appendix (20%)
5. Include two plots: one of temperature and the other of acceleration like the lesson did above (30%)
