B4R Tutorial ESP8266 FTP Client

To complete the B4X FTP season (https://www.b4x.com/android/forum/threads/74320/#content) I've written a client in B4R.

It allows to connect to a FTP server and download or upload files.

There is no SD card involved in this example. It downloads a file and prints its content and uploads a string (array of bytes) as the file content.

The file is stored in an array of bytes (FileBuffer). Its size is set to 1000. You can increase it if you want to download or upload larger files.

This example is based on the very useful GlobalStore module: https://www.b4x.com/android/forum/threads/module-globalstore-global-objects-storage.73863/#content
Note that GlobalStore was rewritten due to a bug in the previous version.

This is not a simple example as the FTP protocol is not so simple.
It is worth going over the code that handles the server messages.
The code in AStreamControl_NewData parses the incoming data and splits it based on the end of line characters.
The last line can be incomplete so it is stored in GlobalStore and is added to the next message.


  • FTPClient.zip
    3.7 KB · Views: 1,406


Licensed User
Hi, I tried Erel's FTP client with a WeMos D1 (compatible) card. I had to change the access port of the FTPClient.ConnectToServer Command from 51042 to 21. Once the code was executed, all the logs present in the program appeared correctly. But this only happened once. Subsequent runs always stopped at the IP display (I used wifi.Connect2).
I have some questions to ask:
1) What could be the reason for this strange behavior of the sketch?
2) FTPClient.Upload ("/ FileFromESP.txt", "Hello !!!!") where do you write the file in the web space? Because at the first correct run I could not find the document.
3) Can this anomalous behavior of the sketch be due to a malfunction of the hardware card?
Thank you all for your kind cooperation.


Licensed User
Hi, I'm making some progress in understanding how FTP CLIENT works. I used FTPClient.ConnectToServer2 and it works for me, unlike FTPClient.ConnectToServer. I ran the code I attached. I cannot see the data in both Download and UpLoad. Below I see the log messages I get. I can't understand why I get the following:

Here is the test code:

FTP Client Test (B4R):
ub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Dim SSID As String = "xxx"
    Dim PWD As String = "yyy"
End Sub

Private Sub AppStart
    If wifi.Connect2(SSID,PWD) Then
        Log("Connected to wireless network.")
        Log("My ip: ", wifi.LocalIp)
        Log("Failed to connect.")
    End If
End Sub

Private Sub Connect(u As Byte)
    FTPClient.ConnectToServer2("www.orologissimi.it", 21, "user", "password")
End Sub

Public Sub FTP_LogIn (Success As Boolean)
    Log("Logged in: ", Success)
    If Success Then
        Log("Failed to connect.")
        'CallSubPlus("Connect", 1000, 0)
    End If
End Sub

Public Sub FTP_DownloadComplete (Success As Boolean, Path() As Byte, Data() As Byte)
    Log("Download complete: ", Success, ", path: ", Path)
    If Success Then
    End If
    FTPClient.Upload("/download/FileFromESP.txt", "Hello!!!!")
End Sub

Public Sub FTP_UploadComplete (Success As Boolean, Path() As Byte)
    Log("Upload complete: ", Success, ", path: ", Path)
End Sub

Thanks for your kind collaboration.


Licensed User
Thanks for the reply.
I tried again following your advice and using port 21 first and then port 51042.
The command line is:
FTPClient.ConnectToServer (Array As Byte (185, 205, 40, 99), 51042, "xxx", "xxx")

I get the following logs:


  • PORTA-51042.jpg
    35.2 KB · Views: 108
  • PORTA-21.jpg
    71.6 KB · Views: 101