[SOLVED] Long time to "read" stored values on Blynk server (after deepsleep)

Hi folks,

I’v been fooling around with my code trying to understand why this is soooooo looonnnng to get actual values from Blynk. I know its not synchronous and async data is not a direct call with a direct answer but if someone experimented a similar situation. I know @costas is a great hero for thoses delicate question.
I need first in my code to read stored values on Blynk and this part is taking few sec before getting values, way to long for me to optimise battery.

Here’s the full code, actually working well.
The goal is to quickly as possible:

  1. Read stored values on Blynk to process accordingly (Longest part of my execution)
  2. Read Sensor values
  3. Do some logic with theses
  4. Take action if not in range
  5. Go to deepsleep for 15 mins (here is 6 sec to debug)
float codeversion = 1.1;
int SensorNumber = 20;
#include <EmonLib.h>

#define BLYNK_PRINT Serial   
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Wire.h>
#define si7021Addr 0x40
//#include "DHT.h"   // Was way to long so changed to Si7021
//#define DHTPIN D4 //pin gpio 12 in sensor
//#define DHTTYPE DHT22   // DHT 22 Change this if you have a DHT11
//DHT dht(DHTPIN, DHTTYPE);
ADC_MODE(ADC_VCC);
unsigned int raw=0;
float volt=0.0;

//int SleepTimer = 9999;
bool MaintState = 9999;
int SetPoint = 9999;
int MajorDev = 9999;
int MinorDev = 9999;
int minortmax;
int minortmin;
int majortmax;
int majortmin;
unsigned long elapsedtime;
float h ;
float t ;
int power ;
int VPIN1;
int VPIN2;
int VPIN3;
int VPIN4;
int VPIN5;

// Mac address should be different for each device in your LAN
//byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress device_ip  (192, 168,  0,  180);
IPAddress dns_ip     (  8,   8,   8,   8);
IPAddress gateway_ip (192, 168,  0,   1);
IPAddress subnet_mask(255, 255, 255,   0);

char auth[] = "REMOVED"; 
char ssid[] = "dlink";
char pass[] = "REMOVED";

//Create an Emon instance
#include <EmonLib.h>                 
#include <emonlib.h>
//Emoncms configurations
char host[] = "emoncms.org";     // name address for emoncms.org
String apikey = "REMOVED";  //api key
int node = 0; //if 0, not used

void setup()
{

  Serial.begin(9600); 
  Wire.begin();
  Wire.beginTransmission(si7021Addr);
  Wire.endTransmission();
  //dht.begin();
  WiFi.config(device_ip, gateway_ip, subnet_mask);
  WiFi.begin(ssid, pass);
  Blynk.config(auth);
  // Setup Blynk
  Blynk.config(auth);
  //WiFi.begin;
  //while (Blynk.connect() == false) {
    //Wait until connected
    //if(millis()>=10000){
       //ESP.deepSleep(sleepTimeS * 1000000, WAKE_RF_DEFAULT);
       //}
    //}
  }

