-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thingspeak_esp32_dB.ino
104 lines (75 loc) · 2.78 KB
/
Thingspeak_esp32_dB.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Adapted from Example from ThingSpeak Library (Mathworks)
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-thingspeak-publish-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include "ThingSpeak.h"
const char* ssid = " SSID"; // your network SSID (name)
const char* password = "Password"; // your network password
int sample;
WiFiClient client;
unsigned long myChannelNumber = 123456;// enter appropriate channel Number
const char * myWriteAPIKey = "type your API Key here";
//Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
int SampleWindow = 20;
float dB_Sound;
void setup() {
Serial.begin(115200); //Initialize serial
// initBME();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected.");
}
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 4095;
// collect data for 50 mS
while (millis() - lastTime < SampleWindow)
{
sample = analogRead(35); // Where the analog pin to the MAX9814 / MAX4466 is connected.
if (sample < 4096) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
float volts = ((peakToPeak * 5) / 4095); // * 0.707; // convert to RMS voltage
float first = log10(volts/0.00631)*20;
float second1 = first + 94 - 44 - 25;
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
int x = ThingSpeak.writeField(myChannelNumber,1,second1, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
Serial.println(String(sample) +" "+ String(signalMax)+ " "+ String(signalMin));
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
lastTime = millis();
}
}