Reader small image

You're reading from  DIY Microcontroller Projects for Hobbyists

Product typeBook
Published inJul 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781800564138
Edition1st Edition
Languages
Right arrow
Authors (2):
Miguel Angel Garcia-Ruiz
Miguel Angel Garcia-Ruiz
author image
Miguel Angel Garcia-Ruiz

Miguel Angel Garcia-Ruiz is an Associate Professor of Computer Science at the School of Computer Science and Technology, Algoma University, Canada. He has taught microcontroller programming and interfacing, human-computer interaction, and interaction design courses. Miguel has a PhD in Computer Science and Artificial Intelligence from Sussex University, England. He has published articles on tinkering with technology applying microcontroller boards. Miguel has conducted research projects funded by Canada's Northern Ontario Heritage Fund (NOHFC), Algoma University, and the Mexican Ministry of Education.
Read more about Miguel Angel Garcia-Ruiz

Pedro Cesar Santana Mancilla
Pedro Cesar Santana Mancilla
author image
Pedro Cesar Santana Mancilla

Pedro Cesar Santana Mancilla is a research professor at the School of Telematics at the University of Colima in Mexico. His research interests focus on human-computer interaction, ICT for elderly people, Internet of Things, and machine learning. He is currently serving as president of the Mexican Association on Human-Computer Interaction (AMexIHC). He is a Senior Member of the IEEE, and ACM and serves as Chair of the Mexican ACM SIGCHI Chapter (CHI-Mexico). Pedro is a member of the Mexican Academy of Computing (AMexComp) and the Mexican Society of Computer Science (SMCC).
Read more about Pedro Cesar Santana Mancilla

View More author details
Right arrow

Chapter 9: IoT Temperature-Logging System

In recent years, the use of the internet has increased. This same increase has allowed the internet to evolve. Now we speak of things connected to this network; devices for everyday use that were not originally designed to have connectivity. This evolution has created the concept of the Internet of Things (IoT), which is defined by Morgan in Forbes (2014) as the "interconnection to the internet of commonly used devices that can complete tasks in an automated way."

The IoT is present in practically all fields of daily life, from health to education, known as the Internet of Medical Things (IoMT) and the Internet of Educational Things (IoET), respectively.

In this chapter, you will be introduced to the world of creating IoT applications with a temperature logging application for an STM32 Blue Pill board using an ESP8266 Wi-Fi module. With this knowledge, you will be able to build projects that can connect to the internet and present...

Technical requirements

The hardware components that will be needed to develop the temperature-logging system are as follows:

  • 1 solderless breadboard.
  • 1 Blue Pill STM32 microcontroller board.
  • 1 ST-Link/V2 electronic interface needed for uploading the compiled code to the Blue Pill board. Bear in mind that the ST-Link/V2 requires 4 female to female jumper wires.
  • 1 DS18B20 temperature sensor module.
  • 1 ESP8266 Wi-Fi module.
  • 1 FTDI adapter board.
  • 1 LED.
  • 1 220 ohm resistor.
  • 7 male to male jumper wires.
  • 5 female to female jumper wires.
  • A 5 V power source.

As usual, these components are very common, and there will be no problems in obtaining them. On the software side, you will require the Arduino IDE and the GitHub repository for this chapter: https://github.com/PacktPublishing/DIY-Microcontroller-Projects-for-Hobbyists/tree/master/Chapter09

The Code in Action video for this chapter can be found here: https://bit.ly/3vSwPSu

The...

Connecting a temperature sensor to the Blue Pill board

In this section, we are going to learn the hardware components needed to build a temperature-logging sensor using the STM32 Blue Pill and a temperature module.

To build an electronic device that measures temperature, you will need a sensor that monitors the environment and records temperature data. A microcontroller card is also necessary to be able to read the data from the sensor and to be able to display the information to users. We will begin by having a look at the temperature sensor module.

Introducing the DS18B20 temperature sensor module

Let's get to know the main hardware component's details to build the temperature log: the DS18B20 sensor. It is a digital temperature sensor that can measure air temperature, liquids (using a waterproof version), and soil.

Important note

The DS18B20 temperature sensor has a unique 64-bit serial code, allowing multiple sensors to be connected using just one digital...

Coding a temperature reading system

In this section, we will develop the program to take temperature readers from a sensor. As mentioned, the DS18B20 sensor works with the 1-wire protocol, so we will use the Arduino IDE libraries to program it. Let's get started:

  1. As the first step, we are going to install the OneWire library. Open the Arduino IDE, and then go to the Tools menu and then Manage Libraries (see Figure 9.8):
    Figure 9.8 – Library manager

    Figure 9.8 – Library manager

  2. Next, we will search the library by entering the word OneWire in the search box. We will install the one created by the 1-wire protocol developers, so please install the one from Jim Studt and his colleagues (see Figure 9.9):
    Figure 9.9 – Installing the OneWire library

    Figure 9.9 – Installing the OneWire library

  3. Next, we are going to add the Dallas Temperature library. For this, we enter ds18b20 in the search box and install the library developed by Miles Burton and collaborators (see Figure 9.10). This library is also available from the sensor producers...

