ESP8266 Home Automation Projects Leverage the power of this tiny WiFi chip to build exciting smart home projects Catalin Batrinu BIRMINGHAM - MUMBAI ESP8266 Home Automation Projects 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: November 2017 Production reference: 2091118 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78728-262-9 www.packtpub.com Credits Author Project Coordinator Catalin Batrinu Kinjal Bari Reviewer Proofreader Constantin Tambrea Safis Editing Acquisition Editor Indexer Prachi Bisht Francy Puthiry Content Development Editor Graphics Eisha Dsouza Tania Dutta Technical Editor Production Coordinator Naveenkumar Jain Melwyn Dsa Copy Editor Safis Editing About the Author Catalin Batrinu graduated from the Politehnica University of Bucharest in Electronics, Telecommunications, and Information Technology. He has been working as a software developer in telecommunications for the past 16 years. He has worked with old protocols and the latest network protocols and technologies, so he has experienced all the recent transformations in the telecommunications industry. He has implemented many telecommunications protocols, from access adaptations and backbone switches to high- capacity, carrier-grade switches on various hardware platforms from Wintegra and Broadcom. The Internet of Things came as a natural evolution for him and now he collaborates with different companies to construct the world of tomorrow, which will make our life more comfortable and secure. Using the ESP8266, he has prototyped devices such as irrigation controllers, smart sockets, window shutters, digital addressable lighting controls, and environment controls, all of them controlled directly from a mobile application via the cloud. An MQTT broker with bridging and a WebSockets server was even developed for the ESP8266. Soon, these devices will be part of our daily lives, so we will all enjoy their functionality. Don't forget to then keep an eye on his blog https://myesp8266.blogspot.com and on the new platform as a service that you will discover soon at https://iotcentral.eu. About the Reviewer Constantin Tambrea has been a senior software engineer at Luxoft Romania for more than 9 years, and has been involved in various projects in the telecommunications field. He holds master's and bachelor's degrees in Computer Science from University Politehnica in Bucharest. His main domains of interest are Java Enterprise applications and web development techniques, but recently he has become passionate about the Internet of Things domain, in which he is experimenting with a personal home automation project. I would like to give thanks to Catalin Batrinu, my colleague, and friend, for introducing me to the IoT world and for encouraging me to review his work on this book. www.PacktPub.com 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.comand 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. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Customer Feedback 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/1787282627. 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! Table of Contents Preface 1 Chapter 1: Getting Started with the ESP8266 7 Starting with the ESP8266 chip 8 Installing Arduino IDE 9 Downloading the Arduino Software IDE 9 Configure Arduino IDE 11 Installing the ESP8266 SDK 13 How to install a library 17 Libraries from the Arduino's repository 18 Library is not in the repository 19 It is time for your first program 20 Seeing the result 22 Connecting ESP8266 to Wi-Fi 24 Getting data from the internet 26 Sending data to the internet 35 Summary 41 Chapter 2: Building and Configuring Your Own MQTT Server 42 Message Queue Telemetry Transport 42 Quality of service 43 Security 43 Retain messages and last will 44 Basic terminology 44 Wildcards on topics 46 Introducing Mosquitto broker 47 ESP8266 and MQTT 51 Publishing data from the ESP8266 53 Receiving MQTT messages in the ESP8266 56 Securing Mosquitto 59 Summary 64 Chapter 3: Building a Home Thermostat with the ESP8266 65 SPIFFS 65 SPIFFS objects 67 Directory object 69 File object 70 Temperature sensor 73 Summary 85