31,19 €
The ESP8266 Wi-Fi Module is a self contained System on Chip (SOC) with an integrated TCP/IP protocol stack and can give any microcontroller access to your Wi-Fi network. It is capable of either hosting an application or offloading all Wi-Fi networking functions from another application processor.
This book contains practical recipes that will help you master all ESP8266 functionalities. You will start by configuring and customizing the chip in line with your requirements. Then you will focus on core topics such as on-board processing, sensors, GPIOs, programming, networking, integration with external components, and so on. We will also teach you how to leverage Arduino using the ESP8266 and you'll learn about its libraries, file system, OTA updates, and so on. The book also provide recipes on web servers, testing, connecting with the cloud, and troubleshooting techniques. Programming aspects include MicroPython and how to leverage it to get started with the ESP8266. Towards the end, we will use these concepts and create an interesting project (IOT).
By the end of the book, readers will be proficient enough to use the ESP8266 board efficiently.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 279
Veröffentlichungsjahr: 2017
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, 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: April 2017
Production reference: 1240417
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78728-810-2
www.packtpub.com
Author
Marco Schwartz
Reviewer
Catalin Batrinu
Acquisition Editor
Prachi Bisht
Content Development Editor
Trusha Shriyan
Technical Editor
Varsha Shivhare
Copy Editor
Safis Editing
Project Coordinator
Kinjal Bari
Proofreader
Safis Editing
Indexer
Francy Puthiry
Graphics
Kirk D'Penha
Production Coordinator
Nilesh Mohite
Cover Work
Nilesh Mohite
Marco Schwartz is an electrical engineer, entrepreneur, and blogger. He has a master's degree in electrical engineering and computer science from Supélec, France, and a master's degree in micro engineering from the Ecole Polytechnique Fédérale de Lausanne (EPFL), Switzerland.
He has more than five years of experience working in the domain of electrical engineering. Marco's interests center around electronics, home automation, the Arduino and Raspberry Pi platforms, open source hardware projects, and 3D printing. He has several websites about the Arduino, including the open home automation website, which is dedicated to building home automation systems using open source hardware. Marco has written another book on home automation and the Arduino, called Arduino Home Automation Projects. He has also written a book on how to build Internet of Things projects with the Arduino, called Internet of Things with the Arduino Yun, by Packt Publishing.
Catalin Batrinu graduated from the Politehnica University of Bucharest in Electronics, Telecommunications, and Information Technology. He has worked as a software developer in telecommunications for the past 16 years.
He started working with old protocols to the latest network protocols and technologies so he caught all the transformations in telecommunication industry.
He has implemented many telecommunication protocols, from access adaptations and backbone switches to high capacity carrier-grade switches on various hardware platforms, such as Wintegra and Broadcom.
Internet of Things came as a natural evolution for him, and now he collaborates with different companies to construct the world of the future that will make our life more comfortable and secure.
Using ESP8266, he has prototyped devices such as irrigation controller, smart sockets, window shutters, lighting control using Digital Addressable Lighting Control (DALC), and environment control, all of them being controlled directly from a mobile application over the cloud. Even an MQTT broker with a bridging and web socket server has been developed for the ESP8266. Soon, all these devices will be part of our daily life, so we will all enjoy their functionality.
You can read his blog at http://myesp8266.blogspot.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.
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/1787288102.
If you'd like to join our team of regular reviewers, you can e-mail 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!
The ESP8266 chip is a powerful and cheap microcontroller with an onboard Wi-Fi connection. It is also very easy to use, thanks to the compatibility with the Arduino IDE. Therefore, it's just the perfect chip to build the Internet of Things (IoT) projects.
Inside this book, we'll see how to build IoT projects using the ESP8266, via several step-by-step tutorials. At the end, you will know how to use all the functions of the ESP8266, and you will be able to build your own projects with this amazing Wi-Fi chip.
Chapter 1, Configuring the ESP8266, will be about getting started with the ESP8266, and learning how to configure the ESP8266 and all the hardware/software components that you need to use it.
Chapter 2, Your First ESP8266 Projects, will be about learning how to make your first simple projects with the ESP8266.
Chapter 3, More ESP8266 Functions, will be about learning advanced functions of the ESP8266, as using the file storage system.
Chapter 4, Using MicroPython on the ESP8266, will be focused on using the powerful & simple MicroPython language to build projects with the ESP8266.
Chapter 5, Cloud Data Monitoring, will be about connecting your ESP8266 to the cloud, in order to monitor your projects from anywhere in the world.
Chapter 6, Interacting with Web Services, will exploit the Wi-Fi connectivity of the ESP8266 to connect to existing Web services, such as IFTTT or Google Drive.
Chapter 7, Machine to Machine Interactions, will be about building projects where ESP8266 boards communicate directly with each other via the cloud.
To build the projects you will find in this book, any experience with programming and/or electronics is of course appreciated, but you will be able to follow even if you have little experience in the field, as we will start from the absolute basics of the ESP8266.
This book is for people who want to build their own Internet of Things projects, using the ESP8266 as the platform to easily build those projects.
It is also for people already building IoT projects, for example with Arduino, and those who want to discover another platform to build IoT projects.
In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows:
This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
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 e-mail <[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 e-mailed directly to you.
You can download the code files by following these steps:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/ESP8266-Internet-of-Things-Cookbook. 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 http://www.packtpub.com/sites/default/files/downloads/ESP8266InternetofThingsCookbook_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.
In this chapter, we will cover:
This being the first chapter, we will be looking at how to get started with configuring the ESP8266 to connect to the Internet. This chapter will be the stepping stone to the much more fun and exciting projects that are in this book. Therefore, follow the instructions provided carefully.
In this chapter, you will learn how to set up the Arduino IDE and upload sketches to the ESP8266. You will also be guided on how to choose an ESP8266 module for your project and how to use the ESP8266 to connect to Wi-Fi networks and the Internet.
To start us off, we will look at how to set up an Arduino IDE development environment so that we can use it to program the ESP8266. This will involve installing the Arduino IDE and getting the board definitions for our ESP8266 module.
The first thing you should do is download the Arduino IDE if you do not already have it installed on your computer. You can do that from this link:
https://www.arduino.cc/en/Main/Software.
The web page will appear as shown. It features that latest version of the Arduino IDE. Select your operating system and download the latest version that is available when you access the link (it was 1.6.13 at the time of writing):
When the download is complete, install the Arduino IDE and run it on your computer.
Now that the installation is complete, it is time to get the ESP8266 definitions. Open the preference window in the Arduino IDE from File|Preferences or by pressing CTRL+Comma.
Copy this URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json.
Paste it in the file labeled AdditionalBoardManagerURLs, as shown in the following screenshot. If you are adding other URLs too, use a comma to separate them:
Open the board manager from the Tools|Boardmenu and install the ESP8266 platform. The board manager will download the board definition files from the link provided in the preferences window and install them. When the installation is complete, the ESP8266 board definitions should appear as shown in the screenshot. Now you can select your ESP8266 board from the Tools|Boardmenu:
The Arduino IDE is an open source development environment used for programming Arduino boards and Arduino-based boards. It is also used to upload sketches to other open source boards, such as the ESP8266. This makes it an important accessory when creating Internet of Things (IoT) projects.
These are the basics for the Arduino framework and they will be applied throughout this book to develop IoT projects.
The ESP8266 module is a self-contained System On Chip (SOC), which features an integrated TCP/IP protocol stack that allows you to add Wi-Fi capability to your projects. The module is usually mounted on circuit boards that break out the pins of the ESP8266 chip, making it easy for you to program the chip and to interface with input and output devices.
ESP8266 boards come in different forms, depending on the company that manufactures them. All the boards use Espressif's ESP8266 chip as the main controller, but they have different additional components and different pin configurations, giving each board unique additional features.
Therefore, before embarking on your IoT project, take some time to compare and contrast the different types of ESP8266 boards that are available. This way, you will be able to select the board that has features best suited for your project.
The simple ESP8266-01 module is the most basic ESP8266 board available on the market. It has eight pins, which include four General Purpose Input/Output (GPIO) pins, serial communication TX and RX pins, an enable pin and power pins, and VCC and GND. Since it only has four GPIO pins, you can only connect three inputs or outputs to it.
The 8-pin header on the ESP8266-01 module has a 2.0 mm spacing that is not compatible with breadboards. Therefore, you have to look for another way to connect the ESP8266-01 module to your setup when prototyping. You can use female to male jumper wires to do that:
The ESP8266-07 is an improved version of the ESP8266-01 module. It has 16 pins, which consist of nine GPIO pins, serial communication TX and RX pins, a reset pin, an enable pin and power pins, and VCC and GND. One of the GPIO pins can be used as an analog input pin. The board also comes with a UFL. connector that you can use to plug an external antenna in case you need to boost Wi-Fi signal.
Since the ESP8266 has more GPIO pins, you can have more inputs and outputs in your project. Moreover, it supports both SPI and I2C interfaces, which can come in handy if you want to use sensors or actuators that communicate using any of those protocols. Programming the board requires the use of an external FTDI breakout board based on USB to serial converters, such as the FT232RL chip.
The pads/pinholes of the ESP8266-07 have a 2.0 mm spacing, which is not breadboard- friendly. To solve this, you have to acquire a plate holder that breaks out the ESP8266-07 pins to a breadboard-compatible pin configuration, with 2.54 mm spacing between the pins. This will make prototyping easier.
This board has to be powered from a 3.3V, which is the operating voltage for the ESP8266 chip:
The Olimex ESP8266 module is a breadboard-compatible board that features the ESP8266 chip. As with the ESP8266-07 board, it has SPI, I2C, serial UART, and GPIO interface pins. In addition to that, it also comes with a SecureDigitalInput/Output (SDIO) interface, which is ideal for communication with an SD card. This adds six extra pins to the configuration, bringing the total to 22 pins.
Since the board does not have an on-board USB to serial converter, you have to program it using an FTDI breakout board or a similar USB to serial board/cable. Moreover, it has to be powered from a 3.3V source, which is the recommended voltage for the ESP8266 chip:
The SparkfunESP8266 Thing is a development board for the ESP8266 Wi-Fi SOC. It has 20 pins that are breadboard-friendly, which makes prototyping easy. It features SPI, I2C, serial UART, and GPIO interface pins, enabling it to be interfaced with many input and output devices. There are eight GPIO pins, including the I2C interface pins.
The board has a 3.3V voltage regulator, which allows it to be powered from sources that provide more than 3.3V. It can be powered using a micro USB cable or Li-Po battery. The USB cable also charges the attached Li-Po battery, thanks to the Li-Po battery charging circuit on the board.
Programming has to be done via an external FTDI board:
The Adafruit Huzzah ESP8266 is a fully standalone ESP8266 board. It has a built-in USB to serial interface that eliminates the need for using an external FTDI breakout board to program it. Moreover, it has an integrated battery charging circuit that charges any connected Li-Po battery when the USB cable is connected. There is also a 3.3V voltage regulator on the board that allows the board to be powered with more than 3.3V.
Though there are 28 breadboard friendly pins on the board, only 22 are useable. Ten of those pins are GPIO pins and they can also be used for SPI as well as I2C interfacing. One of the GPIO pins is an analog pin:
All the ESP8266 boards will add Wi-Fi connectivity to your project. However, some of them lack important features and are difficult to work with. So the best option would be to use the module that has the most features and is easy to work with. The Adafruit ESP8266 fits the bill.
The Adafruit ESP8266 is completely standalone and easy to power, program, and configure due to its on-board features. Moreover, it offers many input/output pins that will enable you to add more features to your projects. It is affordable and small enough to fit in projects with limited space.
Wi-Fi isn't the only technology that we can use to connect our projects to the Internet. There are other options such as Ethernet and 3G/LTE. There are shields and breakout boards that can be used to add these features to open source projects. You can explore these other options and see which works for you.
Now that we have chosen the board to use in our project, you can proceed to the next step, which is understanding all the components we will use with Adafruit ESP8266 in this book.
To demonstrate how the ESP8266 works, we will use some additional components. These components will help us learn how to read sensor inputs and control actuators using GPIO pins. Through this you can post sensor data to the Internet and control actuators from Internet resources, such as websites.
The components we will use include:
Let's discuss the three sensors that we will be using.
The DHT11 is a digital temperature and humidity sensor. It uses a thermistor and capacitive humidity sensor to monitor the humidity and temperature of the surrounding air, and produces a digital signal on the data pin. A digital pin on the ESP8266 can be used to read the data from the sensor data pin.
The DHT11 sensor is not very precise, but it is perfect for experimenting, which we'll be doing in this book.
A photocell is a light sensor that changes its resistance depending on the amount of incident light it is exposed to. They can be used in a voltage divider setup to detect the amount of light in the surroundings. In a setup where the photocell is used in the VCC side of the voltage divider, the output of the voltage divider goes high when the light is bright and low when the light is dim. The output of the voltage divider is connected to an analog input pin and the voltage readings can be read:
The soil humidity sensor is used for measuring the amount of moisture in soil and other similar materials. It has two large exposed pads that act as a variable resistor. If there is more moisture in the soil, the resistance between the pads drops, leading to a higher output signal. The output signal is connected to an analog pin from where its value is read.
This sensor is mainly used for demonstration purposes, but it is perfect for the projects we'll do in this book.
Let's discuss the actuators.
A relay is a switch that is operated electrically. It uses electromagnetism to switch large loads using small voltages. It consists of three parts: a coil, spring, and contacts. When the coil is energized by a high signal from a digital pin on the ESP8266, it attracts the contacts, forcing them closed. This completes the circuit and turns on the connected load. When the signal on the digital pin goes low, the coil is no longer energized and the spring pulls the contacts apart. This opens the circuit and turns off the connected load:
A power switch tail kit is a device that is used to control standard wall outlet devices with microcontrollers. It is already packaged to prevent you from having to mess around with high voltage wiring. Using it, you can control appliances in your home using the ESP8266:
A water pump is used to increase the pressure of fluids in a pipe. It uses a DC motor to rotate a fan and create a vacuum that sucks up the fluid. The sucked fluid is then forced to move by the fan, creating a vacuum again that sucks up the fluid behind it. This in effect moves the fluid from one place to another:
A breadboard is used to temporarily connect components without soldering. This makes it an ideal prototyping accessory that comes in handy when building circuits:
Jumper wires are flexible wires that are used to connect different parts of a circuit on a breadboard:
A micro USB cable will be used to connect the Adafruit ESP8266 board to the computer:
Having