What am I doing wrong? esp8266 ch340 dht22 Blynk 2.0"

To receive a notification you have to enable notifications like this

Have you exceeded your 100 event limit?

Pete.

Friends, I post the entire project for you to review, to see what I’m failing. First of all, Thanks
PD: Pete never received notifications, so I must not have exceeded the limit of 100 daily.

#define BLYNK_TEMPLATE_ID "TMPLdnAA_caL"
#define BLYNK_DEVICE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "U1z4xPEw1FgiedaDoDo9U5h2VqxC93_K"
#define bandera
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>


char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "MEDINA-FTTH";
char pass[] = "dinofeli";

#define DHTPIN 4       

//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
#define DHTTYPE DHT22     

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;


void sendSensor()
{
 float nem = dht.readHumidity();
 float sicaklik = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

 if (isnan(nem) || isnan(sicaklik)) {
   Serial.println("Failed to read from DHT sensor!");
   return;
}
 // You can send any value at any time.
 // Please don't send more that 10 values per second.
 Blynk.virtualWrite(V1, nem);
 Blynk.virtualWrite(V0, sicaklik);

 
if (V0 > 30){
    Blynk.logEvent("HOTPUSH", "ESTO SE ESTA CALENTANDO,") ;
}

}
void sendAlert()
{
 float nem = dht.readHumidity();
 float sicaklik = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  
 
 // You can send any value at any time.
 // Please don't send more that 10 values per second.
   Blynk.virtualWrite(V0, sicaklik);
}

void setup()
{
 // Debug console
 Serial.begin(115200);

 Blynk.begin(auth, ssid, pass);
 // You can also specify server:
 //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
 //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

 dht.begin();

 // Setup a function to be called every second
 timer.setInterval(5000L, sendSensor);
 timer.setInterval(60000L, sendAlert);
 

 
}

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

You can use API to test the event, just to make sure it’s working properly

https://docs.blynk.io/en/blynk.cloud/trigger-events

If you re-read what I said, you’ll see that I asked about reaching the EVENT limit, not the NOTIFICATION limit…

I don’t see the point of the void sendAlert() function and its associated timer because…
a) it doesnt send an alert
b) it does no if (isnan(nem) || isnan(sicaklik)) error checking
c) it only sends the temperature value to Blynk.

You should tidy-up your code and remove this.

Your void sendSensor() function contains this piece of code…

You can’t read the value from virtual pin V0 in this way, so the if statement will never evaluate as true so the event will never be logged. Your code should read…

if (sicaklik > 30)
{
  Blynk.logEvent("HOTPUSH", "ESTO SE ESTA CALENTANDO,") ;
  // you could add a serial print message in here for debugging
}

But, as I already explained, a prolonged period where sicaklik > 30 will result in you exceeding your 24 hour event limit very quickly, and your events, and their corresponding notifications, will stop working for 1 day.

Pete.

hello Pete, after your recommendations I did a bit of cleaning in the code, and then I started to investigate how I can make it limit daily notifications. I would like you to tell me if I am on the right path. Because it doesn’t send me since I put the if with the timer. The idea there is that it sends me notifications only every 120 seconds. Obviously adding it would happen but it was to try, but I don’t get more. I wait your answer. Thanks.


#define BLYNK_TEMPLATE_ID "TMPLdnAA_caL"
#define BLYNK_DEVICE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "U1z4xPEw1FgiedaDoDo9U5h2VqxC93_K"
#define DHTPIN 4
// Comment this out to disable prints and save space
//#define BLYNK_PRINT Serial
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
#define DHTTYPE DHT22
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "MEDINA-FTTH";
char pass[] = "dinofeli";


DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void setup()
  {
 // Debug console
    Serial.begin(115200);
    Blynk.begin(auth, ssid, pass);
 // You can also specify server:
 //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
 //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
    dht.begin();
    timer.setInterval(5000L, sendSensor);
 // Setup a function to be called every second
  float temp = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
     if (temp > 30)
    {
    timer.setInterval(120000L, sendAlert);
    }
  }
void sendSensor()
  {
    float nem = dht.readHumidity();
    float temp = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
     Blynk.virtualWrite(V1, nem);
     Blynk.virtualWrite(V0, temp);
  }