void getvalues()
{
    //do{
    //delay(1000);
    //h = dht.readHumidity();
    //t = dht.readTemperature();
    //}
    //while (isnan(t)); 

    unsigned int data[2];
    Wire.beginTransmission(si7021Addr);
    //Send humidity measurement command
    Wire.write(0xF5);
    Wire.endTransmission();
    delay(50);
 
    // Request 2 bytes of data
    Wire.requestFrom(si7021Addr, 2);
    // Read 2 bytes of data to get humidity
    if(Wire.available() == 2)
    {
    data[0] = Wire.read();
    data[1] = Wire.read();
    }
 
    // Convert the data
    h  = ((data[0] * 256.0) + data[1]);
    h = ((125 * h) / 65536.0) - 6;
 
    Wire.beginTransmission(si7021Addr);
    // Send temperature measurement command
    Wire.write(0xF3);
    Wire.endTransmission();
    delay(50);
 
    // Request 2 bytes of data
    Wire.requestFrom(si7021Addr, 2);
 
    // Read 2 bytes of data for temperature
    if(Wire.available() == 2)
    {
    data[0] = Wire.read();
    data[1] = Wire.read();
    }
 
    // Convert the data
    t  = ((data[0] * 256.0) + data[1]);
    t = ((175.72 * t) / 65536.0) - 46.85;
    //float fahrTemp = celsTemp * 1.8 + 32;
  
    VPIN1 = SensorNumber * 5;
    VPIN2 = SensorNumber * 5 + 1;
    VPIN3 = SensorNumber * 5 + 2;
    VPIN4 = SensorNumber * 5 + 3;
    VPIN5 = SensorNumber * 5 + 4;
    //Serial.print(VPIN1);
    //Serial.print(VPIN2);
    //Serial.print(VPIN3);
    Blynk.virtualWrite(VPIN1, t);
    Blynk.virtualWrite(VPIN2, h);
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print("Batt Voltage: ");
    int power = (ESP.getVcc());
    Serial.println(power);
    //Serial.println(volt);
    Blynk.virtualWrite(VPIN3, power);
  
  }

  void emoncms()
  {
    Serial.println("EMONCMS SECTION");
    WiFiClient emoClient;
    const int httpPort = 80;

  if (!emoClient.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  String json = "{Temp";
  json += SensorNumber;
  json += ":";
  json += t;
  json += ",Hum";
  json += SensorNumber;
  json += ":";
  json += h;  
  long rssi = WiFi.RSSI();
  Serial.print("RSSI: ");
  Serial.println(rssi);
  json += ",RSSI";
  json += SensorNumber;
  json += ":";
  json += rssi;
  json += ",BVolt";
  json += SensorNumber;
  json += ":";
  json += power;
  json += ",ElapsedTime";
  json += SensorNumber;
  json += ":";
  json += elapsedtime;
  json += "}";

  String url = "/input/post.json?node=";
  url += node;
  url += "&apikey=";
  url += apikey;
  url += "&json=";
  url += json;

  // This will send the request to the server
  emoClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
                  "Host: " + host + "\r\n" +
                  "Connection: close\r\n\r\n");
  //delay(1000);
  //Serial.print("URL: ");
  //Serial.print(url);
  //Serial.print("Host: ");
  //Serial.print(host);
  Serial.println("END OF EMONCMS SECTION");
 } //end sendtoemoncms
 
  
  void notification()
  {
    minortmax = SetPoint + MinorDev;
    minortmin = SetPoint - MinorDev;
    majortmax = SetPoint + MajorDev;
    majortmin = SetPoint - MajorDev;
    Serial.print("minortmax: ");
    Serial.print(minortmax);
    Serial.print(" minortmin: ");
    Serial.print(minortmin);
    Serial.print(" majortmax: ");
    Serial.print(majortmax);
    Serial.print(" majortmin: ");
    Serial.println(majortmin);
   
    
    if (MaintState == 0) {
    
    if (t <= majortmin || t >= majortmax) {
      Serial.println("MAJOR Temperature warning: " + String(t) + "C");
      Blynk.notify("Major deviation on sensor: " + String(SensorNumber) + " actually at: " + String(t) + "C and the setpoint is at: " + String(SetPoint) + "C");
      Blynk.email("REMOVED", "Major deviation", "Sensor: " + String(SensorNumber) + " is at: " + String(t) + "C and the setpoint is at: " + String(SetPoint) + "C");
     }
    
    else if (t <= minortmin || t >= minortmax) {
      Serial.println("Minor Temperature warning: " + String(t) + "C");
      Blynk.notify("Minor deviation on sensor: " + String(SensorNumber) + " actually at: " + String(t) + "C and the setpoint is at: " + String(SetPoint) + "C");
      Blynk.email("REMOVED", "Minor deviation", "Sensor: " + String(SensorNumber) + " is at: " + String(t) + "C and the setpoint is at: " + String(SetPoint) + "C");
     }
     
    if (t > minortmin && t < minortmax) {
      Serial.println("Temperature is within accepted value: " + String(t) + "C");
    }
    if ((power < 3260) && (power > 3245))
    {
      //Blynk.notify("Sensor: " + SensorNumber + "Battery low: " + power + ("mV"));
      //Serial.print("Sensor: " + SensorNumber + "Battery low: " + power + ("mV"));
    }
    else if (power <= 3245)
    {
      //Blynk.notify("Sensor: " + SensorNumber + "Battery critical: " + power + ("mV"));
      //Serial.print("Sensor: " + SensorNumber + "Battery critical: " + power + ("mV"));
    }
    //Serial.print(String("Sensor: " + SensorNumber + "Battery is normal: ") + power + ("mV"));
    
} //end if maintstate == 0
} //end function

BLYNK_CONNECTED() {
  // Request the latest state from the server
    Blynk.syncVirtual(V103);// 20=103 21=108 22=113
    Blynk.syncVirtual(V104);// 20=104 21=109 22=114
    Blynk.syncVirtual(V125);
    Blynk.syncVirtual(V126);
    //Blynk.syncVirtual(V127);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V104) {
    MaintState = param.asInt();
    Serial.print("MaintenanceState: ");
    Serial.println(MaintState);
   }
  BLYNK_WRITE(V103) {
    SetPoint = param.asInt();
    Serial.print("SetPoint: ");
    Serial.println(SetPoint);
  }
  BLYNK_WRITE(V125) {
    MinorDev = param.asInt();
    Serial.print("MinorDev: ");
    Serial.println(MinorDev);
  }
  BLYNK_WRITE(V126) {
    MajorDev = param.asInt();
    Serial.print("MajorDev: ");
    Serial.println(MajorDev);
  }
   /*BLYNK_WRITE(V127) {
    SleepTimer = param.asInt();
    Serial.print("SleepTimer: ");
    Serial.println(SleepTimer);
  }*/
  
void loop()
{
  checkvalues();
  getvalues();
  notification();
  elapsedtime = millis();
  Serial.println("Runtime total : " + String(elapsedtime) + "ms");
  emoncms();
  ESP.deepSleep(1 * 6 * 1000000, WAKE_RF_DEFAULT);
  delay(200);
}

void checkvalues()
{
  Blynk.run();
  do { 
  Serial.println("DO WHILE");
  Serial.print("MinorDev: ");
  Serial.println(MinorDev); 
  Serial.print("MajorDev: ");
  Serial.println(MajorDev);
  Serial.print("SetPoint: ");
  Serial.println(SetPoint);
  Serial.print("MaintState: ");
  Serial.println(MaintState);
  Serial.print("SleepTimer: ");
  Serial.println(SleepTimer);
  long rssi = WiFi.RSSI();
  Serial.print("RSSI: ");
  Serial.println(rssi);
  Blynk.run();
  }
  while ((MinorDev > 40) || (MajorDev > 40) || (SetPoint > 40) || (MaintState > 40));// (|| SleepTimer > 40)
}

Found this discussion and will now figure out how to reduce delay in library.
Case is closed for now.

Thanks

You may also want to look into cleaning up the loop() with the use of the BlynkTimer

Yes I usually do use timers but this is a 1 shot deal, loop() is running only once :slight_smile:

I just modified the blynkprotocol.h and now connecting is only taking 1 sec instead of 6 !!!