IoT Patterns: Remote Control - Lighting Control System

This post provides an overview and learning objectives of Chapter 6 from my book Building Arduino Projects for the Internet of Things.

Introduction

The remote control is currently one of the most popular IoT patterns. Examples of this pattern can be found in IoT applications that let you remotely control things such as lights, thermostat and garage door using handheld devices or computers. It has mainly been used for home automation applications so far.

In this chapter, you are going to build a Lighting Control System. The figure below shows components of a Lighting Control System. The first component is an Android app that lets users control lights. It publishes messages to MQTT broker whenever the user taps on the app screen. The second component is an MQTT broker, and the final component of this IoT application is an Arduino device that turns lights on or off based on messages received from MQTT broker.

CodifyThings - Building Arduino Projects for the Internet of Things (IoT)

Learning Objectives

At the end of this chapter you will be able to:

  • Write code to turn LEDs connected to Arduino on or off
  • Subscribe Arduino to an MQTT broker
  • Build an Android app that publishes to MQTT broker

Final Product

CodifyThings - Building Arduino Projects for the Internet of Things (IoT)


IoT Patterns: Realtime Clients - Intrusion Detection System

This post provides an overview and learning objectives of Chapter 5 from my book Building Arduino Projects for the Internet of Things.

Introduction

An important pattern of IoT is the ability to sense data and make it available to users in real-time, such as with home monitoring solutions, perimeter security applications, and inventory alerts.

In this chapter, you are going to build an example of this pattern, an intrusion detection system. The figure below shows components of an intrusion detection system. The first component is an Arduino device that has a motion sensor attached to it. The second component is an MQTT broker. You will use the publish-subscribe model of MQTT for sending intrusion detection notifications in real-time (for details, see Chapter 3). The final component of your IoT application is an Android app that subscribes to the MQTT broker and shows an alert notification to users whenever Arduino detects an intrusion and publishes a message to the MQTT broker.

CodifyThings-Building-Arduino-Projects-for-Internet-of-Things_5-1

Learning Objectives

At the end of this chapter you will be able to:

  • Read motion sensor data from Arduino
  • Publish sensor data to an MQTT broker
  • Build an Android app that subscribes to an MQTT broker
  • Display a notification in the app whenever a new message is published to the MQTT broker

Final Product

CodifyThings-Building-Arduino-Projects-for-Internet-of-Things_5-26
CodifyThings-Building-Arduino-Projects-for-Internet-of-Things_5-27 CodifyThings-Building-Arduino-Projects-for-Internet-of-Things_5-28


Book Overview: Building Arduino Projects for the Internet of Things

My new book Building Arduino Projects for the Internet of Things was released earlier today. It is available for purchase on all major stores (Buy Now). This post provides an overview of the book.

What This Book Covers

Adeel Javed - Beginner's Guide to the Internet of Things

This book is based on my personal experience of getting started with IoT. It is divided into two logical sections. The first one teaches the basics of building IoT applications and the second section follows a project-based approach. At the end of each chapter, you will have a working prototype of an IoT application.

Part 1: Building Blocks

Chapters 1-3 cover the building blocks of IoT:

  • Chapter 1, “Arduino Basics,” introduces the Arduino prototyping platform, which is used throughout the book.
  • Chapter 2, “Internet Connectivity,” discusses the different options available for connecting things to the Internet.
  • Chapter 3, “Communication Protocols,” teaches you which communication protocols are and which ones are available for IoT.

Part 2: Prototypes

Chapters 4-12 use the information covered in Part 1 to build prototypes of IoT applications.

Download Source Code

Download code directly from Apress website or the GitHub repository.


Adeel Javed - Internet of Things

IoT Sensors - #8: Capacitive Touch Sensor

This article is the eighth in a series on Internet of Things (IoT) sensors. It teaches you about various sensors available and how to read data from them.

Hardware

  • Arduino Uno
  • Capacitive Touch Sensor
  • Jumper Cables

