Hello,
When ESP32 try to connect WIFI, how I reset pin from LOW state to HIGH state ? It seem Blynk Edgent does not allow to do anything while trying connect WIFI or offline.
Sorry my poor English
Thank you
Sak
Hello,
When ESP32 try to connect WIFI, how I reset pin from LOW state to HIGH state ? It seem Blynk Edgent does not allow to do anything while trying connect WIFI or offline.
Sorry my poor English
Thank you
Sak
You need to provide more information.
Pete.
Hello Pete,
Here is my code.
#define BLYNK_TEMPLATE_ID "xxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "xxxxxxxxxx"
#define BLYNK_FIRMWARE_VERSION "0.1.2"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
#define Relay 13
#define Pump 27
#define sensorInput 36
#define WaterLevel 14
// Uncomment your board, or configure a custom board in Settings.h
//#define USE_ESP32_DEV_MODULE
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_TTGO_T_OI
#include "BlynkEdgent.h"
int SoilValue ;
int MoisturePercent ;
byte MoistureSet ;
byte ForcePump ;
double PumpDelaytime = 0;
byte Schedule;
byte waterPumpDelay=0;
byte waterPumpDelay1=0;
byte PumpState;
byte RelayState;
byte ScheduleState;
byte WaterState;
byte state;
int buttonState=0;
WidgetLED led1(V1); // LED ของ ปั้ม
WidgetLED led2(V2);// LED ของ วาลว
WidgetLED led3(V3);// LED ของ Level
BlynkTimer timer;
BLYNK_CONNECTED() {
Blynk.syncAll();
}
BLYNK_WRITE(V4)
{// ค่าที่ตั้งจาก Slide bar ว่าต้องการความชื้นเท่าไร (ค่าควบคุมความชื้น)
MoistureSet = param.asInt();
}
BLYNK_WRITE(V9)
{ //รับค่ามาจากการกดรดน้ำแบบทันที
ForcePump = param.asInt();
}
BLYNK_WRITE(V0)
{ //รับค่ามาจากตารางรดน้ำประจำวัน
Schedule = param.asInt();
}
BLYNK_WRITE(V6)
{//รับค่า Delay Time จาก Blynk
PumpDelaytime = param.asDouble();
}
void WaterPlant(){
SoilValue = analogRead(sensorInput);
MoisturePercent=map(SoilValue,2400,900,0,100);
Blynk.virtualWrite(V7, MoisturePercent);
Blynk.virtualWrite(V8, SoilValue);
Blynk.virtualWrite(V9, ForcePump);
buttonState = digitalRead(WaterLevel);
if (buttonState == HIGH)
//if(WaterLevel == 0)
{
//digitalWrite(LedLevel, HIGH);// LED ของ Schedule ดับ
led3.off(); //widget LED ของ Level ดับ
WaterState=0;
if( ForcePump == 0)
{
waterPumpDelay=0;
if(Schedule == 1)//ถึงเวลาที่ตั้งค่า Schedule จะส่งค่ามาเป็น 1
{
// digitalWrite(LedSchedule, LOW);// LED ของ Schedule ติด
ScheduleState=1;
if(MoisturePercent >=MoistureSet)//ค่าความชื้นที่วัดได้มากกว่าค่าที่ตั้งบน แอบ จะไม่มีการทำงาน
{
digitalWrite(Pump, HIGH);
PumpState=0;
digitalWrite(Relay, HIGH);
RelayState=0;
led1.off();
led2.off();
waterPumpDelay1=0;
state=0;
}else if(waterPumpDelay1==0)//ถ้าค่าความชื้นวัดได้ต่ำกว่าค่าที่ตั้ง Schedule และ pump จะทำงาน
{//ให้หน่วงเวลาก่อน
digitalWrite(Relay, LOW);
RelayState=1;
led2.on();
WaterDelay();//หน่วงเวลา
digitalWrite(Pump,LOW); //ปั้มทำงาน
PumpState=1;
led1.on();//ส่งค่าให้ LED ปั้มติด
waterPumpDelay1=1;// บันทึกว่ามีการหน่วงเวลาแล้ว
state=1;
}else //ถ้ามาการเช็คว่าหน่วงเวลาแล้ว water pump Dalay =1 จะไม่มีการหน่วงใน loop นี้ เนื่องจาก blynk จะ ตรวจการทำงานใหม่ทุกครั้งไม่เหมือน arduino คือ จบ
{
digitalWrite(Relay, LOW);
RelayState=1;
digitalWrite(Pump,LOW);
PumpState=1;
led1.on();
led2.on();
waterPumpDelay1==0;
state=2;
}
}else //กรณีไม่ถึงเวลาที่ตั้งไว้ Schedule = 0 ทุกอย่างจะถูก reset
{
ScheduleState=0;
digitalWrite(Pump, HIGH);
PumpState=0;
digitalWrite(Relay, HIGH);
RelayState=0;
led1.off();
led2.off();
waterPumpDelay1==0;
state=3;
}
}else if(ForcePump == 1 && waterPumpDelay==0 ) // ถ้ามีการกดบังคับให้รดน้ำ ForcePump =1
{
digitalWrite(Relay, LOW);
RelayState=1;
led2.on();
WaterDelay();
digitalWrite(Pump,LOW);
PumpState=1;
led1.on();
waterPumpDelay=1;
state=4;
}else if(ForcePump == 1 && waterPumpDelay==1 )
{
digitalWrite(Relay, LOW);
led2.on();
RelayState=1;
digitalWrite(Pump,LOW);
PumpState=1;
led1.on();
waterPumpDelay==1;
state=5;
}
}else
{
led3.on(); //widget LED ของ Level ติด
digitalWrite(Pump, HIGH);
PumpState=0;
digitalWrite(Relay, HIGH);
RelayState=0;
led1.off();
led2.off();
WaterState=1;
ForcePump=0;
}
}
void WaterDelay(){
double delay_time = PumpDelaytime*60000;
double start_millis = millis();
Serial.print("Starting ");
Serial.print(delay_time);
Serial.println(" milleseconds delay...");
delay(delay_time);
double duration = millis() - start_millis;
Serial.print("Delay completed - time taken = ");
Serial.print(duration);
if (duration < delay_time)
{
Serial.print(" <<<<< ERROR!!");
}
Serial.println();
Serial.println();
}
}
void setup()
{
pinMode(Relay,OUTPUT);
pinMode(Pump,OUTPUT);
pinMode(WaterLevel,INPUT);
Serial.begin(115200);
delay(100);
BlynkEdgent.begin();
timer.setInterval(3000L, WaterPlant); //timer will run every 3 sec
}
void loop(){
BlynkEdgent.run();
timer.run();
}
While ESP32 disconnects WIFI, if Pin 13 and Pin 27 (solenoid valve and motor pump) are LOW state. The solenoid valve and motor pump will continue turn on until I press stop button from Blynk app to stop them . But when ESP32 disconnects WIFI, I can’t stop them. I found that ESP32 attemps to connect Blynk Server, I don’t know how to abort power on solenoid valve and motor pump. I would like to turn them off by reset Pin 13 and Pin 27 to HIGH state to protect overload motor pump. when accidentally disconnect WIFI for long time.
Thank you,
Sakchai
There are lots of things wrong with your code, including the use of delays, and I’d suggest you fix those.
Have you tried adding a test to check whether your WiFi is connected, using !WL_CONNECTED
and setting your pins HIGH if this test is false?
Pete.
Hi Pete,
I will review my code agian and I have never tried !WL_CONNECTED
. I will add and test it also.
Thank you very much,
Sakchai