Designing a prototype of smart temperature and humidity sensor product (MCU access)

Last Updated on2020-08-06 06:18:49

Overview

As the basic sensing category, temperature and humidity sensors are broadly used in the field of smart home. Collecting temperature and humidity through temperature and humidity sensors can realize a variety of intelligent scene linkage. For example, during the hot summer, when the temperature and humidity sensor detects that the indoor temperature is higher than 29 ℃, it will turn on the air conditioner. When the indoor temperature drops below 23 ℃, it will turn off the air conditioner. The temperature is always in a comfortable range, which greatly facilitates our lives.

Based on the Tuya Smart platform, the development of a smart temperature and humidity sensor can be completed simply and quickly, and the product can be intelligently connected on the App. This tutorial describes how to use the Tuya sandwich development board and Arduino to quickly complete a smart temperature and humidity sensor product prototype.

Materials

Hardware (3)
  • NUCLEO-G071RB

    Count:1

    more

  • Tuya Sandwich Wi-Fi MCU Communication Board (E3S)

    Count:1

    more

  • Tuya Sandwich temperature and humidity sensor function board

    Count:1

    more

Steps

  • Step 1: Overview

    The MCU solution is different from the SoC solution. The driver code of the sensor and networking module is written in the MCU. You can develop the MCU code by yourself and have more playability. Before development, you must have an overall understanding of the program to facilitate subsequent development.

    In the temperature and humidity sensing kit of the Tuya sandwich development board, the sensor board collects temperature and humidity data and passes them to the MCU through the I2C interface. The MCU control board is connected to the Wi-Fi communication board through the serial port.

    By using the Tuya smart app to configure the network, the humidity sensor can be displayed and controlled on your phone and linked with other smart devices. The MCU model is STM32G071RB, and the Arduino IDE for programming is used, which can be easily transplanted to each Arduino series development board.

  • Step 2: Hardware connection

    The Tuya sandwich development board sensor kit consists of the following parts.

    • Temperature and humidity sensor function board, which carries a SHT30 temperature and humidity sensor and I2C interface. This board collects and forwards the temperature and humidity data.

    • Wi-Fi MCU communication board, which carries a Tuya TYWE3S module and works for network connection. The module is burned with general firmware, and the MCU is connected to the Tuya serial port protocol, and therefore the module, App, and cloud platform can be used together.

    • MCU control board is based on the ST official NUCLEO-G071RB development board, it receives the sensor data and controls the module communication. The NUCLEO-G071RB development board supports the Arduino interface.

    After assembling the control board, communication board and function board of the sandwich development board kit, it is shown as follows.

    image.png

  • Step 3: Downloading the sample

    The software development is based on Arduino to achieve protocol interaction between MCU, sensors, and modules. Firstly, the communication between the MCU and the module can be adjusted to realize network configuration of the App, and the MCU data is transmitted to the App.

    Step 3.1: Environment setting up

    For detailed instruction on environment setting up of sandwich development board, see MCU Development environment setup.

    The preceding reference talks about how to add the ST development board to the Arduino IDE, and integrate the STM32CubeProgrammer to realize the compilation and download function. You must read it carefully.

    Step 3.2: Downloading the sample

    After the environment is ready, you can download the sample code for the combined temperature and humidity sensor kit, and use the Tuya smart app network configuration to experience the demo function. For more information, click Temperature and humidity sensor sample.zip.

    Upon using the sample, you can long press the button on the MCU development board for more than 2 seconds to switch the network configuration mode of the module. By default, the sample uses SHT30 periodic mode, which collects sensor data on a periodic basis, by short press the button the data can be reported.

  • Step 4: Sensor debugging during software development

    After experiencing the sample, you can learn the process of how to implement the demo and develop a custom temperature and humidity sensor.

    Step 4.1: Download the development kit

    Create a project, and then refer to the tutorial for transplanting the Tuya MCU SDK development kit MCU Quick Start for more information.

    During the creation of the product, you must choose Security & Sensors > Temperature and Humidity Sensor > Custom > Temperature and Humidity Sensor, shown as follows. When the MCU is connected, select a custom solution before downloading to the MCU development package.

    image.png

    The function is selected according to your needs. Here, the basic function can only select the temperature and humidity display function by default.

    image.png

    When choosing the firmware, note that the default firmware of the Wi-Fi module is general firmware, rather than the low-power firmware. You must change the power consumption type to standard power consumption. Select the standard general firmware before you can download to the relevant MCU development kit.

    image.png

    image.png

    Step 4.2: Hardware test

    After downloading the relevant MCU development kit, you can use the Tuya module debugging assistant in the development kit. The assistant simulates the MCU mode and cooperate with the debugging module communication board to verify whether the module is communicating properly. You can skip this step if you are sure that the communication board is available. If you have any questions about the sending and receiving data of the protocol during debugging, you can also use this assistant to view the correct data exchange format. For more information, see Wi-Fi Module MCU Development Overview.

    Step 4.2: Porting and debugging

    After successful porting, you will see the selected product panel. The temperature and humidity are now showing the initial value. Next, you must transplant the SHT30 driver code to obtain the temperature and humidity data and pass it to the App to display.

    For detailed instruction on migration and debugging, see MCU Quick Start.

    image.png

  • Step 5: Sensor debugging during software development

    You can download the SHT30 driver sample for reference. SHT30_Drive.zip

    Add the SHT30 driver to the project, take the periodic mode acquisition as an example, the application layer code sample is as follows.

    #include <Wire.h>
    #include <SHT3XD.h>
    
    ClosedCube_SHT3XD sht3xd;
    
    void setup()
    {
    	Serial.begin(9600);
    
    	sht3xd.begin(0x44);
    	Serial.print("SHT3XD Serial #");
    	Serial.println(sht3xd.readSerialNumber());	
    
    	if (sht3xd.periodicStart(REPEATABILITY_HIGH, FREQUENCY_10HZ) != NO_ERROR)
    		Serial.println("[ERROR] Cannot start periodic mode");
    
    }
    
    void loop()
    {
    	printResult("Periodic Mode", sht3xd.periodicFetchData());
    	delay(250);
    }
    
    void printResult(String text, SHT3XD result) {
    	if (result.error == NO_ERROR) {
    		Serial.print(text);
    		Serial.print(": T=");
    		Serial.print(result.t);
    		Serial.print("C, RH=");
    		Serial.print(result.rh);
    		Serial.println("%");
    	}
    	else {
    		Serial.print(text);
    		Serial.print(": [ERROR] Code #");
    		Serial.println(result.error);
    	}
    }
    

    After successfully acquiring the temperature and humidity sensor data, the MCU needs to transmit the data to the App for display. The module will filter the same data, so it is recommended to periodically acquire the data, and then report when the data changes, reducing the frequency of data reporting. The reporting code is as follows.

    if(updata_flag==1)
    {
      mcu_dp_value_update(DPID_TEMP_CURRENT,T); // VALUE type data reporting;
      mcu_dp_value_update(DPID_HUMIDITY_VALUE,RH); // VALUE type data reporting;
    }
    

    image.png

  • Step 6: Scene linkage for extended function

    After all functions are debugged, the App can receive the data of the temperature and humidity sensor. The most common scenario of smart sensors is to collect sensor data to link with other products. If you have Tuya ecological devices or development boards, they can configure the scene linkage function on the App. For more information, see Scenario Connection Settings.

Summary

By leveraging the Tuya Smart platform, the Tuya sandwich development board, Arduino IDE programming, you can quickly set up a smart temperature and humidity sensor product prototype.