Blynk app shows Device offline after approx 10 min of operation. is any problem with my code?
The code is simple to turn on relay after one operation to another to automate washing.
Kindly help @Gunner
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <SPI.h>
WidgetLCD lcd(V1);
BlynkTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXXXXX";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXX";
char pass[] = "XXX";
//-------- Relay State Setups -------//
#define CLOCKWISE HIGH
#define ANTICLOCKWISE LOW
#define RELAY_ON LOW
#define RELAY_OFF HIGH
//----------------------------------//
//int onoff;
// PORTS //
#define MotorPowerPort D1
#define MototrDirectionPort D2
#define WaterFillValvePort D5
#define WaterDrainValvePort D6
#define button1Pin D7
// GLOBAL PROCESS VARIABLES //
unsigned long process_start = 0L;
unsigned long process_duration = 0L;
int motor_status = CLOCKWISE;
int process = 0;
String process_name = "";
int process_max = 13;
long millis_padding = 0;
// ------------------------ //
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(4, 0, "Washing"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
pinMode (button1Pin, INPUT);
digitalWrite(button1Pin, HIGH);
delay(1000);
processInit();
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
timer.run();
while (digitalRead(button1Pin) == LOW)
{if (doProcess())
{
processReset();
lcd.print(4, 0, "Washing");
lcd.print(4, 1, "Complete");
delay(3000);
}
}
}
BLYNK_WRITE(V10)
{
Serial.println("WebHook data:");
Serial.println(param.asStr());
}
//------------------PROCESS AND OPERATIONS ---------------------------------//
boolean doProcess() //Return is Completed !
{
if (process == 0)drainWater(1);
else if (process == 1)fillWater(3);
else if (process == 2)doWash(4000, 1, "SOAKING");
else if (process == 3)doSoak(15);
else if (process == 4)doWash(4000, 10, "WASHING");
else if (process == 5)drainWater(2);
else if (process == 6)fillWater(2);
else if (process == 7)doWash(5000, 8, "RINSING");
else if (process == 8)drainWater(5);
else if (process == 9)fillWater(3);
else if (process == 10)doWash(6000, 7, "RINSING");
else if (process == 11)Blynk.virtualWrite(V10, "https://maker.ifttt.com/trigger/hi/with/key/fkozy2KdfBixxxxxxxxxxxVjPmd1x");
else if (process == 12)drainWater(8);
else if (process == 13)Blynk.virtualWrite(V10, "https://maker.ifttt.com/trigger/hi/with/key/fxxxxxxxxxxxxxxxxxxxd1x");
else return true;
processUpdate();
return false;
}
double process_pivot = 0;
void processUpdate()
{
double process_current = millis_padded();
if ((process_current - process_start) >= process_duration)
{
process++;
processReset();
delay(1000);
}
else
{
if ((process_current - process_pivot) > 1000) //If past a minimum of one sec
{
//double remain=(process_remain/1000)/60;
process_pivot = process_current;
lcd.clear();
lcd.print(4,1 ,process_name);
process_current = (process_current - process_start);
process_current = process_current / 1000;
if (process_current >= 60)
{
process_current = process_current / 60;
lcd.print(0, 0,(int)process_current);
lcd.print(3, 0, "Min");
}
else
{
lcd.print(0, 0,(int)process_current);
lcd.print(3, 0,"Sec");
}
lcd.print(6, 0,"/");
process_current = process_duration / 1000;
if (process_current >= 60)
{
process_current = process_current / 60;
lcd.print(8, 0,(int)process_current);
lcd.print(10, 0,"Min");
}
else
{
lcd.print(8, 0,(int)process_current);
lcd.print(10, 0,"Sec");
}
}
}
}
void processInit()
{
pinMode(MotorPowerPort, OUTPUT);
pinMode(MototrDirectionPort, OUTPUT);
pinMode(WaterFillValvePort, OUTPUT);
pinMode(WaterDrainValvePort, OUTPUT);
pinMode(button1Pin, INPUT_PULLUP);
process_duration = 0;
process = 0;
process_start = millis_padded();
motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF);
digitalWrite(MototrDirectionPort, CLOCKWISE);
digitalWrite(WaterFillValvePort, RELAY_OFF);
digitalWrite(WaterDrainValvePort, RELAY_OFF);
}
void processReset()
{
process_start = millis_padded();
process_duration = 0;
motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF);
digitalWrite(MototrDirectionPort, CLOCKWISE);
digitalWrite(WaterFillValvePort, RELAY_OFF);
digitalWrite(WaterDrainValvePort, RELAY_OFF);
}
void setProcessDuration(int mins)
{
process_duration = mins2millis(mins);
}
//---------------------Process In Detail --------------------------------------//
void doWash(int cycle_delay, int time_mins, String title)
{
process_name = title;
setProcessDuration(time_mins);
if (motor_status == CLOCKWISE)motor_status = ANTICLOCKWISE;
else motor_status = CLOCKWISE;
digitalWrite(MotorPowerPort, RELAY_OFF); //TURN OFF MAIN POWER TO MOTOR
delay(1000);
digitalWrite(MototrDirectionPort, motor_status); //SET MOTOR DIRECTION
delay(1000);
digitalWrite(MotorPowerPort, RELAY_ON); //TURN ON MAIN POWER TO MOTOR
delay(cycle_delay);
}
void fillWater(int time_mins)
{
process_name = "FILLING";
setProcessDuration(time_mins);
digitalWrite(WaterFillValvePort, RELAY_ON);
}
void drainWater(int time_mins)
{
process_name = "DRAINING";
setProcessDuration(time_mins);
digitalWrite(WaterDrainValvePort, RELAY_ON);
}
void doSpin(int time_mins, int DIR)
{
process_name = "DRY";
setProcessDuration(time_mins);
digitalWrite(WaterDrainValvePort, RELAY_ON);
delay(3000);
digitalWrite(MototrDirectionPort, CLOCKWISE);
delay(1000);
digitalWrite(MotorPowerPort, RELAY_ON);
}
void doSoak(int time_mins)
{
process_name = "SOAKING";
setProcessDuration(time_mins);
delay(1000);
//JUST WAIT :)
}
//---------------------------------------------------------------------------------------//
unsigned long mins2millis(int mins)
{
return (((unsigned long)mins) * 60 * 1000);
}
double millis_padded()
{
return (millis() + millis_padding);
}