Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
ESP8266 Home Automation Projects
ESP8266 Home Automation Projects

ESP8266 Home Automation Projects: Leverage the power of this tiny WiFi chip to build exciting smart home projects

Arrow left icon
Profile Icon Batrinu Profile Icon Tambrea
Arrow right icon
€15.99 €23.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (4 Ratings)
eBook Nov 2017 196 pages 1st Edition
eBook
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Batrinu Profile Icon Tambrea
Arrow right icon
€15.99 €23.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (4 Ratings)
eBook Nov 2017 196 pages 1st Edition
eBook
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

ESP8266 Home Automation Projects

Building and Configuring Your Own MQTT Server

Now that you have learned how to use analog and digital outputs, to send and receive data via Wi-Fi, it is time to move on the next part where you will discover the MQTT protocol and how to use it.

In the previous chapter the ESP8266 was only communicating with servers: in this chapter, we will see how ESP8266 modules can communicate with each other through Message Queue Telemetry Transport (MQTT).

Message Queue Telemetry Transport

Usually, end devices have a limited memory and CPU power, running on batteries, so connecting them with servers requires a light protocol. Enter MQTT, which was invented in 1999 by Andy Stanford-Clark from IBM and Arlen Nipper from Arcom, a SCADA protocol design initially for battery operated devices to supervise oil pipelines. Later in 2010, IBM released it as a royalty-free protocol. In 2014, OASIS announced that the MQTT v.3.1.1 had become an OASIS standard and a lot of MQTT clients were developed for all programming languages.

The characteristics of MQTT are listed as follows:

  • Data agnostic: MQTT can transport all kind of data, from sensor data to images or over the air updates
  • Lightweight and bandwidth efficient: Smallest frame is only 2 bytes long
  • Provide QoS: Three Quality of Service (QoS) levels
  • Runs on top of the TCP/IP stack
  • Simple...

Introducing Mosquitto broker

Eclipse MosquittoTM is an open source MQTT broker that implements the MQTT v3.1 and MQTT v.3.1.1 standards and provides a lightweight method to transport messages, allowing publish and subscription for low power sensors, mobile devices, embedded computers, and micro controllers.

You can install Mosquitto on a Raspberry Pi or on AWS instance or on a VirtualBox Linux instance directly from your Linux repository distribution; or you can get the source code and compile it yourself if you want support from websockets.

Installing from your Linux distribution repository:

  1. First upgrade to the latest version:
    sudo apt update && sudo apt upgrade
  1. Then install mosquito:
    sudo apt install mosquitto  

You should see the following screen:

Installing Mosquitto
  1. After installing the Mosquitto broker verify that the broker is started and install...

ESP8266 and MQTT

To use the ESP8266 as a client for sending data to a broker you will need a library that offers MQTT support. For this you can use the PubSubClient library, which can be installed like other libraries; see Chapter 1, Getting Started with the ESP8266:

  1. Go to Sketch | Include Library | Manage Libraries..., as follows:
Manage libraries
  1. And search for the PubSubClient library and click Install as in the following screenshot:

Publishing data from the ESP8266

Start a new sketch via File | New in the Arduino IDE and paste in the following code. Include the ESP8266WiFi library and the PubSubClient one:

#include <ESP8266WiFi.h> 
#include <PubSubClient.h> 

Update these with values suitable for your network. Use ifconfig to get the IP address of the server where the Mosquitto broker is installed. If your server has an FQDN name such as myiotserver.com and is registered into the DNS, then instead of the IP address in mqtt_server you can use the FQDN name:

const char* wifi_network = "YOUR_WIFI_SSID"; 
const char* wifi_pass = "YOUR_WIFI_PASSWORD"; 
const char* mqtt_serv_address = "192.168.1.116"; 
const int mqtt_port_number = 1883; 

Instantiate a WiFiClient and pass it to the PubSubClient:

WiFiClient espClient; 
PubSubClient client(espClient); 
long lastMsg...

Receiving MQTT messages in the ESP8266

Now let's publish a message using mosquitto_pub and receive it in the ESP8266.

For this the ESP8266 needs to subscribe to the same topic on which mosquitto_pub will publish the message. Let's call the topic outdoor/light and it will publish on 0 or 1 values. If the ESP8266 receives the value as 1, it will turn on a LED connected to GPIO 12 and if it will receives a 0, it will turn off that LED:

#include <ESP8266WiFi.h> 
#include <PubSubClient.h> 

Update these with values suitable for your network:

 
const char* wifi_network= "YOUR_WIFI_SSID"; 
const char* password = "YOUR_WIFI_PASSWORD"; 
const char* mqtt_serv_address = "YOUR_MQTT_SERVER_IP"; 
const int mqtt_port_number = 1883; 
#define OUTDOOR_LIGHT  12 
 
WiFiClient  espClient; 
PubsubClient client(espClient); 
long lastMsg...

Message Queue Telemetry Transport


Usually, end devices have a limited memory and CPU power, running on batteries, so connecting them with servers requires a light protocol. Enter MQTT, which was invented in 1999 by Andy Stanford-Clark from IBM and Arlen Nipper from Arcom, a SCADA protocol design initially for battery operated devices to supervise oil pipelines. Later in 2010, IBM released it as a royalty-free protocol. In 2014, OASIS announced that the MQTT v.3.1.1 had become an OASIS standard and a lot of MQTT clients were developed for all programming languages.

