BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Program interrupted because device disconnected from WiFi & therefore Blynk-cloud

Hi. I’m having trouble with my arduino code. Basically I’m using an Arduino MKR WiFI 1010 board connected to a WiFi network, that gets information from a temperature and humidity sensor in the same way that a meteorogical station would do it. Also, I want to measure the variables every 10 seconds and after 5 minutes of recording and accumulating that data, it calculates the mean, saves it on a SD card and sends it through Blynk connection. So, I use a SimpleTimer to do that but the problem is that if it gets disconnected for some reason (WiFi disabled, electricity out, etc) the program gives priority to the connection and tries to reconnect at the same time that it’s getting data from the sensor interrupting the desired process (regarding that only need connection to tha server every 5 minutes and not between that period) and then, the timer gets affected because it is supposed to record the data every 5 minutes in specific times (10:00, 10:05, 10:10, etc) IF ONLY the board achieves to reconnect which normally doesn’t happen.

I only desire that if the board gets disconnected it only checks it at the time that the program sends the data and if this is so, making a try to reconnect, save the data into the SD card and continue measuring for the next period of 5 minutes… I know this is so demanding but at least i don’t want my timer gets interrupted in that way… Thanks.

Blynk version: v0.6.1 on MKR WiFi 1010

The code below is a summary version of the code to show how it was basically written


#include <WiFiNINA.h>                      
#include <BlynkSimpleWiFiNINA.h> 
#include <DHT.h>
#include <SD.h> 

DHT sensorTH(4, DHT21)
File file;
SimpleTimer timer;

char auth[] = "****************************";
char ssid[] = "****************************";
char pass[] = "****************************";

byte count = 0;                                    
const unsigned long MSEGS_CALC_MEAN = 300000;   5 min           
const unsigned long MSEGS_DATA= 10000;  // 10 sec                     
const int num_data = MSEGS_CALC_MEAN / MSEGS_DATA;

float temp_prom;
float hum_prom;
float t, h; 

void setup(){
    delay(1000);                // delay 1 sec to begin the program.
    Serial.begin(9600);        
   
    sensorTH.begin();
    SD_Begin(); // Check if SD is connected
    
    Blynk.begin(auth, ssid, pass);
    creating_File(); // Creates the file in the SD card
    
    timer.setInterval(MSEGS_DATA, acumParameter);
    timer.setInterval(MSEGS_CALC_MEAN, getMean);
    timer.setInterval(MSEGS_CALC_MEAN, uploadData);
}

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

void acumParameter(){
    count++;    
    t += sensorTH.readTemperature();
    h += sensorTH.readHumidity();
}

void getMean(){
    if (count == num_data){
        temp_prom = t / num_data;
        hum_prom = h / num_data;
         
    }
}

void uploadData(){
    uploadBlynk();
    uploadSD();
    t = 0; h = 0; count= 0;
}

void uploadBlynk(){
    Blynk.virtualWrite(V0, temp_prom);
    Blynk.virtualWrite(V1, hum_prom) ; 
}

void uploadSD(){
    
    file= SD.open("est1", FILE_WRITE);
    if (file) { 
        file.print(temp_prom); file.print(",");
        file.print(hum_prom); file.print(",");
        file.close();  
    }
    else{
        file.close();
    }
}

Hi,

is blocking code. If you lose Wifi for any reason Blynk.begin will continually attempt to reconnect to Blynk, nothing else will run. You need to use:

WiFi.begin(ssid, pass);  

and

Blynk.config(auth);
Blynk.connect(); 

This will allow the rest of the code to run, even if Wifi is down. Search this forum for non-blocking Blynk connection.

This is a very basic example:

#define BLYNK_PRINT Serial           // This prints to Serial Monitor
//#define BLYNK_DEBUG                 // Optional, this enables more detailed prints
#include <ESP8266WiFi.h>              // for ESP8266
#include <BlynkSimpleEsp8266.h>  // for ESP8266

char auth[] = "aaa";
char ssid[] = "bbb";
char pass[] = "ccc";

void setup() {
  Serial.begin(9600);
  
  WiFi.begin(ssid, pass); 
  
  Blynk.config(auth);
  Blynk.connect();
}

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

cul

billd

1 Like

Thanks for your reply… I’ve done what you suggested but when it didn’t help because when I run the code I let it run with a fixed connection and after some time I disconnected the router on purpose so when the program read the data from the sensor it kept interrupted by the message “Connecting to blynk-cloud.com:80” and my timer functions got affected.

I’d suggest that you post your latest code, and explain in detail what…

means in practice.

Pete.