Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ESP8266 Home Automation Projects

You're reading from   ESP8266 Home Automation Projects Leverage the power of this tiny WiFi chip to build exciting smart home projects

Arrow left icon
Product type Paperback
Published in Nov 2017
Publisher Packt
ISBN-13 9781787282629
Length 196 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
Constantin Tambrea Constantin Tambrea
Author Profile Icon Constantin Tambrea
Constantin Tambrea
Catalin Batrinu Catalin Batrinu
Author Profile Icon Catalin Batrinu
Catalin Batrinu
Arrow right icon
View More author details
Toc

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...
You have been reading a chapter from
ESP8266 Home Automation Projects
Published in: Nov 2017
Publisher: Packt
ISBN-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.
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 £16.99/month. Cancel anytime