basic_demo.ino 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. basic_demo.ino
  3. Example for Seeed PM2.5 Sensor(HM300)
  4. Copyright (c) 2018 Seeed Technology Co., Ltd.
  5. Website : www.seeed.cc
  6. Author : downey
  7. Create Time: August 2018
  8. Change Log :
  9. The MIT License (MIT)
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. */
  26. #include <Seeed_HM330X.h>
  27. #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
  28. #define SERIAL_OUTPUT SerialUSB
  29. #else
  30. #define SERIAL_OUTPUT Serial
  31. #endif
  32. HM330X sensor;
  33. uint8_t buf[30];
  34. const char* str[] = {"sensor num: ", "PM1.0 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
  35. "PM2.5 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
  36. "PM10 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
  37. "PM1.0 concentration(Atmospheric environment,unit:ug/m3): ",
  38. "PM2.5 concentration(Atmospheric environment,unit:ug/m3): ",
  39. "PM10 concentration(Atmospheric environment,unit:ug/m3): ",
  40. };
  41. HM330XErrorCode print_result(const char* str, uint16_t value) {
  42. if (NULL == str) {
  43. return ERROR_PARAM;
  44. }
  45. SERIAL_OUTPUT.print(str);
  46. SERIAL_OUTPUT.println(value);
  47. return NO_ERROR;
  48. }
  49. /*parse buf with 29 uint8_t-data*/
  50. HM330XErrorCode parse_result(uint8_t* data) {
  51. uint16_t value = 0;
  52. if (NULL == data) {
  53. return ERROR_PARAM;
  54. }
  55. for (int i = 1; i < 8; i++) {
  56. value = (uint16_t) data[i * 2] << 8 | data[i * 2 + 1];
  57. print_result(str[i - 1], value);
  58. }
  59. return NO_ERROR;
  60. }
  61. HM330XErrorCode parse_result_value(uint8_t* data) {
  62. if (NULL == data) {
  63. return ERROR_PARAM;
  64. }
  65. for (int i = 0; i < 28; i++) {
  66. SERIAL_OUTPUT.print(data[i], HEX);
  67. SERIAL_OUTPUT.print(" ");
  68. if ((0 == (i) % 5) || (0 == i)) {
  69. SERIAL_OUTPUT.println("");
  70. }
  71. }
  72. uint8_t sum = 0;
  73. for (int i = 0; i < 28; i++) {
  74. sum += data[i];
  75. }
  76. if (sum != data[28]) {
  77. SERIAL_OUTPUT.println("wrong checkSum!!");
  78. }
  79. SERIAL_OUTPUT.println("");
  80. return NO_ERROR;
  81. }
  82. /*30s*/
  83. void setup() {
  84. SERIAL_OUTPUT.begin(115200);
  85. delay(100);
  86. SERIAL_OUTPUT.println("Serial start");
  87. if (sensor.init()) {
  88. SERIAL_OUTPUT.println("HM330X init failed!!");
  89. while (1);
  90. }
  91. }
  92. void loop() {
  93. if (sensor.read_sensor_value(buf, 29)) {
  94. SERIAL_OUTPUT.println("HM330X read result failed!!");
  95. }
  96. parse_result_value(buf);
  97. parse_result(buf);
  98. SERIAL_OUTPUT.println("");
  99. delay(5000);
  100. }