| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- /*
- basic_demo.ino
- Example for Seeed PM2.5 Sensor(HM300)
- Copyright (c) 2018 Seeed Technology Co., Ltd.
- Website : www.seeed.cc
- Author : downey
- Create Time: August 2018
- Change Log :
- The MIT License (MIT)
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- #include <Seeed_HM330X.h>
- #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
- #define SERIAL_OUTPUT SerialUSB
- #else
- #define SERIAL_OUTPUT Serial
- #endif
- HM330X sensor;
- uint8_t buf[30];
- const char* str[] = {"sensor num: ", "PM1.0 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
- "PM2.5 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
- "PM10 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
- "PM1.0 concentration(Atmospheric environment,unit:ug/m3): ",
- "PM2.5 concentration(Atmospheric environment,unit:ug/m3): ",
- "PM10 concentration(Atmospheric environment,unit:ug/m3): ",
- };
- HM330XErrorCode print_result(const char* str, uint16_t value) {
- if (NULL == str) {
- return ERROR_PARAM;
- }
- SERIAL_OUTPUT.print(str);
- SERIAL_OUTPUT.println(value);
- return NO_ERROR;
- }
- /*parse buf with 29 uint8_t-data*/
- HM330XErrorCode parse_result(uint8_t* data) {
- uint16_t value = 0;
- if (NULL == data) {
- return ERROR_PARAM;
- }
- for (int i = 1; i < 8; i++) {
- value = (uint16_t) data[i * 2] << 8 | data[i * 2 + 1];
- print_result(str[i - 1], value);
- }
- return NO_ERROR;
- }
- HM330XErrorCode parse_result_value(uint8_t* data) {
- if (NULL == data) {
- return ERROR_PARAM;
- }
- for (int i = 0; i < 28; i++) {
- SERIAL_OUTPUT.print(data[i], HEX);
- SERIAL_OUTPUT.print(" ");
- if ((0 == (i) % 5) || (0 == i)) {
- SERIAL_OUTPUT.println("");
- }
- }
- uint8_t sum = 0;
- for (int i = 0; i < 28; i++) {
- sum += data[i];
- }
- if (sum != data[28]) {
- SERIAL_OUTPUT.println("wrong checkSum!!");
- }
- SERIAL_OUTPUT.println("");
- return NO_ERROR;
- }
- /*30s*/
- void setup() {
- SERIAL_OUTPUT.begin(115200);
- delay(100);
- SERIAL_OUTPUT.println("Serial start");
- if (sensor.init()) {
- SERIAL_OUTPUT.println("HM330X init failed!!");
- while (1);
- }
- }
- void loop() {
- if (sensor.read_sensor_value(buf, 29)) {
- SERIAL_OUTPUT.println("HM330X read result failed!!");
- }
- parse_result_value(buf);
- parse_result(buf);
- SERIAL_OUTPUT.println("");
- delay(5000);
- }
|