void sendAlert()
  {
    
   
      Blynk.logEvent("HOTPUSH", "ESTO SE ESTA CALENTANDO,") ;
  
  }

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

I compiled it, uploaded it and it gives me notifications but it doesn’t limit them.

A timer isn’t the solution, and even if it was then the way you’ve implemented it will never work because the if statement is in your void setup.

You want your sketch to do this…

  • Take a temp/humidity reading and check that the results are actual numbers.
  • Check if the temperature is above your threshold
  • If it is above the threshold then check if you’ve already sent a notification relating to this over-temperature event
  • if a notification hasn’t been sent then send one, and set a variable to indicate that a notification has been sent.

  • If the temperature is below the threshold then clear the variable that’s use to track whether a notification has been sent.

This will result in one notification being sent for each over temperature event, and when the temperature drops below the threshold temperature it will reset the variable that’s used to track notifications and then allow another notification to be sent if the temperature goes above the threshold again.

This is what I told you in post #17….

All of this can be done within your existing sendSensor function provided you create a global variable to act as the flag which indicates that a notification has been sent. This could be a boolean or integer type of variable, which can be set to true (1) or false (0) to indicate if a notification has been sent or not for the current over temperature event.

Pete.

1 Like

You might want to read this topic that I just posted…

Pete.

Dear Pete, I share the code already polished and modified several times, it controls the temperature and humidity creating notifications for critical temperature in 5 cycles of 5 minutes and then in 5 cycles of 5 hours. Thanks for your help and community.

#define BLYNK_TEMPLATE_ID "TMPLdnAA_caL" //el ID del dispositivo
#define BLYNK_DEVICE_NAME "Quickstart Template" //nombre del template
#define BLYNK_AUTH_TOKEN "U1z4xPEw1FgiedaDoDo9U5h2VqxC93_K" //auth token del template

#define BLYNK_PRINT Serial //para que funcione el monitor serial
#include <ESP8266WiFi.h> //carga la libreria de la placa NODEMCU
#include <BlynkSimpleEsp8266.h> //carga la libreria de Blynk
#include <DHT.h> //carga la libreria del sensor de temperatura/humedad

char auth[] = BLYNK_AUTH_TOKEN; //valida el token con el comando enterior
char ssid[] = "MEDINA-FTTH";  // el usuario del wifi
char pass[] = "dinofeli";  // el password del wifi
#define DHTPIN 4          // el pin digital al que está conectado el sensor
#define DHTTYPE DHT22     // DHT 22 (dht11 si es el sensor mas varato) 
DHT dht(DHTPIN, DHTTYPE); //declara la variable para el sensor
BlynkTimer timer; //declara la variable para el timer
float hum; //la variable global de la humedad
float temp; //la variable global de la temperatura
float termo1; //la variable global del primer ciclo de notificaciones, instantaneo
float termo2; //la variable global del segundo ciclo de notificaciones, luego de 5 minutos del primero
float termo3; //la variable global del tercer ciclo de notificaciones, luego de 5 minutos del segundo
float termo4; //la variable global del cuarto ciclo de notificaciones, luego de 5 minutos del tercero
float termo5; //la variable global del quinto ciclo de notificaciones, luego de 5 minutos del cuarto
float termoh; //la variable global del sexto ciclo de notificaciones, luego de 5 minutos del anterior y se repite cada 60 minutos durante 5 periodos (cada una hora 5 veces)
float limit = 30; //es la variable de limite critico de temperatura, para otros limites utilizar automatización, ejemplo etapa de descongelamiento del equipo