CodifyThings - Internet of Things (IoT) Sensors

Software

Circuit

Step 1: Make sure your Arduino is not connected to a power source.

Step 2: Using jumper cables connect power (VNC) and ground (GND) ports on Arduino to power (+) and ground (-) ports on the breadboard.

Tip: It is a good practice to use red jumper cable for power (+ / VNC) and black jumper cable for ground (- / GND).

Step 3: Now that your breadboard has a power source, using jumper cables connect power (+) and ground (-) ports of your breadboard to power and ground ports of the Capacitive Touch sensor.

Step 4: To read capacitive touch sensor values, you will need to connect a jumper cable from signal (SIG) port of Capacitive Touch sensor to D3 (Digital) port of your Arduino.

Your circuit is now complete and it should look similar to figures below.

CodifyThings - Internet of Things (IoT) Sensors

Arduino Code

Next, you are going to write Arduino code that will read sensor data. Start your Arduino IDE and either type the code provided below or download it from GitHub. All the code goes into a single source file (*.ino) but in order to make it easy to understand and reuse, it has been divided into 3 sections.

  • External Libraries: includes all libraries required to run the program
  • Sensor Setup: code for reading sensor data
  • Standard Arduino Functions: implementation of standard Arduino functions setup() and loop()

[code lang="js"]
/***************************************************************************
* External Libraries
**************************************************************************/

#include <SPI.h>

/*****************************************************************************
* Sensor Setup - Variables & Functions
****************************************************************************/

int CAPACITIVE_SENSOR_PIN = 3;
int capacitiveSensorValue = 0;

void readSensorData()
{
//Read Sensor Value
capacitiveSensorValue = digitalRead(CAPACITIVE_SENSOR_PIN);

//Display Readings
Serial.print("[INFO] Capacitive Touch Sensor Reading: ");
Serial.println(capacitiveSensorValue);
}

/***************************************************************************
* Standard Arduino Functions - setup(), loop()
**************************************************************************/

void setup()
{
pinMode(CAPACITIVE_SENSOR_PIN, INPUT);

// Initialize serial port
Serial.begin(9600);
}

void loop()
{
readSensorData();

delay(1000);
}
[/code]

Final Product

Make sure your Arduino is powered on and code from above has been deployed. As soon as you touch the sensor value of 1 will displayed.

CodifyThings - Internet of Things (IoT) Sensors


Adeel Javed - Internet of Things

IoT Sensors - #7: Flame Intensity

This article is the seventh in a series on Internet of Things (IoT) sensors. It teaches you about various sensors available and how to read data from them.

Hardware

  • Arduino Uno
  • Flame Intensity Sensor
  • Jumper Cables

CodifyThings-Internet-of-Things-Sensors-701

Software

Circuit

Step 1: Make sure your Arduino is not connected to a power source.

Step 2: Using jumper cables connect power (VNC) and ground (GND) ports on Arduino to power (+) and ground (-) ports on the breadboard.

Tip: It is a good practice to use red jumper cable for power (+ / VNC) and black jumper cable for ground (- / GND).

Step 3: Now that your breadboard has a power source, using jumper cables connect power (+) and ground (-) ports of your breadboard to power and ground ports of the Flame Intensity sensor.

Step 4: To read flame intensity values, you will need to connect a jumper cable from the signal (OUT) port of Flame Intensity sensor to A0 (Analog) port of your Arduino.

Your circuit is now complete.

Arduino Code

Next, you are going to write Arduino code that will read sensor data. Start your Arduino IDE and either type the code provided below or download it from GitHub. All the code goes into a single source file (*.ino) but in order to make it easy to understand and reuse, it has been divided into 3 sections.

  • External Libraries: includes all libraries required to run the program
  • Sensor Setup: code for reading sensor data
  • Standard Arduino Functions: implementation of standard Arduino functions setup() and loop()

[code lang="js"]
/***************************************************************************
* External Libraries
**************************************************************************/

