Help for automatic WiFi reconnection

Hi everybody. As a novice I created a weather station with Arduino (first ever project!). The project consists of:

  • 2 Mega 2560,
  • 1 touchscreen display (modified to turn off the backlight),
  • 2 nrf24 modules (to transmit data from the arduino transmitter to the receiving one on the display),
  • 1 esp8266 module (to provide wifi connectivity to the Mega2560 Transmitter).

I had finished it but, by pure chance, after turning off the router I realized that it does not reconnect automatically.

I have searched the net but still can’t solve. Which lines of code should I put in my sketch?

(In Italiano)
Ciao a tutti voi. Da neofita ho creato una stazione meteo con Arduino (primo progetto in assoluto!). Il progetto Ă© costituito da:

  • 2 Mega 2560,
  • 1 display touchscreen (modificato per poter spegnere la retroilluminazione),
  • 2 moduli nrf24 (per trasmettere i dati da arduino trasmettitore a quello ricevente su display),
  • 1 modulo esp8266 (per fornire connettivitĂ  wifi al Mega2560 Trasmettitore).

L’avevo terminato ma, per puro caso, dopo aver spento il router mi son reso conto che non si riconnette in automatico.

Ho cercato in rete ma non riesco ancora a risolvere. Quali righe di codice devo inserire nel mio sketch?

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#include "Wire.h"
#include "DHT.h"
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>

char auth[] = "You should get Auth Token in the Blynk App";
char ssid[] = "NOME RETE WIFI";
char pass[] = "PASSWORD";

#define EspSerial Serial1 // Hardware Serial on Mega, Leonardo, Micro...

#define ESP8266_BAUD 9600 // Your ESP8266 baud rate:

ESP8266 wifi(&EspSerial);

#define DHTPIN 53
#define DHTPIN2 40
#define DHTPIN3 41
#define DHTPIN4 42
#define DHTTYPE DHT11
#define DHTTYPE2 DHT22
#define DHTTYPE3 DHT22
#define DHTTYPE4 DHT22

BlynkTimer timer;

RF24 myRadio (38, 39);
byte addresses[][6] = {"0"};

struct Package
{
  float t ;
  float h ;
  float t2 ;
  float h2 ;
  float t3 ;
  float h3 ;
  float t4 ;
  float h4 ;
} data;

DHT dht(DHTPIN, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE2);
DHT dht3(DHTPIN3, DHTTYPE3);
DHT dht4(DHTPIN4, DHTTYPE4);

void sendSensor()
{
  Blynk.virtualWrite(V5, data.h);
  Blynk.virtualWrite(V6, data.t);
  Blynk.virtualWrite(V7, data.h2);
  Blynk.virtualWrite(V8, data.t2);
  Blynk.virtualWrite(V9, data.h3);
  Blynk.virtualWrite(V10, data.t3);
  Blynk.virtualWrite(V11, data.h4);
  Blynk.virtualWrite(V12, data.t4);
}

void setup()
{
  Serial.begin(9600);
  Serial.print("Starting Trasmettitore \n");
  dht.begin();
  dht2.begin();
  dht3.begin();
  dht4.begin();
  myRadio.begin();
  myRadio.openWritingPipe(addresses[0]);
  myRadio.setDataRate(RF24_1MBPS); //250kbps 2MBPS 1MBPS
  myRadio.setChannel(115);

  delay(1000);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass); 

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  readSensor();
  Serial.println("humidity:");
  Serial.println(data.h);
  Serial.println("temperature:");
  Serial.println(data.t);
  myRadio.write(&data, sizeof(data));
  delay(1000);

  Blynk.run();
  timer.run();

}

void readSensor()
{
  data.h = dht.readHumidity();
  data.t = dht.readTemperature();
  data.h2 = dht2.readHumidity();
  data.t2 = dht2.readTemperature();
  data.h3 = dht3.readHumidity();
  data.t3 = dht3.readTemperature();
  data.h4 = dht4.readHumidity();
  data.t4 = dht4.readTemperature();
}
[/code]```

@Anton85 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:
```

Pete.

1 Like

This has nothing to do with answering your question (which, incidentally, has been answered many times before in this forum) but your void loop is a mes!

You have a timer set-up to call your sendSemsor function once per second, but read your sensor each time the void loop executes. You then prevent this from happening to frequently by adding a blocking delay in your void loop.

What you should be doing is combine your readSensor and sendSensor functions into one function and only have Blynk.run and timer.run in your void loop.

Pete.

Hi Pete, I don’t think I have interpreted your advice well but I try to give you more explanations.
The attached sketch is the transmitter part. Then there is another sketch about the receiver.
In practice, the transmitter collects the data of several dht11 and dht22 sensors to load them both on the blynk and on a touchscreen receiver positioned elsewhere in the house.
I believe this is used to send the dht data to the receiver:

 // Setup a function to be called every second
   timer.setInterval (1000L, sendSensor); 

This, on the other hand, I think is useful only to verify the reading from the terminal:


   readSensor ();
   Serial.println ("humidity:");
   Serial.println (data.h);
   Serial.println ("temperature:");
   Serial.println (data.t);
   myRadio.write (& data, sizeof (data));
   delay (1000); 

Obviously I’m a novice and for the moment I just need help taking the first steps. Thank you.

(In Italiano)
Ciao Pete, non credo di aver interpretato bene il tuo consiglio ma provo a darti piĂş spiegazioni.
Lo sketch allegato é la parte del trasmettitore. Poi c’è un altro sketch che riguarda il ricevitore.
In pratica, il trasmettitore raccoglie i dati di piĂş sensori dht11 e dht22 per caricarli sia su blynk che su un rucevitore touchscreen posizionato in un altro punto della casa.
Questo credo che serva per inviare i dati dei dht al ricevitore:

// Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);

Questo, invece, credo che sia utile solo a verificare la lettura da terminale:

readSensor();
  Serial.println("humidity:");
  Serial.println(data.h);
  Serial.println("temperature:");
  Serial.println(data.t);
  myRadio.write(&data, sizeof(data));
  delay(1000);

Ovviamente sono un neofita e per il momento ho solo bisogno di aiuto nel muovere i primi passi. Grazie.

No, I don’t think you have either.
Maybe if you re-read what I said then it will become clearer.

Pete.

Hi Pete, could this be the solution you propose to me? Have patience, I’m here to learn.

void readSensorsendSensor()
{
  Serial.println("humidity:");
  Serial.println(data.h);
  Serial.println("temperature:");
  Serial.println(data.t);
  myRadio.write(&data, sizeof(data));
  delay(1000);

  data.h = dht.readHumidity();
  data.t = dht.readTemperature();
  data.h2 = dht2.readHumidity();
  data.t2 = dht2.readTemperature();
  data.h3 = dht3.readHumidity();
  data.t3 = dht3.readTemperature();
  data.h4 = dht4.readHumidity();
  data.t4 = dht4.readTemperature();

  Blynk.virtualWrite(V5, data.h);
  Blynk.virtualWrite(V6, data.t);
  Blynk.virtualWrite(V7, data.h2);
  Blynk.virtualWrite(V8, data.t2);
  Blynk.virtualWrite(V9, data.h3);
  Blynk.virtualWrite(V10, data.t3);
  Blynk.virtualWrite(V11, data.h4);
  Blynk.virtualWrite(V12, data.t4);
}

void setup()
{
  Serial.begin(9600);
  Serial.print("Starting Trasmettitore \n");
  dht.begin();
  dht2.begin();
  dht3.begin();
  dht4.begin();
  myRadio.begin();
  myRadio.openWritingPipe(addresses[0]);
  myRadio.setDataRate(RF24_1MBPS); //250kbps 2MBPS 1MBPS
  myRadio.setChannel(115);

  delay(1000);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

  // Setup a function to be called every second
  timer.setInterval(1000L, readSensorsendSensor);
}

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

Almost, but you need to remove this blocking delay…

which is redundant when you use the timer to control the frequency at which your `readSensorsendSensor’ function is executed.

Also, DHT11 sensors in particular (and DHT22 sensors to a slightly lesser extent) don’t like to be read too frequently.
Once every 5 seconds is probably the most frequent that you’d want to interrogate them.

Pete.

Hello everybody. By inserting only these lines in void loop, could I have my arduino reconnected? Or do I have to enter something else?

void loop() 
{
  if(!Blynk.connected())
  {
    Serial.println("Reconnecting ... ");
    Blynk.connect();
  }
  
  Blynk.run();
  timer.run();
}

Hi guys. In the end, sifting through the forum, I solved my problem. To the sketch I indicated in the first post, the following lines must be added. Obviously, if I made any mistakes in the code posted here, I hope for your help. Thank you in advance.

Ciao Ragazzi. Alla fine, spulciando nel forum, ho risolto il mio problema. Allo sketch che indicavo al primo post, bisogna aggiungere le righe seguenti. Ovviamente, se nel codice qui postato ho commesso qualche errore, spero in un vostro aiuto. Grazie anticipatamente.

void setup()
{
...
  timer.setInterval(60 * 1000, reconnectBlynk);
}

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

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