Why when connect with blynk, different value obtained from basic code

Hi all, please I need your help on this code. When without the connection of blynk command, I get value decibel range 35-60 . but when connecting with blynk, the value goes to thousand. anyone know how to solve?

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""
  #include <WiFi.h>
  #include <WiFiClient.h>
  #include <BlynkSimpleEsp32.h>
  char auth[] = BLYNK_AUTH_TOKEN;
  char ssid[] = "";
  char pass[] = ""; 
const int sampleWindow = 50;                              // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;

const byte soundpin=4;
const byte led1= 2;
const byte led2= 17;
const byte led3= 18;

void setup() {
  pinMode (soundpin,INPUT);
  pinMode (led1,OUTPUT);
  pinMode (led2,OUTPUT);
  pinMode (led3,OUTPUT);
  Serial.begin (115200); 
  Blynk.begin(auth, ssid, pass);
}

void loop() {

unsigned long startMillis= millis();
float peakTopeak= 0;

unsigned int signalMax=0;   // minvalue
unsigned int signalMin=1024; //maxvalue

    while(millis() - startMillis < sampleWindow)
    {
        sample = analogRead(soundpin);
        if (sample < 1024)
          {
            if (sample > signalMax)
            {
            signalMax = sample;   //save just max level
            }
            else if(sample < signalMax)
            {
              signalMin = sample; //save just min level
            }
         }  
    }
  peakTopeak = signalMax - signalMin;
  int store = map(peakTopeak,20,900,49.5,90);   //calibrate the db
  int db= store-30; 
  
  Serial.print("\nLoudness: ");
  Serial.print (db);
  Serial.print ("Db(A)\t");

  if (db<=37)
  {
    Serial.print("Levell: Quiet");
    digitalWrite(led1, HIGH);
  }
  else if(db>37 && db<40)
  {
    Serial.print("Level2: Moderate");
    digitalWrite(led2, HIGH);
  }
  else if(db>=40)
  {
    Serial.print("Level3: HIGH");
    digitalWrite(led3, HIGH);
  }
  Blynk.virtualWrite(V0,db);
  delay(1000);
  digitalWrite (led1,LOW);
  digitalWrite (led2,LOW);
  digitalWrite (led3,LOW);
  Blynk.run();
}

First of all, you should read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

what if I want to read the data from analog sensor, and do some calculation like calibrate first by mapping to get decibel values (range 0-120). should I do the analogread n do calibration in void sensorDatasend () function and end with blynk.virtualWrite(V0,db); then in void loop() {blynk.run(); timer.run();}?? I still faced the same number (thousand dbs)

Yes.

Show us what results you are getting in your serial monitor (copy the serial monitor text and paste it between triple backticks - do not post screenshots) and what you are seeing in Blynk.
Show us how you have configured the V0 datastream, and how you’ve configured the widget attached to V0 in your app.
Also, post your modified code.

Pete.

serial monitor

Loudness: 2440340Db(A)	Level3: HIGH
Loudness: 2440340Db(A)	Level3: HIGH
Loudness: 2440340Db(A)	Level3: HIGH

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""
  #include <WiFi.h>
  #include <WiFiClient.h>
  #include <BlynkSimpleEsp32.h>
  char auth[] = BLYNK_AUTH_TOKEN;
  char ssid[] = "";
  char pass[] = ""; 

  BlynkTimer timer;
  const int sampleWindow = 50;                              // Sample window width in mS (50 mS = 20Hz)
  const byte soundpin=4;
  const byte led1= 2;
  const byte led2= 17;
  const byte led3= 18;

  
void setup() {
  pinMode (soundpin,INPUT);
  pinMode (led1,OUTPUT);
  pinMode (led2,OUTPUT);
  pinMode (led3,OUTPUT);
  Serial.begin (115200);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L,sensorDataSend);

}

void sensorDataSend(){
int sample;
unsigned long startMillis= millis();
float peakTopeak= 0;
int db;

unsigned int signalMax=0;   // minvalue
unsigned int signalMin=1024; //maxvalue

    while(millis() - startMillis < sampleWindow)
    {
        sample = analogRead(soundpin);
        if (sample < 1024)
          {
            if (sample > signalMax)
            {
            signalMax = sample;   //save just max level
            }
            else if(sample < signalMax)
            {
              signalMin = sample; //save just min level
            }
         }  
    }
  peakTopeak = signalMax - signalMin;
  int store = map(peakTopeak,20,900,49.5,90);   //calibrate the db
  db= store-30; 
  Serial.print("\nLoudness: ");
  Serial.print (db);
  Serial.print ("Db(A)\t");
  Blynk.virtualWrite(V0,db);
  if (db<=37)
  {
    Serial.print("Levell: Quiet");
    digitalWrite(led1, HIGH);
  }
  else if(db>37 && db<40)
  {
    Serial.print("Level2: Moderate");
    digitalWrite(led2, HIGH);
  }
  else if(db>=40)
  {
    Serial.print("Level3: HIGH");
    digitalWrite(led3, HIGH);
  }
  delay(500);

}
void loop() {
 Blynk.run();
 timer.run();

}

I’d suggest that you add more serial print statements so that you can monitor the values of the various variables, and Tim -on the timestamp in the serial monitor too.

You also need to remove this:

Pete.

I try to do simple coding, receive data from sensor and send to blynk app. When I do without connection of blynk, the serial monitor show range of result from 500-600. As I connect with blynk, 0 value remains. Can you help why?

  #define BLYNK_TEMPLATE_ID "TMPLUbWIVa-J"
