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

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; = 0; 

Start the connection to the Wi-Fi network and set the name of the function that will be called when a message is received from the MQTT broker, as follows:

void setup...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
ESP8266 Home Automation Projects
Published in: Nov 2017Publisher: PacktISBN-13: 9781787282629

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