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.

136 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. Serial.println("DataGet");
  59. }
  60. if ( eElib.available() )
  61. {
  62. Serial.println(eElib.getParams());
  63. eElib.print((char *)&resp200Txt[0],strlen_P(&resp200Txt[0]));
  64. if (eElib.isIndexHtml)
  65. {
  66. eElib.print("<HTML><body><H1>Hello World!</H1>");
  67. //eElib.print("<form method=POST>");
  68. eElib.print("<br>");
  69. eElib.print((int)lowpulseoccupancy);
  70. eElib.print("<br>");
  71. eElib.print((int)ratio);
  72. eElib.print("<br>");
  73. eElib.print((int)concentration);
  74. eElib.print("<br>");
  75. //eElib.print("</form></body>");
  76. eElib.print("</body></HTML>");
  77. //Serial.print(lowpulseoccupancy);
  78. //Serial.print(",");
  79. //Serial.print(ratio);
  80. //Serial.print(",");
  81. //Serial.println(concentration);
  82. }
  83. /*
  84. else if (eElib.isPost)
  85. {
  86. eElib.print("<HTML><body><H1>POST Params: ");
  87. eElib.print(eElib.getParams());
  88. eElib.print("</H1></body>");
  89. eElib.print("</HTML>");
  90. }
  91. else if (eElib.isGet)
  92. {
  93. eElib.print("<HTML><body><H1>GET Params: ");
  94. eElib.print(eElib.getParams());
  95. eElib.print("</H1></body>");
  96. eElib.print("</HTML>");
  97. }*/
  98. eElib.close();
  99. }
  100. }
  101. int ipow(int base, int exp)
  102. {
  103. int result = 1;
  104. for (;;)
  105. {
  106. if (exp & 1)
  107. result *= base;
  108. exp >>= 1;
  109. if (!exp)
  110. break;
  111. base *= base;
  112. }
  113. return result;
  114. }