27,59 €
In this world of technology upgrades, IoT is currently leading with its promise to make the world a more smarter and efficient place.
This book will show you how to build simple IoT solutions that will help you to understand how this technology works. We would not only explore the IoT solution stack, but we will also see how to do it with the world’s most misunderstood programming language - JavaScript. Using Raspberry Pi 3 and JavaScript (ES5/ES6) as the base to build all the projects, you will begin with learning about the fundamentals of IoT and then build a standard framework for developing all the applications covered in this book. You will then move on to build a weather station with temperature, humidity and moisture sensors and further integrate Alexa with it. Further, you will build a smart wearable for understanding the concept of fall detection. You will then extend it with the 'If This Then That' (IFTTT) rules engine to send an email on fall detection. Finally, you will be working with the Raspberry Pi 3 camera module and surveillance with a bit of facial detection using Amazon Rekognition platform.
At the end of the book, you will not only be able to build standalone exciting IoT applications but also learn how you can extend your projects to another level.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 222
Veröffentlichungsjahr: 2017
BIRMINGHAM - MUMBAI
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: December 2017
Production reference: 1211217
ISBN 978-1-78829-294-8
www.packtpub.com
Authors
Arvind Ravulavaru
Copy Editor
Safis Editing
Reviewers
Vijaya Kumar Suda
Project Coordinator
Kinjal Bari
Commissioning Editor
Vijin Boricha
Proofreader
Safis Editing
Acquisition Editor
Reshma Raman
Indexer
Pratik Shirodkar
Content Development Editor
Eisha Dsouza
Graphics
Kirk D'Penha
Technical Editor
Varsha Shivhare
Production Coordinator
Shantanu Zagade
Arvind Ravulavaru is a platform architect at Ubiconn IoT Solutions, with over 9 years of experience in software development and 2 years in hardware and product development. For the last 5 years, he has been working extensively on JavaScript, both on the server side and the client side, and for the last couple of years in IoT, building a platform for rapidly developing IoT solutions, named the IoT Suitcase. Prior to this, he has worked on big data, cloud computing, and orchestration.
Arvind has already written couple of books named Learning Ionic and Learning Ionic - Second Edition, which talks about building Mobile Hybrid applications using Ionic framework v1, v2, and v3.
Vijay Suda has over 17 years of experience in the IT industry. He works as a solution architect for a major consulting firm in the USA, operating in the Java, cloud, IoT, big data, and machine learning spaces. He worked for Tata Consultancy Services and Wipro Technologies in solution architecture, design and development of enterprise level systems with Java/J2EE, and SOA-related technologies. He has experience with various clients in the banking, telecom, and retail domains in Switzerland, Belgium, Mexico, Bahrain, India, and the USA.
He is passionate about implementing AI and machine learning algorithms to make a positive impact on society. He has recently been working on deep learning technologies such as TensorFlow, scikit-learn, and pandas for machine learning.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1788292944.
If you'd like to join our team of regular reviewers, you can email us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
The World of IoT
The world of IoT
What is IoT?
A bit of history
IoT use cases
Technology overview
Product engineering
Summary
IoTFW.js - I
Designing a reference architecture
Architecture
Smart device
Gateway
MQTTS broker
API engine
MongoDB
Web app
Mobile app
Desktop app
Data flow
Smart device to the apps
App to the smart device
Building the reference architecture
Installing Node.js on the server
Installing nodemon
MongoDB
Local installation
Using mLab
MQTTS broker - Mosca
API engine - Node.js and Express
Authorization
MQTT client
API engine testing
Communication between broker and API engine
Raspberry Pi software
Setting up Raspberry Pi
Raspberry Pi MQTTS client
Communication between the broker and the Raspberry Pi
Troubleshooting
Communication between the Raspberry Pi, the broker and the API engine
Web app
Setting up the app
Project structure
App module
Web app services
Web app components
Launching the app
Summary
IoTFW.js - II
Updating the API engine
Integrating web app and API engine
Testing an end-to-end flow using DHT11 and LED
Setting up and updating the Raspberry Pi
Updating the API engine
Updating the web app
Building the desktop app and implementing an end-to-end flow
Building the mobile app and implementing an end-to-end flow
Troubleshooting
Summary
Smart Agriculture
Agriculture and IoT
Designing a smart weather station
Setting up Raspberry Pi 3
Raspberry Pi and MCP3208
Moisture sensor and MCP3208
Raspberry Pi and DHT11
Setting up the API engine
Setting up the web app
Setting up the desktop app
Setting up the mobile app
Summary
Smart Agriculture and Voice AI
Voice AI
Test drive
Building a smart socket
Setting up relay with Raspberry Pi
Managing relay in an API engine
Updating the web app template
Updating the desktop app
Updating the mobile app template
Developing Alexa skill
Creating skill
Training the voice model
ngrok the API engine
Defining the lambda function
Deploying and testing
Summary
Smart Wearable
IoT and healthcare
Smart wearable
Setting up smart wearable
Updating the API engine
Updating the web app
Updating a desktop app
Updating the mobile app template
Summary
Smart Wearable and IFTTT
IFTTT and IoT
Fall detection
Updating Raspberry Pi
Building the IFTTT rules engine
Updating the web app
Updating the desktop app
Updating the mobile app
Summary
Raspberry Pi Image Streaming
MJPEG
Setting up Raspberry Pi
Setting up the camera
Testing the camera
Developing the logic
Updating the API engine
Updating the web app
Updating the desktop app
Updating the mobile app
Motion-based video capture
Updating the Raspberry Pi
Testing the code
Summary
Smart Surveillance
AWS Rekognition
Setting up smart surveillance
Setting up AWS credentials
Seeding the authorized faces
Testing the seed
Deploying to Raspberry Pi
Summary
We are part of a generation where people have already started adapting to IoT products. There is a lot of hype about IoT. This book will focus on building IoT-based applications that will help you to achieve a higher level of understanding when it comes to IoT. It will follow a project-based approach that will teach you to build standalone exciting, applications and will also teach you to extend your project to another level. We are going to use JavaScript as our programming language and Raspberry Pi 3 as our hardware to build interesting IoT solutions.
Chapter 1, The World of IoT, introduces you to the world of IoT. We will be looking at the history of IoT, identifying a few use cases, and getting a technical overview of what were are going to cover in this book.
Chapter 2, IoTFW.js - I, walks you through how to build a reference framework for developing IoT solutions using JavaScript. In this chapter, we cover the high-level architecture and get started with installing the required software. We will start with downloading the base application and stitching the Raspberry Pi together with the MQTTS broker and API engine.
Chapter 3, IoTFW.js - II, continues from where we left off in the previous chapter and completes the implementation of the API engine, web app, desktop app, and mobile app. At the end of this chapter, we implement a simple example with an LED and a temperature sensor, where instructions from the apps will turn the LED on/off and the value of the temperature sensor updates in real time.
Chapter 4, Smart Agriculture, talks about building a simple weather station using the reference architecture we have built. The weather station consists of four sensors, and using these we can monitor farm conditions. We will be making the required changes to the API engine, web app, desktop app, and mobile app.
Chapter 5, Smart Agriculture and Voice AI, shows how we can leverage the power of voice AI technology to build interesting IoT solutions. We are going to work with the smart weather station and add a one-channel mechanical relay to this setup. Then, using voice commands and Amazon Alexa, we are going to manage the weather station.
Chapter 6, Smart Wearable, talks about an interesting use case in the healthcare sector, postoperation patient care. Using a smart wearable device equipped with a simple accelerometer, one can easily detect whether a patient has fallen down. In this chapter, we build the required setup comment to gather the accelerometer values from the sensor.
Chapter 7, Smart Wearable and IFTTT, explains how the data collected from the accelerometer can be used to detect falls and at the same time notify the API engine. Using a popular concept named If This Then That (IFTTT)—we will be building our own rules engine, which will process predefined rules and take action accordingly. In our example, we are going to send an email to the patient's carer if a fall is detected.
Chapter 8, Raspberry Pi Image Streaming, shows how to take advantage of the Raspberry Pi camera module to build a real-time image streaming (MJPEG technology) solution to monitor your surroundings from anywhere in the world. We will also implement motion-based video capture to capture video when motion is detected.
Chapter 9, Smart Surveillance, walks you through the process of image recognition using Amazon's Rekognition platform. We will be capturing an image when motion is detected using the Raspberry Pi 3 camera module. Then, we will send this image to Amazon Rekognition platform to detect whether the image we have taken is of an intruder or of someone we know.
To start building IoT solutions using JavaScript, you need to have the following:
Medium to advanced knowledge of JavaScript – ES5 and ES6
Medium to advanced knowledge of MEAN stack application development
Medium to advanced knowledge of Angular 4
Medium to advanced knowledge of Electron Framework
Medium to advanced knowledge of Ionic Framework 3
Novice to medium knowledge of digital electronic circuits
Novice to medium knowledge of Raspberry Pi
Novice to medium knowledge on sensors and actuators
It is for readers who are already well versed with JavaScript and want to extend their JavaScript knowledge to building hardware solutions in the field of IoT. IoT enthusiasts interested in creating exciting projects will also find this book useful. This book is also useful for readers who are good at developing standalone solutions using Raspberry Pi; this book will help them add IoT capabilities to their existing projects using the world's most misunderstood programming language.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning. Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Now, inside thebrokerfolder, create another folder namedcertsandcdinto that folder." A block of code is set as follows:
// MongoDB connection options mongo: { uri: 'mongodb://admin:[email protected]:41055/iotfwjs' }, mqtt: { host: process.env.EMQTT_HOST || '127.0.0.1', clientId: 'API_Server_Dev', port: 8883 }};
Any command-line input or output is written as follows:
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out certificate.pem
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Once logged in, click on theCreate Newbutton to create a new DB."
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply email [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you. You can download the code files by following these steps:
Log in or register to our website using your email address and password.
Hover the mouse pointer on the
SUPPORT
tab at the top.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on
Code Download
.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Practical-Internet-of-Things-with-JavaScript. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/PracticalInternetofThingswithJavaScript_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
Welcome to advanced IoT with JavaScript. In this book, we will look at building IoT solutions using JavaScript as our programming language. Before we start with the technical deep dive, I would like to talk about the world of IoT, the solutions offered by it, and what responsibilities fall on bestows on us developers who make these products. In this chapter, we will look at the following topics:
The world of IoT
History of IoT
IoT uses cases
Technology overview
Product engineering
Imagine a scenario where you have run out of milk; you have noticed it and put it on your shopping list. But due to unforeseen reasons, you forgot to buy milk; well, you don't have milk for the next day.
Now imagine another scenario: you have a smart fridge, and it noticed that you are running out of milk, puts milk on your shopping list, and then updates your GPS route to come home via the supermarket, but you still forget it.
You have to now face the wrath of your refrigerator.
Now that things are getting real, imagine another situation where your fridge has skipped the middleman, you, and now directly places an order on Amazon, and Amazon delivers it by the time you need your breakfast the next day.
Scenario three is what is we are after. Let one machine talk to another machine and take decisions accordingly; things such as the type of milk, quantity, and expiry date are automatically validated before purchase.
We humans are now using the world of connected devices and smart devices to make our lives better.
If you have been breathing for at least a decade, you must have heard terms such as smart living, smart spaces, and intelligent devices. All these refer to a parent concept called the Internet of Things (IoT).
In simple words, IoT is when we have our electronic, electrical, or electro-mechanical devices connect to the internet and talk to each other.
Smart devices primarily revolve around two things:
Sensors
Actuators
Any solution in the IoT space is either sensing something or actuating something.
With this technology, we have found the solution for Sheldon Cooper (from the Big Bang theory, CBS TV series), where he wants to know who sits on his spot as soon as someone sits on it:
All we do is place a weight sensor underneath the cushion, and if the weight increases, the sensor will trigger the camera pointing at the sofa to take a picture and send a push notification to him with the picture. How about that?
I know I have pushed the examples a bit, but you get the point, right?
IoT has existed in various forms for more than 35 years. The earliest example I found was a Coke machine at Carnegie Mellon University in 1982. Developed by four graduate students, Mike Kazar, David Nichols, John Zsarnay, and Ivor Durham, they hooked up the Coke machine to the internet so that they could check from their desks whether the machine was loaded with cold Coke. Source (https://www.cs.cmu.edu/~coke/).
Sir Timothy John Berners-Lee invented the first webpage in 1991.
Another example is the internet toaster by John Romkey. He connected his toaster to the internet using the TCP/IP protocol. He created one control to turn on the toaster and one control to turn it off. Of course, someone had to put the bread in the toaster:
Another interesting IoT example is the Trojan Room coffee pot. This was created by Quentin Stafford-Fraser and Paul Jardetzky in 1993. A camera was located in the Trojan Room in the computer laboratory of the University of Cambridge. It monitored the coffee pot levels, with an image being updated about three times a minute and sent to the building's server:
As mentioned previously, we can see that even before we could imagine the possibilities, people had already worked on internet-related solutions.
Over the past 2 years, there was one thing that I kept on seeing and started believing strongly:
Not necessity, not boredom, but laziness. In this day and age, nobody wants to do mundane things such as grocery shopping, walking up to a switch, and turning on a light or AC. So, we are searching for new and innovative ways to solve these problems.
Now that you have a feel for IoT, you can imagine the literally infinite possibilities that can be built using this piece of technology.
Based on my observations, IoT use cases can be crudely classified into three parts:
Problem solving
Convenience
Showing off
The problem solving part comes in where IoT is used to solve a real-world problem, for instance, a farmer whose farm is located half a kilometre from their home, and they have to walk all the way to the farm to turn on their water pumps/motors . Another scenario is where a post-operation patient's vital statistics can be sent to the hospital periodically after his/her discharge, to monitor the patient for any abnormalities. This is where IoT fits in pretty well.
Convenience is where you can turn on your air conditioner 30 mins before you reach your home so you can chill as you enter or unlock your door from your work if someone you know knocks at your door and you are not nearby.
showing off is where you go to another country just to turn on or off your porch light, just to show that IoT works.
All of them are forms of consumption of this technology.
In this book, we will look at covering a few solutions that fall into previous use cases.
Now that we know what IoT is, we can start defining the technology stack. In this book, we will build a generic framework using JavaScript for developing IoT applications.
We will follow the approach of cloud computing, where we have a bunch of devices that are connected to the cloud, compared to a fog computing approach, where there is a gateway that can do almost all the things a cloud can but is locally available in the on-premises.
Our smart devices will be powered by Raspberry Pi 3, which has the ability to talk to the cloud over Wi-Fi and also, using its GPIO pins, talk to the sensors and actuators. Using this simple piece of hardware, we will connect sensors and actuators and build some real-world solutions in this book.
Another alternative to Raspberry Pi 3 is Raspberry Pi Zero W, which is a miniature version of Raspberry Pi 3, in case you are looking to build a compact solution.
We will walk through each piece of technology in Chapter 2, IoTFW.js - I and Chapter 3, IoTFW.js - II, and from there on use these technologies to build IoT solutions in various domains.