Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Internet of Things With Arduino Blueprints - Sample Chapter

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Fr

Arduino is a small single-chip computer board that can be


used for a wide variety of creative hardware projects. It is
the ideal open hardware platform for experimenting with
the world of the Internet of Things.
Internet of Things with Arduino Blueprints begins with
projects based on IoT and cloud computing concepts. The
main aim of this book is to teach you how Arduino can
be used for Internet-related projects so that you are able
to control actuators, gather data from various kinds of
sensors, and send and receive data wirelessly across
HTTP and TCP protocols.
You can use these projects as blueprints for many other
IoT projects and put them to good use. By the end of
the book, you will be an expert in the use of IoT with
Arduino to develop a set of projects that can be applied to
IoT applications in the real world.

Who this book is written for

Make a powerful Internet-controlled relay


with an embedded web server to monitor
and control your home electrical appliances
Build a portable Wi-Fi signal strength sensor
to give haptic feedback about signal strength
to the user
Measure water flow speed and volume
with liquid flow sensors and record
real-time readings
Secure your home with motion-activated
Arduino security cameras and upload
images to the cloud
Implement real-time data logging of solar
panel voltage with Arduino cloud connectors
Track locations with GPS and upload location
data to the cloud
Control a garage door light with your
Twitter feed
Control infrared-enabled devices with
IR remote and Arduino

$ 39.99 US
25.99 UK

community experience distilled

P U B L I S H I N G

Pradeeka Seneviratne

This book is intended for those who want to learn


more about Arduino and make Internet-based interactive
projects with Arduino. If you are an experienced software
developer who understands the basics of electronics, then
you can quickly learn how to build the Arduino projects
explained in this book.

What you will learn from this book

Internet of Things with Arduino Blueprints

Internet of Things with


Arduino Blueprints

ee

pl

C o m m u n i t y

E x p e r i e n c e

D i s t i l l e d

Internet of Things with


Arduino Blueprints
Develop interactive Arduino-based Internet projects with
Ethernet and Wi-Fi

Prices do not include


local sales tax or VAT
where applicable

Visit www.PacktPub.com for books, eBooks,


code, downloads, and PacktLib.

Sa
m

Pradeeka Seneviratne

In this package, you will find:

The author biography


A preview chapter from the book, Chapter 6 'GPS Location Tracker with
Temboo, Twilio, and Google Maps'
A synopsis of the books content
More information on Internet of Things with Arduino Blueprints

About the Author


Pradeeka Seneviratne is a software engineer with over 10 years of experience in

computer programming and systems designing. He loves programming embedded


systems such as Arduino and Raspberry Pi. Pradeeka started learning about
electronics when he was at primary college by reading and testing various electronic
projects found in newspapers, magazines, and books.
Pradeeka is currently a full-time software engineer who works with highly
scalable technologies. Previously, he worked as a software engineer for several IT
infrastructure and technology servicing companies, and he was also a teacher for
information technology and Arduino development.
He researches how to make Arduino-based unmanned aerial vehicles and Raspberry
Pi-based security cameras.

Preface
Arduino is a small single-chip computer board that can be used for a wide variety of
creative hardware projects. The hardware consists of a simple microcontroller, board,
and chipset. It comes with a Java-based IDE that allows creators to program the
board. Arduino is the ideal open hardware platform to experiment with the world of
Internet of Things. The credit card-sized Arduino board can be used via the Internet
to make useful and interactive Internet of Things (IoT) projects.
Internet of Things with Arduino Blueprints is a project-based book that begins with
projects based on IoT and cloud computing concepts. This book covers up to eight
projects that will allow devices to communicate with each other, access information
over the Internet, store and retrieve data, and interact with userscreating smart,
pervasive, and always connected environments. It explains how wired and wireless
Internet connections can be used with projects and explains the use of various
sensors and actuators. The main aim of this book is to teach you how Arduino can
be used for Internet-related projects so that users are able to control actuators, gather
data from various kinds of sensors, and send and receive data wirelessly across
HTTP and TCP protocols.
Finally, you can use these projects as blueprints for many other IoT projects and put
them to good use. By the end of the book, you will be an expert in the use of IoT with
Arduino to develop a set of projects that can relate very well to IoT applications in
the real world.

