Reader small image

You're reading from  Raspberry Pi and MQTT Essentials

Product typeBook
Published inSep 2022
PublisherPackt
ISBN-139781803244488
Edition1st Edition
Right arrow
Author (1)
Dhairya Parikh
Dhairya Parikh
author image
Dhairya Parikh

Dhairya Parikh is an Electronics Engineer who currently works as a Data Engineer at Accenture. He has a year’s experience in building and maintaining data pipelines for a huge amount of data. In his free time, he builds IoT and Machine Learning projects and even writes about them. He has written several project articles for Circuit Cellar, which is a monthly tech magazine. He makes projects which positively impacts the society, making people’s life easier.
Read more about Dhairya Parikh

Right arrow

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:

  1. Configure the pins for connecting the sensors.
  2. Connect to the MQTT broker (the Pi’s broker in this case).
  3. Subscribe to the relevant MQTT topics.
  4. Get the sensor values and publish them to their particular topics. This will reflect on the dashboard in real time.
  5. 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...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Raspberry Pi and MQTT Essentials
Published in: Sep 2022Publisher: PacktISBN-13: 9781803244488

Author (1)

author image
Dhairya Parikh

Dhairya Parikh is an Electronics Engineer who currently works as a Data Engineer at Accenture. He has a year’s experience in building and maintaining data pipelines for a huge amount of data. In his free time, he builds IoT and Machine Learning projects and even writes about them. He has written several project articles for Circuit Cellar, which is a monthly tech magazine. He makes projects which positively impacts the society, making people’s life easier.
Read more about Dhairya Parikh