Created
June 2, 2017 09:05
-
-
Save tomekc/deae397359a55e5cd161bf4de9447021 to your computer and use it in GitHub Desktop.
Noise level meter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266HTTPClient.h> | |
#include <ESP8266WiFi.h> | |
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz) | |
unsigned int sample; | |
#define BLINK_LED 16 | |
#define WIFI_LED_PIN 16 | |
#define NUM_MEASURES 1200 // Sampling freq. is 20 hz | |
#define FIELD_ID 2 | |
// WiFi information | |
const char WIFI_SSID[] = "your wifi network name"; | |
const char WIFI_PSK[] = "your wifi password"; | |
const char *apiKey = "ThingSpeak API key"; | |
int measures[NUM_MEASURES]; | |
struct SoundStats { | |
int average; | |
int levelMin; | |
int levelMax; | |
}; | |
int getNoiseSample() { | |
unsigned long startMillis = millis(); // Start of sample window | |
unsigned int peakToPeak = 0; // peak-to-peak level | |
unsigned int signalMax = 0; | |
unsigned int signalMin = 1024; | |
while (millis() - startMillis < sampleWindow) | |
{ | |
sample = analogRead(A0); | |
if (sample < 1024) // 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 | |
} | |
} | |
delay(1); | |
} | |
peakToPeak = signalMax - signalMin; | |
return peakToPeak; | |
} | |
char url[1024]; | |
char* postData(int value) { | |
sprintf(url, "http://api.thingspeak.com/update?api_key=THINGSPEAK_API_KEY&field%d=%d", FIELD_ID, value); | |
Serial.println(url); | |
return url; | |
} | |
void connectToWiFi() { | |
byte led_status = 1; | |
// Set WiFi mode to station (client) | |
WiFi.mode(WIFI_STA); | |
// Initiate connection with SSID and PSK | |
WiFi.begin(WIFI_SSID, WIFI_PSK); | |
Serial.println("Connecting to WiFi"); | |
// Blink LED while we wait for WiFi connection | |
while ( WiFi.status() != WL_CONNECTED ) { | |
digitalWrite(WIFI_LED_PIN, led_status); | |
led_status ^= 0x01; | |
delay(100); | |
} | |
Serial.println("[WiFi] Connected"); | |
// Turn LED on when we are connected | |
digitalWrite(WIFI_LED_PIN, HIGH); | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
pinMode(A0, INPUT); | |
pinMode(BLINK_LED, OUTPUT); | |
connectToWiFi(); | |
} | |
int getSamples() { | |
int sum = 0; | |
int minAmpli = 1024; | |
int maxAmpli = 0; | |
for (int j = 0; j<NUM_MEASURES; j++) { | |
int sample = getNoiseSample(); | |
measures[j] = sample; | |
sum += sample; | |
if (sample > maxAmpli) { maxAmpli = sample; } | |
if (sample < minAmpli) { minAmpli = sample; } | |
delay(1); | |
} | |
int average = sum / NUM_MEASURES; | |
return average; | |
} | |
char buffer[128]; | |
void loop() { | |
// put your main code here, to run repeatedly: | |
digitalWrite(BLINK_LED, LOW); | |
delay(1000); | |
int noiseValue = getSamples(); | |
Serial.println(noiseValue); | |
HTTPClient http; | |
printf("[WiFi] status %d\n", WiFi.status()); | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.println("[WiFi] Reconnecting"); | |
byte led_status = 0; | |
while ( WiFi.status() != WL_CONNECTED ) { | |
digitalWrite(WIFI_LED_PIN, led_status); | |
led_status ^= 0x01; | |
delay(100); | |
} | |
} | |
http.begin(postData(noiseValue)); | |
int httpCode = http.GET(); | |
// httpCode will be negative on error | |
if(httpCode > 0) { | |
// file found at server | |
if(httpCode == HTTP_CODE_OK) { | |
Serial.println("[HTTP] Reading sent to server"); | |
} | |
} else { | |
Serial.printf("[HTTP] GET failed, code %d, error: %s\n", httpCode, http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
digitalWrite(BLINK_LED, HIGH); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment