SlideShare una empresa de Scribd logo
1 de 14
Descargar para leer sin conexión
Home Automation in the Cloud with the ESP8266 & Adafruit IO
Created by Marc-Olivier Schwartz
Last updated on 2015-07-28 12:50:09 PM EDT
2
3
4
5
6
7
12
14
Guide Contents
Guide Contents
Introduction
Hardware & Software Requirements
Building the Sensor Module
Building the Lamp Controller Module
Programming the Modules
Controlling the Project from Adafruit IO
How to Go Further
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 2 of 14
Introduction
The ESP8266 is an amazing little chip which has onboard WiFi capabilities, an integrated
processor, and also comes at less than $10.
This makes it the perfect chip for DIY electronics projects, and especially in the home automation
field. There are many breakout boards available for the ESP8266, but in this guide we are going to
use the Adafruit HUZZAH ESP8266 breakout, which is a very convenient ESP8266 breakout board.
In this project, we are going to use the ESP8266 to build two components which are very useful in
home automation: a sensor module, and a lamp controller.
We are also going to link these two projects to Adafruit IO, which will allow you to control this small
home automation system from anywhere in the world. Let's start!
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 3 of 14
Hardware & Software Requirements
Let's first see what are the required hardware modules for this project. Remember that we are
going to build two different modules: a sensor board, and a lamp controller.
For both modules, you will need one Adafruit HUZZAH ESP8266 breakout, which will be the central
component of each module. You will also need a breadboard and some male/male jumper wires to
make the necessary connections.
For the sensor board, you will also need a DHT22 sensor, which we will use to measure the
temperature & humidity. You can also use a DHT11 sensor which is cheaper but less precise
(which is the one you will see on the pictures), you will only have to change one line of code.
For the lamp controller, we are going to use a PowerSwitch Tail 2, which makes it really easy to
attach a lamp (or any electrical device) to your project.
Finally, you will need one FTDI friend board (or a FTDI/USB cable) to program the ESP8266
breakout board.
On the software side, you will need the Arduino IDE that you can get from:
https://www.arduino.cc/en/main/software (http://adafru.it/fGz)
After it is installed, add the ESP8266 package by following these steps:
Open the Preferences window in the Arduino IDE
Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board
Manager URLs field.
Open the Boards Manager from Tools > Board menu and install the esp8266 platform
You will also need to install the following Arduino libraries:
Adafruit MQTT library (http://adafru.it/fp6)
DHT sensor library (http://adafru.it/aJX)
Finally, also create an Adafruit IO account if it is not done yet. You can do so by going to:
http://io.adafruit.com/ (http://adafru.it/fH9)
You will need your Adafruit account name & Adafruit AIO key later in this tutorial.
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 4 of 14
Building the Sensor Module
Let’s now see how to configure the hardware for the sensor module. Thanks to the Adafruit
ESP8266 board, it is really simple to build this module on a breadboard.
The first step is to place the ESP8266 breakout board on the breadboard, as well as the DHT
sensor. Then, connect the V+ (or VCC) pin from the ESP8266 board to the first pin of the DHT
sensor (look at the picture below).
After that, connect the GND pin of the ESP8266 to the last pin of the DHT. Finally, connect pin
number 5 of the ESP8266 breakout board to the pin number 2 of the DHT sensor.
This is the final result:
As mentionned before, you will see that I used a DHT11 sensor on this picture. However, the code
you will find on GitHub is made for the DHT22 sensor, and I really suggest using a DHT22 sensor
as it is more precise.
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 5 of 14
Building the Lamp Controller Module
We are now going to see how to build the lamp controller module. This first step is to place the
ESP8266 breakout board on the breadboard.
Then, the only thing you need to do is to connect the PowerSwitch Tail Kit. Connect the two pins on
the right (-in and Ground) on the GND pin of the ESP8266 board and the +in pin to the pin number
5 of the ESP8266.
Then, also connect a lamp or any electrical device to the PowerSwitch, and the other end of
the PowerSwitch to the mains electricity.
This is the completey assembled lamp controller:
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 6 of 14
Programming the Modules
In this section, we are going to see how to program the two modules that you just built. We will start
by looking at the most important pieces of the code, first for the sensor module, and then for the
lamp controller. Note that you can find the whole code for this guide on the corresponding GitHub
repository:
https://github.com/openhomeautomation/adafruit-io-esp8266 (http://adafru.it/fHb)
The sketch for the ESP8266 sensor module starts by including the right libraries:
Then, we define on which pin the DHT sensor is connected to:
After that, there is the section of the code where you need to modify some data. You need to enter
your WiFi network name & password, your Adafruit account name, and your AIO key:
We also create the DHT sensor instance:
We also define on which feed we want to send the data to. By default, the sketch will simply send
temperature measurements to a feed called temperature, and humidity measurements to a feed
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"
#define DHTPIN 5
#define DHTTYPE DHT22
// WiFi parameters
#define WLAN_SSID "WLAN_SSID"
#define WLAN_PASS "WLAN_PASS"
// Adafruit IO
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "AIO_USERNAME"
#define AIO_KEY "AIO_KEY"
DHT dht(DHTPIN, DHTTYPE, 15);
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 7 of 14
called humidity.
You can of course change this by changing the following line:
In the setup() function of the sketch, we initialise the DHT sensor:
Then, in the loop() function of the sketch, we constantly check if we are still connected to Adafruit
IO. If that's not the case, we reconnect the board to Adafruit IO:
After that, we make the temperature & humidity measurements:
We then send these measurements to their respective feed:
Finally, we repeat the operation every 10 seconds:
const char TEMPERATURE_FEED[] PROGMEM = AIO_USERNAME "/feeds/temperature";
dht.begin();
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}
int humidity_data = (int)dht.readHumidity();
int temperature_data = (int)dht.readTemperature();
if (! temperature.publish(temperature_data))
Serial.println(F("Failed to publish temperature"));
else
Serial.println(F("Temperature published!"));
if (! humidity.publish(humidity_data))
Serial.println(F("Failed to publish humidity"));
else
Serial.println(F("Humidity published!"));
delay(10000);
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 8 of 14
Let's now see the specificities of the lamp controller sketch. We have to define on which pin the
PowerSwitch Tail is connected to:
We also define a new feed for the project, simply called lamp:
In the setup() function, we set the pin of the PowerSwitch Tail to an output:
We also make the project subscribe to the lamp feed:
Then, in the loop() function, we have to listen to incoming messages from Adafruit IO, to know if we
need to turn the lamp on or off. It starts by creating a subscription instance:
Then, we listen for incoming messages. If the message is 'ON', we switch the lamp on. And if it's
'OFF', we simply switch the lamp off again. This is done by the following piece of code:
const int lamp_pin = 5;
const char LAMP_FEED[] PROGMEM = AIO_USERNAME "/feeds/lamp";
pinMode(lamp_pin, OUTPUT);
mqtt.subscribe(&lamp);
Adafruit_MQTT_Subscribe *subscription;
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 9 of 14
Note that you can find the complete code for this guide inside the GitHub repository of the project:
https://github.com/openhomeautomation/adafruit-io-esp8266 (http://adafru.it/fHb)
We are now going to program the board. The first step is to plug the FTDI friend board to the
ESP8266 breakout:
while (subscription = mqtt.readSubscription(1000)) {
// we only care about the lamp events
if (subscription == &lamp) {
// convert mqtt ascii payload to int
char *value = (char *)lamp.lastread;
Serial.print(F("Received: "));
Serial.println(value);
// Apply message to lamp
String message = String(value);
message.trim();
if (message == "ON") {digitalWrite(lamp_pin, HIGH);}
if (message == "OFF") {digitalWrite(lamp_pin, LOW);}
}
}
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 10 of 14
Open the code with your Arduino IDE, and select ‘Adafruit HUZZAH ESP8266’ from the
Tools>Boards menu of the Arduino IDE. Also select the Serial port corresponding to the FTDI
board you are using.
Then, put the ESP8266 board in bootloader mode so you can program it. On the Adafruit ESP8266
board, it is really simple: just hold the GPIO0 button pressed, and then press the Reset button.
Make sure that you also modified the code with your own data (WiFi network credentials & Adafruit
IO credentials).
After that, upload the code to the board. When it is finished, open the Serial monitor, and reset the
board: you should see that the board automatically connects to Adafruit IO.
Of course, you need to repeat the operation for both modules in the project.
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 11 of 14
Controlling the Project from Adafruit IO
It's now time to create our Adafruit IO dashboard & control our modules from anywhere in the
world.
The first step is to create a new dashboard at:
https://io.adafruit.com (http://adafru.it/eZ8)
If it is not done yet, create the feeds that we used in our project: temperature, humidity, and lamp.
Then, add a new 'Gauge' widget, and link it to the temperature feed. Do the same for humidity:
Then, create a 'toggle button' widget, and link it to the lamp feed:
Your dashboard is now ready to be used. If you followed all the instructions in this guide so far, you
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 12 of 14
should see that the temperature & humidity gauges have already been updated, as the ESP8266
sensor module already sent data to Adafruit IO:
Finally, test the toggle button by clicking on it:
You should immediately see that the lamp (or the electrical device) connected to the PowerSwitch
Tail is turning on. Simply click on the button again to switch it off.
Congratulations, you now learned the basics on how to build home automation modules with the
ESP8266 & control them from anywhere with Adafruit IO!
© Adafruit Industries
https://learn.adafruit.com/home-automation-in-the-cloud-with-the-
esp8266-and-adafruit-io
Page 13 of 14
How to Go Further
There are of course several ways to improve this project. One way is to connect more of these
modules to your Adafruit IO dashboard. You can perfectly have several lamp modules for example,
and control them all from your Adafruit IO dashboard.
You can also experiment with different kind of modules. For example, you can build cheap motion
sensors based on the ESP8266, and monitor them as well from your Adafruit IO dashboard.
© Adafruit Industries Last Updated: 2015-07-28 12:50:10 PM EDT Page 14 of 14

Más contenido relacionado

La actualidad más candente

If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 

La actualidad más candente (20)

LET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERSLET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERS
 
Verilog full adder in dataflow & gate level modelling style.
Verilog full adder in dataflow  & gate level modelling style.Verilog full adder in dataflow  & gate level modelling style.
Verilog full adder in dataflow & gate level modelling style.
 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solution
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
 
Let us c chapter 4 solution
Let us c chapter 4 solutionLet us c chapter 4 solution
Let us c chapter 4 solution
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solutionLet us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3
 
Monadic Java
Monadic JavaMonadic Java
Monadic Java
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
halfadder & halfsubtractor using 4:1 MUX
halfadder & halfsubtractor using 4:1 MUXhalfadder & halfsubtractor using 4:1 MUX
halfadder & halfsubtractor using 4:1 MUX
 
Deep C
Deep CDeep C
Deep C
 
inheritance
inheritanceinheritance
inheritance
 
Friend function
Friend functionFriend function
Friend function
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Theory of automata and formal languages Unit 4
Theory of automata and formal languages Unit 4Theory of automata and formal languages Unit 4
Theory of automata and formal languages Unit 4
 
ADE VIVA QUESTIONS
ADE VIVA QUESTIONSADE VIVA QUESTIONS
ADE VIVA QUESTIONS
 
froglogic Coco Code Coverage Presentation
froglogic Coco Code Coverage Presentationfroglogic Coco Code Coverage Presentation
froglogic Coco Code Coverage Presentation
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 

Destacado

Pump control with tank level monitoring
Pump control with tank level monitoringPump control with tank level monitoring
Pump control with tank level monitoring
Asif Sheriff
 

Destacado (14)

Pump control with tank level monitoring
Pump control with tank level monitoringPump control with tank level monitoring
Pump control with tank level monitoring
 
Remote tanklevelmonitor
Remote tanklevelmonitorRemote tanklevelmonitor
Remote tanklevelmonitor
 
Home Automation by ESP8266 #iotconfua
Home Automation by ESP8266 #iotconfuaHome Automation by ESP8266 #iotconfua
Home Automation by ESP8266 #iotconfua
 
home automation
home automationhome automation
home automation
 
InterCon 2016 - Backend do IoT com RethinkDB e Python
InterCon 2016 - Backend do IoT com RethinkDB e PythonInterCon 2016 - Backend do IoT com RethinkDB e Python
InterCon 2016 - Backend do IoT com RethinkDB e Python
 
Esp8266 Workshop
Esp8266 WorkshopEsp8266 Workshop
Esp8266 Workshop
 
Home Automation Over Internet Project (Ev Otomasyon Projesi)
Home Automation Over Internet Project (Ev Otomasyon Projesi)Home Automation Over Internet Project (Ev Otomasyon Projesi)
Home Automation Over Internet Project (Ev Otomasyon Projesi)
 
Android Mobile - Home Automation
Android Mobile - Home Automation Android Mobile - Home Automation
Android Mobile - Home Automation
 
How to get started with home automation
How to get started with home automationHow to get started with home automation
How to get started with home automation
 
home automation using ARM7 controller
home automation using ARM7 controllerhome automation using ARM7 controller
home automation using ARM7 controller
 
Android Control Hardware and Arduino IoT ( 22 Aug 15 )
Android Control Hardware and Arduino IoT ( 22 Aug 15 )Android Control Hardware and Arduino IoT ( 22 Aug 15 )
Android Control Hardware and Arduino IoT ( 22 Aug 15 )
 
Ambalaj Tutumları Lisans Tezi
Ambalaj Tutumları Lisans TeziAmbalaj Tutumları Lisans Tezi
Ambalaj Tutumları Lisans Tezi
 
Arduino Lecture 1 - Introducing the Arduino
Arduino Lecture 1 - Introducing the ArduinoArduino Lecture 1 - Introducing the Arduino
Arduino Lecture 1 - Introducing the Arduino
 
Arduino based intelligent greenhouse Project
Arduino based intelligent greenhouse ProjectArduino based intelligent greenhouse Project
Arduino based intelligent greenhouse Project
 

Similar a Home automation-in-the-cloud-with-the-esp8266-and-adafruit-io

Intro to arduino
Intro to arduinoIntro to arduino
Intro to arduino
José Faria
 
presentation on IOT.pptx
presentation on IOT.pptxpresentation on IOT.pptx
presentation on IOT.pptx
ShabinJerin
 

Similar a Home automation-in-the-cloud-with-the-esp8266-and-adafruit-io (20)

Adafruit Huzzah Esp8266 WiFi Board
Adafruit Huzzah Esp8266 WiFi BoardAdafruit Huzzah Esp8266 WiFi Board
Adafruit Huzzah Esp8266 WiFi Board
 
ESP32 WiFi & Bluetooth Module - Getting Started Guide
ESP32 WiFi & Bluetooth Module - Getting Started GuideESP32 WiFi & Bluetooth Module - Getting Started Guide
ESP32 WiFi & Bluetooth Module - Getting Started Guide
 
Embedded system application
Embedded system applicationEmbedded system application
Embedded system application
 
ppt for mini project (1).ppt
ppt for mini project (1).pptppt for mini project (1).ppt
ppt for mini project (1).ppt
 
Esp8266 v12
Esp8266 v12Esp8266 v12
Esp8266 v12
 
Electronics ESP processors
Electronics ESP processorsElectronics ESP processors
Electronics ESP processors
 
Intro to arduino
Intro to arduinoIntro to arduino
Intro to arduino
 
NodeMCU || Controlling and observing a robotic car with a smartphone through...
NodeMCU || Controlling and observing a robotic car with a  smartphone through...NodeMCU || Controlling and observing a robotic car with a  smartphone through...
NodeMCU || Controlling and observing a robotic car with a smartphone through...
 
Cassiopeia Ltd - ESP8266+Arduino workshop
Cassiopeia Ltd - ESP8266+Arduino workshopCassiopeia Ltd - ESP8266+Arduino workshop
Cassiopeia Ltd - ESP8266+Arduino workshop
 
IoT Platform
IoT PlatformIoT Platform
IoT Platform
 
IoT Platform
IoT PlatformIoT Platform
IoT Platform
 
presentation on IOT.pptx
presentation on IOT.pptxpresentation on IOT.pptx
presentation on IOT.pptx
 
Arduino to Control Bulbs using Web App
Arduino to Control Bulbs using Web AppArduino to Control Bulbs using Web App
Arduino to Control Bulbs using Web App
 
Ch_2_8,9,10.pptx
Ch_2_8,9,10.pptxCh_2_8,9,10.pptx
Ch_2_8,9,10.pptx
 
Voice controlled smart home
Voice controlled smart home Voice controlled smart home
Voice controlled smart home
 
arduino
arduinoarduino
arduino
 
Chapter 2.doc
Chapter 2.docChapter 2.doc
Chapter 2.doc
 
Ijecet 06 08_002
Ijecet 06 08_002Ijecet 06 08_002
Ijecet 06 08_002
 
Arduino and Circuits.docx
Arduino and Circuits.docxArduino and Circuits.docx
Arduino and Circuits.docx
 
Digital home automation with arduino bluetooth
Digital home automation with arduino bluetoothDigital home automation with arduino bluetooth
Digital home automation with arduino bluetooth
 

Último

+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
Health
 
如何办理麦考瑞大学毕业证(MQU毕业证书)成绩单原版一比一
如何办理麦考瑞大学毕业证(MQU毕业证书)成绩单原版一比一如何办理麦考瑞大学毕业证(MQU毕业证书)成绩单原版一比一
如何办理麦考瑞大学毕业证(MQU毕业证书)成绩单原版一比一
ozave
 
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
62qaf0hi
 
如何办理新西兰林肯大学毕业证(Lincoln毕业证书)成绩单原版一比一
如何办理新西兰林肯大学毕业证(Lincoln毕业证书)成绩单原版一比一如何办理新西兰林肯大学毕业证(Lincoln毕业证书)成绩单原版一比一
如何办理新西兰林肯大学毕业证(Lincoln毕业证书)成绩单原版一比一
opyff
 
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
wsppdmt
 
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
avy6anjnd
 
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
ezgenuh
 
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
avy6anjnd
 
如何办理(爱大毕业证书)爱丁堡大学毕业证成绩单留信学历认证真实可查
如何办理(爱大毕业证书)爱丁堡大学毕业证成绩单留信学历认证真实可查如何办理(爱大毕业证书)爱丁堡大学毕业证成绩单留信学历认证真实可查
如何办理(爱大毕业证书)爱丁堡大学毕业证成绩单留信学历认证真实可查
huxs9sacp
 
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
nirzagarg
 
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE AbudhabiAbortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
Abortion pills in Kuwait Cytotec pills in Kuwait
 
在线定制(UBC毕业证书)英属哥伦比亚大学毕业证成绩单留信学历认证原版一比一
在线定制(UBC毕业证书)英属哥伦比亚大学毕业证成绩单留信学历认证原版一比一在线定制(UBC毕业证书)英属哥伦比亚大学毕业证成绩单留信学历认证原版一比一
在线定制(UBC毕业证书)英属哥伦比亚大学毕业证成绩单留信学历认证原版一比一
qh1ao5mm
 

Último (20)

01552_14_01306_8.0_EPS_CMP_SW_VC2_Notebook.doc
01552_14_01306_8.0_EPS_CMP_SW_VC2_Notebook.doc01552_14_01306_8.0_EPS_CMP_SW_VC2_Notebook.doc
01552_14_01306_8.0_EPS_CMP_SW_VC2_Notebook.doc
 
+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
+97470301568>>buy vape oil,thc oil weed,hash and cannabis oil in qatar doha}}
 
如何办理麦考瑞大学毕业证(MQU毕业证书)成绩单原版一比一
如何办理麦考瑞大学毕业证(MQU毕业证书)成绩单原版一比一如何办理麦考瑞大学毕业证(MQU毕业证书)成绩单原版一比一
如何办理麦考瑞大学毕业证(MQU毕业证书)成绩单原版一比一
 
T.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptx
T.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptxT.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptx
T.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptx
 
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
 
SEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVE
SEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVESEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVE
SEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVE
 
John deere 7200r 7230R 7260R Problems Repair Manual
John deere 7200r 7230R 7260R Problems Repair ManualJohn deere 7200r 7230R 7260R Problems Repair Manual
John deere 7200r 7230R 7260R Problems Repair Manual
 
如何办理新西兰林肯大学毕业证(Lincoln毕业证书)成绩单原版一比一
如何办理新西兰林肯大学毕业证(Lincoln毕业证书)成绩单原版一比一如何办理新西兰林肯大学毕业证(Lincoln毕业证书)成绩单原版一比一
如何办理新西兰林肯大学毕业证(Lincoln毕业证书)成绩单原版一比一
 
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
 
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be WrongIs Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
 
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
如何办理莱斯大学毕业证(Rice毕业证)毕业证成绩单原版一比一
 
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
一比一原版(UVic学位证书)维多利亚大学毕业证学历认证买留学回国
 
JOHN DEERE 7200R 7215R 7230R 7260R 7280R TECHNICAL SERVICE PDF MANUAL 2680PGS...
JOHN DEERE 7200R 7215R 7230R 7260R 7280R TECHNICAL SERVICE PDF MANUAL 2680PGS...JOHN DEERE 7200R 7215R 7230R 7260R 7280R TECHNICAL SERVICE PDF MANUAL 2680PGS...
JOHN DEERE 7200R 7215R 7230R 7260R 7280R TECHNICAL SERVICE PDF MANUAL 2680PGS...
 
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
如何办理美国华盛顿大学毕业证(UW毕业证书)毕业证成绩单原版一比一
 
如何办理(爱大毕业证书)爱丁堡大学毕业证成绩单留信学历认证真实可查
如何办理(爱大毕业证书)爱丁堡大学毕业证成绩单留信学历认证真实可查如何办理(爱大毕业证书)爱丁堡大学毕业证成绩单留信学历认证真实可查
如何办理(爱大毕业证书)爱丁堡大学毕业证成绩单留信学历认证真实可查
 
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
 
Muslim Call Girls Churchgate WhatsApp +91-9930687706, Best Service
Muslim Call Girls Churchgate WhatsApp +91-9930687706, Best ServiceMuslim Call Girls Churchgate WhatsApp +91-9930687706, Best Service
Muslim Call Girls Churchgate WhatsApp +91-9930687706, Best Service
 
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE AbudhabiAbortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
 