#include &lt;SPI.h&gt;

/*****************************************************************************
* Sensor Setup - Variables &amp; Functions
****************************************************************************/

int FLAME_SENSOR_PIN = A0;
float flameSensorValue = 0.0;

void readSensorData()
{
//Read Mositure Sensor Value
flameSensorValue = analogRead(FLAME_SENSOR_PIN);

//Display Readings
Serial.print("[INFO] Flame Sensor Reading: ");
Serial.println(flameSensorValue);
}

/***************************************************************************
* Standard Arduino Functions - setup(), loop()
**************************************************************************/

void setup()
{
// Initialize serial port
Serial.begin(9600);
}

void loop()
{
readSensorData();

delay(1000);
}
[/code]

Final Product

Note: For testing purposes make sure you are in an open space away from items that can catch fire. This post used a flame lighter for testing.

Make sure your Arduino is powered on and code from above has been deployed. As soon as your sensor gets close to a flame, the readings will start going down.

CodifyThings - Internet of Things (IoT) Sensors


Adeel Javed - Internet of Things

IoT Sensors - #6: Location Detection (GPS)

This article is the sixth in a series on Internet of Things (IoT) sensors. It teaches you about various sensors available and how to read data from them.

Hardware

  • Arduino Uno
  • Location Detection Sensor (GPS)
  • Jumper Cables

CodifyThings - Internet of Things (IoT) Sensors

Software

Circuit

Step 1: Make sure your Arduino is not connected to a power source.

Step 2: Using jumper cables connect power (VNC) and ground (GND) ports on Arduino to power (+) and ground (-) ports on the breadboard.

Tip: It is a good practice to use red jumper cable for power (+ / VNC) and black jumper cable for ground (- / GND).

Step 3: Now that your breadboard has a power source, using jumper cables connect power (+) and ground (-) ports of your breadboard to power and ground ports of the GPS.

Step 4: To read coordinates from GPS, you will need to connect a jumper cable from RX (Receive) port of GPS to D3 (Digital) port of Arduino.

Step 5: Similar to step 4, you will also need to connect a jumper cable from TX (Transmit) port of GPS to D2 (Digital) port of Arduino.

Note: Depending on your GPS module you might need to add transistors in the circuit as well, please check specifications of your GPS module.

Your circuit is now complete and it should look similar to figures below.

CodifyThings - Internet of Things (IoT) Sensors

Arduino Code

Next, you are going to write Arduino code that will read sensor data. Start your Arduino IDE and either type the code provided below or download it from GitHub. All the code goes into a single source file (*.ino) but in order to make it easy to understand and reuse, it has been divided into 3 sections.

  • External Libraries: includes all libraries required to run the program
    • <TinyGPS.h> - https://github.com/mikalhart/TinyGPS/releases/tag/v13

  • Sensor Setup: code for reading sensor data
  • Standard Arduino Functions: implementation of standard Arduino functions setup() and loop()

[code lang="js"]
/***************************************************************************
* External Libraries
**************************************************************************/

#include <SPI.h>
#include <TinyGPS.h>
#include <SoftwareSerial.h>

/*****************************************************************************
* Sensor Setup - Variables & Functions
****************************************************************************/

TinyGPS gps;
SoftwareSerial ss(2, 3); // GPS TX = Arduino D2, GPS RX = Arduino D3

static void smartdelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}

void getGPSCoordinates()
{
float latitude;
float longitude;
unsigned long age = 0;

gps.f_get_position(&latitude, &longitude, &age);

// Transmit sensor data
if(latitude != TinyGPS::GPS_INVALID_F_ANGLE &&
longitude != TinyGPS::GPS_INVALID_F_ANGLE)
{
Serial.print("[INFO] Latitude: " + String(latitude));
Serial.println(", Longitude: " + String(longitude));
}
else
{
Serial.println("[INFO] Searching for Satellite");
}

smartdelay(10000);
}

