
Fig. 1: List of components required for MQTT Protocol based ESP8266 to ESP8266 IoT Communication


First of all, the ESP8266 boards need to be loaded with the firmware code. In this tutorial, the firmware code is written using Arduino IDE. It is loaded to the ESP8266 boards using the Arduino UNO. The generic ESP8266 boards are used in this project. The boards do not have any bootstrapping resistors, no voltage regulator, no reset circuit and no USB-serial adapter. The ESP8266 module operates on 3.3 V power supply with current greater than or equal to 250 mA. So, CP2102 USB to serial adapter is used to provide 3.3 V voltage with enough current to run ESP8266 modules reliably in every situation.








HiveMQ Broker Initialization in ESP8266 Firmware



You may also like:
Project Source Code
### //Program to /* * File Name - esp-01_PUBSUB_esp-02.ino * Created: 20/3/2017 11:02:11 AM * Main Source Code for ESP to ESP clinets communication with HiveMQ broker */ /* * Output Pins of the ESP8266 *GPIO02 - ledPin */ #include//Include library for ESP8266 configuration #include //Include library for MQTT //WIFI setup const char* ssid = "Replace it with the SSID of the Wi-Fi Network"; const char* password = "Replace it with the password of the Wi-Fi Network"; //The broker or server domain name const char* mqtt_server = "broker.mqtt-dashboard.com"; // Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient espClient; //create the MQTT client objects PubSubClient client(espClient); void setup() { pinMode(2,OUTPUT); //configure the led pin as OUTPUT Serial.begin(115200); //Set the baudrate of ESP8266 to show the process on serial monitor setup_wifi(); //Initialize the Wifi //By setting server and messgae callback function, the client is ready to use & set 1883 for listener port for the broker client.setServer(mqtt_server, 1883); client.setCallback(Received_Message); } /* * Function Name = setup_wifi * To establish the WIFI connection with ESP8266 */ void setup_wifi(){ delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); //Connect to the WIFI WiFi.begin(ssid, password); // Wait until the connection has been confirmed before continuing while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } /* * Function name = Received_message * Client receives the message and functions accordingly * Input parameter = Topic, Payload and message length * Output parameter = void */ void Received_Message(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } // Handle the message we received // Here, we are only looking at the characters of the received message (payload[0]) // If it is 1, turn the led will be on. // If it is 0, turn the led will be off. if((char)payload[0] == '1') //on digitalWrite(2,HIGH); else if((char)payload[0] == '0') //off digitalWrite(2,LOW); Serial.println(); } /* * Function Name = Reconnect * To establish the connection of ESP8266 with MQTT broker * and Will publish and subsribe to the topic */ void reconnect() { // Loop until we're reconnected while (!c lient.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("ESP8266Client")) { Serial.println("connected"); // Once connected, publish an announcement... client.publish("ESP8266/ESP02", "1"); // ... and resubscribe client.subscribe("ESP8266/ESP01"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void loop() { //When ESP8266 client will be connected to the broker, //then call the function reconnect and publish and subscribe the data from broker. if (!client.connected()) { reconnect(); } client.loop(); } ###
Circuit Diagrams
Filed Under: IoT, IoT tutorials, Tutorials








could you provide the libraries used?