StepH and BLYNK_WRITE, device offline problem

I have project for my off-grid solar system. Arduino messures battery bank voltage and when batteries are full, then Arduino turns on relay, which turns on power relay, which turns on 24V heating element for water tankt. And of course I am using Blynk to see what heppens in my garage.
I am using Ardiuno uno + ESP8266, software serial connection
Android 8.0.0, latest Blynk version. I use Blynk server.

Everything worked fine. But I decided to add StepH widget to change target Volts variable. And now Blynk after random time says - Your Project is Offline, in serial output I can see long Blynk PING. The problem startet when I added BLYNK_WRITE section to my code. The problem is related to RelejaSlegsana() function, because when I commented all lines (Blink.virtulalWrite and Blynk.notify) in my code, then it works.

This is working code, but I would like to have Blynk functions in my RelejaSlegsana() function.

I hope you can help me.


 //Temperaturas sensoram
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Temperatura=0.00;

//Voltmetram un relejam
float MerkjaVolti=27.50; // Pie cik voltiem iesleegties relejam
float Stepvalue=27.50;
float voltage = 0.00;
float Volti=0.00;
float R1 = 100000.0; // resistance of R1 (100K) 
float R2 = 10000.0; // resistance of R2 (10K) 
int analog_value = 0; //voltmetra arduino pin
int sledzis = 0; //sledza stavoklis
int iii=0; //iesleegta releja ciklam

//Blynk darbibai
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
// Blynk autorizacijas tokens
char auth[] = "---";
// Your WiFi credentials.
char ssid[] = "---";
char pass[] = "---";
// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, millis() / 1000);
}

void TemperaturasNolasisana()
{
    sensors.requestTemperatures(); 
    Temperatura=sensors.getTempCByIndex(0);
    Blynk.virtualWrite(V0, Temperatura);
    Serial.print ("Temperatura ");
    Serial.print (Temperatura);
    Serial.print('\n');
}

void VoltuMerisana()
{
  analog_value = analogRead(A0);     // read the value at analog input pin A0
  voltage = (analog_value * 5) / 1024.0; 
  Volti = voltage / (R2/(R1+R2)); 
  if (sledzis==1){Volti=Volti-0.2;}
  if (sledzis==0){Volti=Volti+0.2;}
  Blynk.virtualWrite(V1, Volti);
  Blynk.virtualWrite(V4, MerkjaVolti);
  Serial.print ("Volti ");
  Serial.print (Volti);
  Serial.print('\n');
}

void RelejaSlegsana()
{
  if (Volti >= MerkjaVolti)
    {
      digitalWrite(8, LOW);//iesleedz sleedzi
      sledzis = 1;
      //Blynk.virtualWrite(V2, "Relejs ON");
      //Blynk.notify("Relejs ON");
      Serial.print ("Relejs ON ");
      Serial.print('\n');
      for (iii=0; iii <= 300; iii++)
        {
        TemperaturasNolasisana();
        VoltuMerisana();
        //Blynk.virtualWrite(V3, iii);
        Serial.print (iii);
        Serial.print('\n');
        delay(1000);
        }
      //Blynk.notify("Relejs OFF");
     } 
     else
     {
      delay(1000);
      digitalWrite(8, HIGH);//izsleedz sleedzi
      //Blynk.virtualWrite(V2, "Relejs OFF");
      delay(1000);
      //Blynk.virtualWrite(V3, "--");
      Serial.print ("Relejs OFF ");
      Serial.print('\n');
      sledzis = 0;
      }
}

void setup()
{
//Blync--------
  // Debug console
  Serial.begin(9600);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(30);
 Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);
  timer.setInterval(1000L, myTimerEvent);
  
//Voltmetrs---------
   Serial.println("DIGITAL VOLTMETER");
   Serial.print ("Meerkja Volti ");
   Serial.print (MerkjaVolti);
   Serial.print('\n');
   Blynk.virtualWrite(V4, MerkjaVolti);
   Blynk.virtualWrite(V9, MerkjaVolti);
   sensors.begin(); //temp sensoram
   pinMode(8, OUTPUT); // Releja PIN
   digitalWrite(8, HIGH); //izsleedz sleedzi
}

BLYNK_WRITE(V9) // Step widget set to 0.1 steps
{
MerkjaVolti = param.asFloat(); // Get value as float
Serial.println(String("stepValue = ") + MerkjaVolti);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  //delay (1000);
  TemperaturasNolasisana();
  delay (1000);
  VoltuMerisana();
  //delay (1000);
  RelejaSlegsana();
   //BLYNK_WRITE();
 // delay (3000);
  // 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!
}


It’s going offline because of the way that you’ve structured your code. If it was working before without disconnections then you were very lucky. Adding-in your new code has made the problem worse, because your new RelejaSlegsana contains a one second delay.

Blynk needs to have a very clean void loop, like this:

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

Your various functions to take readings and upload them to Blynk should be called using timers.
You currently have one timer defined which is called once every second:

  timer.setInterval(1000L, myTimerEvent);

This calls the function myTimerEvent every 1000 miliseconds.

This is how you should call your other timers - preferably at differing intervals.

Pete.

Thank you for your answer. I am new in programming :slight_smile:
Can I put my functions here?

void setup()
{
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(30);
  Blynk.begin(auth, wifi, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1000L, TemperaturasNolasisana);
  timer.setInterval(1000L, VoltuMerisana);
  timer.setInterval(1000L, RelejaSlegsana);
}

And loop code only:

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

So, the way you’ve set it up, all the timed functions are being called once every second.

You’d be better calling them on different schedules (which is why I put “preferably at differing intervals”) so that you stagger the intervals at which the varying functions are called. Maybe call one every 1.5 seconds, another every 1.75 seconds etc.

You’ll also need to tidy-up the functions that ate being called - removing the delay()s from your new function.

Pete.

Thank you. I will try tonight and give feedback.

Your suggestion helped me. Thanks.

I need only think samething differend for my FOR cycle with delay(1000). I want that my relay is turned on for about 5 min, but during this time I also want to get Temperature and Voltage values.

Do a bit of searching on the forum for @Gunner’s tutorials on timers. Once you’ve studied them you’ll know about 1000 times more than me about timers.

Pete.

search for timer.setTimeout

timer.setTimeout(300000L, RelejaSlegsana); //300000= 5 min