/***************************************************************************
* Standard Arduino Functions - setup(), loop()
**************************************************************************/

void setup()
{
// Initialize serial port
Serial.begin(115200);

// Initialize serial port for GPS data
ss.begin(9600);

}

void loop()
{
// Get GPS Coordinates
getGPSCoordinates();
}
[/code]

Final Product

Make sure your Arduino is powered on and code from above has been deployed. As soon as the code has been deployed, open serial monitor.

CodifyThings - Internet of Things (IoT) Sensors


Adeel Javed - Internet of Things

IoT Sensors - #5: Proximity Detection

This article is the fifth in a series on Internet of Things (IoT) sensors. It teaches you about various sensors available and how to read data from them.

Hardware

  • Arduino Uno
  • Proximity Detection Sensor
  • Jumper Cables

CodifyThings - Internet of Things (IoT) Sensors

Software

Circuit

Step 1: Make sure your Arduino is not connected to a power source.

Step 2: Using jumper cables connect power (VNC) and ground (GND) ports on Arduino to power (+) and ground (-) ports on the breadboard.

Tip: It is a good practice to use red jumper cable for power (+ / VNC) and black jumper cable for ground (- / GND).

Step 3: Now that your breadboard has a power source, using jumper cables connect power (+) and ground (-) ports of your breadboard to power and ground ports of the Proximity Detection sensor.

Step 4: To trigger a burst connect a jumper cable from TRIG pin of the sensor to D2 (Digital) port of Arduino.

Step 5: To read echo connect a jumper cable from ECHO pin of the sensor to D3 (Digital) port of Arduino.

Your circuit is now complete and it should look similar to figures below.

CodifyThings - Internet of Things (IoT) Sensors

Arduino Code

Next, you are going to write Arduino code that will read sensor data. Start your Arduino IDE and either type the code provided below or download it from GitHub. All the code goes into a single source file (*.ino) but in order to make it easy to understand and reuse, it has been divided into 3 sections.

  • External Libraries: includes all libraries required to run the program
  • Sensor Setup: code for reading sensor data
  • Standard Arduino Functions: implementation of standard Arduino functions setup() and loop()

[code lang="js"]
/***************************************************************************
* External Libraries
**************************************************************************/

#include <SPI.h>

/*****************************************************************************
* Sensor Setup - Variables & Functions
****************************************************************************/

int calibrationTime = 30;
#define TRIGPIN 2 // Pin to send trigger pulse
#define ECHOPIN 3 // Pin to receive echo pulse

void calibrateSensor()
{
//Give sensor some time to calibrate
Serial.println("[INFO] Calibrating Sensor ");

for(int i = 0; i < calibrationTime; i++)
{
Serial.print(".");
delay(1000);
}

Serial.println("");
Serial.println("[INFO] Calibration Complete");
Serial.println("[INFO] Sensor Active");

delay(50);
}

void readSensorData()
{
// Generating a burst to check for objects
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(10);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);

// Distance Calculation
float distance = pulseIn(ECHOPIN, HIGH);

Serial.println("[INFO] Object Distance: " + String(distance));
}

/***************************************************************************
* Standard Arduino Functions - setup(), loop()
**************************************************************************/

void setup()
{
// Initialize serial port
Serial.begin(9600);

// Set pin mode
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);

// Calibrate sensor
calibrateSensor();
}

void loop()
{
// Read sensor data
readSensorData();

// Delay
delay(5000);
}
[/code]

Final Product

Make sure your Arduino is powered on and code from above has been deployed. As soon as the code has been deployed, open serial monitor.

CodifyThings - Internet of Things (IoT) Sensors


Adeel Javed - Internet of Things

IoT Sensors - #4: Motion Detection

This article is the fourth in a series on Internet of Things (IoT) sensors. It teaches you about various sensors available and how to read data from them.

Hardware

  • Arduino Uno
  • Motion Detection Sensor
  • Jumper Cables

