Hi, something wrong is happening with my events.
I have 2 events called “min_temp” and “max_temp” but only the second one can trigger a notification, the code is the same for both of the function that triggers a notification but “min_temp”, even if on the serial monitor shows it work, on blink doesn’t show nothing.
I tried to swap the 2 events in the code but still the same result.
Here it is my code:
#include <Arduino.h>
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//Temperature libraries
#include <OneWire.h>
#include <DallasTemperature.h>
//Wifi Manager libraries
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
//Blynk connection libraries
#include <BlynkSimpleEsp8266.h>
//Blynk ID
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN ""
#define BLYNK_FIRMWARE_VERSION "0.1.0"
//#define BLYNK_PRINT Serial
BlynkTimer timer;
#define INTERVAL 1000l //BlynkTimer timer interval
void tempRequest(); //temperature reading function
#define TEMP_PIN 12 //temperature sensor signal pin
#define MIN_TEMP 0 //min temperature threshold
#define MAX_TEMP 100 //max temperature threshold
OneWire oneWire(TEMP_PIN);
DallasTemperature temp(&oneWire);
float temp_C; //temperature variable in celsius
int trs_min, trs_max, trs_pointer; //thresholds
bool alert_sent = false; //alert flag variable
void setup() {
Serial.begin(9600);
temp.begin();
//BEGIN WIFI MANAGER CONFIG
WiFiManager wifiManager;
wifiManager.setTimeout(120);
if (!wifiManager.autoConnect("WiFi Temperature", "password"))
{
Serial.println("failed to connect, we should reset as see if it connects");
delay(3000);
ESP.reset();
delay(5000);
}
//BLINK SETUP AND CONNECTION
Blynk.begin(BLYNK_AUTH_TOKEN, WiFi.SSID().c_str(), WiFi.psk().c_str());
Blynk.syncAll();
timer.setInterval(INTERVAL, tempRequest);
}
void loop() {
Blynk.run();
timer.run();
}
void tempRequest()
{
temp.requestTemperatures();
temp_C = temp.getTempCByIndex(0);
/*
Serial.print("Temperature is: ");
Serial.print(temp_C, 1);
Serial.println(" C");
Serial.println(" ");
*/
Blynk.virtualWrite (V0, temp_C); //send temperature value to blynk
/*Warning notification*/
if (temp_C > trs_max && alert_sent == false)
{
Blynk.logEvent("max_temp"); //send push notification to app
alert_sent = true;
Serial.println(alert_sent);
Serial.println(" ");
Serial.println ("TEMPERATURE IS TOO HIGH");
}
else if (temp_C <= trs_max && temp_C > trs_min && alert_sent == true) //if temp. goes below threshold flag variable is reset
{
alert_sent = false;
Serial.println(alert_sent);
}
if (temp_C < trs_min && alert_sent == false)
{
Blynk.logEvent("min_temp"); //send push notification to app
alert_sent = true;
Serial.println(alert_sent);
Serial.println(" ");
Serial.println ("TEMPERATURE IS TOO LOW");
}
else if (temp_C >= trs_min && temp_C < trs_max && alert_sent == true) //if temp. goes below threshold flag variable is reset
{
alert_sent = false;
Serial.println(alert_sent);
}
}
/*INCREMENT/DECREMEN MAX*/
BLYNK_WRITE (V1)
{
int pinData = param.asInt();
if (trs_pointer == 0)
{
if (pinData == 1)
{
if (trs_max == MAX_TEMP)
{
trs_max = MAX_TEMP;
}
else
{
trs_max ++;
}
Blynk.virtualWrite (V3, trs_max);
}
}
else
{
if (pinData == 1)
{
if (trs_min == trs_max -2)
{
trs_min = trs_max - 2;
}
else
{
trs_min ++;
}
Blynk.virtualWrite (V4, trs_min);
}
}
}
/*DECREMENT*/
BLYNK_WRITE (V2)
{
int pinData = param.asInt();
if (trs_pointer == 0)
{
if (pinData == 1){
if (trs_max == trs_min + 2)
{
trs_max = trs_min + 2;
}
else
{
trs_max --;
}
Blynk.virtualWrite (V3, trs_max);
}
}
else
{
if (pinData == 1)
{
if (trs_min == MIN_TEMP)
{
trs_min = MIN_TEMP;
}
else
{
trs_min --;
}
Blynk.virtualWrite (V4, trs_min);
}
Blynk.virtualWrite (V4, trs_min);
}
}
}
/*SYNC MAX THRESHOLD*/
BLYNK_WRITE(V3)
{
trs_max = param.asInt();
}
/*SYNC MIN THRESHOLD*/
BLYNK_WRITE(V4)
{
trs_min = param.asInt();
}
/*THRESHOLD POINTER*/
BLYNK_WRITE(V5)
{
trs_pointer = param.asInt();
}