The characteristics of MQTT are listed as follows:

  • Data agnostic: MQTT can transport all kind of data, from sensor data to images or over the air updates
  • Lightweight and bandwidth efficient: Smallest frame is only 2 bytes long
  • Provide QoS: Three Quality of Service (QoS) levels
  • Runs on top of the TCP/IP stack
  • Simple to develop: Clients exist for all operating systems and programming languages
  • Central broker: Can connect different devices...

Introducing Mosquitto broker


Eclipse MosquittoTM is an open source MQTT broker that implements the MQTT v3.1 and MQTT v.3.1.1 standards and provides a lightweight method to transport messages, allowing publish and subscription for low power sensors, mobile devices, embedded computers, and micro controllers.

You can install Mosquitto on a Raspberry Pi or on AWS instance or on a VirtualBox Linux instance directly from your Linux repository distribution; or you can get the source code and compile it yourself if you want support from websockets.

Installing from your Linux distribution repository:

  1. First upgrade to the latest version:
    sudo apt update && sudo apt upgrade
  1. Then install mosquito:
sudo apt install mosquitto

You should see the following screen:

Installing Mosquitto

  1. After installing the Mosquitto broker verify that the broker is started and install mosquitto-clients as follows:

Verify if Mosquitto is running

  1. Type the following command:
sudo apt install mosquitto-clients

You will get the...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Harness the power of the ESP8266 Wi-Fi chip to build an effective Home Automation System
  • Learn about the various ESP8266 modules
  • Configuring the ESP8266 and making interesting home automation projects
  • A step-by-step guide on the ESP8266 chip and how to convert your home into a smart home.

Description

The ESP8266 is a low-cost yet powerful Wi-Fi chip that is becoming more popular at an alarming rate, and people have adopted it to create interesting projects. With this book, you will learn to create and program home automation projects using the ESP8266 Wi-Fi chip. You will learn how to build a thermostat to measure and adjust the temperature accordingly and how to build a security system using the ESP8266. Furthermore, you will design a complete home automation system from sensor to your own cloud. You will touch base on data monitoring, controlling appliances, and security aspects. By the end of the book, you will understand how to completely control and monitor your home from the cloud and from a mobile application. You will be familiar with the capabilities of the ESP8266 and will have successfully designed a complete ready-to-sell home automated system.

Who is this book for?

This book is targeted at people who want to build connected and inexpensive home automation projects using the ESP8266 Wi-Fi chip, and to completely automate their homes. A basic understanding of the board would be an added advantage.

What you will learn

  • Get, compile, install, and configure an MQTT server
  • Use the Wi-Fi connectivity feature to control appliances remotely
  • Control several home appliances using the ESP8266 Wi-Fi chip
  • Control and monitor your home from the cloud using ESP8266 modules
  • Stream real-time data from the ESP8266 to a server over WebSockets
  • Create an Android mobile application for your project

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 28, 2017
Length: 196 pages
Edition : 1st
Language : English
ISBN-13 : 9781787285385
Category :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Nov 28, 2017
Length: 196 pages
Edition : 1st
Language : English
ISBN-13 : 9781787285385
Category :
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 87.97
ESP8266 Robotics Projects
€24.99
ESP8266 Home Automation Projects
€29.99
ESP8266 Internet of Things Cookbook
€32.99
Total 87.97 Stars icon

Table of Contents

8 Chapters
Getting Started with the ESP8266 Chevron down icon Chevron up icon
Building and Configuring Your Own MQTT Server Chevron down icon Chevron up icon
Building a Home Thermostat with the ESP8266 Chevron down icon Chevron up icon
Control Appliances from the ESP8266 Chevron down icon Chevron up icon
Using ESP8266 to Build a Security System Chevron down icon Chevron up icon
Securing Your Data Chevron down icon Chevron up icon
Real-Time Communication Chevron down icon Chevron up icon
Adding a Mobile Application to Your Smart Home Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(4 Ratings)
5 star 50%
4 star 0%
3 star 50%
2 star 0%
1 star 0%
Amazon Customer Feb 11, 2020
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
I don't often write reviews on books, but you probably need a heads up. I'm only at chapter 2 and the previous labs are out of date. It references APIs on a wunderground that are completely different and would take a lot of effort to reverse engineer them. The Thingspeak lab is also obsolete as that API is using https and not http which means change in the how the wifi library is used. I managed to get that one working with a bit of googling. I think its a good reference but don't expect all the labs to work. Like I said I'm only at chapter 2 and half the labs no longer work. The author has not bothered to create an amendment on the packtpub website to update the labs, only a minor typo correction.
Amazon Verified review Amazon
Frank J DeCaro Sep 14, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good beginner book good examples
Amazon Verified review Amazon
Angel Aug 29, 2019
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
It is a book that helps quickly to start in the programming of ESP8266 chps, in order to DIY IoT devices with WiFi connection. The downside of the Kindle edition (I do not know for paperback), when reading directly on the computer, is that many pages are badly formatted. The opening and closing curly brackets are not aligned and the code is not indented to the right. Sometimes the code is formatted in very narrow columns, so that each sentence is broken into 3 or more lines, which makes it very difficult to follow.I have some screenshots, to prove it.
Amazon Verified review Amazon
Toth Andras Feb 12, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great book for ESP8266 beginners and pro's too ! The Security chapter and ESP8266 MQTT broker sections are really surprizing!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.