CodifyThings - Internet of Things (IoT) Sensors

Software

Circuit

Step 1: Make sure your Arduino is not connected to a power source.

Step 2: Using jumper cables connect power (VNC) and ground (GND) ports on Arduino to power (+) and ground (-) ports on the breadboard.

Tip: It is a good practice to use red jumper cable for power (+ / VNC) and black jumper cable for ground (- / GND).

Step 3: Now that your breadboard has a power source, using jumper cables connect power (+) and ground (-) ports of your breadboard to power and ground ports of the Motion Detection sensor.

Step 4: To read motion sensor values, you will need to connect a jumper cable from signal (middle) port of Motion Detection sensor to D3 (Digital) port of your Arduino.

Your circuit is now complete and it should look similar to figures below.

CodifyThings - Internet of Things (IoT) Sensors

Arduino Code

Next, you are going to write Arduino code that will read sensor data. Start your Arduino IDE and either type the code provided below or download it from GitHub. All the code goes into a single source file (*.ino) but in order to make it easy to understand and reuse, it has been divided into 3 sections.

  • External Libraries: includes all libraries required to run the program
  • Sensor Setup: code for reading sensor data
  • Standard Arduino Functions: implementation of standard Arduino functions setup() and loop()

[code lang="js"]
/***************************************************************************
* External Libraries
**************************************************************************/

#include <SPI.h>

/*****************************************************************************
* Sensor Setup - Variables & Functions
****************************************************************************/

int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3;

void calibrateSensor()
{
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);

Serial.println("[INFO] Calibrating Sensor ");

for(int i = 0; i < calibrationTime; i++) { Serial.print("."); delay(1000); } Serial.println(""); Serial.println("[INFO] Calibration Complete"); Serial.println("[INFO] Sensor Active"); delay(50); } void readSensorData() { if(digitalRead(pirPin) == HIGH) { if(lockLow) { lockLow = false; Serial.print("[INFO] Activty Detected @ "); Serial.print(millis()/1000); Serial.print(" secs"); Serial.println(""); delay(50); } takeLowTime = true; } if(digitalRead(pirPin) == LOW) { if(takeLowTime) { lowIn = millis(); takeLowTime = false; } if(!lockLow && millis() - lowIn > pause)
{
lockLow = true;

Serial.print("[INFO] Activty Ended @ "); //output
Serial.print((millis() - pause)/1000);
Serial.print(" secs");
Serial.println("");

delay(50);
}
}
}

/***************************************************************************
* Standard Arduino Functions - setup(), loop()
**************************************************************************/

void setup()
{
// Initialize serial port
Serial.begin(9600);

// Calibrate motion sensor
calibrateSensor();
}

void loop()
{
readSensorData();
}
[/code]

Final Product

Make sure your Arduino is powered on and code from above has been deployed. As soon as the code has been deployed, open serial monitor.

CodifyThings - Internet of Things (IoT) Sensors


Adeel Javed - Internet of Things

IoT Sensors - #3: Soil Moisture

This article is the third in a series on Internet of Things (IoT) sensors. It teaches you about various sensors available and how to read data from them.

Hardware

  • Arduino Uno
  • Soil Moisture Sensor
  • Jumper Cables

CodifyThings - Internet of Things (IoT) Sensors

Software

Circuit

Step 1: Make sure your Arduino is not connected to a power source.

Step 2: Using jumper cables connect power (VNC) and ground (GND) ports on Arduino to power (+) and ground (-) ports on the breadboard.

Tip: It is a good practice to use red jumper cable for power (+ / VNC) and black jumper cable for ground (- / GND).

Step 3: Now that your breadboard has a power source, using jumper cables connect power (+) and ground (-) ports of your breadboard to power and ground ports of the Soil Moisture sensor.

Step 4: To read light intensity values, you will need to connect a jumper cable from Analog signal port of Soil Moisture sensor to A0 (Analog) port of your Arduino.

