Raspberry Pi Talking to ESP8266 using MQTT

Control led from both server and client side using MQTT.

Raspberry Pi Talking to ESP8266 over WiFi using MQTT

MQTT is a machine-to-machine (M2M) data transfer protocol . MQTT was created with the goal of collecting data from many devices and then transporting that data to the IT infrastructure. It is lightweight, and therefore ideal for remote monitoring, especially in M2M connections that require a small code footprint or where network bandwidth is limited.

How MQTT work

MQTT is a publish/subscribe protocol that allows edge-of-network devices to publish to a broker. Clients connect to this broker, which then mediates communication between the two devices. Each device can subscribe, or register, to particular topics. When another client publishes a message on a subscribed topic, the broker forwards the message to any client that has subscribed.

MQTT is bidirectional, and maintains stateful session awareness. If an edge-of-network device loses connectivity, all subscribed clients will be notified with the “Last Will and Testament” feature of the MQTT server so that any authorized client in the system can publish a new value back to the edge-of-network device, maintaining bidirectional connectivity.

The project is divided in 3 parts

First, we create MQTT server on RPi and install some libraries.

Second, we will install libraries in Arduino IDE for NodeMCU to be work with MQTT, upload the code and check whether server is working or not.

Third, we create a script in Rpi ,upload the required code in NodeMCU and run the python script to control leds from both server and client side.

Here, server is RPi and client is NodeMCU.”