ESP work offline

Hi

I work with ESP8266 standalone,
in my app i send to thee ESP number that represent time to close the relay.
i noticed that if after i send the number from the app after few seconds the internet connection lost,
the time to close the relay stuck and not count up - so the relay work as long as the ESP offline,
can someone tell what can i do to make the relay close in the end of the count even there is no internet connection?
the try to connect time delay the countTime (timer.setInterval(100L,countTime); )
this is the log:

countTime:88
countTime:89
countTime:90
countTime:91
countTime:92
countTime:93
countTime:94
[61298] Connecting to blynk-cloud.com:8442
Tempfunc
countTime:95
work2: 30
minTime: 30
timeLeft: 30
nowTime: 0
timeLeft: 30
nowTime: 0
[67667] Connecting to blynk-cloud.com:8442
Tempfunc
countTime:96
work2: 30
minTime: 30
timeLeft: 30
nowTime: 0
timeLeft: 30
nowTime: 0
[74716] Connecting to blynk-cloud.com:8442
countTime:97
[81449] Connecting to blynk-cloud.com:8442

Now that’s promising. I hate threads that start with the mention of Arduino’s as I know it’s going to mean lots of heartache and time wasted for all concerned :slight_smile:

Could you please post your formatted sketch so we can try to debug it.

Precisely which ESP are you using, hopefully not the ESP01 as they are almost as difficult to work with as those Arduino’s.

Just checking “your history” and this came up from April ESP disconnecting (from WiFi after sending data from app)

Your ESP was disconnecting, like now, and when @Gunner asked you to provide your code we heard nothing more from you.

We can only help if you provide what we request as we are not going to give you a list of the 74 things that could be causing the disconnects and the associated fixes for each of those issues.

If your sketch is “top secret classified” then just say so and we will work around it and sign the Government / client NDA’s.

Best way is to start with the basic standalone sketch and run it for a few days until you know you have a solid connection to Blynk. Then you know any future problems are almost certainly down to your bad coding.

Hi Costas

Thanks for the fast replay.
last time i posted for the disconnecting issue - the ESP was the problem.
i use esp01, but i manege to use GPIO4,5,14, (i did some expansion).

I’ll share my code, but all i want to know if is it possible to do what i want.

can you tell me if it possible ?

Yes it’s possible.

ok

see my code below:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <OneWire.h>

#define boiler 14
#define OnLed  4   
#define systemLED 5

void buttonLedWidget();
void Temperature();
void LEDtoggle();
void OnTime();
void countTime();


int minutse = 0;
unsigned long minTime = 0;
unsigned long work2 = 0;
unsigned long nowTime = 0;
float ESP201ADCVref=1.10;
unsigned long count = 0;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "get from BLYNK";

WidgetLED led1(0);
WidgetLED led5(3);
//WidgetLED led4(16);

SimpleTimer timer;

OneWire  ds(2);  // on pin 2 between VCC&DATA (a 4.7K resistor is necessary)

const int btnPin = 17;

WidgetLED led3(V3);

bool dudon = false;
bool dune = true;
bool manual = false;
int work = 0;



//************ Menu widget *************
//
BLYNK_WRITE(V1) 
{
  int getTime = 0;
  switch (param.asInt())
  {
    case 1: // Item 1
      Serial.println("off");
      getTime = 0;
      break;
    case 2: // Item 1
      Serial.println("30 minuts");
      getTime = 30;
      break;
    case 3: // Item 2
      Serial.println("45 minuts");
      getTime = 45;
      break;
    case 4: // Item 3
      Serial.println("60 minuts");
      getTime = 60;
      break;
    case 5: // Item 4
      Serial.println("75 minuts");
      getTime = 75;
      break;
    case 6: // Item 5
      Serial.println("90 minuts");
      getTime = 90;
      break;
    case 7: // Item 6
      Serial.println("120 minuts");
      getTime = 120;
      break;
    default:
      Serial.println("off");
      getTime = 0 ;
  }
  work = getTime;
 // OnTime();
}
void setup()
{
  Serial.begin(9600);
  
//  Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "LEDANI_2.4", "0522941258");
  Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "Nir&Rotem", "11335577");
