Onenet Unicorn Seat Application development: Serial port reading PM25 sensor data

Source: Internet
Author: User

As an environmental data monitoring station, the first step is to obtain the data of respirable particulate matter in the atmosphere. In order to detect PM25 data, we use the HLPM025K3 model sensor, the Beijing Maritime Union, which uses a laser method to measure PM25 and PM10 data.

This type of sensor detects the following objects:

M2.5: Measuring 0.3-2.5 micron particulate matter in air;

PM10: Measures 0.3-10 micron particulate matter in the air.

The communication protocol used by the sensor is as follows:

(1) Baud rate: 9600bit/s; Data bits: 8 bits; stop bit: 1 bit; check bit: none;

(2) data transmission interval is 0.8~1.2s (seconds),

(3) Data format: 7 bytes, where check digit =pm2.5 (h) +pm2.5 (L) +pm10 (h) +PM10 (L).

(4) Data processing:

The received data is calculated by formula to get PM2.5 and PM10 values, for example:

Pm2.5= (PM2.5 (H) x256+pm2.5 (L)) x0.1;

Pm10= (PM2.5 (H) x256+pm2.5 (L)) x0.1

To connect the sensor to the onenet Kylin seat, we must look at its interface requirements:

As shown in the need for 5V power supply and a serial port and a Gpio port, we found the USART1 (PA9:USART1_TX,PA10:USART1_RX) port on the Kylin seat has been led to the J5 terminal row J5_6 and J5_5. At the same time need a GPIO port we choose PA8, because also led to the J5 terminal row J5_7. Only 5V power supply we did not find out, but this is no problem, all 5V power supply is quoted from the power switch SW1 6th foot, we welded a wire to solve the problem. As for grounding (GND) has been led to the terminal row J5_9 and j6_2, directly use it.

After connecting the line we use STM32CUBEMX to configure the hardware interface, serial communication parameters, interrupts, clocks, etc., and generate basic configuration code, as shown in:

The configuration code is as follows:

static void Mx_usart1_uart_init (void)

{

Huart1. Instance = USART1;

Huart1. Init.baudrate = 9600;

Huart1. Init.wordlength = uart_wordlength_8b;

Huart1. Init.stopbits = Uart_stopbits_1;

Huart1. init.parity = Uart_parity_none;

Huart1. Init.mode = Uart_mode_tx_rx;

Huart1. Init.hwflowctl = Uart_hwcontrol_none;

Huart1. init.oversampling = uart_oversampling_16;

if (Hal_uart_init (&huart1)! = HAL_OK)

{

Error_Handler ();

}

Hal_uart_receive_it (&huart1, (uint8_t *) Rxbuffer, 7);

}

The last line above is used to turn on the receive interrupt and set the receive buffer and receive buffer to receive the maximum amount of data, because the sensor's return byte is a fixed 7 bytes, so we set it straight. Of course, after the interrupt is generated, the Interrupt service program shuts down the interrupt, so we need to re-open it in the callback function in order to receive it for the next time.

static void Mx_gpio_init (void)

{

Gpio_inittypedef gpio_initstruct;

__hal_rcc_gpiod_clk_enable ();

__hal_rcc_gpioa_clk_enable ();

Hal_gpio_writepin (Senser_set_gpio_port, Senser_set_pin, Gpio_pin_set);

Gpio_initstruct.pin = Senser_set_pin;

Gpio_initstruct.mode = gpio_mode_output_pp;

Gpio_initstruct.speed = Gpio_speed_freq_high;

Hal_gpio_init (Senser_set_gpio_port, &gpio_initstruct);

}

void Hal_uart_mspinit (uart_handletypedef* huart)

{

Gpio_inittypedef gpio_initstruct;

if (HUART->INSTANCE==USART1)

{

__hal_rcc_usart1_clk_enable ();

Gpio_initstruct.pin = Gpio_pin_9;

Gpio_initstruct.mode = gpio_mode_af_pp;

Gpio_initstruct.speed = Gpio_speed_freq_high;

Hal_gpio_init (Gpioa, &gpio_initstruct);

Gpio_initstruct.pin = gpio_pin_10;

Gpio_initstruct.mode = Gpio_mode_input;

Gpio_initstruct.pull = Gpio_nopull;

Hal_gpio_init (Gpioa, &gpio_initstruct);

Hal_nvic_setpriority (usart1_irqn, 0, 0);

HAL_NVIC_ENABLEIRQ (USART1_IRQN);

}

}

After the configuration is completed, we also need to follow the protocol to parse the data, the resolution is completely to translate the protocol into a programming language, very simple, not to mention here. Finally, one more. The results show that:

Onenet Unicorn Seat Application development: Serial port reading PM25 sensor data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.