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

Major Project 1: IoT Weather Station

Now that we are more knowledgeable about the topics discussed in the previous chapters, we will be making our first major project: an IoT weather station. This chapter gives step-by-step instructions on how to build this. The instructions will be divided into the following sections:

  • Hardware requirements
  • Code explanation
  • Raspberry Pi setup

The aim of this project is to build a fully functional weather station (based on the popular NodeMCU development board) whose readings can be monitored on a Node-RED dashboard in real time, which will be hosted on the Raspberry Pi. Note that we will be using MQTT as the communication protocol between the NodeMCU and the Raspberry Pi, whose host is also the Pi (hence, the dashboard will be only available on the local network). The final breadboard circuit is shown in Figure 5.1.

Figure 5.1 – Your very own NodeMCU-based weather station!

We will now look at the...

Hardware requirements

To build our weather station, we will require a development board that fetches the sensor values and sends them to a particular destination, Node-RED in our case, through a communication protocol. For the development board, we will utilize the NodeMCU development board for this project primarily due to its relevant features and cost-effectiveness. Please refer to the following figure to find the components required to build our weather station (Figure 5.2).

Figure 5.2 – The required hardware for the project

Next, we need to choose appropriate sensors so that the readings we get are both reliable and accurate. Hence, we choose the following three sensors for our project:

  • A DHT11 temperature and humidity sensor
  • A BMP280 pressure sensor
  • A CCS811 air quality sensor

We will also need something that we can use to interface all the sensors to the development board. For this, we will use a breadboard and some connecting...

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...

Raspberry Pi setup

The Raspberry Pi will be the host for the local MQTT broker in this project and also the dashboard hosting device. The dashboard for this project will be created using Node-RED and the Node-RED dashboard module, which will both be running on the Pi.

The previous chapters cover in detail how to set up and activate the MQTT broker (refer to Chapter 1,Introduction to Raspberry Pi and MQTT, to refresh your memory). If you have followed the book by chapter, you will already have the MQTT broker up and running on boot. The next step is to create the Node-RED flow and the dashboard for our project. Please follow these step-by-step instructions, which will walk you through the entire setup process.

Starting Node-RED

Run the node-red-start command on the Pi to start Node-RED at the following IP address: <pi's ip address>:1880. Please refer to Figure 5.8 for reference.

Figure 5.8 – Starting Node-RED on the Raspberry Pi through...

Summary

This chapter guided you through building your very first major project using MQTT and Raspberry Pi. We built an IoT weather station based on the NodeMCU development board. We started by discussing the hardware requirements for the weather station and briefly introduced the sensors we would be using. Then, we moved on to interfacing these sensors to the NodeMCU board and proceeded with writing the code for our weather station. In the next section, we set up the Raspberry Pi for this project. We created our project dashboard on Node-RED and added a new alert feature to our project as well.

As discussed above, this is a monitoring-based project, which only allows us to fetch the data from a node and display it on the dashboard. In the next chapter, we will explore the control capabilities of this system by creating a dashboard to control a smart home device (relay) based on another very popular development board, the ESP32 board. As a bonus, we will even create a printed circuit...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi and MQTT Essentials
Published in: Sep 2022Publisher: PacktISBN-13: 9781803244488
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

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