Build an iOS app that connects to IoT device using Bluetooth

You must be aware of term “IoT” Internet of Things, this is one of the hot technology worldwide nowadays, as many products, devices are available in the market.

I won’t say detailed knowledge of IoT, but at the end of this article, you will have a high-level idea of how IoT system works. To understand it, we will create an iOS demo app which will send/receive data from IoT compatible device with the help of Arduino.

In this article, we will cover following points:
– Introduction to IoT
– Arduino Overview
– iOS Demo App to understand end to end flow.

Introduction to IoT
Internet of things 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 heart monitor.
Lets see what are the opportunities in IoT, why it became one of the hot technology:
  • The connected world of devices, people and data helps to create numerous business opportunities for many sectors. For example, If I own car parts manufacturing business then I might want to know which parts are most popular. Using IoT, I can use a sensor in a showroom, to detect which areas are more popular or in which area customer spends more time. I will use this data to identify parts and increase production of these parts.
  • Real-time updates offer resources to improve decision making more accurate.
  • 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.

There are many other opportunities which accelerated the market for Internet of Things. It is predicted that by 2020, 25 billion devices will be available in the market.

The network in IoT will be decided on the factors such as range, data, security, and power. These factors will decide the choice of network whether it is the internet, Bluetooth, WiFi or any other. IoT is used for the devices that would not necessary to have an internet connection. It is used for a device that can communicate with the network which can be the internet, Bluetooth, NFC or anything else. For short-range communications technology is of course Bluetooth. It is expected to be key for wearable products. For example, Smartwatch, Fitness band. There are many sources available for IoT, so we will not dig into this.

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 operate 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 is an open-source hardware and software. Arduino boards are able to read inputs from the 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 following inputs and outputs:
Inputs:
  • Temperature, Humidity, Pressure etc
  • Light, Infrared signals
  • Sounds
  • Motion captures
  • Heart rate, muscle movement
  • Electrical current
  • Touch, Fingerprints
Outputs:
  • LEDs
  • LCDs
  • Speakers
  • Motors
  • The internet
There are different types of Arduino boards available depending on features like an 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 microcontroller 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 at https://www.arduino.cc/en/Main/Software.

Arduino programming language:
The coding language that Arduino uses is very much like C++, which is a common language in the world of computing.
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 initialisation stuff 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 Function:
  • 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 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 LED on-off. This program send/receives data over the Bluetooth and turns on-board LED on-off depending on 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 the Bluetooth. For that, I have used Core-Bluetooth framework. You can download it here.
On launch of this app, it will try to connect to a nearby Bluetooth device which is Arduino board in our case. After successful connection, this app can send instructions to the board to turn LED on-off.
Steps To Run:
  • Upload LED demo program on Arduino board. You have to upload it by using Arduino IDE. You will find more details for uploading it in the referenced link above.
  • On successful upload, keep power up Arduino board.
  • Launch iOS demo app. It will connect automatically to the board on which program is uploaded.
  • Once it is connected, red line in the app turns green. Now you can send instructions to turn LED on-off on the board by using this app.
  • Once it is connected, red line in the app turns green. Now you can send instructions to turn LED on-off on the board by using this app.

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

Conclusion
This article is a good starting point for anyone who is interested in connecting an iOS app to IoT device using Bluetooth Low Energy. We saw how to hook up an Arduino board with an iOS app. We have a 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!

Software Development for IoT – The Cypress Way: Development tools for Bluetooth Smart Applications– Part 2

This is in continuation of the series of blogs where we will be covering some of common development tools used for Bluetooth smart applications. In this part, I will be describing the popular Bluetooth LE SDK from Cypress Semiconductors.

Continue reading Software Development for IoT – The Cypress Way: Development tools for Bluetooth Smart Applications– Part 2

Development tools for Bluetooth Smart Applications – nRF5 SDK from Nordic

 

This is first of the series of blogs where we will be covering some of common development tools used for Bluetooth smart applications. In this part, I will be describing the popular Bluetooth LE SDK from Nordic.

A brief Introduction – Bluetooth LE

Bluetooth SIG announced the formal adoption of Bluetooth® Core Specification version 4.0 in 2011 which included the hallmark Bluetooth Smart (low energy)  Feature. Continue reading Development tools for Bluetooth Smart Applications – nRF5 SDK from Nordic

NFC in Android : An Introduction

What is NFC ?

Near Field Communication (NFC) is a set of short-range wireless technologies, typically requiring a distance of 4cm or less to initiate a connection. NFC allows you to share small payloads of data between an NFC tag and an Android-powered device, or between two Android-powered devices. NFC-related APIs in Android are introduced starting from API level 9. Continue reading NFC in Android : An Introduction