Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

Currently, the Internet of Things (IoT) is considered one of the biggest trends in the market, but the actual development situation is quite different: there are many protocols, components, and tools available, making it overwhelming for developers. Often, they don’t know where to start, and the efficiency and outcomes of development are not effectively guaranteed. So how can we improve this situation? Let’s hear from the users of the Tiangong platform.
Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT
Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT
Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

The reason why Baidu Cloud’s Tiangong IoT platform is increasingly favored by developers is that it provides an end-to-end basic platform from the edge to the cloud, covering IoT data access, device management, data processing, data storage, data visualization, IoT security, and edge computing, significantly reducing the difficulty of IoT application development, allowing IoT developers to focus on application innovation.

Recently, I received a submission where the author completed the setup of a simple environmental monitoring station using the Baidu Cloud Tiangong IoT platform in just 20 minutes, achieving environmental data collection and visualization, which can be considered a model of efficient IoT application development.

Here is the full submission (with benefits at the end)

Recently, my company had an IoT project that needed to collect environmental data and visualize it. Since I had never done an IoT hardware-related project before, I looked for IoT solutions in the market. In practical project experience, I found that IoT projects are not as straightforward as traditional web architectures (LNMP) and involve various protocols and components. Without a good solution, developers need to spend a lot of effort on data access, protocol support, and data processing, leading to low development efficiency.

After much deliberation, I found the Baidu Cloud Tiangong IoT platform quite appealing. To quickly validate feasibility, before starting the formal project (which is also based on ESP8266), I first used temperature and humidity as basic data to create this small demo for practice, completing the entire process in 20 minutes with very good results. I hope this article can provide some useful help to everyone.

Preparation of Basic IoT Hardware

For the development board, I chose a board called NodeMCU. NodeMCU is a very user-friendly and inexpensive IoT development board, equipped with a USB-to-serial chip, WiFi, and multiple GPIO interfaces, allowing connection to multiple sensors. The hardware of NodeMCU is based on ESP8266, with built-in WiFi support.

For the sensor, since this demo is for practice and does not require high precision, I chose a low-cost temperature and humidity sensor: DHT11. DHT11 is a temperature and humidity sensor that can be used to test environmental temperature and humidity, with a humidity accuracy of ±5% RH, temperature accuracy of ±2°C, humidity range of 20-90% RH, and temperature range of 0~50°C.

With the basic IoT hardware selected, let’s look at the preparation of the IoT cloud platform.

Preparation of IoT Cloud Platform

My goal is to upload data to the cloud and visualize it. Of course, if conditions allow, you can also set up your own MQTT Broker and data storage and display parts, but this requires a lot of time and effort. Here, to make development quicker, I chose a cloud service that provides an IoT platform.

After evaluating several IoT platforms, I decided to choose Baidu Cloud Tiangong for three main reasons:

  • Baidu Cloud Tiangong supports standard MQTT access.

  • No coding is required for the storage process.

  • It provides ready-made visualization report-making tools.

Next, I registered and operated on the Baidu Cloud Tiangong platform.

First, register a Baidu Cloud Tiangong account and authenticate the Tiangong IoT platform. In the left panel under ‘Product Services’ – ‘IoT Services’, select ‘Device Access’ and purchase a one-month package (choose the number of messages according to your situation; since I report a lot of data, I chose 2 million messages). Then, enter the project list and create a device-type project. Click on the project name to enter the project details page and click ‘Create New Device Model’ .

Then, fill in the ‘Name’ and ‘Add Attributes’. The attributes here are the information we need to collect and report, so I added two attributes:

  • Temperature (温度) number type

  • Humidity (湿度) number type

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

After creating the device model, the next step is to create the device shadow. You can customize the name, which will be used later. Select the model you just created for the device model and click create to complete it. After creation, obtain the connection information for future use (the key in the connection information is only displayed once after generation).

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

So far, our preparation work is complete. The data storage part (time series database TSDB) can be enabled at this step or connected later. Next, I will complete the setup of the simple environmental monitoring station in six steps.

1. Selection of Development Board System

This development board supports multiple operating systems, such as NodeMCU (lua), ESPEasy (WebUI), MicroPython, and the Mongoose OS (NodeJS) that we will use today.

No matter which of the above firmware you choose, it can run MQTT. For example, in the Baidu Cloud Tiangong documentation, the lua version under NodeMCU firmware.

Since Mongoose OS does not require third-party tools for flashing the system and has a web console for easy management and debugging, I chose the Mongoose OS firmware this time for convenience.

2. Wiring

Connect the sensor to the board using Dupont wires. DHT11 generally has three pins, but the labeling and order may vary depending on the manufacturer, so please check the module’s labeling during wiring. The one I have is labeled as follows:

  • VCC (+, 3.3v, etc.)

  • DATA (D, S, SIG, etc.)

  • GND (-, G, etc.)

Connect VCC and GND to the corresponding labeled positions on the board; DATA can be connected to any GPIO pin.

Since D4 is close, I chose GPIO pin D4.

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

Wiring Diagram

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

Physical Diagram

3. Flashing the System

First, you need to install the driver.

The NodeMCU boards available for purchase online generally have either CP2102 or CH340 USB-to-serial chips, and the driver download addresses are:

CP2102:https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers

CH-340:http://www.wch.cn/search?q=340&t=downloads

After downloading the driver, we need to start the installation.

We will download Mongoose OS. Depending on your operating system, go to the MongooseOS website: https://mongoose-os.com/software.html to select the corresponding installation method.

My operating system is macOS, so I directly used the following command to install.

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

After the installation is complete, the browser will automatically open. At this point, you will see the guide interface.

If it does not start, you can enter cd .mos/bin/ or go to the custom installation directory and execute ./mos to run the management interface. (Windows users can double-click directly).

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

Next is flashing the firmware. Connect the board to the computer using a Micro-USB data cable, and in the guide interface, select the corresponding port for the board and click Select.

The platform will default to (ESP8266), and for the app part, I chose demo-js, which I will modify later to collect and report data.

For the WiFi part, fill in the hotspot you want the board to connect to (Note: only supports 2.4G). Then click Done, and the indicator light on the board will flash at a faster frequency.

4. Collecting and Reporting Data

After flashing is complete, you can see the code editing interface in the browser. The system will execute init.js after startup, and since our code is quite short, we will write everything in this file.

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

The code is as follows, where:

  • pin corresponds to the ESP8266 number, so the D4 we wired corresponds to GPIO pin 2 here.

  • In $baidu/iot/shadow/xxxxxxxxxxx/update, replace xxxxxxxxxxx with the name of the device shadow you created earlier in Baidu Cloud Tiangong.

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

You may be curious about where the previous connection information went. In MongooseOS, the MQTT connection information is written in the configuration file.

Click on the DeviceConf on the left side of the MongooseOS browser interface to see the MQTT configuration interface. Fill in the previous connection information and click Save in the upper left corner. Additionally, switch to Expert View and set the RPC mode to Disabled.

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

Then return to the Projects code editing interface, click the icon that looks like an upload (as shown), save the file, and upload it to the board to restart.

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

At this point, you can see that the Logs are printing temperature and humidity information, and showing successful reporting (Yes). If it shows no, you can use the local Log and the ‘Platform General Function’ – ‘Logs’ in Tiangong to troubleshoot errors.

After successful transmission, you can see the reported data on the Tiangong ‘Device Shadow Details’ page.

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

Thus, we have completed the entire process of data collection and reporting.

Wait a minute… wasn’t there a nice picture in the second image at the beginning? Is it fake?

Of course not. The next part requires no coding, just configuration on the cloud platform.

5. Data Storage

In the chart I displayed, there are real-time values and line charts. The line chart shows historical data over a period of time. Therefore, we need to store the data. Initially, I thought about storing the data in MySQL or MongoDB, but after looking at some IoT platforms, especially Baidu Cloud Tiangong, I found that ‘Time Series Database (TSDB)’ is a better choice.

Later, I discovered that in Tiangong, the data we report to the device shadow can be simply stored in TSDB without writing any code. However, if more complex logic processing is needed, it seems that code must still be written, or a service called rule engine can be used.

To get back on track, since what we want to store is quite simple, we will use the existing features. On the ‘Device Details’ page (where we previously obtained the connection information), click edit, enable ‘Storage Configuration’, change ‘Do Not Store’ to ‘Store on Report’, choose which database to store it in (create one if it doesn’t exist, the default configuration is sufficient), and fill in the metric name as monitor, then click save.

This way, the storage part is configured, and every piece of data reported will automatically be stored in TSDB. You can select ‘Time Series Database’ from the left navigation to view the historical data stored in the corresponding database.

6. Create a Nice Report

After storing the IoT data, the next step is to visualize the data.

Similarly, continue using a feature called ‘Device Visualization’ from Tiangong. This is also one of the main reasons I chose this Baidu Cloud platform.

This feature has less guidance, and I learned how to use it by reading the documentation. After figuring it out, I found it super easy and convenient. I will use the report from this project as an example to roughly explain the steps:

  • First, go to Device Visualization and click Create ‘Dashboard’

  • Once inside, there are two tabs at the top; select ‘Data Table’

  • On the left, create a new data table, since I have two gauge charts and one time series line chart, I need to create two data tables, one for time series database type and one for device management type.

  • After creating, return to ‘Dashboard’. Drag the needed charts to the blank area, click the corresponding chart, and set the binding data on the right as shown in the figure below:

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

This only introduces a small part of the functionality; if you want to display more parts, you can refer to the documentation.

Conclusion

At this point, the setup of a simple environmental monitoring station is complete. Let’s take a look at the specific effects:

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

Want to know how much this project cost in total?

It might shock you: 20 yuan!!!

20 yuan = (NodeMCU board 13.5 yuan) + (DHT11 sensor 4.5 yuan) + (Time Series Database 2 yuan)

This is a basic temperature and humidity collection and reporting system. We can also continue to enhance its functionality. For example, add infrared, so that when the temperature and humidity reach a specified threshold, it triggers the air conditioning switch; develop a mobile app or WeChat mini-program to control the infrared remote on the board…

There are many similar scenarios, but I won’t elaborate on them here.

End Benefits

Thanks to this thoughtful developer for allowing us to experience the practical situation of an IoT project.

To encourage developers to actively participate, Baidu Cloud will provide a batch of small gifts. Please, everyone, in the comments, tell us what applications you would like to develop through Tiangong. The top five students with the most likes and who pass the Baidu Cloud enterprise certification will receive a NodeMCU development board from Baidu Cloud Tiangong.

Don’t wait, act now, everyone!

Build a Simple Environmental Monitoring Station in 20 Minutes Using IoT

Leave a Comment

Your email address will not be published. Required fields are marked *