[SOLVED] Sync very very slow

Hey Guys, i need help…
i am trying to create a pool-control device, handling two pumps and a valve.
at the moment status is i can read 4 temp sensors, a fotoresistor and all the other stuff needs to be done later…

my main issue now: i am trying to use some step controls in the app for changing the values “difftempsen”, “difftempsoll” and “solltemp”
the values should be stored on the server, reloaded if the nodemcu looses its power/connection and being changeable by the app

its working but it takes ages till i get values…

dont judge me i am new to all this stuff :baby:


#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DallasTemperature.h>
//#include <SPI.h>
//#include <Ethernet.h>
//#include <BlynkSimpleEthernet.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

char auth[] = "blablabla";


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "wifi";
char pass[] = "wifipass";


// Construct an LCD object and pass it the 
// I2C address, width (in characters) and
// height (in characters). Depending on the
// Actual device, the IC2 address may change.
LiquidCrystal_I2C lcd(0x27, 20, 4);

const int LightPin = A0;
const int PoolPumpPin = D5;
const int reservePIN = D6;
const int SolarValvePin = D7;
const int SolarPumpPin = D8;
const unsigned char One_Wire_Bus = D4;
OneWire oneWire(One_Wire_Bus);
DallasTemperature sensoren(&oneWire);
float pooltemp, poolendtemp, solartemp, solarendtemp; 
bool timeridle = true;
bool solaridle = true;
bool solarpumpidle = true;
bool manualidle = true;
bool isFirstConnect = true;
int difftempsen; //= 3;
int difftempsoll; //= 3;
int stabiint = 0;
int solltemp; // = 35;
int lightvalue = 0;


BlynkTimer timer;
WidgetRTC rtc;

// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
    // Synchronize time on connection
  rtc.begin();
  if (isFirstConnect) {
 
 isFirstConnect =false;
  }
 Blynk.syncAll();
 //Blynk.virtualWrite(V6, difftempsen);
    
  //get data stored in virtual pin V0 from server
 
 // Blynk.syncVirtual(V6);
  //Blynk.syncVirtual(V7);
}

BLYNK_WRITE(V6) // Slider widget writing to V4
{ 
    difftempsen = param.asInt(); //sliderValue is a global variable you declared before void setup()
    Serial.println("hole wert von difftempsen");
}
BLYNK_WRITE(V7) // Slider widget writing to V4
{ 
    difftempsoll = param.asInt(); //sliderValue is a global variable you declared before void setup()
    Serial.println("hole wert von difftempsoll");
    Serial.println(difftempsoll);
}
BLYNK_WRITE(V8) // Slider widget writing to V4
{ 
    solltemp = param.asInt(); //sliderValue is a global variable you declared before void setup()
    Serial.println("hole wert von solltemp");
    Serial.println(solltemp);
}

void myTimerEvent()  //Temperaturen senden an Blynk
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
Blynk.virtualWrite(V0, lightvalue);
//Blynk.virtualWrite(V40, difftempsen);
if(pooltemp != 85.0 && pooltemp != -127.0)
  { 
    Blynk.virtualWrite(V1, pooltemp);
  }else   { 
    Blynk.virtualWrite(V1, NULL);
  }
if(poolendtemp != 85.0 && poolendtemp != -127.0)
  {
    Blynk.virtualWrite(V2, poolendtemp);
  } else  {
    Blynk.virtualWrite(V2, NULL);
  }
if(solartemp != 85.0 && solartemp != -127.0)
  { 
    Blynk.virtualWrite(V3, solartemp);
  }else  { 
    Blynk.virtualWrite(V3, NULL);
  }
if(solarendtemp != 85.0 && solarendtemp != -127.0)
  {
    Blynk.virtualWrite(V4, solarendtemp);
  } else
    {
    Blynk.virtualWrite(V4, NULL);
  } 
  //Serial.println(pooltemp);
}

void Tempcontrol()  //Temperaturgesteuerte Relaischaltung der Pumpen/Valve/
{
if ((pooltemp < (solartemp - difftempsen)) && (pooltemp < (solltemp - difftempsoll))) {
  if (stabiint < 4 )  {
    stabiint++;
   } else if (stabiint == 4) {
    solaridle=false;
    digitalWrite(PoolPumpPin, LOW);
    digitalWrite(SolarValvePin, LOW);
    //displaytemp();
    } 
} 
else {
    if (stabiint > 0 )  {
    stabiint--;
   } 
   if (stabiint == 0 && timeridle ==true && manualidle == true) {
      digitalWrite(PoolPumpPin, HIGH);
      digitalWrite(SolarValvePin, HIGH);
      solarpumpidle = true;
      solaridle=true;
      //switchdisplays();
      //dint++;
     }else if (stabiint == 0 && timeridle == true && manualidle == false) {
      digitalWrite(SolarValvePin, HIGH);
      solarpumpidle = true;
      solaridle=true;
      //switchdisplays();
      //dint++;
    } else if (stabiint == 0){
      solaridle=true;
     }
 }
}
void solarpump() 
{
  if (lightvalue >= 500){
    if (solarpumpidle == true) {
      Serial.println("Solarpump ON");
      digitalWrite(SolarPumpPin, LOW);
      solarpumpidle = false;
    }
  } else if (solaridle == false){
    if (solarpumpidle == true) {
      Serial.println("Solarpump ON");
      digitalWrite(SolarPumpPin, LOW);
      solarpumpidle = false; 
       }   
       Serial.println("keep it running due solartemp");
  }
      
   else
  {
    //if (solarpumpidle == false) {
    //Serial.println("Solarpump OFF");
    digitalWrite(SolarPumpPin, HIGH);
    solarpumpidle = true;
    
  // }
    //Serial.println(solaridle);
 }
}