//   Blynk.begin("5a2d531baafa4c16855dfac4cbbc1275", "NIR", "11335577");
//   Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "hereO", "kgpswifi");
//   Blynk.begin("1e0fc8aa350c47e88bbb078dc1f6302a", "iPhone", "123456789");
// attachInterrupt(digitalPinToInterrupt(0), pushButtton, CHANGE);

 pinMode(boiler, OUTPUT);
 pinMode(OnLed, OUTPUT);
 pinMode(systemLED, OUTPUT);
 digitalWrite(OnLed,HIGH);
 digitalWrite(systemLED,LOW);
 digitalWrite(boiler,HIGH);

  timer.setInterval(200L, pushButtton);
  timer.setInterval(10000L,Temperature);
  timer.setInterval(100L,countTime);
  timer.setInterval(10000L,OnTime);
  timer.setInterval(10000L,sendTime);

  
 
  while (Blynk.connect()==false)
  {
      Serial.println("try to connect");  
  }
  led1.on();
  //led4.on();
  led5.on();
  Blynk.virtualWrite(V1, "pick", 0);
}

bool lastbutton = 0;
void loop()
{
  Blynk.run();
  timer.run();
}

int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;

void pushButtton()
{
  bool button = digitalRead(0);  
  Blynk_Delay(1);
  bool button2 = digitalRead(0);
  button = button && button2;
  
  if(button && !lastbutton)
  {
    Serial.println("pressed"); 
    if (state == HIGH)
    {
      state = LOW;
      work = 130;
    }      
    else
    {
      state = HIGH;
      work = 0;
    }
  } 
  digitalWrite(V3, state);
  lastbutton = button;
}

