The reason for this project

This simple project aims to show the essential parts of an IoT system that the “things” – here is iPhone and LED – connect together as a “net”. Although it’s simple in the concept, but when I jump into the making, it’s quite tough and much knowledge to learn. This project will walk you through (1)make an iPhone application using CoreBluetooth Framework, (2) understand the Bluetooth Low Energy stack and especially the HCI command, and finally (3) understand how to use PWM mode on Raspberry Pi to dim an LED.

Although it is not mandatory, you should know the basic about Bluetooth from this article

LED Dimming Using iPhone and Raspberry Pi
LED Dimming Using iPhone and Raspberry Pi

What the project have done

The achievement of this project is:

  • Write an iOS application using CoreBLuetooth Framework to advertising the UUID service
  • On Raspberry Pi, write a bash shell script to capture the Bluetooth advertising packet
  • On Raspberry Pi, use GPIO.12 as PWM mode to control the light of LED

Detail specification

To keep it simple, I use the first byte in the UUID of the advertising service to send the light levels from 0x00 to 0xFF which means that led is less light to most light accordingly. For example, if the UUID is “E20A39F4-73F5-4BC4-A12F-17D1AD07A961”, it means the led level is “E2” in hex and 266 in decimal. And the rest of UUID is unchanged. Using UUID as transfer value is not practical, but it is good enough to show how to use Bluetooth signal to control an object – an LED in this case. From iOS, I write an application to broadcast the UUID.

On Raspberry Pi, I use the hcitool and hcidump commands to scan and parse the HCI packets to get the light level value – the first byte of service UUID. Because the UUID information is the higher-layer data, hcidump can not understand the UUID itself. I mean we can not get UUID information with hcidump (without –raw option). We have to understand the format of HCI commands and events to extract the UUID from hcidump –raw correctly. This task consumes much time.

Finally, when I have the light level on Raspberry Pi, I need to set a value to a PWM pinout to change the light level of LED.

Hardware inventory

The hardware list used in this project:

  • 1 x Raspberry Pi 2 Model B
  • 1 x Bluetooth USB dongle
  • 1 x LED + 1KOhm resister
  • 1 x Wifi USB dongle(optional)

Source Code Explanation

iOS application: “LedDimmer”

This explains how iPhone/iPad control LED level via Bluetooth protocol. Here are the two main methods to work with Bluetooth advertising

As the specification, I use the first byte of service UUID as LED level. Anytime the slider change value (levelSliderDidChange method called), I will update LED level value into service UUID and restart Bluetooth advertising. Then Pi will catch the new LED level and change light accordingly.

To download the full source code, go to “Download” section of this article.

Bash Shell script on Raspbian

The basic idea here is we need to parse the hci command/event to get the service UUID broadcasted by above iOS app, then extract the first byte of the UUID for LED level. I use “hcidump –raw” to dump the received HCI commands/events to hex_dump.txt file every 0.1 seconds. To understand all HCI commands/events, please refer the Understand Bluetooth HCI Commands and Events article. To keep it simple, we just care about the UUID bytes inside the received HCI events. This command is used to extract exactly LED level from service UUID.

The packet variable here is raw dump data from hcidump command in hex format. It looks like

Then we parse the first byte of the UUID for LED level as following:
Then, this value will be set to PWM 18 pin, which is connected to LED as demo video you have seen, with this line of code

To understand how to use the GPIO pins on RaspberryPi, you can refer the Understand “gpio readall” Bash shell command output on Raspberry Pi article.

Cheers!

Use iPhone to control Raspberry Pi as LED dimmer
Tagged on:         
Share This

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

Subscribe

To download the latest tutorials

You have successfully download

Enter Email To Download

Join our mailing list to receive the latest Application for free from IoTBreaks.

You have Successfully Subscribed!

flash Raspbian to SDCard

Enter Email To Download

Join our mailing list to receive the latest Application for free from IoTBreaks.

You have Successfully Subscribed!

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!