Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FS.h"
- #include "SD.h"
- #include <SPI.h>
- #define AOUT_PIN1 36 // ESP32 pin GPIO36 (ADC0) that connects to AOUT pin of moisture sensor
- #define AOUT_PIN2 39 // ESP32 pin GPIO36 (ADC3) that connects to AOUT pin of moisture sensor
- #define SD_CS 5
- String dataMessage;
- int moisture_threshold = 2500;
- int ledPin1 = 2;
- int ledPin2 = 4;
- int value1 = 0;
- int value2 = 0;
- void setup() {
- Serial.begin(9600);
- // set the ADC attenuation to 11 dB (up to ~3.3V input)
- analogSetAttenuation(ADC_11db);
- pinMode(ledPin1, OUTPUT);
- pinMode(ledPin2, OUTPUT);
- // Initialize SD card
- SD.begin(SD_CS);
- if(!SD.begin(SD_CS)) {
- Serial.println("Card Mount Failed");
- return;
- }
- uint8_t cardType = SD.cardType();
- if(cardType == CARD_NONE) {
- Serial.println("No SD card attached");
- return;
- }
- Serial.println("Initializing SD card...");
- if (!SD.begin(SD_CS)) {
- Serial.println("ERROR - SD card initialization failed!");
- return; // init failed
- }
- File file = SD.open("/data.txt");
- if(!file) {
- Serial.println("File doens't exist");
- Serial.println("Creating file...");
- writeFile(SD, "/data.txt", "ESP32 and SD Card \r\n");
- }
- else {
- Serial.println("File already exists");
- }
- file.close();
- }
- void loop() {
- readMsensors();
- logSDCard();
- delay(2000); //Wait for 2 seconds before writing the next data
- }
- void logSDCard() {
- dataMessage = "Moisture Value 1 = " + String(value1) + ". Moisture Value 2 = " + String(value2) + ".\n";
- Serial.print("Save data: ");
- Serial.println(dataMessage);
- appendFile(SD, "/data.txt", dataMessage.c_str());
- }
- // Write to the SD card (DON'T MODIFY THIS FUNCTION)
- void writeFile(fs::FS &fs, const char * path, const char * message) {
- Serial.printf("Writing file: %s\n", path);
- File file = fs.open(path, FILE_WRITE);
- if(!file) {
- Serial.println("Failed to open file for writing");
- return;
- }
- if(file.print(message)) {
- Serial.println("File written");
- } else {
- Serial.println("Write failed");
- }
- file.close();
- }
- // Append data to the SD card (DON'T MODIFY THIS FUNCTION)
- void appendFile(fs::FS &fs, const char * path, const char * message) {
- Serial.printf("Appending to file: %s\n", path);
- File file = fs.open(path, FILE_APPEND);
- if(!file) {
- Serial.println("Failed to open file for appending");
- return;
- }
- if(file.print(message)) {
- Serial.println("Message appended");
- } else {
- Serial.println("Append failed");
- }
- file.close();
- }
- void readMsensors() {
- value1 = analogRead(AOUT_PIN1); // read the analog value from sensor
- value2 = analogRead(AOUT_PIN2);
- Serial.print("Moisture value 1: ");
- Serial.print(value1);
- Serial.print(". Moisture value 2: ");
- Serial.println(value2);
- if (value1 > moisture_threshold) {
- digitalWrite(ledPin1, HIGH);
- } else {
- digitalWrite(ledPin1, LOW);
- }
- if (value2 > moisture_threshold) {
- digitalWrite(ledPin2, HIGH);
- } else {
- digitalWrite(ledPin2, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment