add basic SD-card support for reading files
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
Based on the the following sketches:
|
Based on the the following sketches:
|
||||||
- SimpleWifiServer example
|
- SimpleWifiServer example
|
||||||
- SD_Test
|
- ESP32/SD_Test
|
||||||
- IRremote/SendDemo
|
- IRremote/SendDemo
|
||||||
|
|
||||||
|
|
||||||
@@ -13,15 +13,26 @@
|
|||||||
http://yourAddress/L turns it off
|
http://yourAddress/L turns it off
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Circuit:
|
Circuit:
|
||||||
* LED attached to pin 13 (optional)
|
* LED attached to pin 13 (optional)
|
||||||
* IR Transmitter attached to pin 4 (connect to 5v or 3v3)
|
* IR Transmitter attached to pin 4 (connect to 5v or 3v3)
|
||||||
* SD-Module to load HTML and CSS from SD-card
|
* SD-Module to load HTML and CSS from SD-card
|
||||||
|
*
|
||||||
|
* Connect the SD card to the following pins:
|
||||||
|
*
|
||||||
|
* SD Card | ESP32
|
||||||
|
* 3v3 3.3V
|
||||||
|
* CS 5
|
||||||
|
* MOSI 23
|
||||||
|
* CLK 18
|
||||||
|
* MISO 19
|
||||||
|
* GND GND
|
||||||
|
|
||||||
developed for the ESP32vn IoT Uni board 2024
|
developed for the ESP32vn IoT Uni board 2024
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
#include "FS.h"
|
||||||
|
#include "SD.h"
|
||||||
|
#include "SPI.h"
|
||||||
|
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <IRremote.hpp>
|
#include <IRremote.hpp>
|
||||||
@@ -30,12 +41,153 @@
|
|||||||
|
|
||||||
WiFiServer server(80);
|
WiFiServer server(80);
|
||||||
|
|
||||||
|
|
||||||
|
/**** SD-Card Functions ****/
|
||||||
|
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
|
||||||
|
Serial.printf("Listing directory: %s\n", dirname);
|
||||||
|
|
||||||
|
File root = fs.open(dirname);
|
||||||
|
if(!root){
|
||||||
|
Serial.println("Failed to open directory");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(!root.isDirectory()){
|
||||||
|
Serial.println("Not a directory");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = root.openNextFile();
|
||||||
|
while(file){
|
||||||
|
if(file.isDirectory()){
|
||||||
|
Serial.print(" DIR : ");
|
||||||
|
Serial.println(file.name());
|
||||||
|
if(levels){
|
||||||
|
listDir(fs, file.path(), levels -1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.print(" FILE: ");
|
||||||
|
Serial.print(file.name());
|
||||||
|
Serial.print(" SIZE: ");
|
||||||
|
Serial.println(file.size());
|
||||||
|
}
|
||||||
|
file = root.openNextFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void readFile(fs::FS &fs, const char * path){
|
||||||
|
Serial.printf("Reading file: %s\n", path);
|
||||||
|
|
||||||
|
File file = fs.open(path);
|
||||||
|
if(!file){
|
||||||
|
Serial.println("Failed to open file for reading");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.print("Read from file: ");
|
||||||
|
while(file.available()){
|
||||||
|
Serial.write(file.read());
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void testFileIO(fs::FS &fs, const char * path){
|
||||||
|
File file = fs.open(path);
|
||||||
|
static uint8_t buf[512];
|
||||||
|
size_t len = 0;
|
||||||
|
uint32_t start = millis();
|
||||||
|
uint32_t end = start;
|
||||||
|
if(file){
|
||||||
|
len = file.size();
|
||||||
|
size_t flen = len;
|
||||||
|
start = millis();
|
||||||
|
while(len){
|
||||||
|
size_t toRead = len;
|
||||||
|
if(toRead > 512){
|
||||||
|
toRead = 512;
|
||||||
|
}
|
||||||
|
file.read(buf, toRead);
|
||||||
|
len -= toRead;
|
||||||
|
}
|
||||||
|
end = millis() - start;
|
||||||
|
Serial.printf("%u bytes read for %u ms\n", flen, end);
|
||||||
|
file.close();
|
||||||
|
} else {
|
||||||
|
Serial.println("Failed to open file for reading");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
file = fs.open(path, FILE_WRITE);
|
||||||
|
if(!file){
|
||||||
|
Serial.println("Failed to open file for writing");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t i;
|
||||||
|
start = millis();
|
||||||
|
for(i=0; i<2048; i++){
|
||||||
|
file.write(buf, 512);
|
||||||
|
}
|
||||||
|
end = millis() - start;
|
||||||
|
Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============ IR Functions ============ */
|
||||||
|
void sendButton(int command)
|
||||||
|
{
|
||||||
|
Serial.println(command);
|
||||||
|
Serial.flush();
|
||||||
|
IrSender.sendSony(0xF, command, 2, 12);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ============ Setup ============ */
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
pinMode(LED_PIN, OUTPUT); // set the LED pin mode
|
|
||||||
|
|
||||||
|
pinMode(LED_PIN, OUTPUT); // set the LED pin mode
|
||||||
|
|
||||||
delay(10);
|
delay(10);
|
||||||
|
|
||||||
|
/* ============ SD-Card Setup ============ */
|
||||||
|
if(!SD.begin()){
|
||||||
|
Serial.println("Card Mount Failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uint8_t cardType = SD.cardType();
|
||||||
|
|
||||||
|
if(cardType == CARD_NONE){
|
||||||
|
Serial.println("No SD card attached");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.print("SD Card Type: ");
|
||||||
|
if(cardType == CARD_MMC){
|
||||||
|
Serial.println("MMC");
|
||||||
|
} else if(cardType == CARD_SD){
|
||||||
|
Serial.println("SDSC");
|
||||||
|
} else if(cardType == CARD_SDHC){
|
||||||
|
Serial.println("SDHC");
|
||||||
|
} else {
|
||||||
|
Serial.println("UNKNOWN");
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
|
||||||
|
Serial.printf("SD Card Size: %lluMB\n", cardSize);
|
||||||
|
|
||||||
|
listDir(SD, "/", 0);
|
||||||
|
readFile(SD, "/index.html");
|
||||||
|
Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
|
||||||
|
Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
|
||||||
|
|
||||||
|
|
||||||
|
/* ============ IR Setup ============ */
|
||||||
|
IrSender.begin(IR_SEND_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN); // Specify send pin and enable feedback LED at default feedback LED pin
|
||||||
|
Serial.println();
|
||||||
|
Serial.print(F("Send IR signals at pin "));
|
||||||
|
Serial.println(IR_SEND_PIN);
|
||||||
|
|
||||||
|
/* ============ Webserver Setup ============ */
|
||||||
|
|
||||||
// We start by connecting to a WiFi network
|
// We start by connecting to a WiFi network
|
||||||
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
@@ -50,11 +202,6 @@ void setup() {
|
|||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
IrSender.begin(IR_SEND_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN); // Specify send pin and enable feedback LED at default feedback LED pin
|
|
||||||
Serial.println();
|
|
||||||
Serial.print(F("Send IR signals at pin "));
|
|
||||||
Serial.println(IR_SEND_PIN);
|
|
||||||
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println("WiFi connected.");
|
Serial.println("WiFi connected.");
|
||||||
Serial.println("IP address: ");
|
Serial.println("IP address: ");
|
||||||
@@ -63,8 +210,6 @@ void setup() {
|
|||||||
server.begin();
|
server.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
WiFiClient client = server.available(); // listen for incoming clients
|
WiFiClient client = server.available(); // listen for incoming clients
|
||||||
|
|
||||||
@@ -87,16 +232,8 @@ void loop() {
|
|||||||
client.println();
|
client.println();
|
||||||
|
|
||||||
// the content of the HTTP response follows the header:
|
// the content of the HTTP response follows the header:
|
||||||
client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 13 on.<br>");
|
client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 13 on.<br>Click <a href=\"/L\">here</a> to turn the LED on pin 13 off.<br><a href=\"/power\">power</a><br><a href=\"/play\">play</a><br><a href=\"/pause\">pause</a><br><a href=\"/stop\">stop</a><br><a href=\"/prev\">previous track</a><br><a href=\"/next\">next track</a><br>");
|
||||||
client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 13 off.<br>");
|
client.print("<form action=\"/api/control-deck\" method=\"post\"><button name=\"control\" type=\"submit\" value=\"play\">play</button></form><br><form action=\"/api/control-deck\" method=\"get\"><input name=\"textinput\" type=\"text\" /><button type=\"submit\">send</button></form><br>");
|
||||||
client.print("<a href=\"/power\">power</a><br>");
|
|
||||||
client.print("<a href=\"/play\">play</a><br>");
|
|
||||||
client.print("<a href=\"/pause\">pause</a><br>");
|
|
||||||
client.print("<a href=\"/stop\">stop</a><br>");
|
|
||||||
client.print("<a href=\"/prev\">previous track</a><br>");
|
|
||||||
client.print("<a href=\"/next\">next track</a><br>");
|
|
||||||
client.print("<form action=\"/api/control-deck\" method=\"post\"><button name=\"control\" type=\"submit\" value=\"play\">play</button></form><br>");
|
|
||||||
client.print("<form action=\"/api/control-deck\" method=\"get\"><input name=\"textinput\" type=\"text\" /><button type=\"submit\">send</button></form><br>");
|
|
||||||
|
|
||||||
// The HTTP response ends with another blank line:
|
// The HTTP response ends with another blank line:
|
||||||
client.println();
|
client.println();
|
||||||
@@ -148,9 +285,4 @@ void loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendButton(int command)
|
|
||||||
{
|
|
||||||
Serial.println(command);
|
|
||||||
Serial.flush();
|
|
||||||
IrSender.sendSony(0xF, command, 2, 12);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user