Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By xtal
#33216 Looking at read/write I'm confused....
#1 I assume I can use file manager to save a file 123.txt
#2 How would I read data from the file I see no file open?
The data is the wprint <data>
I need to save some memory if possible so think I can store the wprint data in flash
read it into a var then WPRINT |var| or will it all need to be WPRINT |htmlvar(var)|
User avatar
By GengusKahn
#33246 Hi there,

This is for the Arduino IDE but the principle is the same....hope this is of use.......

Reading from a File........

Code: Select all   else if(sPath=="/table")
  {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.print("<html><head><title>Environmental Data Logger</title></head><body>");
          client.print("<font color=\"#000000\"><body bgcolor=\"#d0d0f0\">");
          client.print("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">");
          client.print("<h1>Environment Monitor<BR>Data Log </h1>");
          client.print("<FONT SIZE=+1><a href=\"/\">Sensor Gauges Page</a><BR><a href=\"/graph\">Sensor Graph Page</a><BR><a href=\"/diag\">Diagnostic Page</a><BR><a href=\"/lcd\">LCD TEXT Display Page</a><BR>Sample Interval  Approx. 60 Seconds<BR>");
  File logF = SPIFFS.open("/humidlog.CSV", "r");
    if (!logF) {
      lcd.clear();
      lcd.print("rOr humidlog.CSV");
    }
    String sTable;           //Constructed string to print, if math is to be performed on readings
 //                         //This should be done prior to string conversion at saving to file.
    String DsTable;        //Discarded characters used to seperate the data into single readings
    sTable = "<table style=\"width:100%\"><tr><th>Time / GMT</th><th>Date   </th><th>IntT &deg;C</th><th>IntHum &#037;</th></tr>";
    sTable += "<style>table, th, td {border: 2px solid black; border-collapse: collapse;} th, td {padding: 5px;} th {text-align: left;}</style><tr><td>";
     DsTable = logF.readStringUntil('\'');
   logF.setTimeout(0);
   while(logF.available()) {
      sTable += logF.readStringUntil('-');
      sTable += "</td><td>";
      sTable += logF.readStringUntil('\'');
      sTable += "</td><td>";
     DsTable = logF.readStringUntil(',');
      sTable += logF.readStringUntil(',');
      sTable += "</td><td>";
      sTable += logF.readStringUntil(']');
      sTable+="</td></tr>";
      client.print(sTable);
     DsTable = logF.readStringUntil('\'');
      sTable = "<tr><td>";
  }
    logF.close();
    delay(1);
    client.print("<FONT SIZE=-1>environmental.monitor.log@gmail.com<BR><FONT SIZE=-2>ESP8266 With 1602 I2C LCD, DS1307 and DHT22 Peripherals Logging to SPIFFS<BR><FONT SIZE=-2>Compiled Using ver. 1.6.5-1160-gef26c5f, built on Sep 30, 2015");
    client.println("</body></html>");
    delay(10);   
client.stop();
return;
  }


Writing to a File......

Code: Select all   hI = dht.readHumidity();  //  Do any math at this stage as the file contains strings formatted by '= String and ,=Numeric for Google Charts......
   tI = dht.readTemperature();
 lastdata="";   // This is for Google Gauges....
    lastdata += tI;
    lastdata += ",hum=";
    lastdata += hI;
  logdata = "['";      //   This is the start of the construction of the string to print to file...
if (tm.Hour <10){logdata += "0";}   
  logdata += tm.Hour;
  logdata += ":";
if (tm.Minute <10){logdata += "0";}
  logdata += tm.Minute;
  logdata += "-";
if (tm.Day <10){logdata += "0";}
  logdata += tm.Day;
  logdata += "/";
if (tm.Month <10){logdata += "0";}
  logdata += tm.Month;
  logdata += "',";
  logdata += tI;
  logdata += ",";
  logdata += hI; 
  logdata += "],"; 
File dataFile1 = SPIFFS.open("/humidlog.CSV", "a");      //  Append data to file.....
  if (dataFile1) {
    dataFile1.println(logdata);  //  Print the Data into the file
    Dfsize = dataFile1.size();  // Check the size of the file SPIFFS on a 512KB is only 64K !!
    dataFile1.close();  // close file before proceeding......
    delay(1);
      }
    else {  //    Make sure the SPIFFS did not return an error !!!
      lcd.clear();
      lcd.print("rOr humidlog.CSV");
      delay(10000);
    }