Chat freely about anything...

User avatar
By danbicks
#33960 @GengusKahn,

This is brilliant, well done buddy. I signed up for a free email account modified the routine and have it working perfectly.
How hard would it be to create a similar send mail routine, but instead to upload the contents of a SPIFFS file to a FTP server.

Well done mate, really good work.

PS: Is their an option to have the ESP to the base64 encode?

Dans
User avatar
By GengusKahn
#34200
danbicks wrote:@GengusKahn,

This is brilliant, well done buddy. I signed up for a free email account modified the routine and have it working perfectly.
How hard would it be to create a similar send mail routine, but instead to upload the contents of a SPIFFS file to a FTP server.

Well done mate, really good work.

PS: Is their an option to have the ESP to the base64 encode?

Dans



The routine is not reliable if you can try feel free....I could log in manually but not with the sketch below please try.....
For anonymous FTP use a made up name.... user name "anonymous" password "minimethere@thisthat.com", this will work with commercial ftp to allow development.

Code: Select all/*
   FTP passive client for IDE v1.0.1 and w5100/w5200
   Posted October 2012 by SurferTim
   Modified 6 June 2015 by SurferTim
chopped to death.........
*/

#include <ESP8266WiFi.h>
#include "FS.h"

// comment out next line to write to FS from FTP server
//#define FTPWRITE
const char* ssid = "ssid";
const char* password = "password";

// change to your server............... ftp.hp.com  15.217.130.34
IPAddress server( 15, 217, 130, 34 );

WiFiClient client;
WiFiClient dclient;

char outBuf[128];
char outCount;

// change fileName to your file (8.3 format!)
//char fileName[13] = "test.txt";
String fileName ="/test.txt";
void setup()
{
  Serial.begin(9600);

//  digitalWrite(10,HIGH);

  if(!SPIFFS.begin())
  {
    Serial.println(F("FS init fail"));         
  }
//File fh = SPIFFS.open(fileName, "W");
//fh.close();
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.println(".");
  }
  delay(2000);
  Serial.println(WiFi.localIP());
  Serial.println(F("Ready. Press f or r"));
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'f')
  {
    if(doFTP()) Serial.println(F("FTP OK"));
    else Serial.println(F("FTP FAIL"));
  }

  if(inChar == 'r')
  {
    readFS();   
  }

}

File fh;

byte doFTP()
{

  File fh = SPIFFS.open("/test.txt", "r");


  if(!fh)
  {
    Serial.println(F("FS open fail"));
    return 0;   
  }

#ifndef FTPWRITE 
  if(!fh.seek(0, SeekSet))
  {
    Serial.println(F("Rewind fail"));
    fh.close();
    return 0;   
  }
#endif

  Serial.println(F("FS opened"));

  if (client.connect(server,21)) {
    Serial.println(F("Command connected"));
  }
  else {
    fh.close();
    Serial.println(F("Command connection failed"));
    return 0;
  }

  if(!eRcv()) return 0;

  client.println(F("USER anonymous"));
//  delay(3000);

  //if(!eRcv()) return 0;
  client.println(F("PASS minimethere@thisthat.com"));

  if(!eRcv()) return 0;
  client.println(F("SYST"));

  if(!eRcv()) return 0;

  client.println(F("Type I"));

  if(!eRcv()) return 0;

  client.println(F("PASV"));

  if(!eRcv()) return 0;

  char *tStr = strtok(outBuf,"(,");
  int array_pasv[6];
  for ( int i = 0; i < 6; i++) {
    tStr = strtok(NULL,"(,");
    array_pasv[i] = atoi(tStr);
    if(tStr == NULL)
    {
      Serial.println(F("Bad PASV Answer"));   

    }
  }

  unsigned int hiPort,loPort;

  hiPort = array_pasv[4] << 8;
  loPort = array_pasv[5] & 255;

  Serial.print(F("Data port: "));
  hiPort = hiPort | loPort;
  Serial.println(hiPort);

  if (dclient.connect(server,hiPort)) {
    Serial.println(F("Data connected"));
  }
  else {
    Serial.println(F("Data connection failed"));
    client.stop();
    fh.close();
    return 0;
  }

#ifdef FTPWRITE
  client.print(F("STOR "));
  client.println("/test.txt");
#else
  client.print(F("RETR "));
  client.println("/test.txt");
#endif

  if(!eRcv())
  {
    dclient.stop();
    return 0;
  }

#ifdef FTPWRITE
  Serial.println(F("Writing"));

  byte clientBuf[64];
  int clientCount = 0;

  while(fh.available())
  {
    clientBuf[clientCount] = fh.read();
    clientCount++;

    if(clientCount > 63)
    {
      dclient.write(clientBuf,64);
      clientCount = 0;
    }
  }

  if(clientCount > 0) dclient.write(clientBuf,clientCount);

#else
  while(dclient.connected())
  {
    while(dclient.available())
    {
      char c = dclient.read();
      fh.write(c);     
      Serial.write(c);
    }
  }
#endif

  dclient.stop();
  Serial.println(F("Data disconnected"));

  if(!eRcv()) return 0;

  client.println(F("QUIT"));

  if(!eRcv()) return 0;

  client.stop();
  Serial.println(F("Command disconnected"));

  fh.close();
  Serial.println(F("FS closed"));
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;

  while(!client.available()) delay(1);

  respCode = client.peek();

  outCount = 0;

  while(client.available())
  { 
    thisByte = client.read();   
    Serial.write(thisByte);

    if(outCount < 127)
    {
      outBuf[outCount] = thisByte;
      outCount++;     
      outBuf[outCount] = 0;
    }
  }

  if(respCode >= '4')
  {
    efail();
    return 0; 
  }

  return 1;
}


void efail()
{
  byte thisByte = 0;

  client.println(F("QUIT"));

  while(!client.available()) delay(1);

  while(client.available())
  { 
    thisByte = client.read();   
    Serial.write(thisByte);
  }

  client.stop();
  Serial.println(F("Command disconnected"));
  fh.close();
  Serial.println(F("FS closed"));
}

void readFS()
{
 File fh = SPIFFS.open(fileName, "r");

  if(!fh)
  {
    Serial.println(F("FS open fail here"));
    return;   
  }

  while(fh.available())
  {
    Serial.write(fh.read());
  }

  fh.close();
}

User avatar
By march_seven
#34246
GengusKahn wrote:Hi there, have you tried using the SPIFFS SPI Flash File System......

http://arduino.esp8266.com/versions/1.6.5-1160-gef26c5f/doc/reference.html#file-system-object-spiffs

Code: Select all#include <ESP8266WiFi.h>
#include "FS.h"
// above needs included........
// Below are some snippets.....
  if (!SPIFFS.begin()) {
    Serial.println("SPIFFS init failed");
  }
 
    if (!SPIFFS.format()) {
      Serial.println("format failed");
    }
    Dir root = SPIFFS.openDir("/");
    int count = 0;
    while (root.next()) {
      ++count;
    }
    if (count > 0) {
      Serial.println("some files left after format");// Remember to setup Serial.....
    }

int lst=0;

        File dataFile1 = SPIFFS.open("/humidlog.CSV", "w");
             dataFile1.print("22");
             dataFile1.println(" Celsius ");
             dataFile1.close();
dataFile1 = SPIFFS.open("/humidlog.CSV", "r+");
  if (dataFile1) {
  for (lst=0;lst<2048;lst++){     
      dataFile1.print("2nd Cavity Humidity = ");
      dataFile1.print("55");
      dataFile1.print("   2nd  Cavity  Temp  = ");
      dataFile1.print("16");
      dataFile1.print(" Celsius ");
      dataFile1.print("   Bedroom Humidity = ");
      dataFile1.print("41");
      dataFile1.print("  Bedroom Temperature = ");
      dataFile1.print("22");
      dataFile1.println(" Celsius ");
  }
      dataFile1.close();
      Serial.println("Closing humidlog.CSV");

    delay(500);
      }
    else {
      Serial.println("error opening humidlog.CSV");
    }


Load the Webpage from SPIFFS....
7 - 2KB files for the Gauge + lastpres - BMP180 Pressure reading + Footer again 2KB.....
See the Output....

http://bbc.in/1PkBPYE

Code: Select all                        client.println("HTTP/1.1 200 OK");     
                        client.println("Content-Type: text/html");
                        client.println("Connection: close");
                        client.println();
      // send web page
      for (int i = 0; i < 7; ++i) {
      String name = "/presure";
      name += i;
      name += ".htm";

      File webFile = SPIFFS.open(name, "r");
      if (!webFile) {
        lcd.clear();       
        lcd.print("ror open webpage");
        while(1);
        }
             while(webFile.available()) {
             client.print(webFile.readString()); // send web page to client
             }
        }
                           client.print(lastpres);       
                      File webFile = SPIFFS.open("/presureF.htm", "r");        // open web page file
                        if (webFile) {
                           while(webFile.available()) {
                           client.print(webFile.readString()); // send web page to client
                            }
                        }



hi
i using the esp-01 which only have 1Mbyte flash。

could you please tell me how to init the spiffs ?

how could we flash the web page in the flash IC?

BR