1, RT thread
1. Introduction
RT thread nano is a minimalist version of hard real-time kernel. It is developed by C language, adopts object-oriented programming thinking, and has a good code style. It is a tailorable, preemptive real-time multitasking RTOS. Its memory resources are very small, and its functions include relatively complete real-time operating system characteristics such as task processing, software timer, semaphore, mailbox and real-time scheduling. It is suitable for 32-bit ARM entry-level MCU widely used in home appliances, consumer electronics, medical equipment, industrial control and other fields.
Refer to official documents https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-nano/an0038-nano-introduction
2. Use
Declare task thread, task stack size and task function
//Task thread struct rt_thread smoke_thread; struct rt_thread fire_thread; struct rt_thread light_thread; //Task stack rt_uint8_t rt_smoke_thread_stack[512]; rt_uint8_t rt_fire_thread_stack[512]; rt_uint8_t rt_light_thread_stack[512]; //Task function void smoke_task_entry(void *parameter); void fire_task_entry(void *parameter); void light_task_entry(void *parameter);
Create thread and start thread scheduling
//Initialize thread function void MX_RT_Thread_Init(void) { //Initialize thread /* Static thread object Thread name Thread entry function Thread entry parameters Thread stack address Thread stack space size thread priority Thread time slice */ rt_thread_init(&smoke_thread,"smoke",smoke_task_entry,RT_NULL,&rt_smoke_thread_stack[0],sizeof(rt_smoke_thread_stack),2,60); rt_thread_init(&fire_thread,"fire",fire_task_entry,RT_NULL,&rt_fire_thread_stack[0],sizeof(rt_fire_thread_stack),3,60); rt_thread_init(&light_thread,"light",light_task_entry,RT_NULL,&rt_light_thread_stack[0],sizeof(rt_light_thread_stack),4,60); //Turn on thread scheduling rt_thread_startup(&smoke_thread); rt_thread_startup(&fire_thread); rt_thread_startup(&light_thread); }
Define task functions
//Smoke detection task void smoke_task_entry(void *parameter) { //Flash once every 1000ms while(1) { Detect_Smoke(); //abnormal if(!SmokeFlog){ //The corresponding indicator lights up HAL_GPIO_WritePin(GPIOF,GPIO_PIN_9,GPIO_PIN_RESET); //Turn on the buzzer HAL_GPIO_WritePin(GPIOF,GPIO_PIN_8,GPIO_PIN_SET); } else{ //The corresponding indicator is off HAL_GPIO_WritePin(GPIOF,GPIO_PIN_9,GPIO_PIN_SET); //No abnormality, close the buzzer if(SmokeFlog && FireFlag && LightFlag){ HAL_GPIO_WritePin(GPIOF,GPIO_PIN_8,GPIO_PIN_RESET); } } //Delay 1000ms rt_thread_mdelay(1000); } } //Flame detection task void fire_task_entry(void *parameter) { //Flash once every 1000ms while(1) { Detect_Fire(); //abnormal if(!FireFlag){ //The corresponding indicator lights up HAL_GPIO_WritePin(GPIOF,GPIO_PIN_10,GPIO_PIN_RESET); //Turn on the buzzer HAL_GPIO_WritePin(GPIOF,GPIO_PIN_8,GPIO_PIN_SET); } else{ //The corresponding indicator is off HAL_GPIO_WritePin(GPIOF,GPIO_PIN_10,GPIO_PIN_SET); //No abnormality, close the buzzer if(SmokeFlog && FireFlag && LightFlag){ HAL_GPIO_WritePin(GPIOF,GPIO_PIN_8,GPIO_PIN_RESET); } } //Delay 1000ms rt_thread_mdelay(1000); } } //Illumination detection task void light_task_entry(void *parameter) { //Flash once every 1000ms while(1) { Detect_Light(); //abnormal if(!LightFlag){ //The corresponding indicator lights up HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13,GPIO_PIN_RESET); //Turn on the buzzer HAL_GPIO_WritePin(GPIOF,GPIO_PIN_8,GPIO_PIN_SET); } else{ //The corresponding indicator is off HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13,GPIO_PIN_SET); //No abnormality, close the buzzer if(SmokeFlog && FireFlag && LightFlag){ HAL_GPIO_WritePin(GPIOF,GPIO_PIN_8,GPIO_PIN_RESET); } } //Delay 1000ms rt_thread_mdelay(1000); } }
2, ZigBee
1. Introduction
ZigBee is a new wireless communication technology, which is suitable for a series of electronic components and equipment with short transmission range and low data transmission rate. ZigBee wireless communication technology can achieve coordinated communication between thousands of micro sensors based on special radio standards. Therefore, this technology is often called Home RF Lite wireless technology and FireFly wireless technology. ZigBee wireless communication technology can also be applied to small-scale control and automation based on wireless communication. It can save the wired cables between computer equipment and a series of digital devices, and can realize the wireless networking between a variety of different digital devices, so that they can communicate with each other or access the Internet.
ZigBee is translated as "purple bee", which is similar to Bluetooth. It is an emerging short-range wireless communication technology for sensor and control applications. Proposed by IEEE 802.15 working group and developed by its TG4 working group.
ZigBee wireless communication technology is a network technology applied to Internet communication based on the way bees communicate with each other. Compared with traditional network communication technology, ZigBee wireless communication technology shows more efficient and convenient characteristics. As a short-range, low-cost and low-power wireless network technology, ZigBee wireless communication technology is based on 802.15.4 Wireless Standard approved by IEEE. This technology is especially suitable for services with small data traffic. It can be installed in a series of fixed and portable mobile terminals. At the same time, ZigBee wireless communication technology can also realize GPS function.
ZigBee technology is essentially a low rate bidirectional wireless network technology, which is developed by IEEE Developed from 802.15.4 Wireless standard, it has the advantages of low complexity, short distance, low cost and low power consumption. It uses the 2.4GHz band, which defines the ZigBee technology in IEEE Application services supported on 802.15.4 standard media. The main development direction of ZigBee alliance is to establish an infrastructure, which is based on Interoperability platform and configuration file, and has the advantages of low cost and scalable embedded. Building the Internet of things development platform is conducive to the transformation of research results and the pairing of industry, University and research. It is a simple way to realize the Internet of things.
2. Use
Connect the coordinator with the serial port 2 of STM32F407, poll and accept the data sent by the terminal about every five seconds, and then send it to STM32F407 through the serial port. STM32F407 receives the data received by interrupt processing through the serial port, analyzes it, and sends the data to oneNet server through transparent transmission through esp8266 WiFi module.
/Accept interrupt handling callback void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { //Serial port I if(huart==&huart1){ com1[length1++]=c; if(com1[length1-2] == 0x0D && com1[length1-1] == 0x0A){ HAL_UART_Transmit(&huart3, (uint8_t *)&com1, strlen(com1),0xFFFF); length1=0; printf("%s",com1); memset(com1,0,sizeof(com1)); } //Reset interrupt HAL_UART_Receive_IT(&huart1, (uint8_t *)&c, 1); } //Serial port II else if(huart==&huart2){ { com2[length2++]=c; if(length2>99){ length2=0; memset(com2,0,sizeof(com2)); } else{ if(com2[length2-2] == 0x0D && com2[length2-1] == 0x0A){ //Get temperature t[0]=com2[2]; t[1]=com2[3]; t[2]='\r'; t[3]='\n'; //Get humidity h[0]=com2[6]; h[1]=com2[7]; h[2]='\r'; h[3]='\n'; SendDataFlag=1; //Printing temperature and humidity printf("temperature: %d \t humidity: %d",((com2[2]-'0')*10+(com2[3]-'0')),((com2[6]-'0')*10+(com2[7]-'0'))); length2=0; memset(com2,0,sizeof(com2)); } } } //Reset interrupt HAL_UART_Receive_IT(&huart2, (uint8_t *)&c, 1); } else if(huart==&huart3){ buf[length3++]=c; if(buf[length3-2] == 0x0D && buf[length3-1] == 0x0A){ for(int i=0;i<length3;i++){ com3[i]=buf[i]; } memset(buf,0,sizeof(buf)); length3=0; } //Reset interrupt HAL_UART_Receive_IT(&huart3, (uint8_t *)&c, 1); } }
The while loop of main function detects the sending data flag every 500ms. When it is detected, it sends data through oneNet
while (1) { if(SendDataFlag==1){ WIFI_SendTempData(t); memset(t,0,sizeof(t)); WIFI_SendHumData(h); memset(h,0,sizeof(h)); HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_14); rt_thread_mdelay(500); HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_14); rt_thread_mdelay(500); SendDataFlag=0; } rt_thread_mdelay(500); }
3, esp8266WiFi module
The WiFi module is connected to STM32F407 serial port 3. When the main thread receives the temperature and humidity data sent by serial port 2, it sends the data to oneNet through the WiFi module.
Initialize WiFi module
void WIFI_Init(void){ HAL_UART_Transmit(&huart3, (uint8_t *)"AT\r\n", strlen("AT\r\n"),0xFFFF); rt_thread_mdelay(100); printf("%s",com3); memset(com3,0,sizeof(com3)); //Configure to AP+STA mode HAL_UART_Transmit(&huart3, (uint8_t *)"AT+CWMODE=3\r\n", strlen("AT+CWMODE=3\r\n"),0xFFFF); rt_thread_mdelay(100); printf("%s",com3); memset(com3,0,sizeof(com3)); HAL_GPIO_WritePin(GPIOF,GPIO_PIN_9,GPIO_PIN_RESET); //Restart effective HAL_UART_Transmit(&huart3, (uint8_t *)"AT+RST\r\n", strlen("AT+RST\r\n"),0xFFFF); rt_thread_mdelay(5000); printf("%s",com3); memset(com3,0,sizeof(com3)); HAL_GPIO_WritePin(GPIOF,GPIO_PIN_10,GPIO_PIN_RESET); //Query device IP HAL_UART_Transmit(&huart3, (uint8_t *)"AT+CIFSR\r\n", strlen("AT+CIFSR\r\n"),0xFFFF); rt_thread_mdelay(1000); printf("%s",com3); memset(com3,0,sizeof(com3)); //Set server IP address port number HAL_UART_Transmit(&huart3, (uint8_t *)"AT+CIPSTART=\"TCP\",\"183.230.40.33\",80\r\n", strlen("AT+CIPSTART=\"TCP\",\"183.230.40.33\",80\r\n"),0xFFFF); rt_thread_mdelay(2000); printf("%s",com3); memset(com3,0,sizeof(com3)); HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13,GPIO_PIN_RESET); //Turn on transparent transmission mode HAL_UART_Transmit(&huart3, (uint8_t *)"AT+CIPMODE=1\r\n", strlen("AT+CIPMODE=1\r\n"),0xFFFF); rt_thread_mdelay(1000); printf("%s",com3); memset(com3,0,sizeof(com3)); //Start transmission HAL_UART_Transmit(&huart3, (uint8_t *)"AT+CIPSEND\r\n", strlen("AT+CIPSEND\r\n"),0xFFFF); rt_thread_mdelay(1000); HAL_GPIO_WritePin(GPIOE,GPIO_PIN_14,GPIO_PIN_RESET); }
send data
data format
POST /devices/equipment ID/datapoints?type=5 HTTP/1.1 api-key: Device key Host:api.heclouds.com Content-Length:Data length ,;temp,89
void WIFI_SendTempData(char *data){ HAL_UART_Transmit(&huart3, (uint8_t *)"POST /devices/Your own equipment ID/datapoints?type=5 HTTP/1.1\n", strlen("POST /devices/Your own equipment ID/datapoints?type=5 HTTP/1.1\n"),0xFFFF); HAL_UART_Transmit(&huart3, (uint8_t *)"api-key: Your own device key\n", strlen("api-key: Your own device key\n"),0xFFFF); HAL_UART_Transmit(&huart3, (uint8_t *)"Host:api.heclouds.com\n", strlen("Host:api.heclouds.com\n"),0xFFFF); HAL_UART_Transmit(&huart3, (uint8_t *)"Content-Length:10\n\n", strlen("Content-Length:10\n\n"),0xFFFF); HAL_UART_Transmit(&huart3, (uint8_t *)",;Temp,", strlen(",;Temp,"),0xFFFF); HAL_UART_Transmit(&huart3, (uint8_t *)data, strlen(data),0xFFFF); }
4, Sensor detection
MQ-2 smoke sensor
Generally, in addition to smoke detection, abnormal gas, natural gas and gas can also be detected
Working voltage: DC 5V
characteristic
- With signal output indication.
- Dual signal output (analog output and TTL level output)
- TTL output valid signal is low level. (when the output level is low, the signal light is on and can be directly connected to the single chip microcomputer)
- The analog output voltage is 0~5V. The higher the concentration, the higher the voltage.
- It has good sensitivity to liquefied gas, natural gas and city gas.
- It has long service life and reliable stability
- Fast response recovery
read
When we use it, in order to obtain the gas concentration value, we can convert the analog signal into digital signal through ADC by connecting the analog voltage output, and then judge the conversion result accordingly.
#include "smoke.h" double Smoke_Sensor_Value=0; int SmokeFlog=1; void Detect_Smoke(void){ //Start ADC installation and replacement HAL_ADC_Start(&hadc1); //Wait for the conversion to complete. The second parameter is the timeout, in ms HAL_ADC_PollForConversion(&hadc1, 50); //Judge whether the conversion completion flag bit is set if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC)) { /*Start getting conversion values*/ uint16_t value = HAL_ADC_GetValue(&hadc1); Smoke_Sensor_Value=value/33.0; // printf("smoke concentration:%. 2f\r\n",Smoke_Sensor_Value); //When the smoke index is higher than 20, an exception is triggered if(Smoke_Sensor_Value>20){ SmokeFlog=0; } else{ SmokeFlog=1; } } }
Photosensitive resistance
Use the built-in photoresistor on the STM32F407 board for detection
characteristic
During illumination, the resistance is very small. Without illumination, the resistance is very large. The stronger the illumination, the smaller the resistance. When the illumination stops, the resistance returns to its original value.
read
Similarly, ADC is used for conversion, and then detection is carried out according to the conversion results
#include "light.h" double Light_Sensor_Value=0; int LightFlag=1; void Detect_Light(void){ //Start ADC installation and replacement HAL_ADC_Start(&hadc3); //Wait for the conversion to complete. The second parameter is the timeout, in ms HAL_ADC_PollForConversion(&hadc3, 50); //Judge whether the conversion completion flag bit is set if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc3), HAL_ADC_STATE_REG_EOC)) { /*Start getting conversion values*/ uint16_t value = HAL_ADC_GetValue(&hadc3); //The stronger the illumination, the lower the voltage, and the greater the percentage of illumination Light_Sensor_Value=100*1.0-value*1.0/33; // printf("read brightness:%. 2f\r\n",Light_Sensor_Value); //When the light percentage is higher than 90 or lower than 30, an exception is triggered if(Light_Sensor_Value>90 || Light_Sensor_Value<30){ LightFlag=0; } else{ LightFlag=1; } } }
Flame sensor
characteristic
read
According to the document introduction of the sensor, you only need to read the high and low levels of the corresponding pins.
#include "fire.h" #include "rtthread.h" int FireFlag=1; void Detect_Fire(void){ //Low level normal if(HAL_GPIO_ReadPin(GPIOE,FIRE_Pin)==GPIO_PIN_RESET){ FireFlag=1; // printf("normal \ r\n"); } //High level abnormality else if (HAL_GPIO_ReadPin(GPIOE,FIRE_Pin)==GPIO_PIN_SET){ FireFlag=0; // printf("fire \ n"); } }
5, Effect
Data received by oneNet
6, Source code
https://github.com/TangtangSix/SensorDataCollection
https://gitee.com/tangtangsix/SensorDataCollection