Friday, 30 October 2020

Setting up WiFi communication between Raspberry Pi and ESP8266 with MQTT

One of the fundamental components in setting up your DIY smart home or IoT project is to set-up the communication method between your devices and the server.

Given the most accessible wireless network to most of us is our home WiFi, I will be using the MQTT protocol to send messages over WiFi. The main server for this will be a Raspberry Pi and the devices (or clients) are ESP8266, ESP32 or similar devices

Setting up the Raspberry Pi for MQTT

Mosquitto is a software that acts as the MQTT communication broker. The software is responsible to ensure the messages from the connected devices are received and delivered to the correct recipients. The Mosquitto server can installed on the Raspberry Pi using the following terminal instructions (For Raspbian-Jessie sketch). (More details can be found on this Instructable)

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo apt-get install mosquitto -y
sudo apt-get install mosquitto-clients -y

The Eclipse Paho MQTT client library was also installed in order to allow the raspberry pi to connect to mosquitto as a client.

sudo pip install paho-mqtt

Setting up the ESP-8266

Similar to the Raspberry Pi, the ESP8266 processor was also set up for MQTT communication using a library developed by Adafruit to act as an MQTT Client, “Adafruit MQTT Library”. Using the pre-defined functions in this library, the code was written to communicate over MQTT.  (More details can be found on this Instructable)

No comments:

Post a Comment