// Digital clock display of the time
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send time to the App
  Blynk.virtualWrite(V10, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V11, currentDate);
}

void setup() {
  
  pinMode(PoolPumpPin, OUTPUT);
  pinMode(reservePIN, OUTPUT);
  pinMode(SolarValvePin, OUTPUT);
  pinMode(SolarPumpPin, OUTPUT);
  
  digitalWrite(PoolPumpPin, HIGH);
  digitalWrite(reservePIN, HIGH);
  digitalWrite(SolarValvePin, HIGH);
  digitalWrite(SolarPumpPin, HIGH);
  // The begin call takes the width and height. This
  // Should match the number provided to the constructor.
  lcd.begin(20,4);
  lcd.init();

  lcd.backlight();
 
    // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass, IPAddress(192,168,123,118));
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
 //Blynk.begin(auth, ssid, pass);
  
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100);
 
 setSyncInterval(10*60); // Sync interval in seconds (10 minutes)

// Display digital clock every 10 seconds
timer.setInterval(10000L, clockDisplay);
timer.setInterval(1000L, myTimerEvent);
// Display digital clock every 10 seconds
} //end Setup


void loop() {
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
//Push stuff to Blynk
rtc.begin();
timer.run(); // running timer every second
  
//if (pooltemp > 30) {
//digitalWrite(PoolPumpPin, LOW);
//} else{
//digitalWrite(PoolPumpPin, HIGH);
//}
//Licht auslesen & Pumpe ansteuern
lightvalue = analogRead(LightPin);
//Serial.println(lightvalue);
solarpump();

//Temperatur übergeben für Pooltemp und Poolendtemp
  sensoren.requestTemperatures();
  pooltemp = sensoren.getTempCByIndex(0);
  poolendtemp = sensoren.getTempCByIndex(1);
  solartemp = sensoren.getTempCByIndex(2);
  solarendtemp = sensoren.getTempCByIndex(3);
  lcd.setCursor(0, 0);
  lcd.print("PoolOut:");
  lcd.setCursor(15, 0);
    if(pooltemp != 85.0 && pooltemp != -127.0)
      {
        lcd.print(pooltemp);
      }else{
        lcd.print("N.A. ");
      }
   lcd.setCursor(0, 1);
   lcd.print("PoolIn:");
   lcd.setCursor(15, 1);
   if(poolendtemp != 85.0 && poolendtemp != -127.0)
    {
      lcd.print(poolendtemp);
    }else{
      lcd.print("N.A. ");
    }
  lcd.setCursor(0, 2);
  lcd.print("SolarIn:");
  lcd.setCursor(15, 2);
    if(solartemp != 85.0 && solartemp != -127.0)
      {
        lcd.print(solartemp);
      }else{
        lcd.print("N.A. ");
      }
   lcd.setCursor(0, 3);
   lcd.print("SolarOut:");
   lcd.setCursor(15, 3);
   if(solarendtemp != 85.0 && solarendtemp != -127.0)
    {
      lcd.print(solarendtemp);
    }else{
      lcd.print("N.A. ");
    }  
Serial.println(difftempsen) ;
//Serial.println(temperatur1);
//Serial.println(temperatur2);
}


i hope somebody can tell me what i did wrong…

1 Like

You have way too much in your void loop(), all running hundreds of times a second.

When it comes to merging Blynk in with “standard” Arduino programming, you need to keep the main loop clear and all other functions in their own timer controlled groups, otherwise things slow down, Blynk.run() looses “control” and Blynk disconnects.

You already have timers in your code, so you make a void loop() like this:

void loop() {
  Blynk.run();
  timer.run();
}

Leave rtc.begin() in the setup, right after Blynk.begin(), and move the rest into timed voids of their own.

thx, i just did. it did the trick!

i finnaly started cleaning the mess
the main issue seemed to be run rtc.begin() in loop. removing this in loop chainged a lot. but it still was a little laggy.
since i moved everything to timers everything is really smoth!

time to move on with the code :wink:

1 Like