A low cost DIY sound pressure level sensor for enabling environmental noise awareness. https://lukasschwarz.org/noise-sensor
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.

преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "NoiseSensor.h"
  2. #include <SPI.h> // For WiFi101
  3. #include <WiFi101.h>
  4. #include <senseBoxIO.h>
  5. #include <TimeLib.h>
  6. // ---> Fill in your WiFi name (SSID) and password here. <---
  7. #define SECRET_SSID "CHANGEME"
  8. #define SECRET_PASSWORD "CHANGEME"
  9. // Keep these values like this unless you know what you're doing.
  10. const char server[] = "api.opensensemap.org";
  11. const uint8_t tick_count = 60;
  12. const bool verbose = 1;
  13. // ---> Fill this in yourself. <---
  14. // SenseBox IDs from the web interface in order 0, 1, 2, 3, 4. Master is ID 0!
  15. const char *sensebox_ids[] = {"CHANGEME", "", "CHANGEME", "CHANGEME", ""};
  16. // Sensor IDs from the web interface in the same order as above.
  17. const char *sensor_ids[] = {"CHANGEME", "", "CHANGEME", "CHANGEME", ""};
  18. // =================
  19. WiFiClient client;
  20. uint32_t last_reading;
  21. WiFiUDP udp;
  22. static const char ntpServerName[] = "de.pool.ntp.org";
  23. const int timeZone = 0; // Central European Time
  24. void setup() {
  25. sleep(2000);
  26. Serial.begin(115200);
  27. Serial.println("\nStarting wifi connection...");
  28. // Retry WiFi connection until it succeeds.
  29. do {
  30. WiFi.begin(SECRET_SSID, SECRET_PASSWORD);
  31. uint32_t wifi_start_time = millis();
  32. while (WiFi.status() != WL_CONNECTED) {
  33. senseBoxIO.statusRed();
  34. delay(500);
  35. Serial.print(".");
  36. senseBoxIO.statusGreen();
  37. if (millis() - wifi_start_time >= 5000) {
  38. Serial.println("Couldn't connect to Wifi. Try again.");
  39. WiFi.end();
  40. break;
  41. }
  42. }
  43. }
  44. while (WiFi.status() != WL_CONNECTED);
  45. Serial.print("Connected to AP ");
  46. Serial.println(SECRET_SSID);
  47. udp.begin(8888);
  48. setSyncProvider(getNtpTime);
  49. setSyncInterval(300);
  50. // Let the sensors know we're ready and start measuring.
  51. if (!NoiseSensor.begin(tick_count)) {
  52. Serial.println("Something didn't work. Please restart the system.");
  53. while (1) {
  54. senseBoxIO.statusRed();
  55. delay(100);
  56. senseBoxIO.statusNone();
  57. delay(100);
  58. }
  59. }
  60. NoiseSensor.setIds(sensebox_ids, sensor_ids);
  61. sleep(1000);
  62. }
  63. void loop() {
  64. // Shows the openSenseMap's response.
  65. if (!verbose) {
  66. while (client.available()) {
  67. char c = client.read();
  68. Serial.write(c);
  69. }
  70. }
  71. // This check is clumsy but will be replaced by a proper timer interrupt in the future.
  72. if (NoiseSensor.beaconReady()) {
  73. senseBoxIO.statusRed();
  74. NoiseSensor.sendSyncBeacon();
  75. senseBoxIO.statusGreen();
  76. }
  77. // This check is clumsy but will be replaced by a proper timer interrupt in the future.
  78. if (millis() - last_reading >= 2000 && NoiseSensor.requestReady()) {
  79. senseBoxIO.statusRed();
  80. Serial.println();
  81. NoiseSensor.read();
  82. // ---> Choose which sensor's data you want to upload by calling upload(device_id).
  83. upload(0, server);
  84. upload(2, server);
  85. upload(3, server);
  86. senseBoxIO.statusGreen();
  87. // Record the last upload time to make sure we don't read and upload multiple times per second.
  88. last_reading = millis();
  89. }
  90. }
  91. void upload(uint8_t device_id, const char* server) {
  92. Serial.println("Starting upload");
  93. if (client.connected()) {
  94. client.stop();
  95. sleep(500);
  96. }
  97. // If there's a successful connection:
  98. if (client.connectSSL(server, 443)) {
  99. String uploadbuf;
  100. String json = NoiseSensor.buildSenseBoxJSON(device_id);
  101. uint16_t content_length = json.length();
  102. uploadbuf = NoiseSensor.buildHTTPHeader(device_id, server, content_length);
  103. client.println(uploadbuf);
  104. if (verbose) { Serial.println(uploadbuf); }
  105. client.println();
  106. if (verbose) { Serial.println(); }
  107. // For some reason that nobody knows, client.print has an arbitrary input string limit.
  108. // Therefore, we need to split it at some arbitrary length and provide chunks.
  109. for (uint16_t idx = 0; idx < json.length(); idx += 1000) {
  110. client.print(json.substring(idx, idx + 1000));
  111. if (verbose) { Serial.print(json.substring(idx, idx + 1000)); }
  112. }
  113. client.println();
  114. if (verbose) { Serial.println(); }
  115. Serial.println("done!");
  116. } else {
  117. // If we couldn't make a connection:
  118. Serial.println("connection failed. Restart System.");
  119. sleep(1000);
  120. }
  121. Serial.println("Upload done");
  122. }
  123. void sleep(unsigned long ms) { // ms: duration
  124. unsigned long start = millis(); // start: timestamp
  125. for (;;) {
  126. unsigned long now = millis(); // now: timestamp
  127. unsigned long elapsed = now - start; // elapsed: duration
  128. if (elapsed >= ms) // comparing durations: OK
  129. return;
  130. }
  131. }
  132. const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
  133. byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
  134. time_t getNtpTime()
  135. {
  136. IPAddress ntpServerIP;// = IPAddress(176, 9 , 42, 91); // NTP server's ip address
  137. while (udp.parsePacket() > 0) ; // discard any previously received packets
  138. WiFi.hostByName(ntpServerName, ntpServerIP);
  139. if (verbose) {
  140. Serial.println("Transmit NTP Request");
  141. Serial.print(ntpServerName);
  142. Serial.print(": ");
  143. Serial.println(ntpServerIP);
  144. }
  145. sendNTPpacket(ntpServerIP);
  146. uint32_t beginWait = millis();
  147. while (millis() - beginWait < 1500) {
  148. int size = udp.parsePacket();
  149. if (size >= NTP_PACKET_SIZE) {
  150. if (verbose) { Serial.println("Receive NTP Response"); }
  151. udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
  152. unsigned long secsSince1900;
  153. // convert four bytes starting at location 40 to a long integer
  154. secsSince1900 = (unsigned long)packetBuffer[40] << 24;
  155. secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
  156. secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
  157. secsSince1900 |= (unsigned long)packetBuffer[43];
  158. return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
  159. }
  160. }
  161. Serial.println("No NTP Response :-(");
  162. return 0; // return 0 if unable to get the time
  163. }
  164. // send an NTP request to the time server at the given address
  165. void sendNTPpacket(IPAddress &address)
  166. {
  167. // set all bytes in the buffer to 0
  168. memset(packetBuffer, 0, NTP_PACKET_SIZE);
  169. // Initialize values needed to form NTP request
  170. // (see URL above for details on the packets)
  171. packetBuffer[0] = 0b11100011; // LI, Version, Mode
  172. packetBuffer[1] = 0; // Stratum, or type of clock
  173. packetBuffer[2] = 6; // Polling Interval
  174. packetBuffer[3] = 0xEC; // Peer Clock Precision
  175. // 8 bytes of zero for Root Delay & Root Dispersion
  176. packetBuffer[12] = 49;
  177. packetBuffer[13] = 0x4E;
  178. packetBuffer[14] = 49;
  179. packetBuffer[15] = 52;
  180. // all NTP fields have been given values, now
  181. // you can send a packet requesting a timestamp:
  182. udp.beginPacket(address, 123); //NTP requests are to port 123
  183. udp.write(packetBuffer, NTP_PACKET_SIZE);
  184. udp.endPacket();
  185. }