FTP server issue in AMB82-mini

While I’m trying to connect FTP server connection with the proper ftp client functions in Arduino IDE it’s getting the same error.

Usage Fault Status:
Invalid state UsageFault, the processor has attempted to execute an instruction that makes illegal use of the EPSR

S-domain exception from Thread mode, Extended Stack frame on S-PSP
Registers Saved to stack

Stacked:
R0 = 0x70179dd7
R1 = 0x000007e6
R2 = 0x70264168
R3 = 0x00000000
R4 = 0x702642ec
R5 = 0x70264168
R6 = 0x7026416c
R7 = 0x07070707
R8 = 0x08080808
R9 = 0x09090909
R10 = 0x10101010
R11 = 0x11111111
R12 = 0x702e93b0
LR = 0x7010e48d
PC = 0x00000000
PSR = 0x60100000

Current:
LR = 0xffffffed
MSP = 0x20003fd8
PSP = 0x703b0860
xPSR = 0x60000006
CFSR = 0x00020000
HFSR = 0x00000000
DFSR = 0x00000000
MMFAR = 0x00000000
BFAR = 0x00000000
AFSR = 0x00000000
PriMask = 0x00000000
SVC priority: 0x00
PendSVC priority: 0xf0
Systick priority: 0xf0

MSP Data:
20003FD8: 70268C1C FFFFFFF9 20004000 000003E8
20003FE8: E000E000 70268C1C 07070707 70115039
20003FF8: 701150A8 21000000 55E52653 177BE8FB
20004008: FDFCD757 7FBEFFFF 04D9D54E 9B008083
20004018: F665EF7B 5F6F75FF DA2A5982 711BA701
20004028: BFFFBEF5 FFEEAFE9 3F67B587 02E69C49
20004038: DFEEFFFE A4EA9FFC 42834030 00013A00
20004048: D4F7DAAF A3A78E58 18644C12 80228070
20004058: 7AB49DC9 7E79E67F 55864C56 04490280
20004068: DFC6EF76 F276C5D3 C61401C9 B8030755
20004078: 3F6DAF0B 6A2656CD A01308E1 35225550
20004088: F9E2BE55 F76E9BCF 66D06173 0A01E101
20004098: CF3FF7F9 F5D2F5FD B4884553 0A554916
200040A8: BFE76DD5 EF397F7A 2A5F4240 44020B40
200040B8: 17AEFFC9 BDF2330D 10C10201 281A2215
200040C8: DC673EB9 F6B74B9E A5604000 DB01E2FB

PSP Data:
703B0860: 70179DD7 000007E6 70264168 00000000
703B0870: 702E93B0 7010E48D 00000000 60100000
703B0880: 7010E971 7010EA09 7017A88F 702642EC
703B0890: 7026416C 07070707 08080808 7010EA2D
703B08A0: 70265134 0000001D 702642EC 7010EA45
703B08B0: 70265134 7010EA5D 70265134 702642EC
703B08C0: 70179D0C 7010E47B 7010E615 05050505
703B08D0: 06060606 7010F2F7 04040404 70114E85
703B08E0: A5A5A5A5 A5A5A5A5 A5A5A5A5 A5A5A5A5
703B08F0: A5A5A5A5 A5A5A5A5 A5A5A5A5 A5A5A5A5
703B0900: 00000000 800004C0 99A599A5 99A599A5
703B0910: 99A599A5 99A599A5 99A599A5 99A599A5
703B0920: 703B07B0 00001772 70266638 70266638
703B0930: 703B0920 70266630 0000000A 703CD66C
703B0940: 703CD66C 703B0920 00000000 00000001
703B0950: 703A0900 6E69616D 73617420 00A5006B

== NS Dump ==
CFSR_NS = 0x00000000
HFSR_NS = 0x00000000
DFSR_NS = 0x00000000
MMFAR_NS = 0x00000000
BFAR_NS = 0x00000000
AFSR_NS = 0x00000000
MSP_NS = 0x00000000
PSP_NS = 0x00000000
NS HardFault Status Reg = 0x00000000
SCB Configurable Fault Status Reg = 0x00000000
No Back Trace!

Hi @ManiKandan,

Is it possible to send the Arduino sketch?

Thank you.

#include <Arduino.h>
#include “WiFi.h”
#include “VideoStream.h”
#include “StreamIO.h”
#include “RTSP.h”
#include “MotionDetection.h”
#include “VideoStreamOverlay.h”
#include “SD.h”
#include “AmebaFatFS.h”
#include “Base64.h”
#include “FTPClient.h”
#include “SPI.h”

#define CHANNEL 0 // Video channel for streaming & snapshot
#define BUTTON_PIN 9 // GPIO pin connected to the button
#define LED_G AMB_D24 // GPIO pin for LED flash (fixed)

char ssid = “*";
char pass[] = "
”;
int status = WL_IDLE_STATUS;

uint32_t img_addr = 0;
uint32_t img_len = 0;
int img_counter = 0;

#define FTP_SERVER “******"
#define FTP_PORT ****
#define FTP_USERNAME “*******”
#define FTP_PASSWORD "

NetBuf_t *ftpControl;
FtpClient *ftpClient;

VideoSetting config(VIDEO_D1, CAM_FPS, VIDEO_H264_JPEG, 1);
RTSP rtsp;
StreamIO videoStreamer(1, 1);
AmebaFatFS fs;
WiFiClient wifiClient;
const int chipSelect = 10;
File myFile;
// Structure to store image data in RAM
struct ImageData {
uint32_t img_addr;
uint32_t img_len;
String filepath;
};

ImageData capturedImages[10]; // Array to store metadata for up to 10 captured images
int imageIndex = 0; // Index to keep track of the current image

void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_G, OUTPUT);

// Connecting to Wi-Fi
while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(2000);
}
Serial.println("Connected to WiFi!");

// Camera setup
Camera.configVideoChannel(CHANNEL, config);
Camera.videoInit();
rtsp.configVideo(config);
rtsp.begin();
videoStreamer.registerInput(Camera.getStream(CHANNEL));
videoStreamer.registerOutput(rtsp);
if (videoStreamer.begin() != 0) {
    Serial.println("StreamIO link start failed");
}
Camera.channelBegin(CHANNEL);
Serial.println("Camera Ready for Streaming");

// FTP Client setup
ftpClient = getFtpClient();
if (checkFtpClientInitialization(ftpClient) != 0) {
    Serial.println("FTP Client initialization failed.");
    return;
}

Serial.println("Connecting to FTP server...");
if (ftpClient->ftpClientConnect(FTP_SERVER, FTP_PORT, &ftpControl) != 1) {
    Serial.println("[ERROR] FTP connection failed.");
    return;
}

Serial.println("Logging in to FTP server...");
if (ftpClient->ftpClientLogin(FTP_USERNAME, FTP_PASSWORD, ftpControl) == 1) {
    Serial.println("FTP login successful.");
    ftpClient->ftpClientMakeDir("/Images", ftpControl);
} else {
    Serial.println("[ERROR] FTP login failed.");
}

// Set FTP mode (Active or Passive)
if (ftpClient->ftpClientSetOptions) {
    ftpClient->ftpClientSetOptions(FTP_CLIENT_CONNMODE, FTP_CLIENT_PASSIVE, ftpControl);
    Serial.println("Active mode");
} else {
    Serial.println("Error: ftpClientSetOptions function not available");
}

// SD card setup
if (!fs.begin()) {
    Serial.println("[ERROR] SD Card Initialization Failed!");
    return;
}

if (!fs.exists("/Images")) {
    Serial.println("[DEBUG] Creating directory on SD card.");
    if (!fs.mkdir("/Images")) {
        Serial.println("[ERROR] Failed to create directory on SD card.");
    }
} else {
    Serial.println("[DEBUG] directory already exists on SD card.");
}

}

void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
Serial.println(“Button Pressed! Capturing Image…”);
String filepath = captureAndSaveImage();
if (filepath.length() > 0) {
uploadToFTP(filepath.c_str()); // Convert String to const char* using c_str()
}
delay(1000);
}
}

const char* captureAndSaveImage() {
// Generate file path for the captured image
static char filepath[30]; // Make it static to persist after function returns
snprintf(filepath, sizeof(filepath), “/Images/img%d.jpg”, img_counter++);

// // Open file for writing with FA_WRITE mode (without second argument)
 File file = fs.open(filepath);  // Only pass filepath, use the default mode
// if (!file) {
//     Serial.println("[ERROR] Failed to open file for writing.");
//     return "";  // Return an empty string if the file cannot be opened
// }

// Capture image
CamFlash();
Camera.getImage(CHANNEL, &img_addr, &img_len); // Capture image from camera

// Write the captured image to SD card
file.write((uint8_t *)img_addr, img_len);
file.close();

Serial.println("[INFO] Image Captured: " + String(filepath));

// Verify if the file is correctly saved by opening it for reading
File checkFile = fs.open(filepath);  // Only pass filepath for reading
if (!checkFile) {
    Serial.println("[ERROR] File was not written correctly.");
} else {
    Serial.print("[INFO] File size: ");
    Serial.println(checkFile.size());
    checkFile.close();
}

return filepath;  // Return the file path for further use

}

