Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Internet of Things Programming with JavaScript
Internet of Things Programming with JavaScript

Internet of Things Programming with JavaScript: Get the best out of Arduino and Raspberry Pi Zero to develop Internet of Things projects using JavaScript

Arrow left icon
Profile Icon Ruben Oliva Ramos
Arrow right icon
€18.99 per month
Paperback Feb 2017 298 pages 1st Edition
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Ruben Oliva Ramos
Arrow right icon
€18.99 per month
Paperback Feb 2017 298 pages 1st Edition
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Internet of Things Programming with JavaScript

Chapter 1. Getting Started with Raspberry Pi Zero

Before building several projects for a home security system and the control of domestic appliances by electronically controlled systems, in this chapter, we're going to go into an initial configuration and prepare our Raspberry Pi Zero to work in a network, so you can use it for all the projects we will see in this book.

Before we go through the projects, build our network with the devices, and connect our sensor to the boards, it's important to understand the configuration of the Raspberry Pi. The main idea of this chapter is to explain how to set up your Raspberry Pi Zero; we will cover the following topics:

  • Setting up Raspberry Pi Zero
  • Preparing the SD card
  • Installing the Raspbian operating system
  • Configuring your Raspberry Pi Zero with a serial console cable
  • Accessing the network remotely
  • Accessing via remote desktop
  • Configuring a web server

Setting up Raspberry Pi Zero

The Raspberry Pi is a low-cost board dedicated to purpose projects. Here, we will use a Raspberry Pi Zero board. Take a look at the following link:  https://www.adafruit.com/products/2816. I used this board.

In order to make the Raspberry Pi work, we need an operating system that acts as a bridge between the hardware and the user. This book uses the Raspbian Jessy, which can be downloaded from https://www.raspberrypi.org/downloads/. At this link, you will find all of the information you need to download all the pieces of software necessary to use with your Raspberry Pi to deploy Raspbian. You need a micro SD card of at least 4 GB.

The kit that I used to test the Raspberry Pi Zero includes all the necessary things for installing everything and getting the board ready:

Setting up Raspberry Pi Zero

Preparing the SD card

The Raspberry Pi Zero only boots from an SD card and cannot boot from an external drive or USB stick. For this book, it's recommended to use a 4 GB micro SD card.

Installing the Raspbian operating system

There are many operating systems that are available for the Raspberry Pi board, most of which are based on Linux. However, the one that is usually recommended is Raspbian, is an operating system based on Debian, which was specifically made for Raspberry Pi.

In order to install the Raspbian operating system on your Pi, follow the next steps:

  1. Download the latest Raspbian image from the official Raspberry Pi website: https://www.raspberrypi.org/downloads/raspbian/ 
  2. Next, insert the micro SD card into your computer using an adapter. (An adapter is usually given with the SD card.)
  3. Then download Win32DiskImager from https://sourceforge.net/projects/win32diskimager/.

    You will see the following files, as shown in the screenshot, after downloading the folder:

    Installing the Raspbian operating system
  4. Open the file image, select the path where you have the micro SD card, and click on the Write button.
  5. After a few seconds,you have Raspbian installed on your SD card; insert it into Raspberry Pi and connect the Raspberry Pi board to the power source via the micro-USB port.

    In the following screenshot, you can see the progress of the installation:

    Installing the Raspbian operating system

Debugging your Raspberry Pi Zero with a serial console cable

In this section, we will look at how to communicate, the Raspberry Pi Zero from a computer using a TTL serial converter. We can do this debugging with a serial console cable connected to the computer using the USB port. We communicate with the board with a serial cable, because if we want to send commands from our computer to the board, it's necessary to communicate using this cable. You can find the cable at https://www.adafruit.com/products/954:

Debugging your Raspberry Pi Zero with a serial console cable

It’s important to consider that the cable uses 3.3 volts, but we don’t care because we’re using the cable from Adafruit. It is tested to work at this level of voltage.

You need to follow the next steps in order to install and communicate with your Raspberry Pi Zero:

  1. It's necessary that you have a free USB port on your computer.
  2. We need to install the driver for the serial console cable so that the system can recognize the hardware. We recommend that you download the driver from https://www.adafruit.com/images/product-files/954/PL2303_Prolific_DriverInstaller_v1_12_0.zip.
  3. We use an interface (console software), called PuTTY, running on a Windows computer; so we can communicate with our board, the Raspberry Pi. This software can be downloaded and installed from http://www.putty.org/.
  4. For the connections, we need to connect the red cable to  5 volts, the black cable to ground, the white cable to the TXD pin, and the green cable to the RXD pin on the Raspberry Pi Zero.
  5. The other side of the cable connects the plug to the USB port.

This is an image of the connections; it's for the hardware configuration:

Debugging your Raspberry Pi Zero with a serial console cable

Testing and accessing the serial COM interface

Once the driver is installed, we have here the Port COM, which is already installed:

Tip

This configuration is for Windows installation; if you have different operating system, you need to do different steps.

How get the Device Manager screen: On your windows PC, click on the Start icon, go to Control Panel, select System, and then click on Device Manager.

In the following screenshot, you can see the device manager of the USB serial port:

Testing and accessing the serial COM interface
  1. Open the terminal in PuTTY, and select Serial Communication as COM3, Speed as 115200, Parity as None, and Flow Control as None; click on Open:
    Testing and accessing the serial COM interface
  2. When the blank screen appears, press Enter on your keyboard:
    Testing and accessing the serial COM interface
  3. This initiates a connection to your Pi board and asks for your username and password; you will see a screen like the following screenshot, with the authentication login:
    Testing and accessing the serial COM interface
  4. The default username for the Raspberry Pi Zero is pi, and the password is raspberry:
    Testing and accessing the serial COM interface

Connecting to the home network and accessing remotely

Our Raspberry Pi will be working in a real network, so it needs to be set up to work with all the devices that will be together. For this reason, we need to configure our home network. We will show you how to use the Ethernet adapter and the Wi-Fi plug that can be used in the Raspberry Pi Zero.

Connecting with an Ethernet adapter

If you want to connect our Raspberry Pi Zero to the local network, you need to use a USB OTG Host Cable - MicroB OTG male to female from Adafruit. You can find it here: https://www.adafruit.com/products/1099. The board that we're using doesn't have an Ethernet connector, so it's necessary to use it to communicate with the devices from outside.

In the following image, we can see the Ethernet adapter connected to the Raspberry Pi Zero:

Connecting with an Ethernet adapter

This is the connector that you can use to connect your Ethernet adapter and make a link to the network:

Connecting with an Ethernet adapter

Now we need to follow the next steps to configure the Ethernet connection adapter:

  1. Connect your adapter to the converter; I used a TRENDnet NETAdapter, but you can use an Ethernet Hub and USB Hub with Micro USB OTG Connector from Adafruit. You can find it here: https://www.adafruit.com/products/2992m. This is a hub and can be connected to the Ethernet cable or USB devices.
  2. Verify the router configuration, and after both LEDs start blinking, you can see the IP address in your configuration. The DHCP server assigns the IP address to the Raspberry Pi.

This is what you will see as your router configuration on your hostname raspberrypi:

Connecting with an Ethernet adapter

Accessing the Raspberry Pi Zero via SSH

As we know the IP address that our Raspberry Pi has, we will access to it using the PuTTY terminal as we can see in the following screenshot. You need to enter the IP address, and the port is 22 by default; click on the Open button:

Accessing the Raspberry Pi Zero via SSH

After that, we have the login screen as follows:

Accessing the Raspberry Pi Zero via SSH

Use the following command:

sudo ifconfig -a

We can now see the information about the configuration of the Ethernet controller adapter. Eth0 is the Ethernet adapter:

Accessing the Raspberry Pi Zero via SSH

Connecting to the Wi-Fi network

