Hello everyone,
Just bought an Arduino kit, and was able to set a few leds (red yellow and green) to work using a very standard sound sensor. Accuracy is not the best, but it works.
Now I was trying to send this info to a webpage using a WiFi connector.
Wifi connects fine, few ok messages, but I have done something not quite right.
Leds get crazy with the Wifi module. But if I disconnect the Wifi everything works again sound and lights.
Schematics attached
The "sample" on the code bellow seems to be the problem, tried to set a few prints to see what was wrong.
Monitor With WiFi:
12:00:13.792 -> SENSOR_PIN 14
12:00:13.792 -> sample 343
12:00:13.839 -> db 81
12:00:13.839 -> 729
12:00:13.839 -> 5
12:00:13.839 -> 724.00
12:00:13.839 -> SENSOR_PIN 14
12:00:13.839 -> sample 676
12:00:13.885 -> db 66
12:00:13.885 -> 731
12:00:13.885 -> 345
12:00:13.885 -> 386.00
Monitor without the Wifi:
12:00:53.261 -> SENSOR_PIN 14
12:00:53.261 -> sample 347
12:00:53.307 -> db 52
12:00:53.307 -> 401
12:00:53.307 -> 302
12:00:53.307 -> 99.00
12:00:53.307 -> SENSOR_PIN 14
12:00:53.354 -> sample 351
12:00:53.354 -> db 51
12:00:53.354 -> 395
12:00:53.354 -> 312
12:00:53.354 -> 83.00
Bellow the code I am using:
#include "SoftwareSerial.h" //para a placa de WiFi
SoftwareSerial ESP_Serial(9, 8); // RX, TX para a placa de WiFi
String rede = "2.4G_CASA"; //para a placa de WiFi
String senha = "6543212345"; //para a placa de WiFi
String resposta = ""; //para a placa de WiFi
const int sampleWindow = 50;// Sample window width in mS (50 mS = 20Hz)
const int pinoLEDverd = 10; // pino onde o LED verde está conectado
const int pinoLEDamar = 11; // pino onde o LED amarelo está conectado
const int pinoLEDverm = 12; // pino onde o LED vermelho está conectado
unsigned int sample;
#define SENSOR_PIN A0
#define PIN_QUIET pinoLEDverd
#define PIN_MODERATE pinoLEDamar
#define PIN_LOUD pinoLEDverm
void setup ()
{
Serial.begin(9600); //Para o Farol
ESP_Serial.begin(9600); //Para o WiFi
//Para o WiFi
Serial.println("Inicializando..."); //Para o WiFi
delay(1000); //Para o WiFi
//Para o WiFi
Serial.println("Chamando atencao do modulo com AT..."); //Para o WiFi
sendCommand("AT"); //Para o WiFi
readResponse(1000); //Para o WiFi
//Para o WiFi
Serial.println("Mudando o modo com CWMODE=1..."); //Para o WiFi
sendCommand("AT+CWMODE=1"); //Para o WiFi
readResponse(1000); //Para o WiFi
//Para o WiFi
pinMode(SENSOR_PIN, INPUT); // Set the signal pin as input
pinMode(PIN_QUIET, OUTPUT);
pinMode(PIN_MODERATE, OUTPUT);
pinMode(PIN_LOUD, OUTPUT);
digitalWrite(PIN_QUIET, LOW);
digitalWrite(PIN_MODERATE, LOW);
digitalWrite(PIN_LOUD, LOW);
// digitalWrite(pinoLEDverd, LOW);
// digitalWrite(pinoLEDamar, LOW);
// digitalWrite(pinoLEDverm, LOW);
Serial.println("Conectando a rede..."); //Para o WiFi
String CWJAP = "\"AT+CWJAP=\""; //Para o WiFi
CWJAP += rede; //Para o WiFi
CWJAP += "\",\""; //Para o WiFi
CWJAP += senha; //Para o WiFi
CWJAP += "\""; //Para o WiFi
sendCommand(CWJAP); //Para o WiFi
readResponse(10000); //Para o WiFi
//Para o WiFi
delay(2000); //espera de seguranca //Para o WiFi
//Para o WiFi
if (resposta.indexOf("OK") == -1) { //procura na resposta se houve OK //Para o WiFi
Serial.println("Atencao: Nao foi possivel conectar a rede WiFi."); //Para o WiFi
Serial.println("Verifique se o nome da rede e senha foram preenchidos corretamente no codigo e tente novamente."); //Para o WiFi
} else { //Para o WiFi
Serial.println("Sucesso! Conectado a rede WiFi."); //Para o WiFi
}
}
void loop ()
{
unsigned long startMillis= millis(); // Start of sample window
float peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0; //minimum value
unsigned int signalMin = 1024; //maximum value
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(SENSOR_PIN); //get reading from microphone
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
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
int db = map(peakToPeak,20,900,49.5,90); //calibrate for deciBels
if (db <= 59)
{
digitalWrite(PIN_QUIET, HIGH);
digitalWrite(PIN_MODERATE, LOW);
digitalWrite(PIN_LOUD, LOW);
}
else if (db > 59 && db < 69)
{
digitalWrite(PIN_QUIET , LOW);
digitalWrite(PIN_MODERATE, HIGH);
digitalWrite(PIN_LOUD , LOW);
}
else if (db>=69)
{
digitalWrite(PIN_QUIET , LOW);
digitalWrite(PIN_MODERATE, LOW);
digitalWrite(PIN_LOUD , HIGH);
}
Serial.print("SENSOR_PIN ");
Serial.println(SENSOR_PIN);
Serial.print("sample ");
Serial.println(sample);
Serial.print("db ");
Serial.println(db);
Serial.println(signalMax);
Serial.println(signalMin);
Serial.println(peakToPeak);
}
void sendCommand(String cmd) {
ESP_Serial.println(cmd);
}
void readResponse(unsigned int timeout) {
unsigned long timeIn = millis(); //momento que entramos nessa funcao é salvo
resposta = "";
//cada comando AT tem um tempo de resposta diferente...
while (timeIn + timeout > millis()) {
if (ESP_Serial.available()) {
char c = ESP_Serial.read();
resposta += c;
}
}
Serial.println(resposta);
}