Teensy 4.1 Littelfs Down/Upload/change files over Network (FtpServer)

jakespeed

Member
Hello,
I have a question how do I get files (HTML) for the web server on the LittleFS that I created in Flash?

It should run over network, but the existing libraries are not designed for Teensy 4.1. I am currently working with QNethernet, where network connectivity is not the problem.

I was able to run this FTP server "https://github.com/xreef/SimpleFTPServer" with the SD card, unfortunately not with littlefs.

How do you get your files on SD or Littelfs over network ?
greetings
Jake
 
Hi, jakespeed,
Can you tell me better which kind of hardware you have? I Will try to add the LittleFS version.
And what configuration you use with your device.
Bye Renzo
 
Hello Renzo,
Happy new Year! And thanx for reply.
I am using the Teensy 4.1 with the Onboard Network Connector. I run Arduino 1.8.19 and Teensyduino 1.56 all the latest versions.
I played with the NativeEternet and QNethernet libs both are working well.
Tank you for helping me out.
greedings
Jake
 
I wonder if it's possible to copy files from the SD card to PROGMEM? (now that PROGMEM doesn't get wiped on each upload)

I too would love to upload some small png files, to use in a website hosted on the Teensy 4.1. Is there an equivalent to ESP SPIFFS 'data' folder?
 
There is not a way for the HOST computer to copy files onto LittleFS during build or other.

Maybe this can help?

I wrote a variation on PrintDirectory() that instead follows the directory tree and transfers files from SD to LFS media, or form LFS media to SD when called.

It works - but wasn't written to be clean outside of the testing in progress.
> it seems makeRootDirsTest() is just extra work? And could be removed or ignored. Seems it is marked BUGBUG and only called on copy of PROG to SD.

It was written to work on LFS_PROG in this sketch: T4LockBeta/tree/main/PROG
That is a variation of the LFS example sketch for LFS PROG FLASH.

On startup of PROG.INO, I had this code to invoke it where a #define indicated which way the copy was going:
Code:
#define destLFS 1
#define destSD 2
	// xferSD ( destSD ); // do MediaTransfer LFS TO SD
	[B]xferSD [/B]( destLFS ); // do MediaTransfer SD TO LFS

It looks like this in xferdiffs,ino - that share GLOBALS from PROG.INO:
Code:
#include <SD.h>

void [B]xferSD[/B]( int copyType ) { // do MediaTransfer with SD
#define destLFS 1
#define destSD 2

	static bool initSD = true;
	Serial.print("Initializing SD card...");
	if ( initSD && !SD.begin(BUILTIN_SDCARD)) { // see if the card is present and can be initialized:
		Serial.println("\n\n SD Card failed, or not present - Cannot do Xfer\n");
	}
	else {
		initSD = false;
		Serial.println("card initialized.\n\n");
		if ( copyType == destSD ) {
			// char szSDdir[] = "/"; // COPY to SD ROOT
			char szSDdir[] = "LFS_CPY/"; // COPY to SD subdirectory
			if ( '/' != szSDdir[0] )
				SD.mkdir( szSDdir );
			[COLOR="#FF0000"][B]makeRootDirsTest[/B][/COLOR](); // BUGBUG DEBUG make extra subdirs and files to show function
			Serial.println("\n STARTING :: LittleFS copy to SD card XFER ...\n\n");
			mediaTransfer( myfs.open("/"), szSDdir, destSD ); // TOO SD
			Serial.println("\n LittleFS copy to SD card XFER COMPLETE.\n\n");
		}
		else {
			char szLFSdir[] = "/";
			char szSDdir[] = "LFS_CPY";
			Serial.println("\n STARTING :: SD card copy to LittleFS XFER ...\n\n");
			mediaTransfer( SD.open(szSDdir), szLFSdir, destLFS );	// FROM SD
			Serial.println("\n SD card copy to LittleFS XFER COMPLETE.\n\n");
		}
	}
}

void mediaTransfer(File dir, char* szDir, int destMedia) {
	char szNewDir[36];
	while (true) {
		File entry =  dir.openNextFile();
		if (! entry) {
			break;
		}
		if (entry.isDirectory()) {
			strcpy( szNewDir, szDir);
			if ( destMedia == destLFS )
				myfs.mkdir( szNewDir );
			else
				SD.mkdir( szNewDir );
			strcat( szNewDir, entry.name());
			if ( destMedia == destLFS ) myfs.mkdir( szNewDir );
			else SD.mkdir( szNewDir );
			strcat( szNewDir, "/");
			mediaTransfer(entry, szNewDir, destMedia);
		} else {
			uint64_t fileSize, sizeCnt = 0, xfSize = 1;
			char mm[512];
			strcpy( szNewDir, szDir);
			strcat( szNewDir, entry.name() );
			File dataFile;
			if ( destMedia == destLFS ) {
				dataFile = myfs.open(szNewDir, FILE_WRITE_BEGIN);
			}
			else {
				dataFile = SD.open(szNewDir, FILE_WRITE_BEGIN);
			}
			if ( !dataFile )
				Serial.print("\td_FILE: NOT open\n");
			fileSize = entry.size();
			while ( entry.available() ) {
				if ( fileSize < sizeCnt ) break;
				if ( fileSize - sizeCnt >= sizeof(mm) ) xfSize = sizeof(mm);
				else xfSize = fileSize - sizeCnt;
				entry.read( &mm , xfSize );
				dataFile.write( &mm , xfSize );
				sizeCnt += xfSize;
			}
			if (fileSize != sizeCnt ) {
				Serial.print("\n File Size Error:: ");
				Serial.println( entry.name() );
			}
			dataFile.close();
		}
		entry.close();
	}
}

void [COLOR="#FF0000"][B]makeRootDirsTest[/B][/COLOR]() {
	char szDir[36];
	for ( uint32_t ii = 1; ii <= NUMDIRS; ii++ ) {
		sprintf( szDir, "/%lu_dir", ii );
		myfs.mkdir( szDir );
		sprintf( szDir, "/%lu_dir/aFile.txt", ii ); // BUGBUG DEBUG
		file3 = myfs.open(szDir, FILE_WRITE);
		file3.write( szDir , 12 );
		file3.close();

		sprintf( szDir, "/%lu_dir/TEST", ii ); // BUGBUG DEBUG
		myfs.mkdir( szDir ); // BUGBUG DEBUG
		sprintf( szDir, "/%lu_dir/TEST/bFile.txt", ii ); // BUGBUG DEBUG
		file3 = myfs.open(szDir, FILE_WRITE);
		file3.write( szDir , 12 );
		file3.close();
	}
	filecount = printDirectoryFilecount( myfs.open("/") );  // Set base value of filecount for disk
}
 
Good luck, post your results. If questions arise, I might be able to assist in short order.

I should clean it up and incorporate it in current testing work - but it's been 5 months since last used and sort of forgotten.
 
It should be workable. I just took that code and morphed it to ONLY copy LFS to SD for the MTP work underway in current sketch.
 
HI,
i am working now on a website to upload and Download files to LittleFs and the SDcard.
I found some intresting projekts for the ESP32.

But wy is the Littelfs api for ESP32 and Teendy so different ?
I do not understand wy that must be this way, no easy way to bring an ESP project ot the Teensy 4.x
Maybee it is very easy for the professionals.. or don't call it the same name.
greedings
Jake
 
Back
Top