Connectivity problem with server and arduino mega

Good day,
two years ago I started a project to regulate the heating of an electric boiler. In the project I use two arduino mega boards. One board is only to measure the value from dht 22 in one room and send it to the other device. The second device controls the relay and measures a few other values (dht 22 sensors and max 6675 thermometer). The current problem is that the arduino mega with the relay starts disconnecting from the server after about a day after startup. It always disconnects for a few seconds and then reconnects, board can’t be controlled in this time. When I tried the output in the serial monitor, the arduino writes the message “heartbeat timeout”. Thanks for help. I am attaching the code (board with relay):

#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID        ""
#define BLYNK_TEMPLATE_NAME      ""
#define BLYNK_AUTH_TOKEN         ""

#define pinDHT 8
#define pinDHT1 9
#define DHTTYPE DHT22

#include <EthernetENC.h>
#include <BlynkSimpleEthernetENC.h>
#include <max6675.h>
#include <DHT.h>

int pinSO  = 5;   // tep pin
int pinCS  = 4;   //tep pin
int pinSCK = 3;   //tep pin
int hlavni1 = 1;  //main switch
float tepoby2;    //tep from second device
float tepoby1;    //tep from second device with decimals
int tepIN1;       //tep from server for compare
float tepIN2;     //regulation - not important
int tepOUT;       //value to app for check
int kontrola1;    //advanced automation
int kontrola2;    //advanced automation
int zmena;        //advanced automation
int i;            //repetition
int led;          //check in app (led)

float tep1;       //out
float tep;        //room
float teplotaC;   //water

DHT mojeDHT(pinDHT, DHTTYPE);
DHT mojeDHT1(pinDHT1, DHTTYPE);

MAX6675 termoclanek(pinSCK, pinCS, pinSO);

BlynkTimer timer;

BLYNK_WRITE(V0) {
  int hlavni (param.asInt());
  hlavni1 = hlavni;
}

BLYNK_WRITE(V6) {
  int tepIN (param.asInt());
  tepIN1 = tepIN;
}

BLYNK_WRITE(V8) {
  int kontrola (param.asInt());
  kontrola1 = kontrola;
}

BLYNK_WRITE(V9) {
  float tepoby (param.asInt());
  tepoby2 = tepoby;
}

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

  pinMode (7, OUTPUT);

  mojeDHT.begin();
  mojeDHT1.begin();

  Blynk.begin(BLYNK_AUTH_TOKEN);
  timer.setInterval(5000L, myTimer1);
}

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

  delay(1000);
  tep1 = mojeDHT1.readTemperature();
  delay(1000);
  tep = mojeDHT.readTemperature();
  delay(1000);
  teplotaC = termoclanek.readCelsius();

  tepoby1 = tepoby2 * 0.01;
  delay(1000);

  /*
    if (tepoby1 == 0) {
    if (tep > 2) {    //hodnota
      Blynk.logEvent("error", String("Nefunkcni cidlo v oby"));
      Serial.println ("Error1");
      digitalWrite(7, LOW);
      Blynk.virtualWrite(V1, 0);
      Blynk.virtualWrite(V6, 0);
      Blynk.virtualWrite(V7, 0);
      delay(10000);
    }
    if (tep < -2) {   //hodnota
      Blynk.logEvent("error", String("Nefunkcni cidlo v oby"));
      Serial.println ("Error1");
      digitalWrite(7, LOW);
      Blynk.virtualWrite(V1, 0);
      Blynk.virtualWrite(V6, 0);
      Blynk.virtualWrite(V7, 0);
      delay(10000);
    }
    }
    else {
    delay (1000);
    }

    if (kontrola1 == 1) {
    kontrola2 = kontrola2 + 1;
    kontrola1 = 0;
    delay (2000);
    }
    if (kontrola2 == 1) {
    zmena = teplotaC;
    kontrola2 = kontrola2 + 1;
    delay (2000);
    }
    if (kontrola2 == 3) {
    zmena = zmena + 5;    //hodnota
    if (teplotaC <= zmena) {
      Blynk.logEvent("vyp", String("Kotel nebyl dle teploty vody zapnut"));
      Serial.println ("Error2");
    }
    kontrola1 = 0;
    kontrola2 = 0;
    }
  */


  if (hlavni1 == 1) {
    if (tepoby1 <= tepIN1) {
      digitalWrite(7, HIGH);
      led = 1; //Blynk.virtualWrite(V1, 1);
    }
    if (tepoby1 > tepIN1) {
      digitalWrite(7, LOW);
      led = 0; //Blynk.virtualWrite(V1, 0);
    }
    i = 0;
  }

  delay(1000);

  if (hlavni1 == 0) {
    digitalWrite(7, LOW);
    led = 0; //Blynk.virtualWrite(V1, 0);
    if (i == 0) {
      Blynk.logEvent("zakaz", String("Činnost kotle byla upravena hlavním spínačem, toto lze zrušit pouze jeho opětovným přepnutím"));
      i = 1;
    }
  }

  /*
    Serial.println (tep);
    Serial.println (tep1);
    Serial.println (teplotaC);
    Serial.println (tepoby1);
    Serial.println (tepoby2);
    Serial.println (tepIN1);
    Serial.println (tepoby1);
    Serial.println (hlavni1);
  */

  delay(1000);
}
void myTimer()
{
  Blynk.virtualWrite(V2, tep1);
  Blynk.virtualWrite(V5, tep);
  Blynk.virtualWrite(V3, teplotaC);

  Blynk.virtualWrite(V7, tepIN1);
  Blynk.virtualWrite(V4, tepoby1);

  if (led == 1) {
    Blynk.virtualWrite(V1, 1);
  }

  if (led == 0) {
    Blynk.virtualWrite(V1, 0);
  }
}

Other device:

#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID        ""
#define BLYNK_TEMPLATE_NAME      ""
#define BLYNK_AUTH_TOKEN         ""

#define pinDHT 12
#define DHTTYPE DHT22

#include <EthernetENC.h>
#include <BlynkSimpleEthernetENC.h>
#include <DHT.h>

DHT mojeDHT(pinDHT, DHTTYPE);

float tep;
float pomocna;

BlynkTimer timer;

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

  mojeDHT.begin();

  Blynk.begin(BLYNK_AUTH_TOKEN);
  timer.setInterval(1000L, myTimer);
}

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

  float tep = mojeDHT.readTemperature();
  pomocna = tep;
  pomocna = pomocna * 100;
  
  Serial.println(tep);
  delay (2000);
}

void myTimer()
{
  Blynk.virtualWrite(V4, tep);
  Blynk.virtualWrite(V9, pomocna);
}

@Tommy Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Yes, thanks. I am sorry. This is my first post on this forum

Are these two boards using the same Auth token? If so then that’s your problem.

If not then you really need to restructure your code so that your void loops contain only this:

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

and your code contains no delay() commands…

Pete.

I have a different auth tokens on both boards, but the idea with cleaning the loop is genius. I didn’t know this. Thanks for the help. I have one more question, how many timers can I use instead of loop? Otherwise I will try the code and I will write.

Each BlynkTimer object can support up to 16 timers, although I think that this is now restricted to 8 timers for 8-bit MCUs.

More on timers here…

Are you using automations to transfer data from one device to another?

Pete.

Yes, of course. Because, I do not want to have a problem with decimal numbers, that’s why I multiply the value by a hundred before transferring. I do not know any nother way to get this value from one board to second. Have you got any other idea?

Well, that’s not obvious from your code. It would help if clarified this type of issue when you’re describing your setup.

You seem to be implying that it’s not possible to transfer decimal values. How are your datastreams configured.

You can use the REST API, but I’m not sure how you’d do that using a Mega.

Pete.

I’m not saying, it’s not possible, but this is just my feeling. But it’s definitely possible.

In this part of my code:

BLYNK_WRITE(V9) {
  float tepoby (param.asInt());
  tepoby2 = tepoby;
}

Arduino will accept the value, then using automation (on virtual pin V9) the board sent this number to the second board. The automation is set up like this:

i.e, when one board change the value, automtion send this new number to other board.

I think you’re missing my point about automations.

Clearly you know and understand that you’ve set-up an automation which forwards the value from pin V4 on one device to pin V9 on the other device, but the rest of us don’t have that knowledge, and there’s nothing in your code or your description of your setup which makes that obvious.

If you want others to help with your code then it’s a good idea to help yourself by stating these things, or adding clear comments in your code that leaves no doubt about the fact that this is happening.

I’m still confused about your issue (or lack of an issue) with decimals, but I’ll leave you to figure that out.

Pete.