Stacey+= Dubai Calls Girls O525547819 Call Girls In Dubai
Stacey+= Dubai Calls Girls O525547819 Call Girls In DubaiStacey+= Dubai Calls Girls O525547819 Call Girls In Dubai
Stacey+= Dubai Calls Girls O525547819 Call Girls In Dubai
 
在线定制(UBC毕业证书)英属哥伦比亚大学毕业证成绩单留信学历认证原版一比一
在线定制(UBC毕业证书)英属哥伦比亚大学毕业证成绩单留信学历认证原版一比一在线定制(UBC毕业证书)英属哥伦比亚大学毕业证成绩单留信学历认证原版一比一
在线定制(UBC毕业证书)英属哥伦比亚大学毕业证成绩单留信学历认证原版一比一
 

Home automation-in-the-cloud-with-the-esp8266-and-adafruit-io

  • 1. Home Automation in the Cloud with the ESP8266 & Adafruit IO Created by Marc-Olivier Schwartz Last updated on 2015-07-28 12:50:09 PM EDT
  • 2. 2 3 4 5 6 7 12 14 Guide Contents Guide Contents Introduction Hardware & Software Requirements Building the Sensor Module Building the Lamp Controller Module Programming the Modules Controlling the Project from Adafruit IO How to Go Further © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 2 of 14
  • 3. Introduction The ESP8266 is an amazing little chip which has onboard WiFi capabilities, an integrated processor, and also comes at less than $10. This makes it the perfect chip for DIY electronics projects, and especially in the home automation field. There are many breakout boards available for the ESP8266, but in this guide we are going to use the Adafruit HUZZAH ESP8266 breakout, which is a very convenient ESP8266 breakout board. In this project, we are going to use the ESP8266 to build two components which are very useful in home automation: a sensor module, and a lamp controller. We are also going to link these two projects to Adafruit IO, which will allow you to control this small home automation system from anywhere in the world. Let's start! © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 3 of 14
  • 4. Hardware & Software Requirements Let's first see what are the required hardware modules for this project. Remember that we are going to build two different modules: a sensor board, and a lamp controller. For both modules, you will need one Adafruit HUZZAH ESP8266 breakout, which will be the central component of each module. You will also need a breadboard and some male/male jumper wires to make the necessary connections. For the sensor board, you will also need a DHT22 sensor, which we will use to measure the temperature & humidity. You can also use a DHT11 sensor which is cheaper but less precise (which is the one you will see on the pictures), you will only have to change one line of code. For the lamp controller, we are going to use a PowerSwitch Tail 2, which makes it really easy to attach a lamp (or any electrical device) to your project. Finally, you will need one FTDI friend board (or a FTDI/USB cable) to program the ESP8266 breakout board. On the software side, you will need the Arduino IDE that you can get from: https://www.arduino.cc/en/main/software (http://adafru.it/fGz) After it is installed, add the ESP8266 package by following these steps: Open the Preferences window in the Arduino IDE Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. Open the Boards Manager from Tools > Board menu and install the esp8266 platform You will also need to install the following Arduino libraries: Adafruit MQTT library (http://adafru.it/fp6) DHT sensor library (http://adafru.it/aJX) Finally, also create an Adafruit IO account if it is not done yet. You can do so by going to: http://io.adafruit.com/ (http://adafru.it/fH9) You will need your Adafruit account name & Adafruit AIO key later in this tutorial. © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 4 of 14
  • 5. Building the Sensor Module Let’s now see how to configure the hardware for the sensor module. Thanks to the Adafruit ESP8266 board, it is really simple to build this module on a breadboard. The first step is to place the ESP8266 breakout board on the breadboard, as well as the DHT sensor. Then, connect the V+ (or VCC) pin from the ESP8266 board to the first pin of the DHT sensor (look at the picture below). After that, connect the GND pin of the ESP8266 to the last pin of the DHT. Finally, connect pin number 5 of the ESP8266 breakout board to the pin number 2 of the DHT sensor. This is the final result: As mentionned before, you will see that I used a DHT11 sensor on this picture. However, the code you will find on GitHub is made for the DHT22 sensor, and I really suggest using a DHT22 sensor as it is more precise. © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 5 of 14
  • 6. Building the Lamp Controller Module We are now going to see how to build the lamp controller module. This first step is to place the ESP8266 breakout board on the breadboard. Then, the only thing you need to do is to connect the PowerSwitch Tail Kit. Connect the two pins on the right (-in and Ground) on the GND pin of the ESP8266 board and the +in pin to the pin number 5 of the ESP8266. Then, also connect a lamp or any electrical device to the PowerSwitch, and the other end of the PowerSwitch to the mains electricity. This is the completey assembled lamp controller: © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 6 of 14
  • 7. Programming the Modules In this section, we are going to see how to program the two modules that you just built. We will start by looking at the most important pieces of the code, first for the sensor module, and then for the lamp controller. Note that you can find the whole code for this guide on the corresponding GitHub repository: https://github.com/openhomeautomation/adafruit-io-esp8266 (http://adafru.it/fHb) The sketch for the ESP8266 sensor module starts by including the right libraries: Then, we define on which pin the DHT sensor is connected to: After that, there is the section of the code where you need to modify some data. You need to enter your WiFi network name & password, your Adafruit account name, and your AIO key: We also create the DHT sensor instance: We also define on which feed we want to send the data to. By default, the sketch will simply send temperature measurements to a feed called temperature, and humidity measurements to a feed #include <ESP8266WiFi.h> #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" #include "DHT.h" #define DHTPIN 5 #define DHTTYPE DHT22 // WiFi parameters #define WLAN_SSID "WLAN_SSID" #define WLAN_PASS "WLAN_PASS" // Adafruit IO #define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "AIO_USERNAME" #define AIO_KEY "AIO_KEY" DHT dht(DHTPIN, DHTTYPE, 15); © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 7 of 14
  • 8. called humidity. You can of course change this by changing the following line: In the setup() function of the sketch, we initialise the DHT sensor: Then, in the loop() function of the sketch, we constantly check if we are still connected to Adafruit IO. If that's not the case, we reconnect the board to Adafruit IO: After that, we make the temperature & humidity measurements: We then send these measurements to their respective feed: Finally, we repeat the operation every 10 seconds: const char TEMPERATURE_FEED[] PROGMEM = AIO_USERNAME "/feeds/temperature"; dht.begin(); if(! mqtt.ping(3)) { // reconnect to adafruit io if(! mqtt.connected()) connect(); } int humidity_data = (int)dht.readHumidity(); int temperature_data = (int)dht.readTemperature(); if (! temperature.publish(temperature_data)) Serial.println(F("Failed to publish temperature")); else Serial.println(F("Temperature published!")); if (! humidity.publish(humidity_data)) Serial.println(F("Failed to publish humidity")); else Serial.println(F("Humidity published!")); delay(10000); © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 8 of 14
  • 9. Let's now see the specificities of the lamp controller sketch. We have to define on which pin the PowerSwitch Tail is connected to: We also define a new feed for the project, simply called lamp: In the setup() function, we set the pin of the PowerSwitch Tail to an output: We also make the project subscribe to the lamp feed: Then, in the loop() function, we have to listen to incoming messages from Adafruit IO, to know if we need to turn the lamp on or off. It starts by creating a subscription instance: Then, we listen for incoming messages. If the message is 'ON', we switch the lamp on. And if it's 'OFF', we simply switch the lamp off again. This is done by the following piece of code: const int lamp_pin = 5; const char LAMP_FEED[] PROGMEM = AIO_USERNAME "/feeds/lamp"; pinMode(lamp_pin, OUTPUT); mqtt.subscribe(&lamp); Adafruit_MQTT_Subscribe *subscription; © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 9 of 14
  • 10. Note that you can find the complete code for this guide inside the GitHub repository of the project: https://github.com/openhomeautomation/adafruit-io-esp8266 (http://adafru.it/fHb) We are now going to program the board. The first step is to plug the FTDI friend board to the ESP8266 breakout: while (subscription = mqtt.readSubscription(1000)) { // we only care about the lamp events if (subscription == &lamp) { // convert mqtt ascii payload to int char *value = (char *)lamp.lastread; Serial.print(F("Received: ")); Serial.println(value); // Apply message to lamp String message = String(value); message.trim(); if (message == "ON") {digitalWrite(lamp_pin, HIGH);} if (message == "OFF") {digitalWrite(lamp_pin, LOW);} } } © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 10 of 14
  • 11. Open the code with your Arduino IDE, and select ‘Adafruit HUZZAH ESP8266’ from the Tools>Boards menu of the Arduino IDE. Also select the Serial port corresponding to the FTDI board you are using. Then, put the ESP8266 board in bootloader mode so you can program it. On the Adafruit ESP8266 board, it is really simple: just hold the GPIO0 button pressed, and then press the Reset button. Make sure that you also modified the code with your own data (WiFi network credentials & Adafruit IO credentials). After that, upload the code to the board. When it is finished, open the Serial monitor, and reset the board: you should see that the board automatically connects to Adafruit IO. Of course, you need to repeat the operation for both modules in the project. © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 11 of 14
  • 12. Controlling the Project from Adafruit IO It's now time to create our Adafruit IO dashboard & control our modules from anywhere in the world. The first step is to create a new dashboard at: https://io.adafruit.com (http://adafru.it/eZ8) If it is not done yet, create the feeds that we used in our project: temperature, humidity, and lamp. Then, add a new 'Gauge' widget, and link it to the temperature feed. Do the same for humidity: Then, create a 'toggle button' widget, and link it to the lamp feed: Your dashboard is now ready to be used. If you followed all the instructions in this guide so far, you © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 12 of 14
  • 13. should see that the temperature & humidity gauges have already been updated, as the ESP8266 sensor module already sent data to Adafruit IO: Finally, test the toggle button by clicking on it: You should immediately see that the lamp (or the electrical device) connected to the PowerSwitch Tail is turning on. Simply click on the button again to switch it off. Congratulations, you now learned the basics on how to build home automation modules with the ESP8266 & control them from anywhere with Adafruit IO! © Adafruit Industries https://learn.adafruit.com/home-automation-in-the-cloud-with-the- esp8266-and-adafruit-io Page 13 of 14
  • 14. How to Go Further There are of course several ways to improve this project. One way is to connect more of these modules to your Adafruit IO dashboard. You can perfectly have several lamp modules for example, and control them all from your Adafruit IO dashboard. You can also experiment with different kind of modules. For example, you can build cheap motion sensors based on the ESP8266, and monitor them as well from your Adafruit IO dashboard. © Adafruit Industries Last Updated: 2015-07-28 12:50:10 PM EDT Page 14 of 14