#include <SD.h>
// #include <SPI.h>
// #define SD_CS_PIN 5 // Pin connected to the SD card reader's CS pin
// Name of the file to save sensor data
const char* filename = "/sensor_data.txt";
#define THERMISTORPIN 34
const double VCC = 3.3; // NodeMCU on board 3.3v vcc
const double R2 = 10000; // 10k ohm series resistor
const double adc_resolution = 4096; // 12-bit adc
const double A = 0.001129148; // thermistor equation parameters
const double B = 0.000234125;
const double C = 0.0000000876741;
#define CS_PIN 5
int menu = 0;
File root;
void setup() {
Serial.begin(115200);
Serial.print("Initializing SD card... ");
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}
Serial.println("initialization done.");
Serial.println("Files in the card:");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("");
// myFile = SD.open("test.txt", FILE_WRITE);
// Example of reading file from the card:
// File textFile = SD.open("wokwi.txt");
// if (textFile) {
// Serial.print("wokwi.txt: ");
// while (textFile.available()) {
// Serial.write(textFile.read());
// }
// textFile.close();
// } else {
// Serial.println("error opening wokwi.txt!");
// }
}
void loop() {
// Read sensor data
double Vout, Rth, temperature, adc_value;
if (Serial.available() > 0) {
menu = Serial.read();
if ((menu-48) == 2) {rfile(SD, filename);}
}
else{
adc_value = adc_resolution-analogRead(THERMISTORPIN)+0.5; // switch direction
Vout = (adc_value * VCC) / adc_resolution;
Rth = (VCC * R2 / Vout) - R2; // Formula for R2 as Pull-down: Vcc-Rth-R2-GND
/* Steinhart-Hart Thermistor Equation:
* Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3)
*/
temperature = (1 / (A + (B * log(Rth)) + (C * pow((log(Rth)),3)))); // Temperature in kelvin
temperature = temperature - 273.15; // Temperature in degree celsius
// Prepare data string
String dataString = "Temperature: " + String(temperature) + " %";
// Write data to file
wfile(SD, filename, dataString.c_str());
// Print data to Serial
Serial.println(dataString);
// Wait 2 seconds before next loop
delay(1000);
}
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
void wfile(fs::FS &fs, const char * path, const char * message) {
File file = fs.open(path, FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("Message written to file");
} else {
Serial.println("Write failed");
}
file.close();
}
void rfile(fs::FS &fs, const char * path) {
File file = fs.open(path);
Serial.println("-------");
// iff the file is available, read it:
if (file) {
while (file.available()) {
Serial.write(file.read());
}
file.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening data.txt");
}
Serial.println("-------");
}