Testing out the PPD42 Air Quality Sensor, with an MSP430 Launchpad and graphing the data with GNUplot.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.4 KiB

5 years ago
  1. #include <math.h>
  2. //--- made by SKA ---
  3. //--- test EtherEncLib
  4. // adapted by Renato Aloi
  5. // May 2015
  6. // removed SD Card part for future implementation
  7. #include <SPI.h>
  8. #include <EtherEncLib.h>
  9. #if (ESP8266)
  10. #include <pgmspace.h>
  11. #else
  12. #include <avr/pgmspace.h>
  13. #endif
  14. static unsigned char ipaddr[] = { 192, 168, 1, 125 };
  15. static unsigned char macaddr[] = { 0x00, 0x11, 0x22, 0x44, 0x00, 0x25 };
  16. EtherEncLib eElib(80);
  17. const PROGMEM char resp200Txt[] = {"HTTP/1.0 200 OK\n\rContent-Type: text/html\n\rPragma: no-cache\n\r\n\r"};
  18. //sensor start
  19. int DustSensePin = 13;
  20. unsigned long duration;
  21. unsigned long starttime;
  22. unsigned long sampletime_ms = 30000;//sample 30s ;
  23. unsigned long lowpulseoccupancy = 0;
  24. float ratio = 0;
  25. float concentration = 0;
  26. void setup()
  27. {
  28. #if (ESP8266)
  29. Serial.begin(115200);
  30. pinMode(5,OUTPUT); //--- ? -- SS pin must be output # by Renato Aloi
  31. #else
  32. Serial.begin(9600);
  33. pinMode(8,OUTPUT); //--- ? -- SS pin must be output # by Renato Aloi
  34. #endif
  35. eElib.begin(ipaddr,macaddr);
  36. Serial.println(F("------ program start -----------"));
  37. //Serial.println(F("NO SDCARD version")); // by Renato Aloi
  38. pinMode(DustSensePin,INPUT);
  39. starttime = millis();//get the current time;
  40. }
  41. void loop() {
  42. duration = pulseIn(DustSensePin, LOW);
  43. lowpulseoccupancy = lowpulseoccupancy+duration;
  44. if ((millis()-starttime) > sampletime_ms)//if the sample time == 30s
  45. {
  46. ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
  47. //float exponential power - DOESN"T BUILD
  48. //concentration = 1.1*powf(ratio,3)-3.8*powf(ratio,2)+520*ratio+0.62; // using spec sheet curve
  49. //leaner exponential power
  50. concentration = 1.1*ipow(ratio,3)-3.8*ipow(ratio,2)+520*ratio+0.62; // using spec sheet curve
  51. //Serial.print(lowpulseoccupancy);
  52. //Serial.print(",");
  53. //Serial.print(ratio);
  54. //Serial.print(",");
  55. //Serial.println(concentration);
  56. //lowpulseoccupancy = 0;
  57. starttime = millis();
  58. }
  59. if ( eElib.available() )
  60. {
  61. Serial.println(eElib.getParams());
  62. eElib.print((char *)&resp200Txt[0],strlen_P(&resp200Txt[0]));
  63. if (eElib.isIndexHtml)
  64. {
  65. eElib.print("<HTML><body><H1>Hello World!</H1>");
  66. //eElib.print("<form method=POST>");
  67. eElib.print("<br>");
  68. eElib.print((int)lowpulseoccupancy);
  69. eElib.print("<br>");
  70. eElib.print((int)ratio);
  71. eElib.print("<br>");
  72. eElib.print((int)concentration);
  73. eElib.print("<br>");
  74. //eElib.print("</form></body>");
  75. eElib.print("</body></HTML>");
  76. //Serial.print(lowpulseoccupancy);
  77. //Serial.print(",");
  78. //Serial.print(ratio);
  79. //Serial.print(",");
  80. //Serial.println(concentration);
  81. }
  82. /*
  83. else if (eElib.isPost)
  84. {
  85. eElib.print("<HTML><body><H1>POST Params: ");
  86. eElib.print(eElib.getParams());
  87. eElib.print("</H1></body>");
  88. eElib.print("</HTML>");
  89. }
  90. else if (eElib.isGet)
  91. {
  92. eElib.print("<HTML><body><H1>GET Params: ");
  93. eElib.print(eElib.getParams());
  94. eElib.print("</H1></body>");
  95. eElib.print("</HTML>");
  96. }*/
  97. eElib.close();
  98. }
  99. }
  100. int ipow(int base, int exp)
  101. {
  102. int result = 1;
  103. for (;;)
  104. {
  105. if (exp & 1)
  106. result *= base;
  107. exp >>= 1;
  108. if (!exp)
  109. break;
  110. base *= base;
  111. }
  112. return result;
  113. }