Sd card support

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
romix
Normal user
Posts: 16
Joined: 30 Dec 2015, 17:52

Sd card support

#1 Post by romix » 06 May 2016, 10:58

I am using ESPeasy on Wemos d1 mini modules. There is a neat sd-card shield for this module. How hard is it to support piping data to a file on the sd card, to be read/controlled via the web?

See wemos.cc for the d1 mini and its shields.

romix
Normal user
Posts: 16
Joined: 30 Dec 2015, 17:52

Re: Sd card support

#2 Post by romix » 06 May 2016, 19:36

A bit more background.

A client of mine wants to store Smart Meter readings on an SD card, because he does not have a server running to collect the data (trying to persuade him to adopt Domoticz).
Adding DS card support for ser2net is easy (adding a couple of lines based on a define in espeasy.ino and including the libs for sd and spi does the trick.
#if FEATURE_SD
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
} else {Serial.println("initialization done.");}
File dataFile = SD.open("DATA.TXT", FILE_WRITE);
if (dataFile) {
Serial.print("Writing to DATA.TXT...");
dataFile.write(net_buf, bytes_read);
dataFile.close();
}
#endif

Reading the data is also easy adding
#if FEATURE_SD
WebServer.on("/downloadSD", handle_SDdownload);
#endif
and

#if FEATURE_SD
//********************************************************************************
// Ronald experimental SD card support
//
// Web Interface download page for SD card
//********************************************************************************
void handle_SDdownload()
{
if (!isLoggedIn()) return;

if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
} else {Serial.println("initialization done.");}

File dataFile = SD.open("DATA.TXT");
if (dataFile) {
WebServer.sendHeader("Content-Disposition", "attachment; filename=DATA.TXT");
Serial.println("data.txt:");

// read from the file until there's nothing else in it:
WebServer.streamFile(dataFile, "application/octet-stream");
// close the file:
dataFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening data.txt on SD card");
return;
}
}
#endif

does the trick. Off course this a dirty hack. It does not really integrate in the architecture. Would adding a controller plugin 'SD card' _C009 be more appropriate? Do all plug-ins correctly use the controllers? Any ideas on how to properly add support for SD (if at all desirable)?

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests