Sending the Blynk mail when the temperature drops

I want to make sure that when the temperature drops, the mail goes. Bole wrote less, but when reaching the temperature range from 35 to 40 MAIL is constantly sent and other buttons do not work, as soon as the temperature goes out of range everything starts working normally. Did two options, one is commented.

Хочу сделать, чтобы при снижении температуры отправлялся маил. Боле менее написал, но при достижении диапазона температуры от 35 до 40 маил постоянно отправляется и другие кнопки не работают, как только температура выйдет из диапазона все начинает работать нормально. Делал два варианта, один закоментирован.

#define BLYNK_PRINT Serial
#define ONE_WIRE_BUS 2//датчик температуры на 2 пине

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define PIN 15 // контролируемый пин модуля с которого приходит сиграл

char auth[] = "YourAuthToken";

WidgetLED led1(V3);

BlynkTimer timer;

void buttonLedWidget(){
int x = digitalRead(PIN);//читаем пин модуля
if (x == 1) {//проверим состояние
led1.on(); //вкл вирт.светодиод
}
else { //иначе выключим
led1.off();
}
}

char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup()
{

Serial.begin(9600);

Blynk.begin(auth, ssid, pass);
sensors.begin();
pinMode(PIN, INPUT_PULLUP);
timer.setInterval(500L, buttonLedWidget);
}
void sendTemps()
{
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
Blynk.virtualWrite(V4, temp);

//if (temp >= 35 && temp <= 40){
// Blynk.email("your_email@mail.ru", "ВНИМАНИЕ низкая температура теплоносителя меньше 40С");

//}
}
void mail()
{
float sen;
sen=sensors.getTempCByIndex(0);
if (sen >= 35 && sen <= 40){

Blynk.email("your_email@mail.ru", "ВНИМАНИЕ низкая температура теплоносителя меньше 40С");//отправка майла при приходе сигнала
} 
}

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

These should not be directly in the void loop() as they will try to run hundreds of times a second. Put them in timed loops or separately called only when required.

You also need some form of timer or flag counter to prevent multiple emails from going out continuously when temp in range.

I’m going to try now

Search this forum for keywords as this type of issue (emailing only once when required) has been discussed many times already.

Also, when posting code, please format it as per the Welcome Topic

Blynk - FTFC

how to dress timers. I tried, the mail stopped working, messages did not come and the temperature stopped showing up.

I can not find examples.

timer.setInterval(5000L, mail);
  timer.setInterval(2000L, sendTemps);

set timers, everything works. Messages only 3 pcs. will come. I would like one thing.

you should use conditional flags, to do the action only once. like this:

void mail()
{
static bool mailSent = false;
float sen = sensors.getTempCByIndex(0);

if (sen >= 35 && sen <= 40 && !mailSent) {

Blynk.email("your_email@mail.ru", "ВНИМАНИЕ низкая температура теплоносителя меньше 40С");  // отправка майла при приходе сигнала

mailSent = true;
} 
else if (sen < 35 || sen > 40) mailSent = false;
}

this is not a tested code, but i guess you understand the essence…

also, try to use sometimes the right click -> format code, in the arduino ide. it will have much nicer aspect! :wink:

1 Like

yeah, thanks sir @Gunner for formatting, i’m on phone and didn’t managed to format the code

1 Like

Thank you.

if the solution works for you, please mark topic category as “solved”.

I do not know how to do this