Hi, I wonder if someone could give me as a noob some advice, please. I have 2 questions.
My ESP32 goes offline after ± 30s on the Blynk app and website, but it still shows connected to my wifi router. Is there something that I am missing in my code?
Is there a way that I can code a master switch for my relay? So if it is ON via my IF Statement, I can switch it OFF via the switch that I already have in my code?
Thank you in advance.
Code Below:
#define BLYNK_TEMPLATE_ID "#######"
#define BLYNK_DEVICE_NAME "######"
#define BLYNK_AUTH_TOKEN "#######"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "########";
char ssid[] = "####"; // type your wifi name
char pass[] = "######"; // type your wifi password
BlynkTimer timer;
//DHT11 LIB
#include "DHT.h"
#define DHTPIN 14
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float t, h;
//DS18B20 LIB
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float Soiltemp;
//Relay Pin
#define RelayPin 13
//Buzzer Pin
#define BuzzerPin 27
//Variables
int RelayTempHigh=26;
int RelayTempLow=21;
int BuzzerTempHigh=30;
int SoilTempHigh=30;
void sendSensor()
{
//DHT11 Air Temperature and Humidity
h = dht.readHumidity();
t = dht.readTemperature();
Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V1, h);
if (isnan(h) || isnan(t)) {
Serial.println("No Reading from DHT sensor!");
return;
}
//Soil Tempreture
DS18B20.requestTemperatures();
Serial.print(DS18B20.getTempCByIndex(0));
Soiltemp = DS18B20.getTempCByIndex(0);
Serial.println(Soiltemp);
Blynk.virtualWrite(V2, Soiltemp);
//Heat Mat relay on and off with DS18B20
pinMode(RelayPin, OUTPUT);
//Relay ON
if(Soiltemp < RelayTempLow){
digitalWrite(RelayPin, HIGH);
Serial.println("Heat Mat ON!");
Blynk.logEvent("heat_mat_on", "Soil Temperature is under 21°C, Heat Mat is ON!");
}
//Relay OFF
if(Soiltemp > RelayTempHigh){
digitalWrite(RelayPin, LOW);
Serial.println("Heat Mat OFF!");
Blynk.logEvent("heat_mat_off", "Soil Temperature is over 26°C, Heat Mat is OFF!");
}
//Heat Mat Relay Blynk Switch
Blynk.virtualWrite(V3, RelayPin);
//Air & Soil Temperature High Buzzer
pinMode(BuzzerPin, OUTPUT);
if(t > BuzzerTempHigh){
digitalWrite(BuzzerPin, HIGH);
delay(3500);
digitalWrite(BuzzerPin, LOW);
delay(300000);
Serial.println("Air Temperature is over 30°C, Air Greenhouse!");
Blynk.logEvent("air_temp_high", "Air Temperature is over 30°C, Air Greenhouse!");
}
if(Soiltemp > SoilTempHigh){
digitalWrite(BuzzerPin, HIGH);
delay(3500);
digitalWrite(BuzzerPin, LOW);
delay(120000);
Serial.println("Soil Temperature is over 30°C, Air Greenhouse!");
Blynk.logEvent("soil_temp_high", "Soil Temperature is over 30°C, Air Greenhouse!");
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
dht.begin();
DS18B20.begin();
delay(2000);
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates SimpleTimer
}
The term Master Switch is confusing me.
Do you mean a manual override switch? If so then this also gets a mention in the timer tutorial I linked you to.
Hi Pete, If you do not mind. Will you please have a look at my code again and see if I did understand your blocking delays correctly or should the delays be completely removed?
#define BLYNK_TEMPLATE_ID "######"
#define BLYNK_DEVICE_NAME "######"
#define BLYNK_AUTH_TOKEN "#####"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "######";
char ssid[] = "####"; // type your wifi name
char pass[] = "#####"; // type your wifi password
BlynkTimer timer;
BlynkTimer airtempbuzhigh;
BlynkTimer soiltempbuzhigh;
//DHT11 LIB
#include "DHT.h"
#define DHTPIN 14
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float t, h;
//DS18B20 LIB
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float Soiltemp;
//Relay Pin
#define RelayPin 13
//Buzzer Pin
#define BuzzerPin 27
//Variables
int RelayTempHigh=26;
int RelayTempLow=21;
int AirTempHigh=30;
int SoilTempHigh=30;
void sendSensor()
{
//DHT11 Air Temperature and Humidity
h = dht.readHumidity();
t = dht.readTemperature();
Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V1, h);
if (isnan(h) || isnan(t)) {
Serial.println("No Reading from DHT sensor!");
return;
}
//Soil Tempreture
DS18B20.requestTemperatures();
Serial.print(DS18B20.getTempCByIndex(0));
Soiltemp = DS18B20.getTempCByIndex(0);
Serial.println(Soiltemp);
Blynk.virtualWrite(V2, Soiltemp);
//Heat Mat relay on and off with DS18B20
pinMode(RelayPin, OUTPUT);
//Relay ON
if(Soiltemp < RelayTempLow){
digitalWrite(RelayPin, HIGH);
Serial.println("Heat Mat ON!");
Blynk.logEvent("heat_mat_on", "Soil Temperature is under 21°C, Heat Mat is ON!");
}
//Relay OFF
if(Soiltemp > RelayTempHigh){
digitalWrite(RelayPin, LOW);
Serial.println("Heat Mat OFF!");
Blynk.logEvent("heat_mat_off", "Soil Temperature is over 26°C, Heat Mat is OFF!");
}
//Heat Mat Relay Blynk Switch
Blynk.virtualWrite(V3, RelayPin);
}
void air_temp_buz_high()
{
//Air Temperature High Buzzer
pinMode(BuzzerPin, OUTPUT);
if(t > AirTempHigh){
digitalWrite(BuzzerPin, HIGH);
delay(3500);
digitalWrite(BuzzerPin, LOW);
delay(300000);
Serial.println("Air Temperature is over 30°C, Air Greenhouse!");
Blynk.logEvent("air_temp_high", "Air Temperature is over 30°C, Air Greenhouse!");
}
}
void soil_temp_buz_high()
{
//Soil Temperature High Buzzer
pinMode(BuzzerPin, OUTPUT);
if(Soiltemp > SoilTempHigh){
digitalWrite(BuzzerPin, HIGH);
delay(3500);
digitalWrite(BuzzerPin, LOW);
delay(120000);
Serial.println("Soil Temperature is over 30°C, Air Greenhouse!");
Blynk.logEvent("soil_temp_high", "Soil Temperature is over 30°C, Air Greenhouse!");
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
dht.begin();
DS18B20.begin();
delay(2000);
timer.setInterval(1000L, sendSensor);
airtempbuzhigh.setInterval(1000L, air_temp_buz_high);
soiltempbuzhigh.setInterval(1000L, soil_temp_buz_high);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates BlynkTimer
airtempbuzhigh.run();
soiltempbuzhigh.run();
}
You clearly didn’t read the bit of my tutorial that explained that one timer object is sufficient for 16 timers.
Blynk won’t work if you use delays. The Blynk library requires constant communication with the Blynk server and the delay() command blocks code execution. Stop using them.
If you need a non-blocking timer then you can use a timeout timer, as described in my tutorial.
You also need to sort-out your logEvent notifications.
You should read this…
void air_temp_buz_high()
{
//Air Temperature High Buzzer
pinMode(BuzzerPin, OUTPUT);
timer.setTimeout(3500L, []()
{
if(t > AirTempHigh){
digitalWrite(BuzzerPin, HIGH);
Serial.println("Air Temperature is over 30°C, Air Greenhouse!");
Blynk.logEvent("air_temp_high", String("Air Temperature is over ") + t, String("Air Greenhouse!");
}
});
timer.setTimeout(300000L , []()
{
digitalWrite(BuzzerPin, LOW);
});
}
I’m guessing that your buzzer doesn’t work as expected?
I guess you missed this part of my tutorial…
Note that with timeout timers, the code execution is not paused until the timer ends. If you place a Lambda timer in a piece of code then the code execution will ‘step over’ the Lambda timer and continue to execute, until the timer expires.
For example, This code:
Serial.println("Location A");
timer.setTimeout(5000L, []()
{
// When the timer completes, any code here will be executed
Serial.println("The timer has completed");
});
Serial.println("Location B");
Serial.println("Location C");
would produce this output in the serial monitor:
Location A
Location B
Location C
The timer has completed
timer.setTimeout(x_millis, []()
{
// when x_mills has passed ang code here will execute
// and you can start a new nested timer..
timer.setTimeout(y_millis, []()
{
// when y_millis hae passed any code here will execute
}
});
{
});
Hi Pete, Thank you very much for all the assistance.
It looks like my disconnecting and going Offline problem is sorted out, with the new timeout timer code and logEvent code. I also am happy how the buzzer is working at this point.
Thank you very much for not spoon feeding me and made we work till I have the correct code and is happy with the outcome of the code.
Just one last question, It looks like the logEvent code also plays a part in the disconnecting and going offline if the code is not correct?