What this book covers


Chapter 1, Internet-Controlled PowerSwitch, briefly introduces Arduino UNO and
Arduino Ethernet shield basics while focusing on building an Internet-controlled
PowerSwitch using Arduino UNO, the Arduino Ethernet shield, and PowerSwitch
Tail to turn electrical appliances on/off through the Internet via a web-based user
interface. Also, you will learn how to increase the complexity of PowerSwitch by
adding a circuit to track the mains electricity.

Preface

Chapter 2, Wi-Fi Signal Strength Reader and Haptic Feedback, briefly introduces Arduino
Wi-Fi shield basics, vibration motors, and haptic feedback. You will learn how
to make advanced vibration patterns using vibration motors with a haptic motor
controller and the Adafruit haptic library according to the Wi-Fi signal strength
received by the Arduino Wi-Fi shield.
Chapter 3, Internet-Connected Smart Water Meter, focuses on building a flow
sensor-based water meter in conjunction with the Arduino Ethernet shield to
measure water flow rate and volume, and then display them on an LCD screen.
In addition, you will learn how to convert this water meter to a web server and
request readings through the Internet of Ethernet.
Chapter 4, Arduino Security Camera with Motion Detection, explains how to incrementally
develop a Arduino Ethernet shield-based security camera with the Adafruit TTL Serial
JPEG camera and the VC0706 camera library. In addition, you will learn how to add
motion detection functionality and upload the captured images to Flickr.
Chapter 5, Solar Panel Voltage Logging with NearBus Cloud Connector and Xively, briefly
introduces the NearBus cloud connector and Xively, while focusing on building a
solar panel voltage logger with the Arduino Ethernet shield with a few electronic
components. Also, you will learn how to log the output voltage of a solar panel in
combination with NearBus and Xively, and then display real-time data that can be
viewed through a web browser.
Chapter 6, GPS Location Tracker with Temboo, Twilio, and Google Maps, briefly
introduces the GPS shield and how to use the TinyGPSPlus library and the Google
JavaScript API library to build a real-time location tracker to display the current
location of the GPS shield on Google Maps. You will also learn the basics of Temboo
and Twilio cloud services.
Chapter 7, Tweet-a-Light Twitter-Enabled Electric Light, introduces Python, a more
powerful programming language that can be used to read Twitter tweets and write
data to a computer's serial port accordingly. Finally, you will learn to build an
electric light switch that can be controlled using Twitter tweets to turn the switch
on and off.
Chapter 8, Controlling Infrared Devices Using IR Remote, focuses on building an infrared
remote control with the Arduino Ethernet shield and a few electronic components
that can be controlled through the Ethernet or Internet to control IR-enabled devices
remotely. You will learn how to record and reproduce IR commands using the
Arduino IR remote library. In addition, you will learn how to add IR functionality to
non-IR enabled devices.

GPS Location Tracker with


Temboo, Twilio, and
Google Maps
Location tracking is important when you want to find the exact location of movable
objects, such as vehicles, pets, or even people. GPS technology is very helpful in
getting precise locations, which makes it possible to create real-time tracking devices.
In this chapter you will learn:

How to connect the Arduino GPS shield with the Arduino Ethernet board

How to install and use TinyGPSPlus library with the Arduino Ethernet board

How to extract location data and time with Arduino GPS shield in
conjunction with TinyGPSPlus library

About Google Maps JavaScript API that displays the current location on
Google Maps with GPS data

How to get GPS location data by SMS with Twilio and Temboo

[ 127 ]

GPS Location Tracker with Temboo, Twilio, and Google Maps

