I spent this week playing around with connecting an arduino to an iPhone via a bluetooth shield. Paired with playing with an FSR, I’m working my way slowly towards a project idea to get people riding their bikes more frequently.
First, what I ended up with this week:
I was pretty shocked how simple the Arduino side of the bluetooth connection was. Just slapping this bad boy onto the Arduino immediately imbued it with magical communication powers.
On the Arduino side, all that’s required is to initialize a SoftwareSerial object called BLE_Shield on pins 4 and 5, which is where the shield communicates with the Arduino.
in setup(), you simply begin serial communication with a specified baud rate :
and in loop() you can read the serial data from the shield directly with .read() :
The hard part, to be honest, was dealing with the iPhone side of things. I followed a tutorial which gave me a starter xCode project complete with code to connect to the BLE device. After sifting through the example code, I started to get a feel for how it works.
Bluetooth devices have a unique identifier, referred to as a UUID. On the iPhone side, these devices are first scanned for using their UUID. Devices broadcast a “service”, and contained in that service are “characteristics”. The “service” refers to the channel of communication, and some bluetooth devices have multiple services. This device in particular only has one. But within that service, there can be several characteristics, which refer to discrete pieces of information that the device can send or receive. This device can receive information on one characteristic, and send information attached to it’s second characteristic.
From the Swift (iOS programming language) side, the basic strcutre looks like this:
Scan for devices -> Connect to devices service -> Discover characteristics -> Read/Write info to/from that characteristic
It took a bunch of playing, but eventually I was able to simply control the position of a servo motor! It really is the little things.
A lovely second year, Theresa, hosts a soft club where this last week we made homemade FSR’s. I was particular excited about this because it was excatly what I needed to get rolling on my bike project – which will involve using an FSR and an arduino to log the amount of hours (or minutes) that someone spends riding their bike.
The first iteration is to be a “Seat Monitor”. If the user spends too much time sitting down, they will be alerted that it’s probably time to stand up and take a quick break. The upside-down video above shows the logic behind this. If the FSR is compressed, it starts counting (the green light goes on). The counter only increases in value when the FSR is depressed with a certain pressure. i.e. when there’s no pressure, the yellow light is on. Everything idles. When the counter hits a certain value, the red LED goes on. This kills the functionality of the FSR momentarily, while a second counter times out how long the red LED stays on. After a set “reset” time, the device is ready to use again.