I've been trying to speed up logging from this PLC which is an Arduino Mega integrated with SD and an ESP8266, among other things. So I managed to log everything to the SD card from the Mega (board PLDUINO/Mega2560), and I tried sending mySQL batch strings through the Serial port (the ESP8266 is connected to the Mega via Serial2), which worked, kind of, but slowly. So I managed to connect to the SD card with the code on the Mega, once I loaded the older SD library, version 1.09. (I could not init the SD card with current SD libraries, v1.1 and above, in this setup).
So now I've got some code loaded up to the attached ESP8266 card (board PLDuino/ESP-02) and my idea was to read the data logged to the SD card and send a DATA INFILE statement to the mySQL database.
Its not apparent from the abbreviated test code I have included, but I had to load ESP8266WiFi.h to the ESP8266 board in order to communicate over wifi directly to the mySQL database without a php page intermediary. I wasn't able to send statements from the Mega to the database directly over wifi, the ESP8266WiFi library wouldn't compile on the Mega board, had to compile it on the ESP8266 board.
Trying to use SPI to read the SD from the ESP8266 board. The Mega2560 chip select is 53. I.m confused about what the ESP8266 chip select is, have tried many, but I'm wondering whether the issue is that I'm using the wrong chip select or if a different SD library is required. Here is the code that I am uploading to the Mega:
#include <TMRpcm_PLDuino.h>
#include <PLDuino.h>
#include <SPI.h>
#include <SD.h>
HardwareSerial &wifi = Serial2;
Sd2Card card;
bool card_initialized;
void logSD(String dataString){
File dataFile = SD.open("test.txt", FILE_WRITE);
if (dataFile) {
// dataFile.println(dataString);
// dataFile.close();
// print to the serial port too:
// Serial.println(dataString);
}
File myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
Serial.flush();
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
Serial.flush();
// close the file:
myFile.close();
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
Serial.flush();
}
Serial.println("Mtest done.");
delay(5000);
}
String monitorESP()
{
String recv = "";
// Initializing ESP module
PLDuino::enableESP();
// Reset ESP
Serial.println("starting ESP");
digitalWrite(PLDuino::ESP_RST, LOW);
delay(500);
digitalWrite(PLDuino::ESP_RST, HIGH);
delay(500);
while (true){
while (!wifi.available())
delay(500);
recv = wifi.readString();
Serial.println("test:" + recv);
delay(500);
}
}
void setup()
{
// A convenient macro which prints start-up messages to both LCD and Serial.
#define LOG(msg) {Serial.println(msg); }
pinMode(38, OUTPUT);
digitalWrite(38, HIGH);
pinMode(10, OUTPUT);
digitalWrite(53, HIGH);
// NB: This line is necessary in all sketches which use PLDuino library stuff.
PLDuino::init();
// Power-on LCD and set it up
PLDuino::enableLCD();
// Setup serials. Serial2 is connected to ESP-02 Wi-Fi module.
Serial.begin(115200);
Serial2.begin(115200);
// We need to initialize SD card at startup!
LOG("Initializing SD card...")
if (!SD.begin(PLDuino::SD_CS))
LOG("ERROR: Can't initialize SD card!")
delay(500);
//write something to SD to see if can read from ESP
logSD("test SD writes");
digitalWrite(53, LOW);
monitorESP();
}
void loop() {
// put your main code here, to run repeatedly:
}
Here is the code on the ESP8266:
#include <SPI.h>
#include <SD.h>
#define FILENAME "test.txt"
#define ESP_CS 15 //tried 8, 53, 4, 10
void setup()
{
// Initializing Serial.
Serial.begin(115200);
//pinMode(10, OUTPUT);
digitalWrite(SS, LOW);
digitalWrite(ESP_CS,HIGH);
SPI.begin();
//digitalWrite(ESP_SS, LOW);
// Initializing SD card.
if (!SD.begin(ESP_CS))
{
Serial.println("Failed to initialize SD card. Please check it.");
return;
} else {
Serial.println("Initialized!");
}
// Trying to create a file
{
Serial.println("Creating a file...");
// Opening a file for writing...
File f = SD.open(FILENAME, FILE_WRITE);
if (!f)
{
// Stop on error
Serial.println("Failed to create " FILENAME " file. Stop.");
// return;
}
else{
// Write some data there
f.println("Test string");
f.close(); // close when done
}
}
// Trying to read a file
{
Serial.println("Re-opening the file for reading...");
// Trying to open our file.
File f = SD.open(FILENAME, FILE_READ);
if (!f)
{
// Stop on error
Serial.println("Failed to open newly created file " FILENAME ". Stop.");
return;
}
Serial.println("File contents: ");
// Read out file contents and print them to Serial.
while(!f.available())
Serial.write(f.read());
Serial.println(); Serial.println("--end--");
f.close(); // close when done
}
}
void loop()
{
}
I keep getting :
Failed to initialize SD card. Please check it
I couldn' t do SD.open with the Mega2560 code until I downdated the SD library to 1.09. Not sure how to do that with the ESP8266 code, it's using a hardware specific library.
Possibly off topic but related info:
I tried flashing various NodeMCU versions, including a custom version with FatFS and SPI included, but I couldn't even get the lua init code to mount SD0, so not sure what's wrong there either. lua code to mount SD looks like this:
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
-- initialize other spi slaves
-- then mount the sd
-- note: the card initialization process during `file.mount()` will set spi divider temporarily to 200 (400 kHz)
-- it's reverted back to the current user setting before `file.mount()` finishes
vol = file.mount("/SD0", 8) -- 2nd parameter is optional for non-standard SS/CS pin
if not vol then
print("retry mounting")
vol = file.mount("/SD0", 8)
if not vol then
error("mount failed")
end
end
file.open("/SD0/path/to/somefile")
print(file.read())
file.close()
Thanks for any help on this. Or suggestions for other forums.