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 the MQTT server and port to be used via the client.Setserver() function:

void setup() {  
Serial.begin(115200); 
setup_wifi(); 
client.setServer(mqtt_serv_address, mqtt_port_number); 
} 
 
void setup_wifi() { 
 
delay(10); 
  // We start by connecting to a WiFi network 
Serial.println(); 
Serial.print("Connecting to "); 
Serial.println(wifi_network); 
 
WiFi.begin(wifi_network, wifi_pass); 
 
while (WiFi.status() != WL_CONNECTED) { 
WiFi.begin(wifi_network, wifi_pass); 
 
Serial.print("."); 
delay(5000); 
  } 
 
Serial.println(""); 
Serial.println("WiFi connected"); 
Serial.println("IP address: "); 
Serial.println(WiFi.localIP()); 
} 

If keep alive packets from the MQTT server to the ESP8266 module are lost and the communication is interrupted, the reconnect() function will try to connect again to the MQTT server. This reconnect function is also used as a first connection to the server.

After connecting to the MQTT server the ESP8266 will publish a message "Hello world, I am ESP8266!" on the fromEsp8266 topic:

void reconnect() { 
  // Loop until we're reconnected 
while (!client.connected()) { 
Serial.print("Attempting MQTT connection..."); 
    // Attempt to connect 
if (client.connect("ESP8266Client"))  
    { 
Serial.println("connected"); 
      // Once connected, publish an announcement... 
client.publish("fromEsp8266", "Hello world, I am ESP8266!"); 
    } else { 
Serial.print("failed, rc="); 
Serial.print(client.state()); 
Serial.println(" try again in 5 seconds"); 
      // Wait 5 seconds before retrying 
delay(5000); 
    } 
  } 
} 

The loop function will check for connectivity with the MQTT broker, reconnect to it if there is a problem with connection, and every two seconds will publish a message on the fromEsp8266 topic:

void loop() { 
 
if (!client.connected()) { 
reconnect(); 
  } 
client.loop(); 
 
long now = millis(); 
if (now - lastMsg> 2000) { 
lastMsg = now; 
    ++value; 
snprintf (msg, 75, "Hello world #%ld", value); 
Serial.print("Publish message: "); 
Serial.println(msg); 
client.publish("fromEsp8266", msg); 
  } 
} 

After compiling and uploading the code to the ESP8266 module, in a terminal window subscribe to the fromEsp8266 topic. The messages sent by the ESP8266 module will be shown in the terminal window:

Messages published by ESP8266

Observation: Remember that topic names are case-sensitive. As an exercise you can send the pin status (HIGH or LOW) as was presented in Chapter 1Getting Started with the ESP8266.

Use digitalRead(PIN_NUMER) instead of value.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.85.178