Build an iOS app that connects to IoT device using Bluetooth

Internet of Things (IoT) is one of the hottest technologies across the globe nowadays. There are a number of IoT products and devices that are available in the market.

At the end of this blog, you will have a high-level idea of how IoT systems work. To understand, we will create an iOS demo app which will send/receive data from an IoT compatible device with the help of Arduino.

We will cover the following points:

  • Introduction to IoT
  • Arduino Overview
  • iOS demo App to understand the end-to-end flow

Introduction to IoT

IoT is a system of devices connected to the internet with the ability to collect and exchange data. The device or “Thing” in IoT could be any device embedded with electronics, software, and sensors like lights in household, smart air conditioner or person with a heart monitor.
Following is a list of opportunities in IoT and an example of why it became one of the hottest technologies:
  • The connected world of devices, people and data helps to create numerous business opportunities for many sectors. For example, if I own a car parts manufacturing business, I might want to know which parts are the most popular. With IoT, I can use a sensor in a showroom to detect which areas are more popular or in which area a customer spends more time. I can then use this data to identify parts and increase the production of these parts.
  • Real-time updates offer resources to improve accuracy in decision making.
  • Costs of IoT components have significantly gone down which effectively means that the cost of IoT-linked devices is getting more affordable day by day.

Along with the above, there are many other opportunities which have accelerated the market for Internet of Things. It is predicted that by 2020, 25 billion IoT devices will be available in the market. The network in IoT will be decided on factors such as range, data, security, and power. These factors will decide the choice of network: whether it is the internet, Bluetooth, WiFi or something else.

IoT is also used for devices that with no internet connection. It is used for devices that can communicate with a network which such as internet, Bluetooth, NFC etc. For short-range communications, Bluetooth is the preferred technology. It is expected to be the key for wearable products. For example, Smartwatch, Fitness band.

Arduino Overview

We know how to send or receive data over the internet from an iOS app. But many of us don’t know how this data operates the IoT devices. There are open source hardware and software available in the market which is used to control the IoT devices. One of these is Arduino.

 ARDUINO_LEONARDO

Arduino is an open-source hardware and software. Arduino boards are able to read inputs from different sensors and turn it into an output like turning on an LED, activating a motor or publishing it over the internet. These boards can take the following input and output:

Input:
  • Temperature, Humidity, Pressure etc
  • Light, Infrared signals
  • Sounds
  • Motion captures
  • Heart rate, muscle movement
  • Electrical current
  • Touch, Fingerprints
Output:
  • LEDs
  • LCDs
  • Speakers
  • Motors
  • The internet
There are different types of Arduino boards available depending on features like ethernet port, wireless or USB device support. Common specification of these hardware boards are:
  • ATmega 328 8bit chip
  • 5-20V power supply
  • 32 KB flash memory
  • 20 I/O pins

You can tell Arduino boards what to do by sending a set of instructions to the micro-controller on the board. For this, we have to use Arduino Software (IDE) and Arduino programming language.

Arduino IDE

To write code and upload it to the board, Arduino IDE is used. It is available for Mac, Windows, and Linux platform. You can download it from https://www.arduino.cc/en/Main/Software.

Arduino-IDE

Arduino programming language

The coding language that Arduino uses is very much like C++, which is a common language in the world of computing. The two important functions in Arduino language are:
  • setup( ) – Every program should have this function. This runs once at the start of the program like main () function. You can do the initialization in this function.
  • loop( ) – Every program should have this function. This gets called repeatedly. You can use it to actively control the Arduino board.
Other useful functions:
  • pinMode() – Set a pin as input or output
  • digitalWrite() – Set a digital pin high/low
  • digitalRead() – Read a digital pin’s state
  • analogRead() – Read an analog pin
  • analogWrite() – Write an analog value
  • delay() – Wait an amount of time
Example Code:
int ledPin = 3;
// setup initializes serial and the LED pin
void setup(){
     Serial.begin(9600);
     pinMode(ledPin, INPUT);
}

// loop checks the LED pin state each time and broadcast it whether it is high or
// low
void loop(){
    if (digitalRead(ledPin) == HIGH)
         Serial.write('H');
   else
        Serial.write('L');
        delay(1000);
} 
You will find more details about the language at https://www.arduino.cc/reference/en/.
Once you write the code, you can upload it on Arduino board by using Arduino IDE. You can download demo Arduino program to turn the LED on-off. This program sends/receives data over the Bluetooth and turns on-board LED on-off depending on the data received. Also, this will broadcast state of the LED pin. You can use Arduino Leonardo board for this demo.

iOS Demo App 

I have created a sample iOS demo app which will send/receive data over Bluetooth. For that, I have used Core-Bluetooth framework. You can download it here.

Lets see the Bluetooth connectivity coding part now.

CBCentralManager

This class of Core Bluetooth framework is used for scanning and connecting Bluetooth devices. So first create an instance of this class.20

 

 

Scan for peripherals

Once you create the instance of central manager class, call its scanForPerpherals method for scanning bluetooth devices.
21

Discovering Peripherals

When the central manager discovers a peripheral while scanning, it invokes didDiscover delegate method. You can use the peripheral information received in this method parameters.

 22

Connecting to a Peripheral

To send a connect request to the discovered peripheral, we have to call connect method of central manager class along with the peripheral information.

 23

We will get a notification after the creation of a successful connection with the peripheral. didConnect delegate method gets invoked to notify this.

24

Discovering Services

Now, we can discover peripheral’s services by using the central manager. To notify discovered services, it will call the didDiscoverServices delegate method.

25

Discovering Characteristics

Similar to services, the central manager has didDiscoverCharactaristics delegate method to notify about characteristics of the specified service.

26

In this method, we can register for characteristics in which we are interested in.

Read/Write Data

After registration, whenever there is a change in registered characteristics, central manager will invoke didUpdateValueFor delegate method. In this method, we can read and handle the new incoming data.

27

To write data to external peripheral, we need to call writeValue method.28

Now, lets see how the app works. On launch of this app, it will try to connect to a nearby Bluetooth device which is the Arduino board in our case. After successful connection, this app can send instructions to the board to turn the LED on-off.

Steps To Run

  1. Upload LED demo program on Arduino board by using Arduino IDE. You will find more details for uploading it in the referenced link above.
  2. On successful upload, keep power up Arduino board.
  3. Launch iOS demo app. It will automatically connect to the board on which the program is uploaded.
  4. Once it is connected, the red line in the app turns green. Now you can send instructions to turn the LED on-off on the board by using this app.

combine-image

You can modify this demo program for the internet instead of Bluetooth. For internet network, Arduino board has the capability of broadcasting data over the internet.

Conclusion

This article is a good starting point for anyone interested in connecting an iOS app to an IoT device using Bluetooth Low Energy (BLE). We saw how to hook up an Arduino board with an iOS app. We have an LED on Arduino board, but we can easily connect any other sensor to it. You can find the sample projects on github. I hope you’ll find these projects useful. Good luck and have fun!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s