//ahora arrancamos con la funcion sendsensor
void sendSensor(){
                  float hum = dht.readHumidity(); //ingresa en la variable el comando para el valor del sensor (humedad)
                  float temp = dht.readTemperature(); //ingresa en la variable el comando para el valor del sensor (temperatura)
                  //en estas lineas le decimos que nos muestre en el monitor serial si falla la lectura dle sensor
                  if (isnan(hum) || isnan(temp)) {
                                                  Serial.println("Fallo en la lectura del sensor!");
                                                  return;
                                                 }
                  Blynk.virtualWrite(V1, hum);
                  Blynk.virtualWrite(V0, temp);
                  Serial.print("Temperatura : ");
                  Serial.print(temp);
                  Serial.print("  Humedad : ");
                  Serial.println(hum);
                  //comienzo del primer ciclo, instantáneo
                  termo1 = temp; //variable contador para la lógica
                               if(termo1 > limit){
                                Serial.println("adentro del primer ciclo!");
                                Blynk.logEvent("ALERTA", "la temperatura ha superado los [limit]°C!! 1ra notificación");  
                                termo1 = 0; 
                                           
                                            for (int tiempo = 1; tiempo < 100; tiempo++) {
                                                                              hum = dht.readHumidity();
                                                                              temp = dht.readTemperature();                    
                                                                              Blynk.virtualWrite(V1, hum);
                                                                              Serial.print("  Humedad : ");
                                                                              Serial.println(hum);
                                                                              Blynk.virtualWrite(V0, temp);
                                                                              Serial.print("Temperatura : ");
                                                                              Serial.print(temp);
                                                                              delay(3000);
                                                                             }
                                                                                                             termo1 = 0;
                                               }
                  //comienzo del segundo ciclo, luego de 5 minutos
                  termo2 = temp; //variable contador para la lógica
                                if(termo2 > limit){
                                Serial.println("adentro del segundo ciclo!");
                                Blynk.logEvent("ALERTA", "la temperatura ha superado los 30°C!! 2da notificación");
                                                                                                              termo2 = 0; 
                                for (int tiempo = 1; tiempo < 100; tiempo++) {
                                                                              hum = dht.readHumidity();
                                                                              temp = dht.readTemperature();                    
                                                                              Blynk.virtualWrite(V1, hum);
                                                                              Serial.print("  Humedad : ");
                                                                              Serial.println(hum);
                                                                              Blynk.virtualWrite(V0, temp);
                                                                              Serial.print("Temperatura : ");
                                                                              Serial.print(temp);
                                                                              delay(3000);
                                                                              }
                                                                              termo2 = 0;
                                           }
                   //comienzo del tercer ciclo, luego de 5 minutos
                   termo3 = temp; //variable contador para la lógica
                              if(termo3 > limit){
                                Serial.println("adentro del tercer ciclo!");
                                Blynk.logEvent("ALERTA", "la temperatura ha superado los 30°C!! 3ra notificación");
                                termo3 = 0; 
                                for (int tiempo = 1; tiempo < 100; tiempo++) {
                                                                              hum = dht.readHumidity();
                                                                              temp = dht.readTemperature();                    
                                                                              Blynk.virtualWrite(V1, hum);
                                                                              Serial.print("  Humedad : ");
                                                                              Serial.println(hum);
                                                                              Blynk.virtualWrite(V0, temp);
                                                                              Serial.print("Temperatura : ");
                                                                              Serial.print(temp);
                                                                              delay(3000);
                                                                              }
                                                                              termo3 = 0; 
                                                                              }
                   //comienzo del cuarto ciclo, luego de 5 minutos                                                           
                   termo4 = temp; //variable contador para la lógica
                              if(termo4 > limit){
                                Serial.println("adentro del cuarto ciclo!");
                                Blynk.logEvent("ALERTA", "la temperatura ha superado los 30°C!! 4ra notificación");

                                termo4 = 0; 
                                for (int tiempo = 1; tiempo < 100; tiempo++) {
                                                                              hum = dht.readHumidity();
                                                                              temp = dht.readTemperature();                    
                                                                              Blynk.virtualWrite(V1, hum);
                                                                              Serial.print("  Humedad : ");
                                                                              Serial.println(hum);
                                                                              Blynk.virtualWrite(V0, temp);
                                                                              Serial.print("Temperatura : ");
                                                                              Serial.print(temp);
                                                                              delay(3000);
                                                                              }
                                                                                                              termo4 = 0; 
                                                                              }
                   //comienzo del quinto ciclo, luego de 5 minutos
                   termo5 = temp; //variable contador para la lógica
                              if(termo5 > limit){
                                Serial.println("adentro del quinto ciclo!");
                                Blynk.logEvent("ALERTA", "la temperatura ha superado los 30°C!! 5ta notificación");
 
                                termo5 = 0; 
                                for (int tiempo = 1; tiempo < 100; tiempo++) {
                                                                              hum = dht.readHumidity();
                                                                              temp = dht.readTemperature();                    
                                                                              Blynk.virtualWrite(V1, hum);
                                                                              Serial.print("  Humedad : ");
                                                                              Serial.println(hum);
                                                                              Blynk.virtualWrite(V0, temp);
                                                                              Serial.print("Temperatura : ");
                                                                              Serial.print(temp);
                                                                              delay(3000);
                                                                              }
                                                                              
                                termo5 = 0; 
                                                                              }
                              //comienzo del sexto ciclo, luego de 5 minutos, se ejecutan 5 ciclos cada 1 hora (se le asigna la cantidad de ciclos en el FOR)
                              termoh = temp; //variable contador para la logica
                              if(termoh > limit){
                                                  for (int hora = 1; hora < 5; hora++) {
                                                                            Serial.println("adentro del sexto ciclo!");
                                                                            Blynk.logEvent("ALERTA", "la temperatura ha superado los 30°C!! notificación crítica cada una hora");
                                                                            for (int tiempo = 1; tiempo < 1200; tiempo++) {
                                                                              hum = dht.readHumidity();
                                                                              temp = dht.readTemperature();                    
                                                                              Blynk.virtualWrite(V1, hum);
                                                                              Serial.print("  Humedad : ");
                                                                              Serial.println(hum);
                                                                              Blynk.virtualWrite(V0, temp);
                                                                              Serial.print("Temperatura : ");
                                                                              Serial.print(temp);
                                                                              delay(3000);;
                                                                              }
                                                                              }
                                                                              
                                termo5 = 0; 
                                                                              }                                   
                               }
//función para ejecutar iniciales del programa, conectar al wifi y dar tiempo a la lectura del cuerpo del programa
void setup(){
            Serial.begin(115200);
              Blynk.begin(auth, ssid, pass);
              dht.begin();
            timer.setInterval(3000L, sendSensor);
            }

//ejecución de funciones del programa
void loop(){
            Blynk.run();
            timer.run();
           }

I’d recommend that if you use indentations to make your code more readable. The Arduino IDE has an Auto Format feature that will help with this.

Your sketch breaks one of the basic rules of Blynk programming, which is not using blocking delay() commands, as these make the sketch unresponsive to app input, and will lead to disconnections because the Blynk.run() command in the void loop is not being executed frequently enough.

It’s difficult to provide any other constructive comments on the sketch because of the appalling formatting, but it appears that your sendSensor function is repeating the same code multiple times, with the blocking delays to limit the sensor reading frequency, rather than having the code appear once and using the timer to manage that reading frequency.
I guess you’ve done this to compensate for your lack of code in skills, but it’s not the correct approach.

Pete.

1 Like

haha, Pete you look like my professor at the university, who told me that my process was doing the same thing but it was wrong. The indentation is on, unfortunately when I copied it to the forum it all moved. I’m going to get the sensor up and running with your example and test it. And yes, my ability to program is limited because I am a technical and computer integrator, I am not a programmer, that is why I turn to you and to this forum. Greetings and good life.

Probably because you’re using spaced instead of tabs.

Pete.

Pete, I have another question for you, does Blynk automatically notify you when a device is offline or do you also have to develop a notification within the script? Thank you

Not via the default online/offline events, no.

Pete.

that is, if my device is disconnected, the app does not notify me, I have to make a logic?

In the time it’s taken you to ask the question and ignore my answer you could have tested this yourself very easily, by disconnecting the device from its power supply.

Pete.

Hey Pete, you got up on the wrong foot today. In what it took you to answer I was investigating and I already found possible solutions, thank you very much anyway.
PS:: I appreciate it even if it has that bad mood. :slight_smile:

It has nothing to do with bad mood…Pete is always willing to help but your last post is exactly the point that Pete made… people should invest some time to try more them self firstly and not lean backwards, expecting the solution for their problem to be given all the time.

Pete should be giving all the respect he deserves for all the work he does here, which is tremendous. Remember he’s not staff of Blynk.
Pete you’re awesome!!

1 Like

sorry PETEFAN, I have no intention of offending anyone, I have thanked each of Pete’s answers, and I think he understands why I tell him what I say, as he writes to me it is clear that he has a vocation as a teacher and that in addition to trying to help try to get over yourself. It is not bad to put some humor to that or to my answers, if it is wrong to interpret something in the wrong way or to fanaticize with a programming forum. Greetings and good life.