SD recording problem and simultaneous Internet access with Ethernet shield

Hello to all.
So let’s get started (I hope I read how to make your first post and don’t forget anything …)

This project is based on an Arduino mega 2560, an Ethernet keystudio W5500 shield. And a DHT22 temperature and hygrometry sensor
I am trying to perform data acquisition on a hive and I want to store the acquired data in the SD card incorporated in my Ethernet shield and at the same time send this data to the ThingSpeak server.
This is where the worries start.
Indeed I can either:
save correctly to my SD and the IP address of the shield goes crazy (like
or send the data to the ThingSpeak server but backup the data on the SD.

I develop :
if at boot, there is an SD card in the Ethernet shield, I cannot get an IP address from my box, nor get a fixed IP address with the Serial.print (Ethernet.localIP ()) function; in this case send me 255 … and suddenly the data is saved on the SD card, but not on the server (since I do not have a valid IP address)
If I remove this SD card from the reader, I get a local IP address ok and I manage to update the server but since there is no card in the reader so no backup …
I tried to assign the SD drive pin 4, or 10 or 53 as suggested in other posts with no difference.
I also tried the Ethernet2 library (special Arduino Mega it seems) instead of the Ethernet library without more success.

In short I do not know what to do if someone (s) can light my lantern to allow me to complete this project.
A thousand thanks
Well I attach the code:

#include "SD.h"
#include "SPI.h"
#include <LiquidCrystal.h> 
#include "DHT.h"
#include "ThingSpeak.h"
#include <Ethernet.h>      //  Ethernet or Ethernet2 no change...
#include "secrets.h"       

#define DHTPIN A0                              // pin A0 is assigned for DHT
#define DHTTYPE DHT22                          //  definition of sensor type 
DHT dht(DHTPIN, DHTTYPE);                      // creation of the T ° sensor object et hygro

LiquidCrystal monEcran(9,8,7,6,5,4);            // creation of the myScreen object

int pinCS = 53;                                 //Pin 4 is reserved for the SD reader if clock shield if ethernet shield is pin 10 on Uno and 53 on mega ..

EthernetClient client;

unsigned long myChannelNumber = SECRET_CH_ID;         // copy the contents of secret.h variable ChannelID of the thingspeak site
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;     

byte mac[] = SECRET_MAC; // The MAC address of the shield

IPAddress ip(192,168,1,143);  // The default IP address that the shield will take if the box does not assign an IP

File file;                                        // Create the file object named file

void setup()
   pinMode(pinCS, OUTPUT);                         // Writing SD card reader initialization
   Ethernet.init(10);                     // start the ethernet card on pin 10
  char erreur = 0;

  erreur = Ethernet.begin(mac); // We try to start the Ethernet shield WITHOUT an IP address (therefore given via DHCP from the box)

  if (erreur == 0) {
    Serial.println("Parameter setting with fixed ip...");     // if an error has occurred it means that the DHCP assignment is not working. We therefore initialize by forcing an IP
    Ethernet.begin(mac, ip);                            // forcing the ip address if not acquired by box
    Serial.println(Ethernet.localIP());                 // displays the IP address on the serial monitor
  delay(1000);                                       // Give the shield a second to initialize
  //pinMode(pinCS, OUTPUT);                         // Writing SD card reader initialization
  if (SD.begin(pinCS))                                 // if the SD is ok we do the following
    Serial.println("SD card ready to use.");         
  } else
    Serial.println("SD card initialization failed");   
  dht.begin();                                    // start of T ° & hygro dht sensor

  ThingSpeak.begin(client);                       // ThingSpeak startup
  monEcran.begin(16,2);                           //communication is initialized with 16 columns and two lines

void loop()
  Serial.println("Sensor reading in progress ");            

  float h = dht.readHumidity();                   // reads the humidity level
  float t = dht.readTemperature();                // reads the temperature in Celsius (default)
  if (isnan(h) || isnan(t)) {                           // Check if the reading is correct (and start again).
    Serial.println("Unable to read the sensor!");   

 // fix Thingspeak fields with data
  ThingSpeak.setField(1, t);
  ThingSpeak.setField(2, h);

    // writing fields in ThingSpeak
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  if(x == 200){
    Serial.println("ThingSpeak update successful.");
    Serial.println("Channel update problem. code erreur HTTP " + String(x));
    Serial.println(Ethernet.localIP());         // TO CHECK BECAUSE IF SD PRESENTS IT CHANGES THE IP ADDRESS
  if (SD.begin(pinCS)) {    

 String NomFichier ="test.csv";

    file = NomFichier, FILE_WRITE);           // open or create the file if it does not exist
    if (file) {

      file.print(h,1);                              // stores the hygrometry value with 1 decimal point
      file.print(t,1);                               // stores the temperature with 1 digit behind the decimal point
      file.close();                                 //close the file
    } else { 
      Serial.print("error opening file : ");   

  else {
    Serial.println("NO SD!");   

  Serial.print("Humidity: ");           
  Serial.print(h,1);                    // displays the humidity value with a single character behind the comma
  Serial.print(" %\t");                
  Serial.print("Temperature: ");       
  Serial.print(t,1);                    // displays the temperature value with a single digit behind the decimal point
  Serial.println(" °C");              

  monEcran.print ((char)223);       
  monEcran.print(" ");          
  delay(20000);                      //waiting time before repeating the loop 20000 = 20 seconds correct this last value to give rhythm to the measurements 1000 = 1s


// Use this file to store all of the private credentials 
// and connection details

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield

#define SECRET_CH_ID 1076674      // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "3I8TOABF8AQ2KV3C"   // replace XYZ with your channel write API Key

#define SECRET_MAC {0x90, 0xA2, 0xDA, 0x10, 0x40, 0x4F}// Ardiuno mega
//#define SECRET_MAC { 0x90, 0xA2, 0xDA, 0x0E, 0xA5, 0x7E }  // Arduino Uno

Thanks again for all possible help! :wink:

Hello, did you manage to make your code work?
I’m working on a similar project.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.