void uploadToFTP(const char* filepath) {
Serial.println("[INFO] Uploading file: " + String(filepath));

// Check if the file exists on the SD card
if (!fs.exists(filepath)) {
    Serial.println("[ERROR] File does not exist: " + String(filepath));
    return;
}

// Check if the file opens correctly
File file = fs.open(filepath);  // Use FA_READ for reading
if (!file) {
    Serial.println("[ERROR] Failed to open file for reading: " + String(filepath));
    return;
}

// Ensure FTP connection is valid
if (!ftpControl) {
    Serial.println("[ERROR] FTP control connection is NULL.");
    return;
}

// Ensure the directory exists on FTP server
int dirResult = ftpClient->ftpClientMakeDir("/VDP_SBA", ftpControl);
if (dirResult == 1) {
    Serial.println("[INFO] Directory /VDP_SBA ensured on FTP server.");
} else {
    Serial.println("[WARNING] Directory /VDP_SBA might already exist or creation failed.");
}

// Correct file path for FTP
String remoteFilePath = "/VDP_SBA/" + String(filepath).substring(String(filepath).lastIndexOf("/") + 1);
Serial.println("[INFO] Opening FTP file for writing: " + remoteFilePath);
Serial.println("[DEBUG] Local File Path: " + String(filepath));

// Upload the file using FTP
int putResult = ftpClient->ftpClientPut(filepath, remoteFilePath.c_str(), FTP_CLIENT_BINARY, ftpControl);

if (putResult != 1) {
    Serial.println("[ERROR] FTP file upload failed! Check file path and FTP connection.");
} else {
    Serial.println("[INFO] FTP upload completed successfully: " + remoteFilePath);
}

file.close(); // Close the file after uploading

}

void CamFlash() {
for (int i = 0; i < 5; i++) {
digitalWrite(LED_G, HIGH);
delay(100);
digitalWrite(LED_G, LOW);
delay(100);
}
}

In this code FTP server login was successful ,after image capture by button press and stored into SD card I’m accessing the file to upload in FTP server… but upload was getting fail.

Hi @ManiKandan,

How are you setting up the FTP server? I wish to replicate your FTP server setup too.

Thank you.

the ftp server with ip,username,pass and port. it will be in passive to allow ASCII and Binary formats.

Your FTP Client is running on AMB82 Mini. Where is your FTP server running?

Thank you.

FTP server running on TCP server port with passive mode.

Hi @ManiKandan,

FTP Client on AMB82 is performing a PUT request to your server. Can I check if there is such file existing on your FTP server?

PUT is best used when you are updating or replacing existing data on the server

Thank you.

Hi @Kelvin_Huang,

yes,FTP Client perform under PUT request…Here with the ino file…the problem is FTP connection was good…Once image capture and try to upload to server using put request uploading getting failed.I tired with access the file and read the size also it was doing good but uploading case only getting failed.

Hi @ManiKandan,

Your accessing of file and reading of size is on AMB82 Mini or your server? AMB82-Mini right?

Can you try to create an empty file with that same name that you tried to upload on your FTP server?

Thank you.

On AMB82-mini only I’m accessing and reading the file…
let me try with empty file/txt file to upload.

On your server side, you should create an empty file with that same name too.

That way you will be updating / replacing the data.

Thank you.

Hi @Kelvin_Huang,
No why should update or replace in server. can we do upload a new file from AMB82-mini to server… like File Transferring.

Hi @ManiKandan,

Is there any way for you to just create an empty file on your server and try?

I will try if this file creation is necessary.

Thank you.

Hi @ManiKandan,

Is it possible if you zip your current Arduino example and share, I have difficulty compiling your code in Arduino IDE?

I will try to set up a Linux FTP server and test it out.

Thank you.

Hi @Kelvin_Huang,

[INFO] Text File Created: /TextFiles/file0.txt
[INFO] Uploading file: /TextFiles/file0.txt
[WARNING] Directory /TextFiles might already exist or creation failed.
[INFO] Opening FTP file for writing: /TextFiles/file0.txt
It can’t find the file system
[ERROR] FTP file upload failed! Check file path and FTP connection.

This how I’m getting the log…I can able to connect the FTP but uploading is not happening

Hi @ManiKandan,

Can I suggest using HTTPFS to transfer the file?

Instead of AMB82 Mini being the client, there will be a HTTP file server being set up on AMB82 Mini. Then other device will be the client performing a Get request to retrieve the file.

This is a standard SDK example. https://github.com/Ameba-AIoT/ameba-rtos-pro2/blob/b7df0aae8b1213f9fc205a1557767f5092e256fd/project/realtek_amebapro2_v0_example/src/mmfv2_video_example/mmf2_video_example_av_mp4_httpfs_init.c#L4

Thank you.

archive.zip (2.4 KB)

The condition is it’s connecting with FTP server and able to create a directory also… but uploading any kind of file is getting failed.

Thanks, I will try again and let you know if I found out the issue.

Hi @ManiKandan,

Do you have a guide on how you set up your FTP server?

Thank you.