MqttClient.setServer and SD.open conflict issue

My sketch has been using both a mqtt broker running on a network pc, and the SD card on my arduino Mega 2560 with etherNet shield to host a User Interface webpage. I now want to use the SDcard for 2 purposes: (1) to retain a webpage file that I use to host the User Interface, and (2) to add system data backup capability. The sketch has been running OK with both the mqtt client AND the webpage active as follows:

// initialize SD card…
int sdCardChipSelect = 4; //53; // Use pin 4 on all including the Mega
pinMode(sdCardChipSelect, OUTPUT); // Pin 53 must be left as an OUTPUT for the SD library to work
pinMode(10, OUTPUT);
digitalWrite(10, HIGH); // Disable EtherNet
Serial.println(F("\nInitializing the SD card…"));
if (!SD.begin(sdCardChipSelect)) {
Serial.println(F(“ERROR - SD card initialization failed!”));
return; // init failed
}
Serial.println(F(“SUCCESS - SD card initialized.”));
.
.
.
MqttClient.setServer(server, 1883);
MqttClient.setCallback(callback);

and, in my main loop:

webFile = SD.open(SDcardFileName); // open web page file
if (webFile) {
Serial.print(F("Sending Webpage File ")); Serial.println(SDcardFileName);
while (webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}

When I added the SDcard data backup functionality the code creates the backup file (with extension PC210128 .TXT as opposed to PC210128.txt as I specified, and 8-characteers or less!) but fails to open the file for read/write access .
webFile = SD.open(“PC210128 .txt”, FILE_WRITE);
Serial.print(F("webFile = ")); Serial.println(webFile); -------> webFile = 0

I have tracked the problem to a single line of setup code:
MqttClient.setServer(server, 1883);

I am running on a Mega and have tried various combinations of disable and enable attempts WITHOUT success
//pinMode(10, OUTPUT); digitalWrite(10, HIGH); // Disable EtherNet Chip
//pinMode(10, OUTPUT); digitalWrite(10, LOW ); // Enable EtherNet Chip
//pinMode( 4, OUTPUT); digitalWrite( 4, HIGH); // Disable SD Card
//pinMode( 4, OUTPUT); digitalWrite( 4, LOW ); // Enable SD Card
webFile = SD.open(“PC210128 .txt”, FILE_WRITE);
Serial.print(F("webFile = ")); Serial.println(webFile); -------> webFile = 0

I’m near the limit of my understanding.
Is there a pin issue on the Mega: 4, 10, 53?
Is there a port 1883 issue?
Any clues of what am I doing wrong?

Thanks in advance for your help.

I have tracked the problem to a single line of setup code:
MqttClient.setServer(server, 1883);

How? I have serious doubts that this line is responsible for the problems.

Is there a pin issue on the Mega: 4, 10, 53?

No.

Is there a port 1883 issue?

No.

Any clues of what am I doing wrong?

My guess is that the error is in that part of the code you're hiding from us. Maybe the SPI hardware isn't initialized correctly but that's wild guessing without seeing the code.

I really appreciate you’re insights. Here’s the code. Note I do 3 SD Card tests as I progress. I hav annotate the success of each.

// Initialize SD card…
etherNetChipSelect = 10;
sdCardChipSelect = 53; //4; // The documentation I find says to use 53 for the Mega
Serial.print(F("SDcardChipSelect = ")); Serial.println(sdCardChipSelect);

pinMode(etherNetChipSelect, OUTPUT); digitalWrite(etherNetChipSelect, HIGH); // Disable EtherNet Chip
delay(100);
pinMode(53, OUTPUT);
pinMode(sdCardChipSelect, OUTPUT); digitalWrite(sdCardChipSelect, LOW ); // Enable SD Card
delay(100);

Serial.println(F("\nInitializing the SD card…"));
if (!SD.begin(sdCardChipSelect)) {
Serial.println(F(“ERROR - SD card initialization failed!”));
return; // init failed
}
Serial.println(F(“SUCCESS - SD card initialized.”));
// check for htm file
if (!SD.exists(SDcardFileName)) {
Serial.print(F(“ERROR - Can’t find “)); Serial.print(SDcardFileName); Serial.println(F(” file!”));
return; // can’t find index file
}
Serial.print(F(“SUCCESS - Found “)); Serial.print(SDcardFileName); Serial.println(F(” file.”));

digitalWrite(sdCardChipSelect, HIGH); // Disable SD Card
delay(100);
pinMode(etherNetChipSelect, OUTPUT); digitalWrite(etherNetChipSelect, LOW ); // Enable EtherNet Chip
delay(100);

SDtest(); // This test is always successful

// initialize the ethernet device…
Serial.print(F("etherNetChipSelect = ")); Serial.println(etherNetChipSelect);
pinMode(etherNetChipSelect, OUTPUT);
digitalWrite(etherNetChipSelect, LOW ); // Enable EtherNet Chip

Serial.println(F("\nPoolCtrl is attempting to connect to the LAN"));
Ethernet.begin(mac, ip); // initialize the ethernet device
delay(1500); // Wait for the hardware to organize itself
Serial.print(F("PoolCtrl is connected as: ")); Serial.println(Ethernet.localIP());

server2.begin(); // start listening for clients

//Udp.begin(localPort); // NTP Time
//GetTimeFromNTP();

SDtest(); // This test is successful only when sdCardChipSelect = 4

// initialize Schedule Arrays …
for (int indexRow = 0; indexRow < NUM_ROWS; indexRow++) {
//Serial.println(F("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"));
// Serial.println(indexRow);
schNumber[indexRow] = indexRow; // Serial.println(schNumber[indexRow]);
schName[indexRow] = “N”; // Serial.println(schName[indexRow]);
schFunction[indexRow] = “F”; // Serial.println(schFunction[indexRow]);
schRPM[indexRow] = 100 * (indexRow + 1); // Serial.println(schRPM[indexRow]);
schTempSP[indexRow] = 10 * (indexRow + 1); // Serial.println(schTempSP[indexRow]);
schStart[indexRow] = “S”; // Serial.println(schStart[indexRow]);
schStop[indexRow] = “S”; // Serial.println(schStop[indexRow]);
schDays[indexRow] = “D”; // Serial.println(schDays[indexRow]);
//PrintRowFor(indexRow * NUM_COLS);
}

PrintArrays(); // Initialized
TableEEPROM(); //
//PrintEEPROM();

// RePopulate Schedule Arrays from EEPROM …
myInt = LoadArraysFromEEPROM();
//Serial.print(F(“Read “)); Serial.print(myInt); Serial.print(F(” bytes from EEPROM”)); Serial.println(F("\n"));

// MQTT Connection -----------------------------------------------------
MqttClient.setServer(server, 1883);
MqttClient.setCallback(callback);

MqttRetryTime = 0;
MqttPacketTime = millis();
Serial.print(F("Finished Communications Initializations. ClockTime = ")); Serial.println(MqttPacketTime);

// Configure Outputs …
Serial.println(F("\nConfigure & Initalize Digital Outputs"));

for (i = 0 ; i < 16; i++) {
pinMode(ssrPin*, OUTPUT);*
_ digitalWrite(ssrPin*, LOW);_
_
}*_

* // Configure Miscellaneous Analog Inputs & Outputs …*
* Serial.println(F(“Configure & Initalize Analog Inputs & Outputs”));*
* // Configure Miscellaneous Devices …*
* // Start up the OneWire / DallasTemperature Library -----------------------------------------------*
* sensors.begin();*
* Serial.println(F(“Configure & Initalize DS18x20 Temperature Sensors”));*
* discoverOneWireDevices(); // Use to declare DeviceAdresses above, otherwise can disable*
* // Set the resolution to 10 bit (Can be 9 to 12 bits … lower is faster)*
* sensors.setResolution(DS18B20_01, 10);
sensors.setResolution(DS18B20_02, 10);
sensors.setResolution(DS18B20_03, 10);
sensors.setResolution(DS18B20_04, 10);
sensors.setResolution(DS18B20_05, 10);
_
Serial.println(F("\nFinished Initializations"));_
_
MqttPacketTime = millis();_
timeStamp_BootUp_String[0] = MyCharY;
_
Help();_
_
SDtest(); // This test hangs the Mega 2560 after it prints “Next step is to open the file”_
_
} // End #Setup -------------------------------------------------------------------------------------_
void SDtest(void) {
_
Serial.println(F(“Starting SDtest”)); _
_
// open the file. note that only one file can be open at a time,_
_
// so you have to close this one before opening another.*_

* //pinMode(etherNetChipSelect, OUTPUT); digitalWrite(etherNetChipSelect, HIGH); // Disable EtherNet Chip*
* //pinMode(etherNetChipSelect, OUTPUT); digitalWrite(etherNetChipSelect, LOW ); // Enable EtherNet Chip*
* //pinMode(sdCardChipSelect, OUTPUT); digitalWrite(sdCardChipSelect, HIGH); // Disable SD Card*
* //pinMode(sdCardChipSelect, OUTPUT); digitalWrite(sdCardChipSelect, LOW ); // Enable SD Card*
* //pinMode(etherNetChipSelect, OUTPUT); *
* digitalWrite(etherNetChipSelect, HIGH); // Disable EtherNet Chip*
* delay(100);*
* //pinMode(sdCardChipSelect, OUTPUT); *
* digitalWrite(sdCardChipSelect, LOW ); // Enable SD Card*
* delay(100);*
* /////////////////////////////////////////////////////////////////////////////////*
* Serial.println(F(“Next step is to open the file”));*
* webFile = SD.open(“test.txt”, FILE_WRITE);
_
delay(200);_
_
Serial.print(F("webFile = ")); Serial.println(webFile);_
_
// if the file opened okay, write to it:_
_
if (webFile) {_
_
Serial.print(“Writing to test.txt…”);_
_
webFile.println(“testing 1, 2, 3.”);_
_
// close the file:_
_
webFile.close();_
_
Serial.println(“done.”);_
_
} else {_
_
// if the file didn’t open, print an error:_
_
Serial.println(“error opening test.txt”);_
_
}_
_
// re-open the file for reading:_
_
Serial.println(F(“Next step is to open the file again”));_
_
webFile = SD.open(“test.txt”);_
_
delay(200);_
_
Serial.print(F("webFile = ")); Serial.println(webFile);*_

* if (webFile) {*
* Serial.println(“test.txt:”);*
* // read from the file until there’s nothing else in it:*
* while (webFile.available()) {*
* Serial.write(webFile.read());*
* }*
* // close the file:*
* webFile.close();*
* } else {*
* // if the file didn’t open, print an error:*
* Serial.println(“error opening test.txt”);*
* }*
* /////////////////////////////////////////////////////////////////////////*
* //pinMode(sdCardChipSelect, OUTPUT); *
* digitalWrite(sdCardChipSelect, HIGH); // Disable SD Card*
* delay(100);*
* //pinMode(etherNetChipSelect, OUTPUT); *
* digitalWrite(etherNetChipSelect, LOW ); // Enable EtherNet Chip*
* delay(100);*
}

Edit your post and insert code tags! You might have noticed that the forum system mangled the code unreadable if you don’t use code tags.

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