#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.