Guest User

Untitled

a guest
Feb 17th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. err_t print_result(const char* str,u16 value)
  2. {
  3. if(NULL==str)
  4. return ERROR_PARAM;
  5. Serial.print(str);
  6. Serial.println(value);
  7.  
  8.  
  9. return NO_ERROR;
  10. }
  11.  
  12. const char *str[]={"sensor num: ","PM1.0 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
  13. "PM2.5 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
  14. "PM10 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
  15. "PM1.0 concentration(Atmospheric environment,unit:ug/m3): ",
  16. "PM2.5 concentration(Atmospheric environment,unit:ug/m3): ",
  17. "PM10 concentration(Atmospheric environment,unit:ug/m3): ",
  18. };
  19.  
  20. PM1.0 concentration(CF=1,Standard particulate matter,unit:ug/m3): 18
  21. PM2.5 concentration(CF=1,Standard particulate matter,unit:ug/m3): 26
  22. PM10 concentration(CF=1,Standard particulate matter,unit:ug/m3): 30
  23. PM1.0 concentration(Atmospheric environment,unit:ug/m3): 18
  24. PM2.5 concentration(Atmospheric environment,unit:ug/m3): 26
  25. PM10 concentration(Atmospheric environment,unit:ug/m3): 30
  26.  
  27. #include "Seeed_HM330X.h"
  28. #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
  29. #define SERIAL SerialUSB
  30. #else
  31. #define SERIAL Serial
  32. #endif
  33.  
  34.  
  35. HM330X sensor;
  36. u8 buf[30];
  37.  
  38.  
  39. const char *str[]={"sensor num: ","PM1.0 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
  40. "PM2.5 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
  41. "PM10 concentration(CF=1,Standard particulate matter,unit:ug/m3): ",
  42. "PM1.0 concentration(Atmospheric environment,unit:ug/m3): ",
  43. "PM2.5 concentration(Atmospheric environment,unit:ug/m3): ",
  44. "PM10 concentration(Atmospheric environment,unit:ug/m3): ",
  45. };
  46.  
  47. err_t print_result(const char* str,u16 value)
  48. {
  49. if(NULL==str)
  50. return ERROR_PARAM;
  51. SERIAL.print(str);
  52. SERIAL.println(value);
  53.  
  54. return NO_ERROR;
  55. }
  56.  
  57. /*parse buf with 29 u8-data*/
  58. err_t parse_result(u8 *data)
  59. {
  60. u16 value=0;
  61. err_t NO_ERROR;
  62. if(NULL==data)
  63. return ERROR_PARAM;
  64. for(int i=1;i<8;i++)
  65. {
  66. value = (u16)data[i*2]<<8|data[i*2+1];
  67. print_result(str[i-1],value);
  68.  
  69. }
  70. }
  71.  
  72. err_t parse_result_value(u8 *data)
  73. {
  74. if(NULL==data)
  75. return ERROR_PARAM;
  76. for(int i=0;i<28;i++)
  77. {
  78. SERIAL.print(data[i],HEX);
  79. SERIAL.print(" ");
  80. if((0==(i)%5)||(0==i))
  81. {
  82. SERIAL.println(" ");
  83. }
  84. }
  85. u8 sum=0;
  86. for(int i=0;i<28;i++)
  87. {
  88. sum+=data[i];
  89. }
  90. if(sum!=data[28])
  91. {
  92. SERIAL.println("wrong checkSum!!!!");
  93. }
  94. SERIAL.println(" ");
  95. SERIAL.println(" ");
  96. return NO_ERROR;
  97. }
  98.  
  99. void setup()
  100. {
  101.  
  102. SERIAL.begin(115200);
  103. delay(100);
  104. SERIAL.println("Serial start");
  105. if(sensor.init())
  106. {
  107. SERIAL.println("HM330X init failed!!!");
  108. while(1);
  109. }
  110.  
  111. }
  112.  
  113.  
  114. void loop()
  115. {
  116. if(sensor.read_sensor_value(buf,29))
  117. {
  118. SERIAL.println("HM330X read result failed!!!");
  119. }
  120. parse_result_value(buf);
  121. parse_result(buf);
  122. SERIAL.println(" ");
  123. SERIAL.println(" ");
  124. SERIAL.println(" ");
  125. delay(5000);
  126. }
Add Comment
Please, Sign In to add comment