IOT 101: From hardware to cloud — Part I

Hardware — Getting started with Arduino Nano 33 BLE Sense

Leonardo Cavagnis
5 min readJun 8, 2020

IOT is like teenage sex: everyone talks about it, nobody really knows how to do it.

IOT = Internet Of Things
In short, giving telecommunication capabilities to things that in the past didn’t have it. For example: watches, thermostats…
But don’t get confused! Internet, in this context, stands for capability to interact with other devices using a telecommunication channel, not surfing the web.

In this mini-series, we’ll develop a simple IOT gadget: a Smart Thermostat.
This smart “Thing” will comunicate over Bluetooth connection (the “Internet”) with an Android mobile application.

Let’s start!

What is Bluetooth Low Energy (aka BLE)?

Bluetooth is a wireless short-range technology, widely used by smartphones to communicate with electronic devices like ear-phones, fitness trackers, smartwatches and more.

Bluetooth Low Energy (BLE) is an evolution of Bluetooth technology, that introduces two important improvements:

  • Less power consumption thanks to LE technology
  • Better data communication management thanks to the use of Services and Characteristics (we’ll see them later)

Why BLE for IOT ?

Our IOT scenario: Smart Thermostat

Bluetooth plays an important role in the communication between electronic devices and the rest of the world. It allows an easy and low consumption way to send data to a more powerful device like a smartphone.

In this scenario, smartphone acts like a gateway between smart device and cloud. And it can also provide a friendly interface for the user thanks to a mobile application (App).

Arduino: the power of simplicity

A good starting point for your first IOT project is Arduino.
Arduino is an open-source electronic prototyping platform enabling newbies, professionals, hobbyists, etc. to create simple and smart electronic devices.

The best Arduino platform for IOT is the Arduino Nano 33 BLE Sense.
Nano 33 BLE Sense is a cheap and powerful electronic device equipped with a BLE antenna and a series of interesting sensors.

We’ll use this tiny and cool platform to realize our Smart Thermostat.

BLE in a nutshell

#include <ArduinoBLE.h>

ArduinoBLE Library is the simplest way to interact with Bluetooth Low Energy technology on Arduino platform.

Peripheral-Central

BLE implements a client-server (central-peripheral) paradigm, where peripheral (server) has data and central (client) wants that data.

Smart Thermostat plays the role of BLE Peripheral (has the temperature), while Smartphone acts as BLE Central (wants the temperature).

Use BLE.begin() function to initialize the device BLE peripheral capability.

if (!BLE.begin()) {
Serial.println(“Starting BLE failed!”);
while (1);
}

Advertising is not a TV spot

Imagine peripheral continue shouting “Hey! I’m here and my name is …”, until a Central device arrives and says “Ok, guy. I want to listen to what you have got to say”.

Using BLE terms:

  • Hey! I’m here and my name is …” is called advertising.
  • Ok, guy. I want to listen to what you have got to say” is called connection.

Advertising is the first thing that you have to worry about when you deal with BLE peripheral.
Advertising information is contained into the advertising packet and helps central device to retrieve peripheral information, for example:

  • What is your name?
    Contained in the device local name field of advertising packet.
// set peripheral local name
BLE.setLocalName("SmartThermostat");
  • What data do you have?
    Contained in the advertised services field of advertising packet.
// set the UUID for the service peripheral advertises
BLE.setAdvertisedService(environmentalSensingService);

Smart Thermostat has data related to the environment (ambient temperature), so, using advertising packet, it informs central devices that it has the Environmental Sensing Service. Therefore, it exposes environment data using a BLE Service.

But, what is a Service?

Services

Services are used to break up peripheral data into logical subsets.
There are two type of Services:

  • Normative
    Identifiable with a 16-bit UUID (Universal Unique Identifier) assigned and regulated by Bluetooth SIG organization (See complete list here).
// create EnvironmentalSensing service
BLEService environmentalSensingService("181A");
  • Non-normative
    Identifiable with a 128-bit UUID (use this tool to generate a custom 128-bit UUID). They are fully customizable.
// create LED service
BLEService ledService("277eaf48-6698-4da9-8329-335d05343490");

Smart Thermostat implements a normative service (the Environmental Sensing Service), and also a non-normative service called ledService. It is used to know and control the status of the on-board LED of Arduino platform.

Service data are divided in chunks of 20-byte called characteristics.

Characteristics

Characteristics are also divided in two classes: Normative (complete list here) and Non-normative.
Typically, each characteristics expresses a single logical value.
For example, Environmental Sensing Service exposes different ambient data (Temperature, Humidity, etc.). Each data is contained into a single Characteristic.

Smart Thermostat uses the Temperature characteristic to expose ambient temperature measurement.

// create EnvironmentalSensing service
BLEService environmentalSensingService(“181A”);
BLEShortCharacteristic temperatureCharacteristic(“2A6E”, BLERead | BLENotify);

According to Bluetooth GATT Characteristic specification, Temperature characteristic is a 16-bit width data; It is implemented with BLEShortCharacteristic (BLE<datatype>Characteristic) constructor of ArduinoBLE library.

Temperature characteristic is a read-only data. This property is assigned with BLERead constant within the constructor.
But,what’s the BLENotify property?
Read and Notify properties are both read-properties but with a significant difference:

  • Read property: the central reads a data from a peripheral characteristic
  • Notify property: the peripheral informs central device that a new data value is available in a specific characteristic
// create LED service
BLEService ledService("277eaf48-6698-4da9-8329-335d05343490");
BLEByteCharacteristic ledStatusCharacteristic("277eaf49-6698-4da9-8329-335d05343490", BLERead | BLEWrite);

On the other hand ledStatus characteristic (contained in ledService), is a read-write data. This property is assigned by the combination of BLERead and BLEWrite constants within the constructor.
LedStatus characteristic is used to change (Write property) and know (Read property) the status (ON or OFF) of the LED (Light Emitting Diode) mounted on Nano 33 BLE Sense board.

In summary:

  • Read
  • Notify
  • Write

What’s next? Build your first BLE Android App

Now that we have covered how to get started with hardware prototyping platforms like Arduino, we will go in-depth on building our first BLE Android mobile application.

Here, you can find the Smart Thermostat Arduino project! It covers all the topics discussed in this article!

--

--

Leonardo Cavagnis

Passionate Embedded Software Engineer, IOT Enthusiast and Open source addicted. Proudly FW Dev @ Arduino