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...