In this section, we will show you how to configure your Wi-Fi network connection so that your Raspberry Pi Zero can interact with your Wi-Fi network. First, we need to connect the Miniature Wi-Fi (802.11b/g/n) Wi-Fi dongle to the Raspberry Pi using the USB OTG Cable:

Connecting to the Wi-Fi network

How to install the wireless tools

Use the following command to configure the wireless network:

sudo apt-get install wireless-tools

In the following screenshot, we can see the result of the ifconfig command:

How to install the wireless tools

After executing the command, we will see the result of installing wireless-tools:

How to install the wireless tools

Configuring IP address and wireless network

To have a networking configuration, we need to assign an IP address to our device in order to be involved in the network.

Enter the following command:

sudo nano etc/network/interfaces
Configuring IP address and wireless network

In the following configuration file, called interface, we explain what we need to add to the file so that we can connect our Raspberry Pi Zero to the Wi-Fi network for the Wlan0 connection.

We start the file configuration; it means the beginning of the file:

auto lo 

We configure the Ethernet device loopback for the local host and start up the DHCP server:

iface lo inet loopback 
iface eth0 inet dhcp 

Allow the configuration of the wlan0 for Wi-Fi connection:

allow-hotplug wlan0 
auto wlan0

We start up the DHCP server for the Wi-Fi connection and input the name of your ssid and the password. We need to type ssid and password parameters of your Wi-Fi network:

iface wlan0 inet dhcp 
        wpa-ssid "ssid" 
        wpa-psk "password" 

Testing the communication

We need to test whether the device is responding to the other host. Now, if everything is configured well, we can see the following IP address in the Wi-Fi connection:

Testing the communication

We can see in the router configuration the current IP address that is assigned to the wireless network:

Testing the communication

Ping from a computer

Connect the computer to the same network as the Raspberry Pi:

Ping from a computer

You need to ping the IP address of the Raspberry Pi. After we make the ping to the IP Address of the Raspberry Pi Wireless connection, we see the results:

Ping from a computer

Updating the package repository

This will upgrade your Pi board by downloading all the latest packages from the official Raspberry Pi repository, so it's a great way to make sure that your board is connected to the Internet. Then, from your computer, type the following: 

sudo apt-get update

The following screenshot show the Raspberry Pi collecting the packages data:

Updating the package repository

Here we have results after the installation is finished:

Updating the package repository

Remote Desktop

In this section, we need the RDP package with the Raspbian Operating System. To do that, first we need to execute the following command:

sudo apt-get install xrdp 

This command executes and installs the RDP process and updates the package:

Remote Desktop

Remote Desktop with Windows

At the end of this chapter, you want to be able to access the board from your own computer using Remote Desktop; you need to type the IP address of your Raspberry Pi and click on the Connect button:

Remote Desktop with Windows

After we type the IP address of the Raspberry Pi Zero, we will see the following screen; it's necessary to write your username and password:

Remote Desktop with Windows

You need the login information of your Raspberry Pi, username, and password:

Remote Desktop with Windows

This is the main window of the Operating System; you have correctly accessed your Raspberry Pi Remote Access with Remote Desktop:

Remote Desktop with Windows

Configuring a web server

There are several web servers available that we can install on your Raspberry Pi. We're going to install the lighttpd web server. Also, we need to install PHP support, which will help us run a website into our Raspberry Pi and have dynamic web pages.

To install and configure, log in to the Raspberry Pi via the terminal console of PuTTY:

  1. Update the package installer:
            sudo apt-get update
    
  2. Install the lighttpd web server:
            sudo apt-get install lighttpd
    

Once installed, it will automatically start up as a background service; it will do so each time the Raspberry Pi starts up:

  1. To set up our PHP 5 interface for programming with PHP 5, we need to install the PHP5 module support with the following command; this is necessary to have our server, and it can execute PHP files so that we can make our website:
            sudo apt-get install php5-cgi
    
  2. Now we need to enable the PHP FastCGI module on our web server:
            sudo lighty-enable-mod fastcgi-php
    
  3. For the last step, we have to restart the server with the following command:
            sudo /etc/init.d/lighttpd
    