void Temperature()
{
  Serial.println("Tempfunc");
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  
  if ( !ds.search(addr)) {
    //Serial.println("No more addresses.");
   // Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
 
  Blynk_Delay(1000);
 // delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  for ( i = 0; i < 9; i++)  // we need 9 bytes
  {          
    data[i] = ds.read();
  }
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) 
  {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) 
    {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } 
  else 
  {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  
  celsius = (float)raw / 16.0;
  Serial.print("celsius: ");
  Serial.println(celsius);
  Blynk.virtualWrite(V5,celsius);
}

void OnTime()
{
   unsigned long tout; 
   if((!dudon && dune) || work == 0 || (work == 130 && !dudon) )
   {
     work2 = work;  
     if(count < (work*600))
     {
        BoilerOn();
     }
   }
  if((count >= (work*600)) && dudon)
  { 
      Serial.println("tout");
      BoilerOff();
  }
   Serial.print("work2: ");
   Serial.println(work2);
   if (dudon)
   {
     minTime = work2 - count/600; 
     Serial.print("minTime: ");
     Serial.println((minTime));
     nowTime = (count/600);  
     sendTime();
   }
}

void sendTime()
{
  unsigned long timeLeft = 0;
 
  if(dudon &&((minTime > 0) && (work != 130))) timeLeft = (minTime);
  if(minTime <= 0  )timeLeft = 0;
  if(dudon && (minTime == 130))
  {
    Blynk.virtualWrite(V2,"ON");
    Blynk.virtualWrite(V1, "pick", 0);
  }
  else Blynk.virtualWrite(V2,timeLeft);
  
  if(dudon &&((nowTime > 0))) Blynk.virtualWrite(V4,nowTime);
  else
  {
    nowTime = 0;
    Blynk.virtualWrite(V4,nowTime);
  }
  Serial.print("timeLeft: ");
  Serial.println(timeLeft);
  Serial.print("nowTime: ");
  Serial.println(nowTime);
}

void countTime()
{
 if(dudon)
  {
    count++;
    Serial.print("countTime:");
    Serial.println(count);
  }
 else count = 0;
}

void BoilerOn()
{
  digitalWrite(boiler,LOW);
  digitalWrite(OnLed,LOW);
  digitalWrite(systemLED,HIGH);
  dudon = true;
  count = 0;
  Serial.println("dudon");
  Blynk.notify("Boiler ON");
  if(work == 130)Blynk.virtualWrite(V1, "pick", 0);
  state = LOW;
}

void BoilerOff()
{
  digitalWrite(boiler,HIGH);
  digitalWrite(OnLed,HIGH);
  digitalWrite(systemLED,LOW);
  Serial.println("dudoff");
  dudon = false;
  Blynk.virtualWrite(V1, "pick", 0);
  
  
  Blynk.notify("Boiler OFF");
  state = HIGH;
  work = 0;
  work2 = 0;
  minTime = 0;  
}

void Blynk_Delay(int milli)
{
  int end_time = millis() + milli;
  while (millis() < end_time){ }
}

What are the bool variables dudon and dune?

You should try to select more descriptive variable names.

Edit: OK so dudon looks to be boiler ON / OFF state but dune could be anything.

I have lots of ideas, working through the code now.

how can i make the timer keep counting after loosing connection ?
i need that pin 14 will go HIGH in the end of the counting no meter what.

do you have an idea for me ?

You appear to have the following boiler on durations 30, 45, 60, 75, 90, 120 minutes.
You just need a slightly modified loop() as shown below and the following code will be fine.

You should also look into the docs and my posts on Connection Management to ensure you are connected to the server for the maximum time possible.

unsigned int timeRemaining;

timer.setInterval(60000L, countdown);

void countdown(){
  if(timeremaining > 1){
    // switch off the relay code
  }
  else{
    timeremaining--;
  }
}

void loop()
{
  if(Blynk.connected()){    // only process Blynk commands if connected to server
    Blynk.run();
  }
  timer.run();
}

I’m sure the ESP01 Blynkers are looking forward to you adding the details in the “Projects …” section.

Edit: maybe scrap that idea as even with unlimited free time modding an ESP01 into and ESP01A is probably not worth it when you consider the price differential between an ESP01 and a proper plug and play ESP. At least not unless you need hundreds of them in your project.

1 Like

Hi Costas

Can you explain why your solution will work offline (if disconnection occur and there is no internet at all ?)

Because SimpleTimer, now incorporated into the Blynk libraries as BlynkTimer, runs without an internet connection.

1 Like

it seeme that the timer.setInterval(100L,countTime);
have delay because the connection trying as you can see in the log :

countTime:92
countTime:93
countTime:94
[61298] Connecting to blynk-cloud.com:8442 //delay for a lot more then 100ms (as need to be)
Tempfunc
countTime:95
work2: 30
minTime: 30
timeLeft: 30
nowTime: 0
timeLeft: 30
nowTime: 0
[67667] Connecting to blynk-cloud.com:8442 //delay for a lot more then 100ms (as need to be)
Tempfunc
countTime:96
work2: 30

Why are you trying to do something every 100ms when your countdown timer is upwards of 30 minutes?

basically yes, but i can call the count function every 1 sec or above,
it’s not that important for me.

Connecting to Blynk does normally take more than 100ms and the constant reconnects suggest you have a problem with your sketch, your hardware setup or both.

@ladan.nir with decent plug and play ESP’s your original sketch is fine. Boiler set to 30 minutes, time ticking down nicely and temperature readings every 10s. Your hardware setup is probably the issue.

countTime:7586
countTime:7587
countTime:7588
countTime:7589
countTime:7590
countTime:7591
countTime:7592
countTime:7593
countTime:7594
countTime:7595
countTime:7596
countTime:7597
countTime:7598
countTime:7599
Tempfunc
celsius: 31.37
countTime:7600
work2: 30
minTime: 18
timeLeft: 18
nowTime: 12
timeLeft: 18
nowTime: 12
countTime:7601
countTime:7602
countTime:7603
countTime:7604
countTime:7605
countTime:7606
countTime:7607
countTime:7608
countTime:7609
countTime:7610
countTime:7611
countTime:7612
countTime:7613
countTime:7614
countTime:7615
countTime:7616
countTime:7617
countTime:7618

you say that the hardware is the problem, but now the ESP is not connecting to other component,
just to FTDI.

I just tried the your loop:
if(Blynk.connected()){ // only process Blynk commands if connected to server
Blynk.run();
}

it do exactly what i want , thank you.
it keep counting.
but now when the internet connection coming back there is no auto connection to internet
how can i do try to auto connection every 30 sec ?

I have written how to do this many, many times.
Read through my posts.

You don’t need your Blynk_Delay() function when you read the temperature but you do need an additional present = ds.reset() at the end of the function. As you have it at the moment it only reads correct temperature every second iteration.

I am sure you will still be getting lots of disconnections with the code as you have it.

Baud should be fine increased to 115200, it’s not an Arduino you are using.
Stop the 100ms loop and make it at least 1000ms and consider countdown at 60000ms.
Use attachInterrupt for the button not the 200ms loop. I know you have considered it as it’s commented out in your sketch and I know it can take some sorting out but it’s the proper way to catch a button trigger.

can you please send me link to an auto connecting post ?
Thank you very much for you help