Your circuit is now complete and it should look similar to figures below.

CodifyThings - Internet of Things (IoT) Sensors

Arduino Code

Next, you are going to write Arduino code that will read sensor data. Start your Arduino IDE and either type the code provided below or download it from GitHub. All the code goes into a single source file (*.ino) but in order to make it easy to understand and reuse, it has been divided into 3 sections.

  • External Libraries: includes all libraries required to run the program
  • Sensor Setup: code for reading sensor data
  • Standard Arduino Functions: implementation of standard Arduino functions setup() and loop()

[code lang="js"]
/***************************************************************************
* External Libraries
**************************************************************************/

#include <SPI.h>

/*****************************************************************************
* Sensor Setup - Variables & Functions
****************************************************************************/

int MOISTURE_SENSOR_PIN = A0;
float moistureSensorValue = 0.0;

void readSensorData()
{
//Read Mositure Sensor Value
moistureSensorValue = analogRead(MOISTURE_SENSOR_PIN);

//Display Readings
Serial.print("[INFO] Moisture Sensor Reading: ");
Serial.println(moistureSensorValue);
}

/***************************************************************************
* Standard Arduino Functions - setup(), loop()
**************************************************************************/

void setup()
{
// Initialize serial port
Serial.begin(9600);
}

void loop()
{
readSensorData();

delay(5000);
}
[/code]

Final Product

Make sure your Arduino is powered on and code from above has been deployed. As soon as the code has been deployed, open serial monitor.

CodifyThings - Internet of Things (IoT) Sensors


Adeel Javed - Internet of Things

IoT Sensors - #2: Light Intensity

This article is the second in a series on Internet of Things (IoT) sensors. It teaches you about various sensors available and how to read data from them.

Hardware

  • Arduino Uno
  • Light Intensity Sensor
  • Jumper Cables

CodifyThings - Internet of Things (IoT) Sensors

Software

Circuit

Step 1: Make sure your Arduino is not connected to a power source.

Step 2: Using jumper cables connect power (VNC) and ground (GND) ports on Arduino to power (+) and ground (-) ports on breadboard.

Tip: It is a good practice to use red jumper cable for power (+ / VNC) and black jumper cable for ground (- / GND).

Step 3: Now that your breadboard has a power source, using jumper cables connect power (+) and ground (-) ports of your breadboard to power and ground ports of the Light Intensity sensor.

Step 4: To read light intensity values, you will need to connect a jumper cable from signal (OUT) port of Light Intensity sensor to A0 (Analog) port of your Arduino.

Your circuit is now complete and it should look similar to figures below.

CodifyThings - Internet of Things (IoT) Sensors

Arduino Code

Next you are going to write Arduino code that will read sensor data. Start your Arduino IDE and either type the code provided below or download it from GitHub. All the code goes into a single source file (*.ino) but in order to make it easy to understand and reuse, it has been divided into 3 sections.

  • External Libraries: includes all libraries required to run the program
  • Sensor Setup: code for reading sensor data
  • Standard Arduino Functions: implementation of standard Arduino functions setup() and loop()

[code lang="js"]
/***************************************************************************
* External Libraries
**************************************************************************/

#include <SPI.h>

/*****************************************************************************
* Sensor Setup - Variables & Functions
****************************************************************************/

int lightValue;

void readSensorData()
{
//Read Light Sensor Value
lightValue = analogRead(A0);

Serial.print("[INFO] Light Sensor Reading: ");
Serial.println(lightValue);
}

/***************************************************************************
* Standard Arduino Functions - setup(), loop()
**************************************************************************/

void setup()
{
// Initialize serial port
Serial.begin(9600);
}

void loop()
{
readSensorData();

delay(5000);
}
[/code]

Final Product

Make sure your Arduino is powered on and code from above has been deployed. As soon as code has been deployed, open serial monitor.

CodifyThings - Internet of Things (IoT) Sensors