Code explanation
The hardware has been set up and now we need to write and flash the code for it. The code will do the following:
- Configure the pins for connecting the sensors.
- Connect to the MQTT broker (the Pi’s broker in this case).
- Subscribe to the relevant MQTT topics.
- Get the sensor values and publish them to their particular topics. This will reflect on the dashboard in real time.
- Reconnect to the MQTT server if it disconnects.
The last two steps will run indefinitely. Now, we will look at the code in chunks, and finally, we will look at the whole code and the relevant GitHub link. So, let’s get started with the code explanation. As the code is a little complicated, the code has been divided into important subsections for clarity and better understanding.
To import the required libraries, we need the following:
#include <ESP8266WiFi.h> #include <PubSubClient.h> #include <Wire.h> #include <Adafruit_BMP280...