Resetting when WIFI comes back

Hello,

I am a beginner in IOT and I wrote a small program that measures the electricity
consumption of my house and sends it to the remote application every second.

For this I use an arduino UNO R3 card which accesses the internet via WIFI and my box.

Everything works fine but here is my problem:

Every night I turn off the WIFI to turn it back on in the morning.

Sometimes too, due to various problems, he cuts himself during the day and comes back.

So I would like my code to regularly test the connection to Blynk and reset it if necessary.

How to do ?

Thanks for any help.


My code :

#include <SPI.h>
#include <WiFi.h>
#include <BlynkSimpleWifi.h>

char ssid[] = "XXXXXXX";      // your network SSID (name)
char pass[] = "XXXXXX";              // your network password

volatile int periode=0;
volatile int PuissanceInstantanee=0;
volatile int PuissanceInstantaneeDelta=0;
volatile int MemPuissanceInstantanee=0;
volatile int tempo=300;
volatile int TdernierFront=0;
volatile float CoutKwh=0.2228/1000;
volatile float DeltaP=0;
volatile float DeltaT=0;
volatile long NbFlash=0;
volatile long MemNbFlashDelta=0;
volatile int EtatBoutonDelta=0;

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  EtatBoutonDelta = param.asInt();

  if (EtatBoutonDelta==1)
  {
    MemNbFlashDelta=NbFlash;
    Blynk.virtualWrite(V6, 0);
   
    MemPuissanceInstantanee=PuissanceInstantanee;
    DeltaT=millis();    
  }

}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "********");
  Blynk.setProperty(V3, "onImageUrl",  "********");
  Blynk.setProperty(V3, "url", "********");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, PuissanceInstantanee);
  Blynk.virtualWrite(V2, CoutKwh*PuissanceInstantanee);
   
  if (EtatBoutonDelta==1)
  {

    if (PuissanceInstantaneeDelta>=0)
         Blynk.virtualWrite(V7, PuissanceInstantaneeDelta);
    else
    Blynk.virtualWrite(V7, 0); 

    Blynk.virtualWrite(V6, (NbFlash-MemNbFlashDelta)*CoutKwh);  
    
    long TDeltaMilliseconde = (millis()-DeltaT);
    int TDeltaseconde = TDeltaMilliseconde/1000;
    int TDeltaMinute=TDeltaseconde/60;
    int TDeltaSeconde=TDeltaseconde-60*TDeltaMinute;

    Blynk.virtualWrite(V4,TDeltaMinute );  
    Blynk.virtualWrite(V9,TDeltaSeconde ); 
    
  }
}


void setup()
{
  
  pinMode(2,INPUT);
  // Debug console
  Serial.begin(9500);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  // init affichage
  Blynk.virtualWrite(V7, 0);  
  Blynk.virtualWrite(V6, 0);  
  Blynk.virtualWrite(V8,"---");   
  
  timer.setInterval(1000L, myTimerEvent);
  
  attachInterrupt(digitalPinToInterrupt(2), rpm_fan2, FALLING);
  
}

void loop()
{
  Blynk.run();
  timer.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!

}

void rpm_fan2() 
{
  periode=millis()-TdernierFront;
  
  if (periode>tempo)
  {

    PuissanceInstantanee=3600000/periode;
    PuissanceInstantaneeDelta=(PuissanceInstantanee-MemPuissanceInstantanee);
    TdernierFront=millis();
    
    NbFlash++;
   
  }    
}

@Falcon12 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.

Ok, done.

No, you’ve put the triple backticks at the beginning of your post, not immediately before your code.

Pete.

You’re right, sorry

Hi,

This is from the examples - HandleDisconnect.ino
Does this point you in the right direction?

/*************************************************************
  Blynk is a platform with iOS and Android apps to control
  ESP32, Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build mobile and web interfaces for any
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: https://www.blynk.io
    Sketch generator:           https://examples.blynk.cc
    Blynk community:            https://community.blynk.cc
    Follow us:                  https://www.fb.com/blynkapp
                                https://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  This example shows how to keep WiFi connection on ESP8266.

 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

/* Fill in information from Blynk Device Info here */
//#define BLYNK_TEMPLATE_ID           "TMPxxxxxx"
//#define BLYNK_TEMPLATE_NAME         "Device"
//#define BLYNK_AUTH_TOKEN            "YourAuthToken"


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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


int lastConnectionAttempt = millis();
int connectionDelay = 5000; // try to reconnect every 5 seconds

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}

void loop()
{
  // check WiFi connection:
  if (WiFi.status() != WL_CONNECTED)
  {
    // (optional) "offline" part of code

    // check delay:
    if (millis() - lastConnectionAttempt >= connectionDelay)
    {
      lastConnectionAttempt = millis();

      // attempt to connect to Wifi network:
      if (pass && strlen(pass))
      {
        WiFi.begin((char*)ssid, (char*)pass);
      }
      else
      {
        WiFi.begin((char*)ssid);
      }
    }
  }
  else
  {
    Blynk.run();
  }
}

Thanck you microBrian, yes it seems to be able to help me, I will try to use it :slightly_smiling_face: