Review my code

i’m now creating a project, plotting temp and humidity to blynk app, at the same time store data in SD card,
i have arduino uno with ethernet shield w5100 when i run the program it always trying to connect to the cloud server
some times it connects, and once connected it stay about 1 or 2 minutes and than server is down…
here is my prog so you can help me

//#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h> // This part is for Ethernet stuff
#include <OneWire.h>
//#include <SimpleTimer.h>
#define LIMITMAX 35
#define LIMITMIN 10
#define LIMITDHT 80
#include <DHT.h>
#include <SD.h>
#define DHTPIN 3
#define DHTTYPE DHT11     // DHT 11
//SimpleTimer timer;
const int chipSelect = 4;
char auth[] = "d6f128e07e294f849ee3c3d2e8e12a23";
/* WiFi credentials */ 

/* TIMER */ 

/* DS18B20 Temperature Sensor */ 
#include<DallasTemperature.h> 
#define ONE_WIRE_BUS 2 
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature DS18B20(&oneWire); 
int temp_0; 
int temp_1; 
int h;
DHT dht(DHTPIN, DHTTYPE);
void setup() 
{ 
  dht.begin();
Serial.begin(9600); 
//Blynk.begin(auth); 
Blynk.begin(auth);
DS18B20.begin(); 
SD.begin(chipSelect);

} 

void loop() 
{  // timer.run(); // Initiates SimpleTimer

   Blynk.run();
   h = dht.readHumidity();
 Blynk.virtualWrite(V2, h);
  DS18B20.requestTemperatures(); 
temp_0 = DS18B20.getTempCByIndex(0); // Sensor 0 will capture Temp in Celcius 
temp_1 = DS18B20.getTempCByIndex(1); // Sensor 0 will capture Temp in Celcius
 File sdcard_file = SD.open("data.csv", FILE_WRITE);
if (sdcard_file) {
//Serial.print("Temp_0: "); 
Serial.print(temp_0); 
//Serial.print(" oC . Temp_1: "); 
Serial.print(temp_1); 
//Serial.println(" oF"); 
Serial.print(h);
}
Blynk.virtualWrite(V5, temp_0); //virtual pin V5 indoor temp
Blynk.virtualWrite(V6, temp_1); //virtual pin V6 outdoor temp
 if(temp_0>LIMITMAX){
       Blynk.email("bxxx@xxxfr", "Ethernet Alert", "Temperature Increased over limit");
                        Blynk.notify("Attention! température salle 1 élevé!");
 }
 if(temp_0<LIMITMIN){
             Blynk.email("bxxx@xxxfr", "Ethernet Alert", "Temperature Increased over limit");
                        Blynk.notify("Attention! température salle 1 très faible!");
 }
 if(temp_1>LIMITMAX){
  Blynk.email("benhassine.m@hotmail.fr", "Ethernet Alert", "Temperature Increased over limit");
                        Blynk.notify("Attention! température salle 2 élevé!");
 }
     if(temp_1<LIMITMIN){
Blynk.email("bxxx@xxxfr", "Ethernet Alert", "Temperature Increased over limit");
                        Blynk.notify("Attention! température salle 2 très faible!");
     }
  if(h>LIMITDHT){
    Blynk.email("bxxx@xxxfr", "Ethernet Alert", "Temperature Increased over limit");
                        Blynk.notify("Attention! humidité très élevé!");
  }

 
 
} // End of Void Loop

Read through the pages in the Help Center… You need to keep the void loop() clear… containing just Blynk.run(); and timer.run(); when using Blynk.

Use timed functions and juggle your code around accordingly.

You should. Do a bit of googling about how the Arduino Ethernet shield uses the same chipselect line to handle both the Ethernet shield and the SD card.
You’ll find that it’s not possible to use both at once - you need to de-select Eternet before using the SD card, then de-select the SD card before using Ethernet again.

The problem is that Blynk really doesn’t like having the Ethernet connection interrupted in that way, because it needs to respond to the hertabeat from the Blynk server.

My suggestion would be that you take a look at the Superchart widget in Blynk. The temperature data you are collecting can be stored on the Blynk server instead of your SD card and viewed by Superchart. It will probably give you a much better representation of your data than anything you’re able to do with the data you plan to store on the SD card. The data can also be downloaded from the server if you wish (with the Blynk cloud serve this is done in Superchart and the data is sent to you as an email attachment).

Pete.

void loop !
:smile: