I have a ESP32-C3_KitM1-v1.0 (SPARKFUN)with a SD card usinf SPI on pins CS=7,MOSI =6, MISO=5, CLK=4.
I am running the following code and it compiles and runs;
/* 6/15/2023 Created by combining ESP32_Down_Up_SD_WJL with DATALOGGER_H1_PROMATIC_2
6/14/2023 Downloaded ESP32_SD from https://drive.google.com/drive/folders/1CN8U-VyOMm5EoAAntuFtYXbBFIAb3Kog
SPI PINS = mosi=6,miso=5,sck=4,cs=x
I2C PINS = scl= ,sda=
*/
#include <WiFi.h> //Built-in
#include <ESP32WebServer.h> //https://github.com/Pedroalbuquerque/ESP32WebServer download and place in your Libraries folder
#include <ESPmDNS.h>
#include "CSS.h" //Includes headers of the web and de style file
#include <SD.h>
#include <SPI.h> //MOSI PIN=6,MISO PIN=5,SCK PIN=4, SS(CS) PIN=7(PIN 5 FOR SPARKFUN)
#include <Wire.h> //added for RTC i2c
//#define I2C_SDA 2 //added for RTC i2c
//#define I2C_SCL 3 //added for RTC i2c
//from DATALOGGER_H1_PROMATIC_2 thru lines 23
#include "RTClib.h"
RTC_PCF8523 rtc; //original file used RTC_DS1307 rtc
int targetPin = 3;
int target;
int count;
int counter;
int counterPin = 1;
int reset = 0;
ESP32WebServer server(80);
#define servername "MCserver" //Define the name to your server...
#define SD_pin 7 //PIN 7 FOR ESP323 S3 DEV, PIN 5 FOR SPARKFUN ESP32 Thing Plus C ``````````
bool SD_present = false; //Controls if the SD card is present or not
/********* SETUP **********/
void setup(void)
{
Serial.begin(115200);
Wire.begin(); //added for RTC i2c
//from DATALOGGER_H1_PROMATIC_2 thru lines 64
pinMode(targetPin,INPUT);
pinMode(counterPin,INPUT);
Serial.println("BOOTING");
/*********************************************
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning())
{
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2023, 2, 19, 19, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2023, 4, 11, 8, 1, 0));
***********************************************/
WiFi.softAP("MyCircuits", "12345678"); //Network and password for the access point genereted by ESP32
//Set your preferred server name, if you use "mcserver" the address would be http://mcserver.local/
if (!MDNS.begin(servername))
{
Serial.println(F("Error setting up MDNS responder!"));
ESP.restart();
}
Serial.print(F("Initializing SD card..."));
//see if the card is present and can be initialised.
//Note: Using the ESP32 and SD_Card readers requires a 1K to 4K7 pull-up to 3v3 on the MISO line, otherwise may not work
if (!SD.begin(SD_pin))
{
Serial.println(F("Card failed or not present, no SD Card data logging possible..."));
SD_present = false;
}
else
{
Serial.println(F("Card initialised... file access enabled..."));
SD_present = true;
}
/********* Server Commands **********/
server.on("/", SD_dir);
server.on("/upload", File_Upload);
server.on("/fupload", HTTP_POST,[](){ server.send(200);}, handleFileUpload);
server.begin();
Serial.println("HTTP server started");
Serial.println("FILE NAME = DATALOGGER_ESP32_PROMATIC");
}
/********* LOOP **********/
void loop(void)
{
server.handleClient(); //Listen for client connections
//from DATALOGGER_H1_PROMATIC_2 thru lines 177
counter = digitalRead(counterPin);
if (counter == HIGH)
{
Serial.println("counter == HIGH");
count = 0;
}
target = digitalRead(targetPin);
if (target == HIGH && reset == 0)
{
reset = 1;
Serial.print("RESET = ");
Serial.println(reset);
}
if (target == LOW && reset == 0)
{
return;
}
if (target == LOW)
{
Serial.println("target == LOW");
DateTime now = rtc.now();
count = count + 1;
Serial.print("Count = ");
Serial.println(count);
Serial.print(now.month(), DEC);
Serial.print('-');
Serial.print(now.day(), DEC);
Serial.print('-');
Serial.print(now.year(), DEC);
Serial.print(';');
Serial.print(now.hour(), DEC);
Serial.print('-');
Serial.print(now.minute(), DEC);
Serial.print('-');
Serial.println(now.second(), DEC);
Serial.println("opening data fiLe");
File dataFile = SD.open("HIGH_1.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile)
{
Serial.println(F("Writing to SD card"));
dataFile.print(count);
dataFile.print('-');
dataFile.print(now.month(), DEC);
dataFile.print('-');
dataFile.print(now.day(), DEC);
dataFile.print('-');
dataFile.print(now.year(), DEC);
dataFile.print('-');
dataFile.print(now.hour(), DEC);
dataFile.print('-');
dataFile.print(now.minute(), DEC);
dataFile.print('-');
dataFile.print(now.second(), DEC);
//dataFile.print(':');
dataFile.print("\n"); //new line
dataFile.close();
delay(5000);
}
// if the file isn't open, pop up an error:
else
{
Serial.println("error opening HIGH_1.txt");
}
}
}
/********* FUNCTIONS **********/
//Initial page of the server web, list directory and give you the chance of deleting and uploading
void SD_dir()
{
if (SD_present)
{
//Action acording to post, dowload or delete, by MC 2022
if (server.args() > 0 ) //Arguments were received, ignored if there are not arguments
{
Serial.println(server.arg(0));
String Order = server.arg(0);
Serial.println(Order);
if (Order.indexOf("download_")>=0)
{
Order.remove(0,9);
SD_file_download(Order);
Serial.println(Order);
}
if ((server.arg(0)).indexOf("delete_")>=0)
{
Order.remove(0,7);
SD_file_delete(Order);
Serial.println(Order);
}
}
File root = SD.open("/");
if (root) {
root.rewindDirectory();
SendHTML_Header();
webpage += F("<table align='center'>");
webpage += F("<tr><th>Name/Type</th><th style='width:20%'>Type File/Dir</th><th>File Size</th></tr>");
printDirectory("/",0);
webpage += F("</table>");
SendHTML_Content();
root.close();
}
else
{
SendHTML_Header();
webpage += F("<h3>No Files Found</h3>");
}
append_page_footer();
SendHTML_Content();
SendHTML_Stop(); //Stop is needed because no content length was sent
} else ReportSDNotPresent();
}
//Upload a file to the SD
void File_Upload()
{
append_page_header();
webpage += F("<h3>Select File to Upload</h3>");
webpage += F("<FORM action='/fupload' method='post' enctype='multipart/form-data'>");
webpage += F("<input class='buttons' style='width:25%' type='file' name='fupload' id = 'fupload' value=''>");
webpage += F("<button class='buttons' style='width:10%' type='submit'>Upload File</button><br><br>");
webpage += F("<a href='/'>[Back]</a><br><br>");
append_page_footer();
server.send(200, "text/html",webpage);
}
//Prints the directory, it is called in void SD_dir()
void printDirectory(const char * dirname, uint8_t levels)
{
File root = SD.open(dirname);
if(!root){
return;
}
if(!root.isDirectory()){
return;
}
File file = root.openNextFile();
int i = 0;
while(file){
if (webpage.length() > 1000) {
SendHTML_Content();
}
if(file.isDirectory()){
webpage += "<tr><td>"+String(file.isDirectory()?"Dir":"File")+"</td><td>"+String(file.name())+"</td><td></td></tr>";
printDirectory(file.name(), levels-1);
}
else
{
webpage += "<tr><td>"+String(file.name())+"</td>";
webpage += "<td>"+String(file.isDirectory()?"Dir":"File")+"</td>";
int bytes = file.size();
String fsize = "";
if (bytes < 1024) fsize = String(bytes)+" B";
else if(bytes < (1024 * 1024)) fsize = String(bytes/1024.0,3)+" KB";
else if(bytes < (1024 * 1024 * 1024)) fsize = String(bytes/1024.0/1024.0,3)+" MB";
else fsize = String(bytes/1024.0/1024.0/1024.0,3)+" GB";
webpage += "<td>"+fsize+"</td>";
webpage += "<td>";
webpage += F("<FORM action='/' method='post'>");
webpage += F("<button type='submit' name='download'");
webpage += F("' value='"); webpage +="download_"+String(file.name()); webpage +=F("'>Download</button>");
webpage += "</td>";
webpage += "<td>";
webpage += F("<FORM action='/' method='post'>");
webpage += F("<button type='submit' name='delete'");
webpage += F("' value='"); webpage +="delete_"+String(file.name()); webpage +=F("'>Delete</button>");
webpage += "</td>";
webpage += "</tr>";
}
file = root.openNextFile();
i++;
}
file.close();
}
//Download a file from the SD, it is called in void SD_dir()
void SD_file_download(String filename)
{
if (SD_present)
{
File download = SD.open("/"+filename);
if (download)
{
server.sendHeader("Content-Type", "text/text");
server.sendHeader("Content-Disposition", "attachment; filename="+filename);
server.sendHeader("Connection", "close");
server.streamFile(download, "application/octet-stream");
download.close();
} else ReportFileNotPresent("download");
} else ReportSDNotPresent();
}
//Handles the file upload a file to the SD
File UploadFile;
//Upload a new file to the Filing system
void handleFileUpload()
{
HTTPUpload& uploadfile = server.upload(); //See https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WebServer/srcv
//For further information on 'status' structure, there are other reasons such as a failed transfer that could be used
if(uploadfile.status == UPLOAD_FILE_START)
{
String filename = uploadfile.filename;
if(!filename.startsWith("/")) filename = "/"+filename;
Serial.print("Upload File Name: "); Serial.println(filename);
SD.remove(filename); //Remove a previous version, otherwise data is appended the file again
UploadFile = SD.open(filename, FILE_WRITE); //Open the file for writing in SD (create it, if doesn't exist)
filename = String();
}
else if (uploadfile.status == UPLOAD_FILE_WRITE)
{
if(UploadFile) UploadFile.write(uploadfile.buf, uploadfile.currentSize); // Write the received bytes to the file
}
else if (uploadfile.status == UPLOAD_FILE_END)
{
if(UploadFile) //If the file was successfully created
{
UploadFile.close(); //Close the file again
Serial.print("Upload Size: "); Serial.println(uploadfile.totalSize);
webpage = "";
append_page_header();
webpage += F("<h3>File was successfully uploaded</h3>");
webpage += F("<h2>Uploaded File Name: "); webpage += uploadfile.filename+"</h2>";
webpage += F("<h2>File Size: "); webpage += file_size(uploadfile.totalSize) + "</h2><br><br>";
webpage += F("<a href='/'>[Back]</a><br><br>");
append_page_footer();
server.send(200,"text/html",webpage);
}
else
{
ReportCouldNotCreateFile("upload");
}
}
}
//Delete a file from the SD, it is called in void SD_dir()
void SD_file_delete(String filename)
{
if (SD_present) {
SendHTML_Header();
File dataFile = SD.open("/"+filename, FILE_READ); //Now read data from SD Card
if (dataFile)
{
if (SD.remove("/"+filename)) {
Serial.println(F("File deleted successfully"));
webpage += "<h3>File '"+filename+"' has been erased</h3>";
webpage += F("<a href='/'>[Back]</a><br><br>");
}
else
{
webpage += F("<h3>File was not deleted - error</h3>");
webpage += F("<a href='/'>[Back]</a><br><br>");
}
} else ReportFileNotPresent("delete");
append_page_footer();
SendHTML_Content();
SendHTML_Stop();
} else ReportSDNotPresent();
}
//SendHTML_Header
void SendHTML_Header()
{
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send(200, "text/html", ""); //Empty content inhibits Content-length header so we have to close the socket ourselves.
append_page_header();
server.sendContent(webpage);
webpage = "";
}
//SendHTML_Content
void SendHTML_Content()
{
server.sendContent(webpage);
webpage = "";
}
//SendHTML_Stop
void SendHTML_Stop()
{
server.sendContent("");
server.client().stop(); //Stop is needed because no content length was sent
}
//ReportSDNotPresent
void ReportSDNotPresent()
{
SendHTML_Header();
webpage += F("<h3>No SD Card present</h3>");
webpage += F("<a href='/'>[Back]</a><br><br>");
append_page_footer();
SendHTML_Content();
SendHTML_Stop();
}
//ReportFileNotPresent
void ReportFileNotPresent(String target)
{
SendHTML_Header();
webpage += F("<h3>File does not exist</h3>");
webpage += F("<a href='/"); webpage += target + "'>[Back]</a><br><br>";
append_page_footer();
SendHTML_Content();
SendHTML_Stop();
}
//ReportCouldNotCreateFile
void ReportCouldNotCreateFile(String target)
{
SendHTML_Header();
webpage += F("<h3>Could Not Create Uploaded File (write-protected?)</h3>");
webpage += F("<a href='/"); webpage += target + "'>[Back]</a><br><br>";
append_page_footer();
SendHTML_Content();
SendHTML_Stop();
}
//File size conversion
String file_size(int bytes)
{
String fsize = "";
if (bytes < 1024) fsize = String(bytes)+" B";
else if(bytes < (1024*1024)) fsize = String(bytes/1024.0,3)+" KB";
else if(bytes < (1024*1024*1024)) fsize = String(bytes/1024.0/1024.0,3)+" MB";
else fsize = String(bytes/1024.0/1024.0/1024.0,3)+" GB";
return fsize;
}type or paste code here
I changed the ESP card to a ESP32_DevKitc_V4(MOUSER par# 356-ESP32-DEVKITC32E) with SPI pins CS=5, MOSI=23,MISO=19,SCK=18. and the module repeatedly reboots on startup with the following error;
20:35:16.399 -> ELF file SHA256: b700041a655d2db4
20:35:16.399 ->
20:35:16.399 -> E (318) esp_core_dump_flash: Core dump flash config is corrupted! CRC=0x7bd5c66f instead of 0x0
20:35:16.399 -> Rebooting...
20:35:16.399 -> ets Jul 29 2019 12:21:46
20:35:16.399 ->
20:35:16.399 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
20:35:16.399 -> configsip: 0, SPIWP:0xee
20:35:16.399 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
20:35:16.438 -> mode:DIO, clock div:1
20:35:16.438 -> load:0x3fff0030,len:1344
20:35:16.438 -> load:0x40078000,len:13924
20:35:16.438 -> ho 0 tail 12 room 4
20:35:16.438 -> load:0x40080400,len:3600
20:35:16.438 -> entry 0x400805f0
20:35:16.692 -> E (295) spi_flash: Detected size(4096k) smaller than the size in the binary image header(8192k). Probe failed.
20:35:16.692 ->
20:35:16.692 -> assert failed: do_core_init startup.c:326 (flash_ret == ESP_OK)
20:35:16.727 ->
20:35:16.727 ->
20:35:16.727 -> Backtrace: 0x40083b25:0x3ffe3aa0 0x4008d155:0x3ffe3ac0 0x40092eb1:0x3ffe3ae0 0x400e8376:0x3ffe3c10 0x4008322a:0x3ffe3c40 0x40079276:0x3ffe3c90 |<-CORRUPTED.
I am obviously missing something when I switch modules.
Any help appreciated.
Thanks
Bill