Categories
ESP32 ESP8266 (ESP-01/01S) Microcontrollers

Storing Data SPIFFS

Requirements

  • DHT11 Temperature and Humidity
  • USB to UART ESP01 Programmer
  • ESP8266 ESP01/01s (512KB / 1 Mbyte)
  • File System Uploader plug-in installed

Circuity

Code

#include "FS.h"
#include "DHT.h"

#define DHTPIN 2 // the digital pin we are connected to
#define DHTTYPE DHT11 // define type of DHT sensor we are using

DHT dht(DHTPIN, DHTTYPE); // create DHT object

void setup() {
  Serial.begin(115200);
  dht.begin(); // intialize DHT object
  
  // always use this to "mount" the filesystem
  bool ok = SPIFFS.begin();
  if (ok) {
    Serial.println("SPIFFS initialized successfully");
  }
  else{
    Serial.println("SPIFFS intialization error");
  }
  
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);
  //read temperature as Celcius
  float t = dht.readTemperature(); 
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  //open log.txt file 
  File f = SPIFFS.open("/log.txt", "a");
  if (!f) {
    Serial.println("file open failed");
  }
  // save temperature reading
  f.print(t);
  f.println("deg C");
  //close file
  f.close();
  delay(8000);
}

Resources

https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html

https://thebigpotatoe.github.io/Effortless-SPIFFS/

https://www.arduino.cc/reference/en/libraries/effortless-spiffs/