Reader small image

You're reading from  ESP8266 Home Automation Projects

Product typeBook
Published inNov 2017
PublisherPackt
ISBN-139781787282629
Edition1st Edition
Tools
Right arrow
Authors (2):
Catalin Batrinu
Catalin Batrinu
author image
Catalin Batrinu

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 transformations in the telecommunication 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. Internet of Things came as a natural evolution for him and now he collaborates with different companies to construct the world of tomorrow that 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 over the cloud. An MQTT broker with bridging and a WebSockets server was even developed for the ESP8266. Soon, all those devices will be part of our daily life, so we will all enjoy their functionality.
Read more about Catalin Batrinu

View More author details
Right arrow

Chapter 3. Building a Home Thermostat with the ESP8266

In this chapter, we will build a home thermostat with ESP8266. The thermostat will have the following functions:

  • It will read the temperature from a DHT22 temperature sensor
  • It will compare the temperature with the desired one; if it is above it, it will trigger a relay OFF and if it is below, it will trigger the relay ON

But first, let's discuss how we can save data in the ESP8266 and retrieve it. Let's make use of SPIFFS.

SPIFFS


SPI Flash File System (SPIFFS) is a filesystem created for small embedded systems. SPIFFS has many advantages since it allows you to create files and simulate directories.

The following are the features of SPIFFS:

  • Designed for low RAM use on microcontrollers
  • Uses statically sized RAM buffers
  • Posix-like api: open, close, read, write, seek, stat, and so on
  • It can run on any NOR flash, not only the SPI flash. Multiple SPIFFS configurations can run on the same target - and even on the same SPI flash device
  • Implements static wear levelling
  • Built-in filesystem consistency checks
  • Highly configurable and can be adapted for a variety of flash types

I highly encourage you to use SPIFFS in your designs to store data in NOR flash since it is very easy to read and write data, is like a *nix filesystem.

Filesystem size depends on the flash chip size. Depending on the board that is selected in the IDE, you can select different sizes for SPIFFS. For example, in case if you have selected the NodeMcu v1.0 as...

Temperature sensor


There are a lot of temperature sensors that can be used, but for this project we will use a very common one, the DTH22. It can measure temperature and humidity.

The following are the DHT22 characteristics:

  • Low cost
  • 3 to 5V power and I/O
  • 5mA max current used during conversion (while requesting data)
  • Good for 0-100% humidity readings with 2-5% accuracy
  • Good for -40 to 125°C temperature readings ±0.5°C accuracy
  • No more than 0.5 Hz sampling rate (once every two seconds)
  • Body size 15.1mm x 25mm x 7.7mm
  • 4 pins with 0.1" spacing

DHT22 can be found as a separate sensor or as a breakout. It is preferrable to buy the breakout version since it has also the pull-up 4k7 resistor and a capacitor. If you prefer the sensor alone this is the pinout:

DHT22 pinout

Here:

  • VCC: can be between 3V3 and 5V
  • GND: is the ground
  • DATA: is the data pin

Don't forget to add a 4K7 pull-up resistor between the DATA and VCC pin. For the connection with a gas furnance or other heating element, a relay will be added on the...

Summary


In this chapter, you learned how to save your data to the SPIFFS and how to build and control a thermostat. So far, the credentials for the Wi-Fi have been hard coded in the code; in the next chapter we will learn how to use the WiFiManager library to expose an Access Point. Get the Wi-Fi credentials along with other data and save them to the flash, making use of SPIFFS.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
ESP8266 Home Automation Projects
Published in: Nov 2017Publisher: PacktISBN-13: 9781787282629
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Catalin Batrinu

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 transformations in the telecommunication 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. Internet of Things came as a natural evolution for him and now he collaborates with different companies to construct the world of tomorrow that 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 over the cloud. An MQTT broker with bridging and a WebSockets server was even developed for the ESP8266. Soon, all those devices will be part of our daily life, so we will all enjoy their functionality.
Read more about Catalin Batrinu