Hardware and software requirements


You will need the following hardware and software to complete this project:

Hardware requirements

Arduino Ethernet board (https://www.sparkfun.com/products/11229)

SparkFun GPS Shield kit (https://www.sparkfun.com/products/13199)

FTDI Cable 5V (https://www.sparkfun.com/products/9718)

9V DC 650mA wall adapter power supply (https://www.sparkfun.com/

products/10273)

Software requirements

TinyGPSPlus library (https://github.com/mikalhart/TinyGPSPlus/


archive/master.zip)

Getting started with the Arduino GPS


shield
Arduino GPS shield lets your Arduino board receive information from the GPS
(Global Positioning System). The GPS is a satellite-based navigation system
made up of a network of 24 satellites.
Arduino GPS shield consists of a GPS receiver that can be used to receive accurate
time signals from the GPS satellite network and calculate its own position.
Arduino GPS Shield is currently manufactured by various electronics kit suppliers.
The most popular manufacturers are SparkFun Electronics and Adafruit. Throughout
this project, we will use the SparkFun GPS Shield kit (https://www.sparkfun.com/
products/13199).
The kit comes with an EM-506 GPS module and the Arduino stackable header kit.
Click on Assembly Guide (https://learn.sparkfun.com/tutorials/gpsshield-hookup-guide) in the product page, and follow the instructions to solder
the headers and GPS module to the shield.

[ 128 ]

Chapter 6

The Arduino GPS shield kit: Image taken from SparkFun Electronics Image courtesy of
SparkFun Electronics (https://www.sparkfun.com)

Connecting the Arduino GPS shield with


the Arduino Ethernet board
To connect the Arduino GPS shield with the Arduino Ethernet board, perform the
following steps:
1. Stack your Arduino GPS shield with the Arduino Ethernet board.
2. Move the UART/DLINE switch to the DLINE position. This is a two-way
switch that can be used to select the UART or DLINE mode to communicate
GPS shield with Arduino.

UART: This connects the GPS module's serial lines to Arduino


hardware serial (D0/RX and D1/TX).

[ 129 ]

GPS Location Tracker with Temboo, Twilio, and Google Maps

DLINE: This connects the GPS module's serial lines to the Arduino
software serial (D2 and D3). See the solder marks label next
to the UART/DLINE switch.

The Arduino GPS shield PCB: Image courtesy of SparkFun Electronics (https://www.sparkfun.com)

3. Connect the 9V DC power supply to your Arduino Ethernet board. Then,


connect the Arduino Ethernet board to the computer with an FTDI cable
a USB to Serial (TTL Level) converter.
4. Now, download the TinyGPSPlus library from https://github.com/
mikalhart/TinyGPSPlus/archive/master.zip and extract it to your
Arduino Installation's libraries folder.

Testing the GPS shield


Follow these steps to test the GPS shield:
1. Open a new Arduino IDE, then copy and paste the sample code
B04844_06_01.ino from the Chapter 6 code folder of this book. (Note that
this is the sample code included with the TinyGPSPlus library to display
the current location by latitude and longitude with date and time). You can
also open this sketch by navigating to File | Examples | TinyGPSPlus |
DeviceExample on the menu bar.
2. Verify and upload the sketch to your Arduino board or Arduino
Ethernet board.
[ 130 ]

Chapter 6

3. Open the Arduino Serial Monitor by going to Tools | Serial Monitor. The
following output will be displayed:

In each entry, the current location is displayed with latitude, longitude, and
date/time. Next, you will learn how to use these values to display the location
on Google Maps.

Displaying the current location on


Google Maps
Google Maps JavaScript API can be used to display the current location with a
marker on Google Maps. We can simply pass the latitude and longitude to the
Google JavaScript API library and display the current location as a simple marker.

[ 131 ]

GPS Location Tracker with Temboo, Twilio, and Google Maps

The following steps will explain you how to display the Arduino GPS shield's
current location on Google Maps:
1. Open a new Arduino IDE and paste the sample code B04844_06_02.ino
from the Chapter 6 code folder. Verify and upload the sketch to your
Arduino Ethernet Shield or Arduino Ethernet board.
2. The code consists a mix of Arduino, HTML, and JavaScript. Let's look at
some important points of the code.

The following JavaScript function creates a new Google map position


with latitude and longitude:
var myLatlng = new google.maps.LatLng(25.363882,131.044922);

The latitude and longitude values should be replaced with the real
time returning values of the Arduino GPS shield as follows:
var myLatlng = new
google.maps.LatLng(gps.location.lat(),gps.location.lng());

The following JavaScript function will create a map and display a


simple marker on Google Maps based on the location provided by
the map options:
var map = new google.maps.Map(document.getElementById('mapcanvas'), mapOptions);

3. Open your web browser and type the IP address of the Arduino Ethernet
Shield and navigate to the site. Read Chapter 1, Internet-Controlled
PowerSwitch, for information on how to find the IP address of your Arduino
Ethernet Shield. Example: http://192.168.10.177.
4. Your web browser will display your GPS shield's current location on the
Google Map, as shown in the following screenshot:

[ 132 ]

Chapter 6

The current location of the Arduino GPS shield is displayed on the Google Map with a marker icon

In the next section, you will learn how to send the current GPS location by SMS to
the client using Twilio and Temboo.

Getting started with Twilio


The Twilio platform provides API to programmatically send, receive, and track SMS
messages worldwide, while also letting you test your SMS-enabled applications
within a sandbox environment.

Creating a Twilio account


Using your Internet browser, visit https://www.twilio.com/. The Twilio landing
page will be displayed with the sign up and log in options.
Click on the SIGN UP button. You will be navigated to the new user registration form.

[ 133 ]

GPS Location Tracker with Temboo, Twilio, and Google Maps

Fill out the form with the relevant information and then click on the Get Started
button. You will be navigated to the Human Verification page:
1. In this page, select your country from the drop-down list and type your
phone number in the textbox.
2. Click on the Text Me button. You will be navigated to the Enter Verification
Page. Meanwhile, your mobile phone will receive an SMS message
containing a verification code.
3. In the Enter Verification Page, enter the verification code and click on the
Submit button.
After successfully verifying your Twilio account, you will be navigated to the
Twilio Getting Started page. This means that you have successfully created
a Twilio account.

Twilio's getting started page

[ 134 ]

Chapter 6

Finding Twilio LIVE API credentials


At the top of the Twilio getting started page, you will find the Show API Credentials
link. Click on it, and the API Credentials panel will expand and display the
following information:

ACCOUNT SID

AUTH TOKEN

Twilio API Credentials: Account SID and Auth Token

You will need the ACCOUNT SID and AUTH TOKEN in the next section when
you connect your Twilio account with Temboo. However, the default account type
is a trial account with limited API calls allocated. If you want to get the full benefit
of Twilio, upgrade the account.

[ 135 ]

GPS Location Tracker with Temboo, Twilio, and Google Maps

Finding Twilio test API credentials


At the top-right of the page, click on your account name, and from the drop-down
menu, click on Account. The Account Settings page will appear, as shown here:

Twilio test API credentials: Test Account SID and Test Auth Token

In this page, under API Credentials, you can find the Test Account SID and Test
Auth Token to test your apps with Twilio.

[ 136 ]

Chapter 6

Get your Twilio number


Your Twilio account provides phone numbers to use with Voice, SMS, and MMS.
You can obtained one such number by following these instructions:
1. Click on the Voice, SMS & MMS menu item at the top of the page.
2. Click on GETTING STARTED in the submenu of Voice, SMS & MMS.
3. Click on the Get your Twilio Number button. Your first Twilio phone
number will generate and you can choose the number by clicking on the
Choose this number button. Also, you can search for a different number
by clicking on Search, for a different number link.
Some countries, such as Australia, do not have SMS capability for trial
accounts. Use a United States number, which will enable you to send
SMS internationally.

Twilio phone number

[ 137 ]

GPS Location Tracker with Temboo, Twilio, and Google Maps

4. The following page will be displayed as a confirmation. You can further


configure your Twilio phone number by clicking on the Configure your
number button:

Twilio Phone number configuration page

Creating Twilio Choreo with Temboo


The Temboo provides us with a Choreo to send an SMS using the Twilio account.
This Choreo uses Twilio API credentials to authenticate and send SMS to destination
phone numbers. The advantage is that by using Temboo Choreos, you can write
more complex functions using few lines of code.

Sending an SMS with Twilio API


To send an SMS with Twilio API, perform the following steps:
1. Sign in to your Temboo account. If you still don't have a Temboo account,
create one as discussed in Chapter 5, Solar Panel Voltage Logging with NearBus
Cloud Connector and Xively.
2. Under CHOREOS, expand Twilio, then expand SMSMessages and
click on SendSMS.
3. The right-hand side of the page will load the Twilio SendSMS
configuration form.
[ 138 ]

Chapter 6

4. Turn ON the IoT Mode.

Twilio SendSMS form

5. Fill out the following textboxes with your Twilio API settings:

AccountSID: Type the Twilio Test Account SID

AuthToken: Type the Twilio Test AuthToken

Body: You can type any text message here in order to test

From: Type Twilio Sandbox number (use your Twilio phone number)

To: The destination phone number (use your phone number that
associated with the Twilio account)

6. Click on the Run button to send the SMS to your phone.


[ 139 ]

GPS Location Tracker with Temboo, Twilio, and Google Maps

Send a GPS location data using Temboo


To send a GPS location data using Temboo, perform the following steps:
1. Open a new Arduino IDE and copy and paste the sketch B04844_06_03.ino
from the Chapter 6 code folder.
2. Replace the ToValue and FromValue phone numbers, as shown here:
String ToValue = "+16175XXX213";
SendSMSChoreo.addInput("To", ToValue);
String FromValue = "+16175XXX212";
SendSMSChoreo.addInput("From", FromValue);

3. Save the B04844_06_03.ino sketch in your local drive inside a new folder.
Copy the code generated in the HEADER FILE section under the Twilio
SendSMS section, and paste it into a new Notepad file. Save the file as
TembooAccount.h in the same location.
4. Verify the sketch. If you get a compiler error indicating that the
TembooAccount.h header file is missing, restart the Arduino IDE and open
the B04844_06_03.ino sketch again and then verify. This will probably
solve the issue.
5. Upload the sketch into your Arduino Ethernet board.
6. You will receive the first SMS including the GPS location data from your
device. Wait for 30 minutes. You will receive the second SMS. You can
change the delay between SMS messages by modifying the following
code line as shown:
delay(1800*1000); // wait 30 minutes between SendSMS calls

The value 1,800 seconds is equal to 30 minutes. To convert the 1,800 seconds into
milliseconds, multiply it by 1,000.

Summary
In this chapter, you learned how to connect the Arduino GPS shield with Arduino
Ethernet Shield while displaying the current location using Google maps with
Google Maps JavaScript API. You also used Twilio and Temboo APIs to send
SMS messages with GPS location data to the user.
In the next chapter, you will learn how to build a garage door light that can be
controlled using Twitter tweets with the combination of Python and Python-Twitter
(a Python wrapper around the Twitter API).

[ 140 ]

Get more information Internet of Things with Arduino Blueprints

Where to buy this book


You can buy Internet of Things with Arduino Blueprints from the
Packt Publishing website.
Alternatively, you can buy the book from Amazon, BN.com, Computer Manuals and most internet
book retailers.
Click here for ordering and shipping details.

www.PacktPub.com

Stay Connected:

You might also like