What Can ThingSpeak Do for Us? | IoT Cloud Kit for microbit-MakeCode-Tutorial
Lesson 3: What Can ThingSpeak Do for Us?
1. Learn and use ThingSpeak
2. Learn the application cases of ThingSpeak
3. HTTP communication protocol
4. Use ThingSpeak to receive the data collected by IoT cloud kit.
5. Learn the visual analysis and statistical analysis
Teachers: computer/smartphone, good network, IoT cloud kit.
Student: computer/smartphone, good network, IoT cloud kit.
Lead-in: Every day, we are surrounded by a variety of sensors, which monitor, detect and send data in unique forms. Through the previous 2 lessons, we have understood how the IoT works and the convenience that the IoT brings to us. But we did know how to obtain the data collected by the sensor. Next, we will learn how to receive, process and analyze the data collected by sensors through the ThingSpeak IoT service platform.
Introduction to ThingSpeak
IoT(Internet of Things) service is also cloud service, which provides two functions: 1. Receive data from device and send data to device; 2. Process and save data.
Question: In addition to ThingSpeak, what other IoT service platforms do you know that can collect and analyze data?
What is ThingSpeak
ThingSpeak, supported by MathWorks, is a cloud platform that collects and analyzes IoT data. It can access your data online or offline, and can visualize the data sent by the device in real time (multi-chart supported). And then by executing MATLAB code in ThingSpeak, you can analyze and process these data online. The advantage of ThingSpeak is that for those systems that need to be analyzed, there is no need to build a server or develop web software to build an IoT system. For small and medium-sized IoT systems, ThingSpeak can provide hosting solutions for use in production.
Communication Protocol Used in ThingSpeak
When sending data to the IoT service, the protocol for sending the data needs to be determined by the server. In order to receive messages from the server of the IoT, this function must be prepared in advance. Next, we are going to introduce the communication protocol used in ThingSpeak-HTTP protocol.
In the IoT, there are mainly two methods to send data from the device to the server: 1. Prepare a HTTP/MQTT protocol web system. 2. When performing real-time communication of voice and video, use communication protocols such as WebSocket and WebRTC. This lesson mainly introduces the HTTP protocol.
HTTP protocol is the abbreviation of Hypertext Transfer Protocol. It is a transfer protocol for transferring hypertext markup language (HTML) from a Web server to a local browser. The device uses the HTTP GET method and POST method to access the server, sends the parameters of data storing request and the Body together.
Note: the IFTTT learned before also employs HTTP communication protocol
How to use ThingSpeak
1. Windows system browser access URL: https://ThingSpeak.com/. Enter the homepage of ThingSpeak. Click Get Started Free to enter the page of sign up.
2. Fill in the information(email, address, and name), then click Continue.
3. Confirm the email address, tick Use this email for my MathWorks Account, and then click Continue.
4. Verify the MathWorks account. ThingSpeak will send you an email, click the link in the email. Then, click Continue.
5. Set your password, and click Continue.
6. Sign-up successful.
1. Click " Channels”-“MY Channels”-“New Channel” to create a new channel.
2. Then the project parameter design list will appear. If there are multiple parameters, select multiple Fields. After setting the parameters, slide down and click Save Channel.
3. The uploaded data will be displayed in the Channel, but the newly created Channel has no content.
We learned in the last lesson that Key is an important part of API. If we want to upload the collected data to the corresponding channel, we need to view the API key, which ensures that the data reaches the specified channel. The way to check API key is as follows:
Click API Keys->Write API Key.
What can ThingSpeak do for us
ThingSpeak can collect and analyze the data sent by the hardware. These functions might sound abstract, so we will give a few simple examples. You can use a temperature and humidity sensor to detect the temperature and humidity in the environment; use a soil humidity sensor to detect the water content of the soil in your plant pot; you can also use ultrasonic sensors or Hall sensor to create a home security system. In addition, ThingSpeak provides a free online MATLAB, through which we can directly analyze the data, and can also use it to execute some mini-programs. The so-called mini-programs, most are multi-functional and simple, and involve some network-related tasks, such as web crawlers. The MATLAB function can save the execution results of these mini-programs in the ThingSpeak channel. These program functions are automatically executed by cloud services, and you do not need to install MATLAB locally. From this perspective, ThingSpeak has transformed MATLAB from a language to a service.
Project Practice—Use IoT Cloud Kit to send data to ThingSpeak
In the previous section we learned how to connect the IoT cloud kit to the IFTTT IoT cloud service. Next, the tutorial mainly talks about how to collect the temperature value in the classroom, and how to calculate the average temperature in the classroom using visual analysis and statistical analysis.
Task1: Collect the temperature value in the classroom
At one time, the teacher asked us to observe the average temperature of the classroom during the day. In order to ensure the accuracy of the data, we must basically record it every few minutes. I thought at that time how good it would be if something could record the temperature automatically. Now I can directly use the IoT cloud kit to collect temperature data, and then upload the collected data to the ThingSpeak IoT cloud platform every 10 minutes.
First, we create a "Classroom temperature" channel in ThingSpeak. The steps are as follows:
Check the key
Enter my channel "Classroom temperature", click API Keys to check the key.
Because this project requires the IoT cloud kit, we need to prepare the hardware devices as shown in the figure below.
Insert the WiFi card into the SD card slot on the back of the micro:IoT Board for Cloud.
This project mainly triggers events through the clock function from the expansion board. When the specified period is reached (every 10 minutes), the temperature value will be sent to the channel specified in ThingSpeak. Then display the data through a line chart. The specific program flow chart is as follows:
*Program link address: https://makecode.microbit.org/_PAjeYtJvtFVU
Note: The function "function time" is the same as the water reminder in IFTTT.
After the power is turned on, the time, temperature and other data will be displayed on the OLED. Then open the "Classroom temperature" channel in ThingSpeak. Observe the indoor temperature change. As shown below:
Note: A line chart is used to facilitate the checking of data changes.
In this case, we collected temperature data from 10 am to 4 pm. From the line chart, it can be seen that the highest indoor temperature reaches 25°C and the lowest temperature is about 21°C. IoT platform can allows us to collect temperature data for a whole day or even longer.
Now that we have learned the using ThingSpeak to collect temperature data, we may collect humidity, sound, distance and other data later. In order to facilitate the distinction, we must establish separate channels. During the practice of the project, we must remember the key (API Keys) corresponding to each channel.
Question: Fill in the data you get in the table below.
The highest temperature The lowest temperature The average temperature
Task2: The average temperature in the classroom
We can see the highest temperature and the lowest temperature through the chart above. However, the average temperature requires us to record the temperature data at each time point in turn, and then calculate the final average temperature through a formula. If it is manually recorded and calculated, errors are hard to avoid. If you use the data analysis of the IoT, then we are able to reduce or avoid errors. Then, we will learn to use visual analysis and statistical analysis to calculate the average daily temperature.
Many devices in life now integrate a large number of sensors. For example, mobile phones, it has realized the collection of massive amounts of data. How can we filter the data we want in a bunch of data? Only by analyzing the sensor data according to the purpose can the best value be created for the service. For example, our purpose is to get the average temperature in the classroom on a certain day, how to analyze the data?
From the first lesson, we learned that there are two analysis methods for data analysis, "statistical analysis" and "machine learning". However, if distinguished according to the purpose of analysis, it can be divided into three types: visual analysis based on collection (the one we are going to talk about next); discovery analysis and predictive analysis based on statistical analysis and machine learning.
What is visual analysis
Visual analysis is to make data into charts, which is clear at a glance, according to requirements, through collection. Such as the project that collected the classroom temperature above.
Calculate the average daily temperature
We have mentioned earlier that the parameters of the ThingSpeak visualization chart can be customized. For example, we can set the " " of the chart to "daily", so that we can calculate the average daily temperature in the classroom. As shown below:
In addition to the above line chart, we can also set the chart type to a column chart. As shown below:
What is statistical analysis
Statistical analysis is to collect a large amount of data through mathematical techniques and find the correlation. For example, in order to achieve the purpose of saving energy for air conditioners, you can investigate the temperature of the room and the power consumption of the air conditioner when the air conditioner is operating at a fixed temperature, and then make these data into a table.
Calculate the average daily temperature
Earlier we have learned the calculation of the average temperature by setting the parameters of the ThingSpeak chart. Next, we will export the data in ThingSpeak and calculate the average daily temperature through the table. The steps are as follows:
Export the data in the "Classroom temperature" channel.
Note: Store your data package in a place where you can find it easily. This tutorial is saved on the desktop.
Use the Excel to open the file you saved, as shown in the figure 1.csv file.
Find the data on December 23, 2020. If the exported data contains dates of other days, you can insert two blank data columns to distinguish them. As shown below:
Use the automatic summation formula in Excel to sum the data on December 23, 2020. The sum temperature value is 1006.
4. According to Excel, a total of 44 valid temperature data were measured on December 23, 2020, so the average temperature on December 23, 2020 is 1006/44=22.86364
In this case, we learned two ways to calculate the average temperature in the classroom. According to the data, we can see that the data calculated by the two methods are basically the same. If you don't want to save the data on your computer, you can choose the visual chart ThingSpeak to calculate the average value, because only some parameters modified can the average value of different time periods be calculated. For example, if the Average is set to 60, the hourly average value can be calculated. And the column chart it showed is also clear.
Question: Fill in the accurate data obtained from your own analysis in the table below.
Average hourly temperature Average temperature every 4 hours Average daily temperature
Tips: Use the formula y=αx+β. x is the temperature, y is the electric quantity, and the relationship between the data can be obtained by calculating α and β
In this lesson, we learned how to use ThingSpeak to collect and analyze data, which helped us solve the problem of manually recording data in the past and analyze data. Now that we have mastered how to collect and analyze data, the next project is going to use the Easy IoT platform to learn how to remotely control devices.