兰博

[Tuya Smart] Temperature and hygrometer @ART-Pi_by_RT-Theard

 
Overview

Foreword:

This is the second time I have participated in the training camp of Lichuang & Tuya. I have always wanted to make a custom MCU development plan in the last issue, and this time it is finally here. He's coming, he's coming, he's coming ^.^

-Tuya Smart is a global cloud development platform and AI+IoT developer platform that connects the intelligent needs of consumers, manufacturing brands, OEM manufacturers and chain retailers, and provides developers with one-stop PaaS-level solutions for artificial intelligence IoT plan. It also covers hardware development tools, global cloud, and smart business platform development, providing comprehensive ecological empowerment from technology to marketing channels to create the world's leading IoT OS. The world is big, let’s doodle together!

-Lichuang Mall is a one-stop self-operated shopping mall for purchasing electronic components   operated by Shenzhen Lichuang E-Commerce Co., Ltd.  [1] . Engaged in the retail and small-batch procurement of electronic components, it is a one-stop component procurement platform with complete varieties, spot sales, and quality assurance. It is really easy to use! Lots of information.

In January 2021, Lichuang Open Source Platform held two training camps at the same time. I want to participate in both, but I don’t have the resources to do it. What should I do? Hey, wouldn't it be great if the two projects were connected? It would save a lot of development time.

-Want to know how I did it? Then read on, the specific plan will be explained below!

 

Figure 1: tuya&lc+ temperature and humidity sensor

 

Plan ideas:

The first event in January is the ART-Pi development board expansion . If you want to know more about the RT-Thread system, please go to [RT-Thread Documentation Center] .

Here , we will explain the Android APP board design based on the Tuya module. Interaction between [MainCtrl_ART-Pi] and [tuyaMode]

VDVtBaR7EclTALsFGQmJH9CLl6j34yHSus1wwV0t.png   

Breakdown of ideas :

 

  1. Tuya APP

--Tuya APP generator contains many fixed and commonly used functions, just check and add them. The timing of the event may not all be realized, but it can be gradually improved later. Just add them all here.

--In addition to basic functions, there are also customized functions. Such as adjusting the fan. When the default template function is not included, it is necessary to design a custom APP panel.

The picture shows the design effect of the custom panel.

 

  2.Hardware design

a, [MainCtrl_ART-Pi_SCH] has a DHT11 temperature and humidity sensor onboard. It is installed on the board in a direct plug-in form, and there is a fan interface to facilitate the adjustment of ambient temperature and humidity.

 

 

--DC12V power conversion; four digital tubes; TPAD touch buttons; photoresistor and two ordinary buttons. Data transfer[USART1]

 

 

b, [tuyaMode_SCH] +5V to +3.3V power supply; Tuya WB3S module; 2.23'' OLED reserved.

 

 

  3. Software writing (By RT-thread studio compiler )

a. Function introduction: system solution

-->Initialize the system after powering on, and perform temperature acquisition driver initialization-->Button driver initialization-->Light intensity driver initialization-->Serial port 1 driver initialization-->Tuya wifi protocol initialization.

-->After Tuya is initialized, create a temperature and humidity monitoring thread to read and compare temperature and humidity changes, perform a temperature and humidity upload, and synchronize the digital tube display and the APP display. Suspend and wait for data changes before uploading.

-->Set the running indicator light.

-->Enter the main function loop, turn on the Tuya WiFi receiving service, and synchronize the heartbeat.

-->Complete

 

b, tuya protocol analysis: MCU SDK transplantation

-Select your own products after logging into the Tuya Iot platform . After the hardware design part is deployed (if it is a custom development solution), download the MCU SDK file at the bottom.

-The article contains a [MCU SDK Transplantation_Tuya Smart_Document Center.pdf] which describes in detail the specific method of implementing the communication protocol, just follow the reference.

-Due to space constraints, if you still don’t understand something, please go to [MCU SDK Transplantation-Document Center-Tuya Developer] .

Note: Please confirm whether the product information in the [protocol.h] macro definition corresponds to your project product ID, otherwise your product cannot be linked for distribution network communication.

 

gF2EGx8WecT4asD5rB3LjVCyzDk4s8SnqCFEZLAM.png

 

c. User logic code: select key points to focus on.

--Description: After the system is initialized and started, four tasks are mounted: <1. Temperature and humidity collection<2. Button detection<3. Light intensity collection<4. Serial communication

 

 

--accomplish:

1>Thread one: void hygroThermo(void); as shown in line 112, thread function name>hygroThermo_Run, parameter RT_null, 1024 thread stack, 10 priority, 10 time slices

--Function DH11 is a single bus device. It can read 16-bit data by making a soft driver GPIO simulation form. Decomposing integers and decimals can be displayed on the 4-digit digital tube.

--For specific implementation, please refer to the thread function >hygroThermo_Run() in the attached file [HygroThermo.c]; every 1000 system ticks the temperature and humidity polling digital tube display

 

 

2>Thread two: int TPAD(void); thread creation is the same as one. As shown in Figure 177, MSH is exported to the command line, and this function can be called through the console. This is the function of RT-Thread.

--The function uses the input capture of timer 3 to obtain data. Channel 3 of timer3 collects 10 times. After removing the maximum and minimum values, the average of 8 times is taken to obtain the data.

--For specific implementation, please refer to the thread function > TPAD_Run() in the attached file [TPAD.c]; a key press is detected every 50 system ticks.

 

 

3>Thread three: void light_sensation(void); task creation is the same, so I won’t go into details here.

--The function is implemented by the MCU's ADC1 peripheral, which collects the average data 20 times from external channel 1. For specific implementation, please refer to the attached file [LightSensation.c]

--As shown below, the task function>light_sensation_Run(); detects the light intensity every 50 system ticks, and then sets the brightness of the digital tube, similar to the automatic dimming of a mobile phone.

 

ZEZ0GpRTYwRqO6XcW1G7zujI4wDXyFDJ0YeMmqZi.png

 

4>Thread 4: Serial communication does not create a separate thread, but uses the thread that hosts the function to run part of it.

--Data reception: After main initializes the basic peripherals, it enters the large while loop; in order to ensure that Tuya runs normally and avoids unnecessary bugs, critical section code protection is used.

 

Vajj3HCWDysqSnLEoB7xZWwKEdTKTMcCv00qDYBT.png

 

--Data upload: defined in the [tuyaconvert.c] file. When using it, just fill in the relevant operation functions. As an example, here is the upload of temperature and humidity. There are 279 lines of records to detect the temperature and humidity values, which are detected every 10,000 system ticks. When changes occur, they are refreshed to the mobile phone to maintain data consistency.

 

dwP4aghG2lNyoTbMMcXafJ7bG0g5y7FRotcUqd51.png

 

 

Show results:

 

Figure 2: One-click dehumidification and cooling

 

Figure 3: One-click dehumidification and cooling via APP

S8pJb91M7uKh47UeIgFcLsQj2knxwz8mgOzGuvM1.png

 

Figure 4: Light sensor automatically adjusts brightness

 

 

Precautions:

 

1. Not all functions are displayed. This project was developed using the RT-thread studio compiler and contains the RT-Thread system, which comes with many software packages and integrated extended functions. For example, with the finsh command, you can enter the command through the editor terminal to call the function. Debug information will also be output in the same manner, as shown in the figure indicating whether the thread was successfully started after restarting.

 

O6DnaIxb75eT5xBSyhc7rMmdc4kuVP4pPYUkaA2W.png

 

2. The Tuya module provides 22 default functions. There are also brightness, anti-tamper, timing, switch and other functions, which can be realized one by one according to needs. This project implements the reporting function of temperature and humidity.

3. Due to time and space reasons, the implementation and use of other functions such as 2.23-inch OLED will not be demonstrated here. If you want to know more, please contact me or implement it by yourself.

4. Attachment 7z shows the entire project. .zip The peripheral drivers used in this project (the .c mentioned in the article are all in it). The resources contain some pictures and videos.

 

 

 

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-06-19 01:35:22

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号