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.

173 lines
4.5 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. //copy and paste for 50 zeros
  21. //this should probably not be global...
  22. //Int means max 65535 or so. May need to move to long
  23. //making this too high runs into memory problems
  24. //This resolution will be relatively good enough for now.
  25. //If we need more space, then store only the first value in an unsigned int or long, then
  26. //bit shift it down to reduce size, and store in array in a lower size var.
  27. unsigned int reading[30] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  28. unsigned long sampletime_ms = 60000;//sample 30s ;
  29. unsigned long starttime = 0;
  30. unsigned char x = 0;
  31. unsigned char toplimit = 0;
  32. unsigned long sum = 0; //highest number possible - 32 bit unsigned, so 4 billion
  33. unsigned int average = 0;
  34. unsigned int sumcutdown = 0;
  35. void setup()
  36. {
  37. Serial.println(F("Setup begin"));
  38. #if (ESP8266)
  39. Serial.begin(115200);
  40. pinMode(5,OUTPUT); //--- ? -- SS pin must be output # by Renato Aloi
  41. #else
  42. Serial.begin(9600);
  43. pinMode(8,OUTPUT); //--- ? -- SS pin must be output # by Renato Aloi
  44. #endif
  45. eElib.begin(ipaddr,macaddr);
  46. Serial.println(F("------ program start -----------"));
  47. //Serial.println(F("NO SDCARD version")); // by Renato Aloi
  48. pinMode(DustSensePin,INPUT);
  49. starttime = millis();//get the current time;
  50. }
  51. void loop() {
  52. reading[x] = pulseIn(DustSensePin, LOW, 500000);
  53. //Print out readings on serial, as they come
  54. if (reading[x] != 0){
  55. Serial.println(reading[x]);
  56. x=x+1;
  57. if(x>50){
  58. x = 0;
  59. }
  60. }
  61. if ((millis()-starttime) > sampletime_ms){
  62. //reset these, no need to display on webpage anymore
  63. sum = 0;
  64. average = 0;
  65. toplimit = x; // don't read the zeros
  66. /*
  67. if(toplimit == 0){
  68. toplimit = 1; //to avoid divide by zero, in case sum / toplimit is both zero
  69. }
  70. */
  71. for (x=0; x<toplimit; x++){
  72. sum = sum + reading[x];
  73. }
  74. average = sum / toplimit;
  75. //see work docs. Bit shift to allow for lower resolution for built in print functions.
  76. //outputting an int - 16 bit, but we originally have a 32 bit)
  77. sumcutdown = sum >> 4;
  78. //Serial.print("Average Get!: ");
  79. //Serial.println(average); //average when 0 is 65535, so it's invalid (dividing by zero - sum divided by top limit which are both zero)
  80. //but we don't even want the average. It's just for testing here.
  81. Serial.print("Sum Get!: ");
  82. Serial.println(sum); //this is what we care about
  83. Serial.print("Sumcutdown Get!: ");
  84. Serial.println(sumcutdown); //bit shifted
  85. //reset everything that can be reset after a minute
  86. x = 0;
  87. toplimit = 0;
  88. for (x=0; x<toplimit; x++){
  89. reading[x] = 0;
  90. }
  91. starttime = millis();
  92. }
  93. if ( eElib.available() )
  94. {
  95. Serial.println(eElib.getParams());
  96. eElib.print((char *)&resp200Txt[0],strlen_P(&resp200Txt[0]));
  97. if (eElib.isIndexHtml)
  98. {
  99. eElib.print("<pre><H1>Hello World!</H1>");
  100. //eElib.print("<form method=POST>");
  101. eElib.print("<br>");
  102. eElib.print("The Sum is: ");
  103. eElib.print((unsigned)sumcutdown);
  104. //eElib.print("</form></body>");
  105. eElib.print("</pre>");
  106. }
  107. /*
  108. else if (eElib.isPost)
  109. {
  110. eElib.print("<HTML><body><H1>POST Params: ");
  111. eElib.print(eElib.getParams());
  112. eElib.print("</H1></body>");
  113. eElib.print("</HTML>");
  114. }
  115. else if (eElib.isGet)
  116. {
  117. eElib.print("<HTML><body><H1>GET Params: ");
  118. eElib.print(eElib.getParams());
  119. eElib.print("</H1></body>");
  120. eElib.print("</HTML>");
  121. }*/
  122. eElib.close();
  123. }
  124. }
  125. //unused
  126. /*
  127. int ipow(int base, int exp)
  128. {
  129. int result = 1;
  130. for (;;)
  131. {
  132. if (exp & 1)
  133. result *= base;
  134. exp >>= 1;
  135. if (!exp)
  136. break;
  137. base *= base;
  138. }
  139. return result;
  140. }*/