HW said connected, but server logs don't think so

• Hardware model + communication type.
Wemos d1 mini (latest lib 0.6.0) + local server server-0.41.2-java8.jar

Android 5.1

void setup() {
 Blynk.begin(auth, ssid, password, IPAddress(10,0,0,7), 8080);

t.every(15000, CheckConnection);
t.every(10000, pushData);
}

void CheckConnection(){    // check every 15s if connected to Blynk server
  if(!Blynk.connected()){
    Serial.println("Not connected to Blynk server"); 
    //Blynk.connect();  // try to connect to server with default timeout
    Blynk.begin(auth, ssid, password, IPAddress(10,0,0,7), 8080);
  }
  else{
    Serial.println("Connected to Blynk server");     
  }
}

void pushData() {
  
  Blynk.virtualWrite(V4, sppm);
...
}

void loop()

 t.update();
 delay(100);

so time to time, after few hours working good, HW loose connection to server,
but serial port said: connected

I tried connect and auth
//Blynk.connect(); // try to connect to server with default timeout
Blynk.begin(auth, ssid, password, IPAddress(10,0,0,7), 8080);

but with no luck

looks like Blynk.connected() don’t work properly

This clearly isn’t your full code, and the snippet that you have shared has issues - like no opening curly brackets in your void loop.
I doubt that people will take time to look at it in more detail, and maybe try it out for themselves, unless you post full code that hasn’t been hacked around in this way.

Also, your title talks about server logs, but in the post you talk only about serial monitor and you haven’t shared any server log info or said anything about what the online status of your device shows in the app.

Pete.

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266HTTPClient.h>
#include <BlynkSimpleEsp8266.h>
#include <PubSubClient.h>
#include "Timer.h"
#include "MQ135.h" 
#include <Wire.h>
#include "HTU21D.h"


// ==================================== VARS ==================================

//Create an instance of the object
HTU21D myHumidity;

Timer t;
int LED = D4; // Wemos build in led

int val;// Defines a numeric variable val
float ppm, rzero, temp, humd;
int count = 0;
int i = 0, sppm = 0;
int appm[50];

#define LED_ON   digitalWrite(LED, 1)
#define LED_OFF  digitalWrite(LED, 0)

MQ135 gasSensor = MQ135(A0);

const char* ssid = "xxx";
const char* password = "xxx";

char auth[] = "xxxxxxxxxxxxxxxx";


const char *mqtt_server = "10.0.0.7"; 
const int mqtt_port = 1883; 
const char *mqtt_user = "xxxxx"; 
const char *mqtt_pass = "xxxxxx";

WiFiClient espClient;
PubSubClient mqtt_client(espClient);


void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.println();
}



