A low cost DIY sound pressure level sensor for enabling environmental noise awareness. https://lukasschwarz.org/noise-sensor
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

noisesensor_esp32.ino 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "NoiseSensor.h"
  2. #include <SPI.h> // For WiFi, I think
  3. #include <WiFiClientSecure.h>
  4. #include "time.h"
  5. #define SNTP_UPDATE_DELAY 300000
  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 = 30;
  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", "", ""};
  16. // Sensor IDs from the web interface in the same order as above.
  17. const char *sensor_ids[] = {"CHANGEME", "", "CHANGEME", "", ""};
  18. // =================
  19. WiFiClientSecure client;
  20. uint32_t last_reading;
  21. WiFiUDP udp;
  22. void setup() {
  23. delay(2000);
  24. Serial.begin(115200);
  25. Serial.println("\nStarting wifi connection...");
  26. // Retry WiFi connection until it succeeds.
  27. do {
  28. WiFi.begin(SECRET_SSID, SECRET_PASSWORD);
  29. uint32_t wifi_start_time = millis();
  30. while (WiFi.status() != WL_CONNECTED) {
  31. delay(500);
  32. Serial.print(".");
  33. if (millis() - wifi_start_time >= 5000) {
  34. Serial.println("\nCouldn't connect to Wifi. Try again.");
  35. //WiFi.end();
  36. break;
  37. }
  38. }
  39. }
  40. while (WiFi.status() != WL_CONNECTED);
  41. Serial.print("\nConnected to AP ");
  42. Serial.println(SECRET_SSID);
  43. client.setInsecure();
  44. // Let the sensors know we're ready and start measuring.
  45. if (!NoiseSensor.begin(tick_count)) {
  46. Serial.println("Something didn't work. Please restart the system.");
  47. while (1) {
  48. Serial.println("Error, please restart.");
  49. }
  50. }
  51. NoiseSensor.setIds(sensebox_ids, sensor_ids);
  52. delay(1000);
  53. }
  54. void loop() {
  55. // Shows the openSenseMap's response.
  56. if (verbose) {
  57. while (client.available()) {
  58. char c = client.read();
  59. Serial.write(c);
  60. }
  61. }
  62. // This check is clumsy but will be replaced by a proper timer interrupt in the future.
  63. if (NoiseSensor.beaconReady()) {
  64. NoiseSensor.sendSyncBeacon();
  65. }
  66. // This check is clumsy but will be replaced by a proper timer interrupt in the future.
  67. if (millis() - last_reading >= 2000 && NoiseSensor.requestReady()) {
  68. Serial.println();
  69. NoiseSensor.read();
  70. // ---> Choose which sensor's data you want to upload by calling upload(device_id).
  71. upload(0, server);
  72. upload(2, server);
  73. // Record the last upload time to make sure we don't read and upload multiple times per second.
  74. last_reading = millis();
  75. }
  76. }
  77. void upload(uint8_t device_id, const char* server) {
  78. Serial.println("Start upload");
  79. if (client.connected()) {
  80. client.stop();
  81. delay(500);
  82. }
  83. // If there's a successful connection:
  84. if (client.connect(server, 443)) {
  85. String uploadbuf;
  86. String json = NoiseSensor.buildSenseBoxJSON(device_id);
  87. uint16_t content_length = json.length();
  88. Serial.println("connecting...");
  89. uploadbuf = NoiseSensor.buildHTTPHeader(device_id, server, content_length);
  90. client.println(uploadbuf);
  91. if (verbose) { Serial.println(uploadbuf); }
  92. client.println();
  93. if (verbose) { Serial.println(); }
  94. // For some reason that nobody knows, client.print has an arbitrary input string limit.
  95. // Therefore, we need to split it at some arbitrary length and provide chunks.
  96. for (uint16_t idx = 0; idx < json.length(); idx += 1000) {
  97. client.print(json.substring(idx, idx + 1000));
  98. if (verbose) { Serial.print(json.substring(idx, idx + 1000)); }
  99. }
  100. client.println();
  101. if (verbose) { Serial.println(); }
  102. Serial.println("done!");
  103. } else {
  104. // If we couldn't make a connection:
  105. Serial.println("connection failed. Restart System.");
  106. delay(1000);
  107. }
  108. Serial.println("Upload done");
  109. }