Hi Arduino community,
I have developped an Arduino prototype to control my swimming pool equipments. For that I needed to have a SD Card Reader (to save data parameters) and an Ethernet link (to access my sensors through Internet with MQTT).
Everything is working fine. But randomly, when I restart the Arduino board, SD card is failing to initialize... I need to remove and reinsert the SD card to unblock the setup immediately.
Hereunder, my setup part of my code. The part which stuck randomly is the function SD.begin(chipSelect)...
Do you see something wrong ?
Technical parts used : Arduino Mega Board R3 / Arduino Ethernet Shield 2 (W5500)
void setup(void) {
pinMode(12, INPUT);
pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
u8g2.begin();
u8g2_prepare();
u8g2.clearBuffer();
// Disable SD SPI
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
u8g2.drawStr(2, 2, "Init Ethernet...");
u8g2_prepare();
u8g2.sendBuffer();
// Lancement de la connexion Ethernet en DHCP
if (Ethernet.begin(MAC) == 0) {
u8g2.clearBuffer();
u8g2.drawStr(2, 2, "Erreur DHCP.");
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
u8g2.drawStr(2, 12, "Erreur Shield.");
} else if (Ethernet.linkStatus() == LinkOFF) {
u8g2.drawStr(2, 12, "Cable RJ45 deconnecte.");
}
u8g2.drawStr(2, 22, "Passage IP fixe.");
Ethernet.begin(MAC, IP);
u8g2_prepare();
u8g2.sendBuffer();
}
u8g2.drawStr(2, 12, "Init SD Card...");
u8g2_prepare();
u8g2.sendBuffer();
setupSDCard();
String strHeures = lireHeuresMarchesPompe();
// Initialise le tableau des heures qui est sauvegardé, au démarrage
int indice = 0;
for (int i = 0; i < strHeures.length(); i++) {
if (strHeures.substring(i, i + 1) == "0") {
heures_marche_pompe[indice] = 0;
indice++;
} else if (strHeures.substring(i, i + 1) == "1") {
heures_marche_pompe[indice] = 1;
indice++;
}
}
pinMode(pinPompe, OUTPUT);
pinMode(pinEclairage, OUTPUT);
pinMode(pinInterrupteurEclairage, INPUT);
pinMode(pinBoutonConfiguration, INPUT);
pinMode(pinInterrupteurConfigurationPH, INPUT);
pinMode(pinInterrupteurConfig3, INPUT);
u8g2.drawStr(2, 22, "Init RTC...");
u8g2_prepare();
u8g2.sendBuffer();
rtc.begin();
//rtc.adjust(DateTime(2023, 5, 11, 8, 30, 00)); // Pour mettre à jour l'heure du module RTC
mqttClient.setCallback(callback);
// Première initialisation des capteurs
initialiserPH();
temperatureAir = getTemp(ds);
temperatureEau = getTemp(dsEau);
// Gestion du bidon PH
if (digitalRead(pinBidonPH) == HIGH) {
bidonPHVide = false;
afficherErreur = false;
texteErreur = "";
} else {
bidonPHVide = true;
afficherErreur = true;
texteErreur = "Bidon pH bas...";
}
wdt_enable(WDTO_4S);
wdt_reset();
}
void setupSDCard() {
pinMode(53, OUTPUT);
digitalWrite(53, HIGH); // Workaround pour éviter les initialization failed
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip
while (!SD.begin(chipSelect)) { }
if (!SD.exists("heures.txt")) {
File myFile = SD.open("heures.txt", FILE_WRITE);
myFile.close();
}
}
Thank you !