1.2.28. fejezet, SD kártya olvasó

Kapcsolódó hivtkozások

Egyszerű Fat32 fájlrenszer olvasás

// Create a text file on the SD with this path using short 8.3 names.
#define SFN_PATH "/index.html"
 
// Modify CS_PIN for your chip select pin.
#define CS_PIN SS
 
#include "SdFat.h"
using namespace sdfat;
 
// Insure FAT16/FAT32 only.
SdFat32 sd;
File32 file;
 
void error(const char* msg) {
  Serial.println(msg);
  while(true);
}
 
void setup() {
  int n;
  uint8_t buf[4];
 
  Serial.begin(9600);
  while (!Serial) {}
  Serial.println("Type any character to begin");
  while (!Serial.available()) {}
 
  if (!sd.begin(CS_PIN, SD_SCK_MHZ(15))) error("SD.begin");
 
  file = sd.open(SFN_PATH);
  if (!file) error("open");
 
  while ((n = file.read(buf, sizeof(buf)))) {
   Serial.write(buf, n);
  }
 
 file.close();
}
 
void loop() {
}

Mintakód ESP webszerverhez

#include <SD.h>
#include "SdFat.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "defs.h" //WiFi settings
using namespace sdfat;
 
FS fileSystem = SDFS;
SDFSConfig fileSystemConfig = SDFSConfig();
 
int switchPin = 5;
int switchState = LOW;
 
ESP8266WebServer server(80);
 
 
void handleNotFound() {
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);  
}
 
void setup() {
  Serial.begin(9600);
  //while(!Serial);
 
  fileSystemConfig.setCSPin(SS);
  fileSystemConfig.setAutoFormat(false);
  fileSystem.setConfig(fileSystemConfig);
 
  if (!fileSystem.begin()) {
    Serial.println("Filesystem not initialized!");
    return;
  }
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
 
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  server.on("/switch", []() {
    if (server.hasArg("light")) {
      switchState = server.arg("light") == "ON";
      digitalWrite(switchPin,switchState);
      server.send(200, "text/plain", "{\"result\":\"ok\"}");
    } else {
      server.send(500, "text/plain", "{\"result\":\"no arg: light\"}");
    }
  });
 
  server.on("/stream", []() {
    if (fileSystem.exists("img.png")) {
      File streamFile = fileSystem.open("img.png", "r");
      if (server.streamFile(streamFile, "image/png") != streamFile.size()) { Serial.println("Sent less data than expected!"); }
      streamFile.close();
      return true;
    }
  });
  server.addHook([](const String & method, const String & url, WiFiClient * client, ESP8266WebServer::ContentTypeFunction contentType) {
    Serial.println(url);
    return ESP8266WebServer::CLIENT_REQUEST_CAN_CONTINUE;
  });
 
  server.onNotFound(handleNotFound);
 
  server.serveStatic("/index.html", fileSystem, "/index.html");
 
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  server.begin();
  Serial.println("HTTP server started");
 
  pinMode(switchPin, OUTPUT);
}
 
void loop() {
  server.handleClient();
}