1. Master control solution
The WB3S network module is used as the master control unit. WB3S is a low-power Wi-Fi and luetooth Low Energy combo module developed by Tuya Smart. It consists of a highly integrated radio-frequency identification (RFID) chip BK7231T and a few peripheral components, with a built-in Wi-Fi network protocol stack and various library functions.
To extend the insufficient ADC resources of the module, we need to modify the module by adding a 4-channel analog multiplexer/demultiplexer chip RS2255 to the circuit. As a digitally controlled analog switch, its on-resistance is only 24 ohms, and its leakage current is only 1 nA.
This solution uses many sensors. In order to reduce the wiring and make the layout more concise and neat, you can break out pins on the master board to connect to sensors. The levels at the communication interface between each module and the master control board do not match, and the power supply voltage varies by the modules. To troubleshoot the issue, we have designed the circuit and PCB as follows.
- Circuit diagram
- PCB layout
This solution uses a general-purpose, miniaturized ZE08-CH2O electrochemical formaldehyde module.
The module uses electrochemical principles to detect CH2O in the air, with good selectivity and stability. A built-in temperature sensor provides temperature compensation. Both digital output and analog voltage output are available.
- Electrical specifications are as follows:
- The definitions of pins are as follows:
- The serial communication protocol is as follows:
Note: Gas concentration (PPB) = high bit of gas concentration × 256 + low bit of gas concentration. When converted to ppm: ppm = PPB/1000, 1 ppm × 1.25 = 1.25 mg/m3
Among them,Byte8 check value = (reversed (Byte1+Byte2+…+Byte7))+1.
ZPH02 particles sensor is used to detect PM2.5. Integrating infrared PM2.5 detection principle and mature VOC detection technology, it can detect PM2.5 and VOC in the environment.
This sensor adopts the principle of particle counting to sensitively detect dust particles with a diameter of more than 1 μm. The VOC sensor has an extremely high sensitivity to organic volatile gases.
- Electrical specifications are as follows:
- The definitions of pins are as follows:
- The serial communication protocol is as follows:
Example of calculating PM2.5 value
- In a frame of normal data sent by the sensor, the 3rd bit is
0X12and the 4th bit is0X13. It means that the duty cycle of the sensor output is 18.19%, and the output range of the low pulse rate in the serial port mode is 0.5%–50%. - VOC gas detection: In each frame of data, the 7th bit represents VOC output, including four levels of
0x01to0x04, corresponding to excellent, good, medium, and poor in turn.0x00is displayed when there is no sensor or when a failure occurs.
Among them, Byte8 check value = (reversed (Byte1+Byte2+…+Byte7))+1.
The MQ-2 smoke sensor is used in gas leakage detectors in homes and factories to measure liquefied petroleum gas, benzene, alkane, alcohol, hydrogen, smoke, and more.
- Electrical specifications are as follows:
- Sensor sensitivityFor more information about sensor sensitivity, see the attachment.
To reduce the development cycle, the MQ-2 smoke sensor module is purchased.
Schematic diagram of the module:
As seen from the figure, you can determine the smoke status by detecting the voltage on the AO channel. The higher the voltage, the higher the smoke concentration.
5. Gas sensor solutionThe MQ-4 gas sensor can detect methane and natural gas with high sensitivity. It is widely used in household gas alarms, industrial combustible gas alarms, and portable gas detectors
- Electrical specifications are as follows:
An IR receiver diode is used to detect fire sources or heat sources with a wavelength in the range of 700 to 1000 nm.
- When there is no infrared light, there is a small saturated reverse leakage current (dark current), and the phototube does not conduct the current.
- When there is infrared light, the saturated reverse leakage current immediately increases to form a photocurrent. In a certain range, the photocurrent increases with the incident light intensity.
To reduce the development cycle, a common flame sensor module is purchased.
The schematic diagram is as follows.
The fire status is determined by detecting the voltage on the AO channel. The lower the voltage, the more fierce the fire.
7. Explore application scenariosWe choose and modify a socket with a table lamp, making the product more powerful.
Fix the environment monitor on the desk lamp stand. While detecting the air quality in the room, it can also detect whether the devices connected to the socket have fire hazards. For example, send alerts to users about safety hazards caused by devices containing lithium batteries, such as mobile phones.
You can also link the environment monitor with other devices.
Connect a 1-channel relay module to the desk lamp. When an anomaly occurs, the 220V power grid is automatically disconnected to reduce losses.
Since the adapter board has already been designed, this step is relatively simple. You only need to insert each sensor module into the appropriate position according to the layout, and complete the welding and fixing.
To simplify the trace, you can directly solder the VCC, GND, and TXD of the formaldehyde sensor to the VCC, GND, and RXD of P5. The same is true for other sensors.
- Remove the lampshade, and you can see the LED lamp board and positive and negative power cords inside. Then, remove the LED lamp board with tools such as an electric soldering iron
- Remove the four sponge pads and the screws to remove the base.
- Fix the master control board at the appropriate position in the lampshade area, and solder the positive and negative power cables to the P6 terminal on the master control board. When you press the desk lamp button on the socket, the socket can provide 5V voltage to the master control board. Pay attention to the positive and negative electrodes.
- Modify the lamp circuit, put a relay in the live wire circuit, and break out the relay control pin.
- Solder the control cable from the previous step to the Sig of the master control board P2, and install the lampshade and base.
You can explore creative ideas to decorate the lamp.
- Log in to the Tuya IoT Platform, and click Create.Category: Sensors > Multi-function Sensor.Solution: Custom Solution.Protocol: WiFi+Bluetooth.
- Select your desired functions.
If you have not found the desired functions, you can click Create Functions in the Custom Functions area to customize the functions.
- Select the device panel. The debugging panel can be used when you start debugging. You can configure the panel as you want.
- Select the development method and download the SDK and related files. In this demo, we select SDK development and use the WB3S module.
The demo code is developed based on the SDK of the v1.0.2 protocol. If you are new to SoC development, you can pull the code from the Tuya GitHub Repo for learning and development. You can code based on the relatively simple apps/template-demo.
This demo gets the information about the sensors through three methods: serial port, ADC, and level detection of the pins. The three methods are described as follows
1. Sensors with serial ports (formaldehyde sensor and PM2.5 sensor)According to the formaldehyde sensor documentation, we can find that the sensor data upload format and calculation method are as follows:
/***********************************************************
* Function: get_ch2o_sensor_value
* Input: none
* Output: none
* Return: none
* Notice: get and upload formaldehyde data
***********************************************************/
static VOID get_ch2o_sensor_value(VOID)
{
UINT_T buff_ret, find_head_index = 0;
// High and low bits of formaldehyde data
UCHAR_T ch2o_data_high, ch2o_data_low;
// Checksum
UCHAR_T check_sums = 0x00;
// Serial data buffer
UCHAR_T ch2o_receive_buffer[CH2O_BUFFER_SIZE];
// Point to the formaldehyde data header
UCHAR_T *p_ch2o_value = NULL;
memset(ch2o_receive_buffer, 0, sizeof(ch2o_receive_buffer));
// Read serial port data
bk_uart_recv(CH2O_SENSOR_UART, ch2o_receive_buffer, CH2O_BUFFER_SIZE, 0xFFFF);
// for (find_head_index = 0; find_head_index<CH2O_BUFFER_SIZE; find_head_index++) {
// PR_NOTICE("ch2o_receive_buffer[%d] = %02x", find_head_index, ch2o_receive_buffer[find_head_index]);
// }
// Look for the header sent by the CH2O sensor
for (find_head_index = 0; find_head_index<CH2O_BUFFER_SIZE; find_head_index++) {
if (ch2o_receive_buffer[find_head_index] == 0xff && \
ch2o_receive_buffer[find_head_index+1] == 0x17 && \
ch2o_receive_buffer[find_head_index+2] == 0x04){
//PR_NOTICE("find head is %d", find_head_index);
break;
}
}
// The data collected is incomplete
if (find_head_index > 11) {
PR_ERR("ch2o get uart data no complete!" );
return;
}
// Point to the header in the CH2O data
p_ch2o_value = ch2o_receive_buffer + find_head_index;
// Checksum. Confirm the accuracy of the data read
check_sums = ch2o_check_sum(p_ch2o_value, 9);
if (check_sums ! = *(p_ch2o_value + 8)) {
PR_ERR("ch2o check_sums error");
return;
}
ch2o_data_high = *(p_ch2o_value+4);
ch2o_data_low = *(p_ch2o_value+5);
gs_air_box.ch2o_value = ch2o_data_high * 256 + ch2o_data_low;
//PR_NOTICE("ch2o value is : %d .", gs_air_box.ch2o_value);
// Upload CH2O data to the Tuya IoT Platform
updata_dp_single(gs_air_box.dp_ch2o_value, PROP_VALUE, gs_air_box.ch2o_value);
return;
}The checksum is calculated as follows:
Checksum = (reversed (Byte1+Byte2+…+Byte7))+1.
/***********************************************************
* Function: ch2o_check_sum
* Input: none
* Output: none
* Return: none
* Notice: formaldehyde data checksum
***********************************************************/
static UCHAR_T ch2o_check_sum(UCHAR_T *data, UCHAR_T len)
{
UCHAR_T i, tempq = 0;
data += 1; // Point to data[1]
for(i=0; i<(len-2); i++)
{
tempq += *data;
data++;
}
tempq = (~tempq) + 1;
return (tempq);
}The PM2.5 data can be obtained like the formaldehyde data.
2. ADC sensor (gas sensor and smoke sensor)Click to download the PDF document.
There is only one ADC on the WB3S module, so we use the RS2255 chip for multiplexing. The pins are described as follow
- Initialize the ADC.
/* ADC */
#define ADC_DATA_LEN 4
static tuya_adc_dev_t tuya_adc;
static VOID adc_init(VOID)
{
tuya_adc.priv.pData = Malloc(ADC_DATA_LEN * sizeof(USHORT_T)); // tuya_adc is always used, so the space is not released later.
memset(tuya_adc.priv.pData, 0, ADC_DATA_LEN*sizeof(USHORT_T));
tuya_adc.priv.data_buff_size = ADC_DATA_LEN; // Set the data buffer size.
}- Collect the ADC.
/***********************************************************
* Function: get_adc_value
* Input: none
* Output: adc_value: the collected ADC value
* Return: none
* Notice: get the voltage value collected by the ADC
***********************************************************/
VOID get_adc_value(OUT USHORT_T* adc_value)
{
INT_T ret;
if (adc_value == NULL) {
PR_ERR("pm25_adc_value is NULL");
return;
}
memset(tuya_adc.priv.pData, 0, ADC_DATA_LEN*sizeof(USHORT_T));
ret = tuya_hal_adc_init(&tuya_adc);
if (ret != OPRT_OK) {
PR_ERR("ADC init error : %d ", ret);
return;
}
ret = ret = tuya_hal_adc_value_get(ADC_DATA_LEN, adc_value);
if (ret != OPRT_OK) {
PR_ERR("ADC get value error : %d ", ret);
}
tuya_hal_adc_finalize(&tuya_adc);
return;
}- Get data from the smoke sensor.
/* Smoke sensor */
#define SMOKE_ALARM_LIM 1.0
/***********************************************************
* Function: get_smoke_sensor_value
* Input: none
* Output: none
* Return: none
* Notice: get and upload the smoke data, A1
***********************************************************/
static VOID get_smoke_sensor_value(VOID)
{
USHORT_T smoke_adc_value;
FLOAT_T smoke_volt;
// Reuse the ADC to A1
tuya_gpio_write(RS2255_A, FALSE);
tuya_gpio_write(RS2255_B, TRUE);
tuya_hal_system_sleep(500);
// Get the ADC value of the smoke sensor
get_adc_value(&smoke_adc_value);
//PR_NOTICE("smoke_adc_value : %d ", smoke_adc_value);
// Calculate the actual voltage value
smoke_volt = (smoke_adc_value / 4095.0) * 2.4 * 2;
//PR_NOTICE("smoke_volt : %lf ", smoke_volt);
// Check whether the alarm threshold is reached
if (smoke_volt >= SMOKE_ALARM_LIM) {
gs_air_box.smoke_state = ALARM;
} else {
gs_air_box.smoke_state = NORMAL;
}
// Upload the data
updata_dp_single(gs_air_box.dp_smoke_state, PROP_ENUM, gs_air_box.smoke_state);
//PR_NOTICE("get smoke value, updata...");
return;
}Gas sensors work in a similar way as smoke sensors.
3. Detect the status of getting high and low levels of the pins (flame sensor)The 220V power supply is disconnected after the flame sensor detects the flame, and connected again after the flame sensor is restarted, in order to prevent accidents.
/***********************************************************
* Function: get_smoke_sensor_value
* Input: none
* Output: none
* Return: none
* Notice: get and upload the flame data
***********************************************************/
static VOID get_flame_sensor_value(VOID)
{
if (FALSE == tuya_gpio_read(FLAME_SENSOR_PIN)) {
gs_air_box.flame_state = ALARM;
/* Detect a flame, pull down 220V control pin, and power off */
tuya_gpio_write(POWER_OFF_220V_PIN, FALSE);
} else {
gs_air_box.flame_state = NORMAL;
}
updata_dp_single(gs_air_box.dp_flame_state, PROP_ENUM, gs_air_box.flame_state);
return;
}4. Initialize the deviceDuring starting, initialize the peripherals and pins related to the environment monitor, create a semaphore, preheat the sensor, release the semaphore after the preheating is completed, and then start collecting.
/***********************************************************
* Function: air_box_device_init
* Input: none
* Output: none
* Return: none
* Notice: Initialize the device
***********************************************************/
VOID air_box_device_init(VOID)
{
INT_T opRet = OPRT_OK;
/* Initialize the flame sensor peripherals */
tuya_gpio_inout_set(FLAME_SENSOR_PIN, TRUE);
tuya_gpio_inout_set(POWER_OFF_220V_PIN, FALSE);
/* During starting, pull up the 220V control pin and power on */
tuya_gpio_write(POWER_OFF_220V_PIN, TRUE);
/* ADC multiplexing and pin initialization */
tuya_gpio_inout_set(RS2255_A, FALSE);
tuya_gpio_inout_set(RS2255_B, FALSE);
adc_init();
/* The formaldehyde sensor uses UART2 to receive data, and the baud rate has been modified in the app_init() function */
/* Initialize the serial port of PM2.5 sensor */
ty_uart_init(PM25_SENSOR_UART, TYU_RATE_9600, TYWL_8B, TYP_NONE, TYS_STOPBIT1, (PM25_BUFFER_SIZE * SIZEOF(UCHAR_T)), TRUE);
/* Create semaphore */
opRet = tuya_hal_semaphore_create_init(&preheat_semaphore, 0, 1);
if (opRet ! = OPRT_OK) {
PR_ERR("creat preheat semaphore error : %d", opRet);
}
/* Release the semaphore after 60s preheating, and start to collect sensor data */
opSocSWTimerStart(preheat_timer, SENSOR_PREHEAT_TIME, preheat_semaphore_post_task);
xTaskCreate(acquire_data_task, "acquire sensor data", 512, NULL, TRD_PRIO_2, NULL);
}- Release the semaphore after preheating:
VOID preheat_semaphore_post_task(VOID)
{
// Release the semaphore after preheating
tuya_hal_semaphore_post(preheat_semaphore);
// Turn off the preheating software timer
opSocSWTimerStop(preheat_timer);
// After the preheating is completed, upload the preheating completion data to the Tuya IoT Platform
gs_air_box.preheat_state = false;
updata_dp_single(gs_air_box.dp_preheat, PROP_BOOL, gs_air_box.preheat_state);
}- Polling tasks for collected data of the sensor:
/***********************************************************
* Function: acquire_data_task
* Input: none
* Output: none
* Return: none
* Notice: a task to get sensor data
***********************************************************/
VOID acquire_data_task(VOID)
{
// Wait for preheating to complete
tuya_hal_semaphore_wait(preheat_semaphore);
while (1) {
get_ch2o_sensor_value();
get_flame_sensor_value();
get_gas_sensor_value();
get_pm25_sensor_value();
get_smoke_sensor_value();
tuya_hal_system_sleep(500);
}
}Step 5: Pair and control1. Display the air conditions in real-timeAfter the flashing and authorization are completed, the device can be paired properly. For more information about flashing and authorization, see Burn and Authorize WB Series Modules.
Connect the mobile phone to a Wi-Fi network, turn on Bluetooth, and pair the device as per the instruction. Then, we can control the device with the Tuya Smart app. After successful pairing, you can press and hold the button to enter the pairing mode again. The device panel on the app is shown as follows.
If the concentration of one of smoke, gas or flame is greater than the set value, the Tuya Smart app will display the alarm status and disconnect the 220V power supply. The device panel on the app is shown as follows.




Comments