void setup() {
  Serial.begin(115200);

  pinMode(LED, OUTPUT); // Definition LED as output interface
  
  // start serial port
  Serial.begin(115200);

  setup_wifi();

  ArduinoOTA.setHostname("air_sensor");
      
  ArduinoOTA.onStart([]() {
    Serial.println("Start"); 
 
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd"); 
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready"); 
  Serial.print("IP address: "); 
  Serial.println(WiFi.localIP());

  Serial.println("Ready");
  
  mqtt_client.setServer(mqtt_server, 1883);
  mqtt_client.setCallback(callback);

  int tickEvent = t.every(3000, blynkLed);
  
  t.every(15000, CheckConnection);
  t.every(10000, pushData);
  
  Blynk.begin(auth, ssid, password, IPAddress(10,0,0,7), 8080);

  myHumidity.begin();

}

void mqtt_connect() {
    
    Serial.print("Attempting MQTT connection...");
    
    String clientId = "ESP8266_AIR-";
    clientId += String(random(0xffff), HEX);
  
    if (mqtt_client.connect(clientId.c_str(),mqtt_user, mqtt_pass)) {
      Serial.println("connected");
 
      mqtt_client.subscribe("air/mq135");
    } else {
      Serial.print("failed, rc=");
      Serial.print(mqtt_client.state());
      Serial.println(" try again in 5 seconds");
      
      delay(100);
    }
  
}



void CheckConnection(){    // check every 15s if connected to Blynk server
  if(!Blynk.connected()){
    Serial.println("Not connected to Blynk server"); 

    Blynk.begin(auth, ssid, password, IPAddress(10,0,0,7), 8080);
  }
  else{
    Serial.println("Connected to Blynk server");     
  }
}

void reconnect() {
  // Loop until we're reconnected
   
  
}



void setup_wifi() {

  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}



void blynkLed() {
   
  val = digitalRead(LED); // The digital interface is assigned a value of 3 to read val

  if (val == LOW)  {
    LED_ON;
  } else {
    LED_OFF;
    }
  
}

void pushData() {
  Serial.println(val);
  Serial.println(ppm);
  Serial.println(rzero);
  
  Blynk.virtualWrite(V3, val);
  Blynk.virtualWrite(V4, sppm);
  Blynk.virtualWrite(V5, rzero);
  Blynk.virtualWrite(V6, temp);
  Blynk.virtualWrite(V7, humd);
  
}

int sort_desc(const void *cmp1, const void *cmp2)
{
  // Need to cast the void * to int *
  int a = *((int *)cmp1);
  int b = *((int *)cmp2);
  // The comparison
  return a > b ? -1 : (a < b ? 1 : 0);
  // A simpler, probably faster way:
  //return b - a;
}

void loop() {
  
  if (WiFi.status() != WL_CONNECTED) {
    setup_wifi();
   }
   ArduinoOTA.handle();

  if (!mqtt_client.connected()) {
    mqtt_connect();
  }
  mqtt_client.loop();
   

  humd = myHumidity.readHumidity();
  temp = myHumidity.readTemperature();
    
  //ppm = gasSensor.getPPM ();
  ppm = gasSensor.getCorrectedPPM(temp, humd);

   //Serial.println(rzero);
  val = analogRead(A0);
   //Serial.println(val);
   //Serial.println(ppm);
  rzero = gasSensor.getCorrectedRZero(temp, humd);

  
  
  appm[i]=ppm;
  i++;

  if (i > 49) {
    int appm_length = sizeof(appm) / sizeof(appm[0]);
    // qsort - last parameter is a function pointer to the sort function
    qsort(appm, appm_length, sizeof(appm[0]), sort_desc);
    sppm = appm[i/2];

    char buffer[7];
    dtostrf(sppm, 1, 0, buffer);
    mqtt_client.publish("room/air/mq135", buffer, true);

    dtostrf(temp, 1, 2, buffer);
    mqtt_client.publish("room/air/gy21_temp", buffer, true);

    dtostrf(humd, 1, 0, buffer);
    mqtt_client.publish("room/air/gy21_humd", buffer, true);
    
    i = 0;

    Serial.print("sorted ppm: ");
    Serial.println(sppm);
    Serial.println("");
    Serial.print(" Temperature:");
    Serial.print(temp, 1);
    Serial.print("C");
    Serial.print(" Humidity:");
    Serial.print(humd, 1);
    Serial.println("%");
  
  }

 t.update();
 delay(100);
 
}

full code. Sensitive data masked by xxx
Serial log

 Temperature:21.4C Humidity:42.1%
141
1281.46
578.28
Connected to Blynk server
sorted ppm: 1556

 Temperature:21.4C Humidity:42.0%
150
1559.78
538.66
sorted ppm: 1558

 Temperature:21.4C Humidity:42.0%
Connected to Blynk server
1
1496.73
550.55

local blynk server log when this device connected

12:32:30.962 TRACE- Blynk hardware plain protocol connection detected.
12:32:30.963 TRACE- Incoming HardwareLoginMessage{LoginMessage{id=1, command=LoginHardware, body='xxxxxxxxxxxxxxxxxxxxx'}}
12:32:30.963 DEBUG- Re registering hard channel. [id: 0x4249fb21, L:/10.0.0.7:8080 - R:/10.0.0.88:56501]
12:32:30.964 DEBUG- completeLogin. [id: 0x4249fb21, L:/10.0.0.7:8080 - R:/10.0.0.88:56501]
12:32:30.965 TRACE- Connected device id 0, dash id 532157253
12:32:30.965 INFO - xxxxxxxxxxxx hardware joined.
12:32:31.036 TRACE- Incoming id=2, command=Internal, body='ver0.6.0h-beat10buff-in1024devESP8266buildFeb 12 2019 20:54:39'
12:32:31.037 TRACE- Info command. heartbeat interval 10
12:32:32.460 TRACE- Incoming id=27, command=GetDevices, body='532157253'
12:32:38.205 TRACE- Incoming id=38, command=Ping, body=''

when not connected there is nothing about this device in server log

time to time have errors like this:

12:33:40.017 TRACE- Blynk server IOException.
io.netty.channel.unix.Errors$NativeIoException: syscall:read(..) failed: Connection reset by peer
        at io.netty.channel.unix.FileDescriptor.readAddress(..)(Unknown Source) ~[server-0.41.2-java8.jar:?]
12:33:40.780 TRACE- Incoming id=18, command=Ping, body=''
12:33:41.680 TRACE- Incoming id=40, command=Ping, body=''

but it happened every minute

That’s quite a void loop you’ve got there - taking humidity, temperature and gas reading on every cycle through the loop.
Not surprising that you’re getting disconnections.

Pete.

Don’t understand you. What wrong with this loop ?
Why you think taking this params affect connection to blynk ?

Timers working well, as you can see, every 15 sec, I check connection with blynk server
CheckConnection(), and it return “Connected to Blynk server”
But this is not true.

How do you think should I fix it ?

Read this:
http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

Pete.

Thank you, will try all recommendations from.