In the following screenshot, we show the content of the page that will to appear when we configure the web server and the PHP 5 interface. The web server installs a test placeholder page in the location /var/www. Type the IP address of your Raspberry Pi in the browser, for example, http://192.168.1.105/, and the following screen appears, opening the active page of the configured server:

Configuring a web server

Testing the PHP installation

At this point, we need to test our website with PHP. This can be done by writing a simple PHP script page. If PHP is installed correctly, it will return information about its environment and configuration.

  1. Go to the next folder, where it's the root document:
            cd /var/www/html 
    
    
  2. Create a file called phpinfo.php.

    We use the word nano so that we can get into the file of the system with the privileges and execute the following command:

                sudo nano phpinfo.php
    
  3. After creating the file, as given in the following screenshot, press CTRL-X, and then save the file:
    Testing the PHP installation
  4. In your browser, enter the IP address of your Raspberry Pi, for example, http://192.168.1.105/phpinfo.php, and you should see the following screen:
    Testing the PHP installation

Summary

In the first chapter of this book, we looked at how to configure our Raspberry Pi Zero board so we can use it in later chapters. We looked at what components were needed for the Pi, and how to install Raspbian so we can run the software on our board.

We also installed a web server, which we will be using in some projects of the book. In the following chapter, we are going to dive into how to connect devices to your Raspberry Pi and Arduino boards. We'll also look at the various things we can connect to the Raspberry Pi using GPIO.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • • This is a practical guide to help you configure and build a complete distributed IoT system from scratch using JavaScript
  • • Utilize the power of Node and HTML5 to develop web services and a centralized web server, enabling high-level communication between connected devices
  • • Control all your connected devices from the browser by setting up a common dashboard

Description

The Internet of Things is taking the tech world by storm, and JavaScript is at its helm. This book will get you to grips with this exciting new technology. Where do Node.js, HTML5 and Windows 10 IoT Core come in with JavaScript and IoT? Why Raspberry Pi Zero rather than Arduino? How do you configure and build an IoT network from scratch? All your IoT JavaScript questions are answered in this book.

Who is this book for?

This book is for developers who are interested in learning how to communicate with connected devices in JavaScript to set up an IoT system. Some basic knowledge of JavaScript is expected. Hobbyists who want to explore the potential of IoT in JavaScript will also find this book useful.

What you will learn

  • With this book, you?ll learn about: ? Setting up a centralized web server to connect IoT devices ? Connecting sensors and actuators to devices ? Sending data to web server connected devices ? Understanding Internet of Things using web services and database ? Configuring dashboards using HTML5 and JavaScript ? Controlling and monitoring devices connected from dashboards ? Building apps for smartphones to control different devices

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 17, 2017
Length: 298 pages
Edition : 1st
Language : English
ISBN-13 : 9781785888564
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Feb 17, 2017
Length: 298 pages
Edition : 1st
Language : English
ISBN-13 : 9781785888564
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 95.97
Smart Internet of Things Projects
€32.99
Internet of Things Programming with JavaScript
€32.99
Practical Internet of Things with JavaScript
€29.99
Total 95.97 Stars icon
Banner background image

Table of Contents

9 Chapters
1. Getting Started with Raspberry Pi Zero Chevron down icon Chevron up icon
2. Connecting Things to the Raspberry Pi Zero Chevron down icon Chevron up icon
3. Connecting Sensors - Measure the Real Things Chevron down icon Chevron up icon
4. Control-Connected Devices Chevron down icon Chevron up icon
5. Adding a Webcam to Monitor Your Security System Chevron down icon Chevron up icon
6. Building a Web Monitor and Controlling Devices from a Dashboard Chevron down icon Chevron up icon
7. Building a Spy Police with the Internet of Things Dashboard Chevron down icon Chevron up icon
8. Monitoring and Controlling Your Devices from a Smart Phone Chevron down icon Chevron up icon
9. Putting It All Together Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.