Message in Blynk

Good morning.
I’m new to Blynk and I do not speak any English.
I am working on the implementation of a safety system measuring the concentration of two gases, methane (CH4) and carbon monoxide (CO) and I need to send a one time message to an email, if the concentration remains within a range Established or if the concentration increases exceeding the next limit in the ranges. It would be exceptional, that no message will be sent if the concentration starts to decrease but for now only the message will be sent if it stays in those ranges or exceeds the following range, I clarify, only once.

I include the code in which I process the reading of the sensors and the code for the sending of said message, being CH4 and CO, the concentrations of methane and carbon monoxide respectively.

int MQ4 = 34; int MQ9 = 35;

void setup(){
  pinMode(MQ4, INPUT); pinMode(MQ9, INPUT);
  Serial.begin(115200);
}

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

void concentracion(){
    
  int ADC_MQ4 = analogRead(MQ4); // Leemos la salida analógica del MQ4
  float Vout_MQ4 = ADC_MQ4 * (3.3 / 4096.0); // Convertimos la lectura en un valor de voltaje
  float Rs_MQ4 = ((5.0 - Vout_MQ4) / Vout_MQ4);  // Calculamos Rs
  float Rs_Ro_MQ4 = Rs_MQ4; // / Ro_MQ4; // Calculamos Rs/Ro
  float CH4 = 1103.1*(pow(Rs_Ro_MQ4, -2.71)); // Calculamos la concentración de metano con la ecuación obtenida
  Serial.print("  Concentracion de CH4: ");
  Serial.print(CH4, 3);
  Serial.print(" ppm");

  int ADC_MQ9 = analogRead(MQ9); // Leemos la salida analógica del MQ9
  float Vout_MQ9 = ADC_MQ9 * (3.3 / 4096.0); // Convertimos la lectura en un valor de voltaje
  float Rs_MQ9 = ((5.0 - Vout_MQ9) / Vout_MQ9);  // Calculamos Rs
  float Rs_Ro_MQ9 = Rs_MQ9; // / Ro_MQ9; // Calculamos Rs/Ro
  float CO = 605.62*(pow(Rs_Ro_MQ9, -2.056)); // Calculamos la concentración de monoxido de carbono con la ecuación obtenida
  Serial.print("  Concentracion de CO: ");
  Serial.print(CO, 3);
  Serial.println(" ppm");

  Blynk.virtualWrite(V1, CH4);
  Blynk.virtualWrite(V2, CO);
  delay(1000);
}

void mensaje(){
////////////////// Mensajes para monoxido de carbono /////////////////
  if ((CO < 50) || (CH4 < 30)) {
    Blynk.email("raytejada07@gmail.com", "Concentracion de gases", "La concentracion de gases es normal.");
  }
  if ((CO >= 50) || (CH4 >= 30)) {
    Blynk.email("raytejada07@gmail.com", "Concentracion de gases", "La concentracion de gases a aumentado.");
  }
  if (CO >= 80 || CH4 >= 100) {
    Blynk.email("raytejada07@gmail.com", "Concentracion de gases", "La concentracion de gases supero niveles seguros.");
  }
  if (CO >= 1200 || CH4 >= 500) {
    Blynk.email("raytejada07@gmail.com", "Concentracion de gases", "La concentracion de gases supero niveles criticos.");
  }
  if (CO >= 1500 || CH4 >= 800) {
    Blynk.email("raytejada07@gmail.com", "Concentracion de gases", "La concentracion de gases supero niveles peligrosos.");
  }
  if (CO >= 4000 || CH4 >= 1000) {
    Blynk.email("raytejada07@gmail.com", "Concentracion de gases", "La concentracion de gases supero niveles altamente peligrosos.");
  } 
}

I hope you can help me and thanks in advance.

You need to clear out the loop() by using timers, and get rid of the delay() in the other functions.

Then use “flags” to send the message only once when certain conditions are met.

If you search around this forum, i bet there are some examples you could reference.

1 Like

Hi,

First of all delete concentracion() from the main loop. Than set up 2 (or more if needed) timers that call your 2 (or more) functions like this in the setup

timer.setInterval(5000L, concentracion); //this will call the function every 5s

and delete the delay(1000); from it

Erik

Yes, as mentioned, clean up your void loop() that concentracion() function is trying to run thousands of times a second and while the delay may slow things down, it is also hampering Blynk’s ability to stay online… use timers as shown.

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

I also fixed your code formatting when pasting in this forum…

Blynk - FTFC