Chris Lloyd

MAS.863/4.140/6.943
How To Make (almost) Anything
2019

Week 11: networking and communications

This week, I experimented networking with a device I already use, a heart rate monitor called WHOOP. This product uses Bluetooth Low Energy (BLE) broadcast to connect the heart rate monitor with treadmills and stationary bikes, but I'm interested in connecting it with my device as an input. I chose the ESP.32-WROOM because it has BLE capabilities as opposed to Bluetooth SPP, which is used by the other devices available in fablab.

First, I recreated Neil's hello.world board on Eagle. It's important to leave space for the antenna off the board to increase the efficacy of the BLE. TIP: If you are making this board, add a 2X3 or FTDI connector to this board that allows you to connect to more pins on the board that you don't need at this moment. This will allow more flexibility to experiment with your board. I didn't think about this when I first made my board and will need to remake it to connect to my output device later.

Here are the actual PNG files if you want to mill them:

And here is the final product! The button and the switch allow you to manually reset the board.

I chose to use Arduino to program my ESP32 because there are some great example code to become a BLE client that weren’t in the examples provided in the hello.world code examples. The pitfall of Arduino is that the code is heavy, meaning that everything takes up more space because it refers back to libraries that must be uploaded to the ESP32.

To get the library, follow these steps.

Open Arduino > Preferences >

Where it says "Additional Board Manager URLs" type: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

Open Tools > Board > Manager Boards

Search: ESP32

Install Board

Now you will be able to use the ESP32 board as well as access example code from it’s library!

Next, I attempted to install Neil's hello.world arduino code, but was having serious issues. On closer inspection of my soldering, I realized that pins 1 (GND) and 2 (3v3) were shorted.

After figuring that out, I was able to upload the hello.world code and pair with a device! However, I could never find my WHOOP heart monitor. This is when I learned that there is a difference between serial and BLE communication. BLE is newer and reduces power consumption and cost while maintaining a similar communication range, but does require different coding to pair.

This is when I realized I could use the library of examples that was downloaded with the ESP32 board manager. There is example code for a ESP32 as BLE client.

At first, the data was all weird because it was giving me the length of the data of the heart rate data, but not the data itself, so I needed to use a dereferencing operator to points to the data and then returns the value stored in memory.

Here are the updates to the code that I made.

Here is reference to some of the things that were new to me in this code:

char A data type used to store a character value. Character literals are written in single quotes, like this: 'A' (for multiple characters - strings - use double quotes: "ABC").

Characters are stored as numbers however. You can see the specific encoding in the ASCII chart. This means that it is possible to do arithmetic on characters, in which the ASCII value of the character is used (e.g. 'A' + 1 has the value 66, since the ASCII value of the capital letter A is 65). See Serial.println reference for more on how characters are translated to numbers.

The size of the char datatype is at least 8 bits. It’s recommended to only use char for storing characters. For an unsigned, one-byte (8 bit) data type, use the byte data type.

* Dereferencing is one of the features specifically for use with pointers. The asterisk operator * is used for this purpose. If p is a pointer, then *p represents the value contained in the address pointed by p.
UUID Universally Unique Identifier (UUID) short forms The Bluetooth® Service Discovery Protocol (SDP) specification defines a way to represent a range of UUIDs (which are nominally 128 bits) in a shorter form. A reserved range of 2^32 values can be represented using 32 bits (denoted uuid32). Of these, a sub-range of 2^16 values can be represented using only 16 bits (denoted uuid16). All values in the 2^32 range that are not assigned in this document are reserved pending future revisions of this document. In other words, no value in this range may be used except as specified in this or future revisions of this document. UUID values outside of this range can be allocated as described in [ISO-11578] for any purpose the allocator desires.

Finally, I used my phone to find my heart rate monitors unique identifier and was able to print my heartrate! See here that it tracks my heart rate pretty well (with a very slight delay).