Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ESP8266 Home Automation Projects

You're reading from  ESP8266 Home Automation Projects

Product type Book
Published in Nov 2017
Publisher Packt
ISBN-13 9781787282629
Pages 196 pages
Edition 1st Edition
Languages
Authors (2):
Catalin Batrinu Catalin Batrinu
Profile icon Catalin Batrinu
Constantin Tambrea Constantin Tambrea
Profile icon Constantin Tambrea
View More author details
Toc

Publishing data from the ESP8266


Start a new sketch via File | New in the Arduino IDE and paste in the following code. Include the ESP8266WiFi library and the PubSubClient one:

#include <ESP8266WiFi.h> 
#include <PubSubClient.h> 

Update these with values suitable for your network. Use ifconfig to get the IP address of the server where the Mosquitto broker is installed. If your server has an FQDN name such as myiotserver.com and is registered into the DNS, then instead of the IP address in mqtt_server you can use the FQDN name:

const char* wifi_network = "YOUR_WIFI_SSID"; 
const char* wifi_pass = "YOUR_WIFI_PASSWORD"; 
const char* mqtt_serv_address = "192.168.1.116"; 
const int mqtt_port_number = 1883; 

Instantiate a WiFiClient and pass it to the PubSubClient:

WiFiClient espClient; 
PubSubClient client(espClient); 
long lastMsg = 0; 
char msg[50];
int value = 0; 

The setup() function will start connecting the ESP8266 to the Wi-Fi network by calling the setup_wifi() function and set...

lock icon The rest of the chapter is locked
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 €14.99/month. Cancel anytime}