#define BLYNK_DEVICE_NAME "IoT Sound Monitoring"
#define BLYNK_AUTH_TOKEN "X1M4kjMcKWngRCBVHyaWFZecNuVt47cY"
  #include <WiFi.h>
  #include <WiFiClient.h>
  #include <BlynkSimpleEsp32.h>
  char auth[] = BLYNK_AUTH_TOKEN;
  char ssid[] = "akmalijat0_2.4GHz@unifi";
  char pass[] = "Hyperjackie12#"; 
  const byte soundpin=4;
  BlynkTimer timer;
void setup() {
  pinMode (soundpin,INPUT);
  Serial.begin (115200);
   Blynk.begin(auth, ssid, pass);
   timer.setInterval(1000L,sensorDataSend);
}

void sensorDataSend(){
  int sample= analogRead(soundpin);
  Serial.print(sample);
  Serial.print("\n");
  Blynk.virtualWrite(V0,sample);
}
void loop() {
Blynk.run();
timer.run();
}
0
0
0
0
0
0
0

Okay, it’s the analog pin that you’ve chosen.

GPIO4 is an ADC2 pin…

ADC1_CH0 (GPIO 36)
ADC1_CH1 (GPIO 37)
ADC1_CH2 (GPIO 38)
ADC1_CH3 (GPIO 39)
ADC1_CH4 (GPIO 32)
ADC1_CH5 (GPIO 33)
ADC1_CH6 (GPIO 34)
ADC1_CH7 (GPIO 35)

ADC2_CH0 (GPIO 4)
ADC2_CH1 (GPIO 0)
ADC2_CH2 (GPIO 2)
ADC2_CH3 (GPIO 15)
ADC2_CH4 (GPIO 13)
ADC2_CH5 (GPIO 12)
ADC2_CH6 (GPIO 14)
ADC2_CH7 (GPIO 27)
ADC2_CH8 (GPIO 25)
ADC2_CH9 (GPIO 26)

Pete.

the system went well since I changed to GPIO35. But may I know why sometimes the readings quite away from the usual (30-40dB ).

Loudness: 36Db(A)	Levell: Quiet
Loudness: 34Db(A)	Levell: Quiet
Loudness: 2099233Db(A)	Level3: HIGH
Loudness: 33Db(A)	Levell: Quiet
Loudness: 34Db(A)	Levell: Quiet
Loudness: 36Db(A)	Levell: Quiet
Loudness: 34Db(A)	Levell: Quiet
Loudness: 34Db(A)	Levell: Quiet
Loudness: 34Db(A)	Levell: Quiet
Loudness: 35Db(A)	Levell: Quiet
Loudness: 2099233Db(A)	Level3: HIGH

but when I compare without enable the IoT, which is blynk connection. the output give better which no thousand dbs for the result

Hi pete, Do you have any idea how to make the led light up for about 0.5 sec and OFF before new loop. If i used delay, then the reading will be affected ( huge number in result). same also if i put the digital write and blink virtual write at the top part.

void sensorDataSend() {
  int sample;
  unsigned long startMillis = millis();
  float peakTopeak = 0;
  int db;

  unsigned int signalMax = 0; // minvalue
  unsigned int signalMin = 1024; //maxvalue


  while (millis() - startMillis < sampleWindow)
  {
    sample = analogRead(soundpin);
    if (sample < 1024)
    {
      if (sample > signalMax)
      {
        signalMax = sample;   //save just max level
      }
      else if (sample < signalMax)
      {
        signalMin = sample; //save just min level
      }
    }
  }
  peakTopeak = signalMax - signalMin;
  int store = map(peakTopeak, 20, 900, 49.5, 90); //calibrate the db
  db = store - 17;
  Serial.print("\nLoudness: ");
  Serial.print (db);
  Serial.print ("Db(A)\t");
  Blynk.virtualWrite(V0, db);
  if (db <= 37)
  {
    Serial.print("Levell: Quiet");
    digitalWrite(led1, HIGH);
    Blynk.virtualWrite(V1, 1);
  }
  else if (db > 37 && db < 40)
  {
    Serial.print("Level2: Moderate");
    digitalWrite(led2, HIGH);
    Blynk.virtualWrite(V2, 1);
  }
  else if (db >= 40)
  {
    Serial.print("Level3: HIGH");
    digitalWrite(led3, HIGH);
    Blynk.virtualWrite(V3, 1);
  }
  //delay(500); cannot place delay else high value shown up
  digitalWrite (led1, LOW);
  digitalWrite (led2, LOW);
  digitalWrite (led3, LOW);
  Blynk.virtualWrite(V1, 0);
  Blynk.virtualWrite(V2, 0);
  Blynk.virtualWrite(V3, 0);
}

You can use a Blynk timer in timeout timer mode, like this…

  timer.setTimeout(500L, []() 
  {  
    // When the timer completes, any code here will be executed
    // so put your code to turn off the LED in here
  }); 

But, I suspect that’s not the best solution for you.
I think once you’ve taken your readings and are ready to display the results you would be better turning all three LED’s off, then turning the appropriate LED on, then leaving it on until the next batch of readings have been taken.

Also, you could emulate the old LED VU meter by lighting up the low and medium LEDs at the same time when you have a medium reading, and the low, medium and high LEDs when you have a high reading…

image

Pete.