BLYNK
HOME       📲 GETTING STARTED       đź“— DOCS       âť“HELP CENTER       👉 SKETCH BUILDER

Raspberry pi keeps disconnecting

hi there
Im having a connection issues and I have no idea how to solve it. I have just connected the raspberry pi w zero to blynk I have no code written its just a simple button for a led and it disconnects after about a hour. the pi is still connected to the wifi, the router sits next to the device. I had the exact same problem with using mega/Ethernet, nodemcu and atmega2560/esp8266 so I thinking its a issue keeping connected to the blynk server. Ive noticed plenty people have similar issues but not a simple solution. do you have any advice on this?

There are many examples of re-connection code in C++ which vary from doing a Blynk.connected() test in the void loop, to putting this test into a function and calling it with a timer.

Yes, the complexity of the solution does vary depending on the Blynk library language you’re using, and there may not be much available for whatever library/language you’re running on your Pi Zero. It also varies on your connection method and whether you’re using Blynk.begin or Blynk.config in C++

Pete.

thanks for your quick response its day 1 learning about the pi so it will be a slow process figuring out the issue. I also have the atmega2560+esp8266 running on C++ which I have similar issue the difference is it disconnects and connects constantly but its also can be random when Im running the eventor widget sometimes it misses a comand to turn off a relay and after 12 to 24 to 48 hours its will completely disconnect and require a reboot. at the moment I have to use 2 wifi power points to restart it which is a painful exercise to my project. please look at my code and would like your feedback

 //  LIBRARY
//====================================================
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
//====================================================

//====================================================
char auth[] = "";
char ssid[] = "";
char pass[] = "";
#define EspSerial Serial3
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
BlynkTimer timer;

#define waterLevel_trigger 4     // Water level sensor
#define waterLevel_echo 3
#define MAX_DISTANCE 450 
#define PI 3.1415926535897932384626433832795

//==================================================================================================================
const int Diameter1 = 36.5;
const int Depth1 = 56;
const int Area1 = PI * ((Diameter1 / 2)*(Diameter1 / 2));

int Litres1, distance1, WaterDepth1;

void sendWaterlevel()
{
 long duration1, distance1;
  digitalWrite(waterLevel_trigger, LOW);  
  delayMicroseconds(2); 
  
  digitalWrite(waterLevel_trigger, HIGH);
  delayMicroseconds(10); 
  
  digitalWrite(waterLevel_trigger, LOW);
  duration1 = pulseIn(waterLevel_echo, HIGH);
  distance1 = (duration1/2) / 29.1;

  if (distance1 >= Depth1 || distance1 == 0 ) distance1 = Depth1;

   WaterDepth1 = Depth1 - distance1;
   Litres1 = (Area1 * WaterDepth1) / 1000;

  Blynk.virtualWrite(V1, distance1);
  Blynk.virtualWrite(V2, WaterDepth1);
  Blynk.virtualWrite(V8, Litres1);
  Blynk.virtualWrite(V9, Litres1 / 10);
  
  Serial.println();
  Serial.println("Tank 1 water distance1: " + String(distance1));;
  Serial.println("Tank 1 water depth: " + String(WaterDepth1));     //print depth
  Serial.println("Tank 1 Litres: " + String(Litres1));                         //print litres
   
  delay(200);  
}

//==================================================================================================================



//==================================================================================================================

void reconnectBlynk() {
  if (!Blynk.connected()) {

    Serial.println("Lost connection");
    if(Blynk.connect()) {
      Serial.println("Reconnected");
    }
    else {
      Serial.println("Not reconnected");
    }
  }
}

BLYNK_WRITE(V2) {
  if (param.asInt()) {
    digitalWrite(10, HIGH);
  }
  else {
    digitalWrite(10, LOW);
  }

}
//==================================================================================================================


void setup()
{
  pinMode(10, OUTPUT);
  Serial.begin(115200);
  Serial3.begin(115200);
  mySerial.begin(9600);

  delay(10);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

   Blynk.begin(auth, wifi, ssid, pass);                          //Reguler server
   //Blynk.begin(auth, wifi, ssid, pass, "192.168.43.120", 8080);    //Local server
   timer.setInterval(30*1000, reconnectBlynk);
   timer.setInterval(500L, sendWaterlevel);
   timer.setInterval(100L, sendlevel);
   pinMode(waterLevel_trigger, OUTPUT);
   pinMode(waterLevel_echo, INPUT);
}

void loop()
{
 timer.run(); 
  if(Blynk.connected()) { Blynk.run(); }
   
  if ( Serial3.available() )   {
    Serial.write( Serial3.read() );
  }
  if ( Serial.available() )       {
    Serial3.write( Serial.read() );
  }

}

What’s the purpose of this code?

Also, is it really necessary to calculate the area of your water tank each time? Why not simply calculate this outside of your code and use the result as a multiplier for your water volume calculation?

Also, you’re doing this test…

Which is probably going to block any reconnection attempts and you don’t have an else option that will attempt to rectify the reconnection.
You’re not really helping yourself by choosing an Arduino Mega + ESP-01 combination, as you don’t have access to the full range of WiFi related commands that you’d get with a NodeMCU for example.

Pete.