Learning to connect the ESP8266 module

As we learned at the beginning of the chapter, an electronic device to be considered an IoT device must have an internet connection and make its data available through this medium.

Due to the aforementioned requirement, we will use a module that will give our temperature-logging system the ability to connect to the internet. This component is the ESP8266 Wi-Fi module.

Now, we are going to learn the hardware components needed to connect the STM32 Blue Pill to the internet using the ESP8266 Wi-Fi module. The first thing will be to know and understand the Wi-Fi module.

An introduction to the ESP8266 Wi-Fi module

The ESP8266 is a microcontroller with integrated Wi-Fi communication, and its main advantage is its very low cost compared to other chips with similar characteristics. By itself, it can work as a microcontroller, such as Arduino or Blue Pill, but it is widely used as a Wi-Fi module for other microcontrollers that do not have a...

Coding a program to send the sensed temperature to the internet

Now, we need to develop the software for connecting the temperature sensor to the internet using the ESP8266 Wi-Fi module. Let's begin:

  1. Open the Arduino menu and select Preferences.
  2. Add https://arduino.esp8266.com/stable/package_esp8266com_index.json to the Additional Boards Manager URLs field. You will need to separate the text with a comma from the link of the STM32 module that we installed in the first chapters.
  3. Install the esp8266 platform. Go to the Tools menu and select Board followed by Boards Manager (see Figure 9.16):
    Figure 9.16 – Installing the esp8266 platform

    Figure 9.16 – Installing the esp8266 platform

  4. Including the libraries will be the first step in the code:
    #include <DallasTemperature.h>
    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
  5. setup() will contain all the programming logic. We need to start the serial data transmission and assign the speed of the transfer (this time we will use 115...

Connecting the STM32 Blue Pill board to the internet

The previous code snippets sense a sensor to measure the temperature and send the sensed data to the internet.

When the user requests the temperature from the web browser, the STM32 microcontroller will receive a request to blink an LED and consequently link it to our IoT environment created with the ESP8266 Wi-Fi module.

Figure 9.18 shows the connections required to interface the STM32 and the SP-01:

Figure 9.18 – Connecting the STM32 to the internet

Figure 9.18 – Connecting the STM32 to the internet

Figure 9.19 shows the actual device connections between the STM32 and the SP-01:

Figure 9.19 – Physical connections between the STM32 and SP-01

Figure 9.19 – Physical connections between the STM32 and SP-01

To complete the connection between the STM32 and the SP-01, we need to add a few lines of code to the Chapter09/wifi script:

const int toInternetPin = 0;

In the preceding line, add a constant to store the input pin used to receive the data from the internet...

Summary

What have we learned in this project? Firstly, we learned how to connect a temperature sensor to the STM32 Blue Pill microcontroller board. We then wrote the code to read the temperature and send it to our microcontroller. Subsequently, we learned how to connect a Wi-Fi module to our STM32 and code a sketch to connect the board to the internet.

This project has given us the skills to begin to create an IoT application, a great skill in this hyper-connected world. In the forthcoming chapters, you will be able to apply what you have learned since they consist of projects that require an internet connection. In Chapter 10, IoT Plant Pot Moisture Sensor, you will learn about measuring the moisture of a pot through a sensor and sending it to the cloud. We will visualize the sensor data on a web page.

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
DIY Microcontroller Projects for Hobbyists
Published in: Jul 2021Publisher: PacktISBN-13: 9781800564138
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

Authors (2)

author image
Miguel Angel Garcia-Ruiz

Miguel Angel Garcia-Ruiz is an Associate Professor of Computer Science at the School of Computer Science and Technology, Algoma University, Canada. He has taught microcontroller programming and interfacing, human-computer interaction, and interaction design courses. Miguel has a PhD in Computer Science and Artificial Intelligence from Sussex University, England. He has published articles on tinkering with technology applying microcontroller boards. Miguel has conducted research projects funded by Canada's Northern Ontario Heritage Fund (NOHFC), Algoma University, and the Mexican Ministry of Education.
Read more about Miguel Angel Garcia-Ruiz

author image
Pedro Cesar Santana Mancilla

Pedro Cesar Santana Mancilla is a research professor at the School of Telematics at the University of Colima in Mexico. His research interests focus on human-computer interaction, ICT for elderly people, Internet of Things, and machine learning. He is currently serving as president of the Mexican Association on Human-Computer Interaction (AMexIHC). He is a Senior Member of the IEEE, and ACM and serves as Chair of the Mexican ACM SIGCHI Chapter (CHI-Mexico). Pedro is a member of the Mexican Academy of Computing (AMexComp) and the Mexican Society of Computer Science (SMCC).
Read more about Pedro Cesar Santana Mancilla