๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ก EE's DEV/IoT

[IoT] ESP32 - ์ด๋ฏธ์ง€ FTP ์„œ๋ฒ„์— ์—…๋กœ๋“œ ํ•˜๊ธฐ

by Danna 2021. 2. 2.
728x90
728x90

์ง€๋‚œ๋ฒˆ ๊ธ€์—์„œ ์ž‘์„ฑํ–ˆ๋˜ ๊ฒƒ ์ฒ˜๋Ÿผ, ESP32-Cam ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์˜ˆ์ œ๋กœ ์–ผ๊ตด ๊ฒ€์ถœ, ์ธ์‹, ๋“ฑ๋กํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์ œ๊ณต๋œ๋‹ค. ํ•˜์ง€๋งŒ, ์กธ์—…์ž‘ํ’ˆ ์ฃผ์ œ๋Š” "๋งˆ์Šคํฌ ์œ ๋ฌด์™€ ์ฒด์˜จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌด์ธ ์ถœ์ž… ๊ด€๋ฆฌ ์‹œ์Šคํ…œ" ์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋งˆ์Šคํฌ ์ฐฉ์šฉ ์œ ๋ฌด๋ฅผ ๊ฒ€์ถœํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ ํƒœ์Šคํฌ์˜€๋‹ค.

 

ESP32 ์—์„œ ์ œ๊ณต๋˜๋Š” ์˜ˆ์ œ๋กœ๋Š” ๋งˆ์Šคํฌ๋ฅผ ์ฐฉ์šฉํ•œ ์–ผ๊ตด์„ ์ธ์‹ํ•  ์ˆ˜ ์—†์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ–ˆ๋‹ค. ๋งˆ์Šคํฌ ์ฐฉ์šฉ ์œ ๋ฌด๋ฅผ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•ด ๋จธ์‹ ๋Ÿฌ๋‹์„ ์ด์šฉํ•˜๋ฏ€๋กœ Ubuntu ์„œ๋ฒ„๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ „์†กํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์•ผํ–ˆ๋‹ค.

 

ESP32 ์—์„œ FTP ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ESP32_FTPClient ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. FTP ์„œ๋ฒ„์— ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑํ•˜๊ธฐ, ํŒŒ์ผ ์“ฐ๊ธฐ, ์ด๋ฏธ์ง€ ์“ฐ๊ธฐ ๊ธฐ๋Šฅ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , ์˜ˆ์ œํŒŒ์ผ๋กœ ์ œ๊ณต๋œ๋‹ค.

 

ldab/ESP32_FTPClient

An FTP-Client for the ESP32. Contribute to ldab/ESP32_FTPClient development by creating an account on GitHub.

github.com

ESP32-Cam ์„ ์ด์šฉํ•ด์„œ ์‚ฌ์ง„์„ ์ดฌ์˜ํ•˜๊ณ  FTP ์„œ๋ฒ„์— ์—…๋กœ๋“œ ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • FTPClient ์™€ ๊ด€๋ จ๋œ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋“ค๋งŒ ๋‚จ๊ฒจ๋‘” ๊ฒƒ์ด๋ผ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ์ ์ ˆํ•˜๊ฒŒ ์ถ”๊ฐ€ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค!

  • ํ˜„์žฌ ์‚ฌ์ง„์ด ์ดฌ์˜๋œ ์‹œ๊ฐ„์œผ๋กœ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ NTPClient ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ–ˆ๋‹ค. 

ESP32_FTPClient ํด๋ž˜์Šค์˜ instance ์ธ ftp ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

  • FTP server ์˜ ip ํ˜น์€ domain ์ฃผ์†Œ, server ์˜ user id, password ๋ฅผ ์ธ์ž๋กœ ๋„˜๊ธด๋‹ค.

  • setup ํ•จ์ˆ˜ ๋‚ด์—์„œ Wifi, ์นด๋ฉ”๋ผ ์„ธํŒ…์„ ์ง„ํ–‰ํ•œ ํ›„ beginFTP ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด FTP ์ „์†ก์„ ์‹œ์ž‘ํ–ˆ๋‹ค.

  • loop ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ setup ํ•จ์ˆ˜ ๋‚ด์—์„œ ์‹คํ–‰ํ•œ ์ด์œ ๋Š” ๋ชจ์…˜ ์„ผ์„œ๋กœ ์ธํ•œ ๋ถ€ํŒ…์„ ์„ค์ •ํ•ด๋‘์—ˆ๊ณ , ๋ถ€ํŒ… ์ดํ›„ ๋™์ž‘๋“ค์„ ์ˆ˜ํ–‰ํ•œ ํ›„ ๋‹ค์‹œ DeepSleep ๋ชจ๋“œ๋กœ ์ง„์ž…ํ•˜๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค. (์ฝ”๋“œ ๋‚ด์—์„œ๋Š” ์ƒ๋žต๋˜์–ด ์žˆ๋‹ค.)

์ด๋ฏธ์ง€๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•œ beginFTP ํ•จ์ˆ˜

  • ๋จผ์ € ftp.OpenConnection() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ์„ ์‹œ๋„ํ•œ๋‹ค. 

  • ํ˜„์žฌ ์‹œ๊ฐ„์„ ์–ป๊ธฐ ์œ„ํ•ด getDateTime() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ์ด๋ฏธ์ง€ ํŒŒ์ผ๋ช…์„ ์ง€์ •ํ•œ๋‹ค. 

    • ํ•ด๋‹น ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์€ 2020_10_4_14:55:55 ๊ฐ™์€ ํ˜•ํƒœ๊ฐ€ ๋˜๋„๋ก ์ž‘์„ฑํ–ˆ๋‹ค.

  • ์นด๋ฉ”๋ผ๋กœ๋ถ€ํ„ฐ ์‚ฌ์ง„์„ ์ดฌ์˜ํ•˜๊ณ , FTP ์„œ๋ฒ„์— ์—…๋กœ๋“œํ•œ๋‹ค.

    • esp_camera_fb_get() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์บก์ณ๋œ ์ด๋ฏธ์ง€๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

    • FTPClient ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Type I ๋Š” ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์˜๋ฏธํ•œ๋‹ค.

  • ftp.CloseConnection() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ์„ ์ข…๋ฃŒํ•œ๋‹ค.

#include <ESP32_FTPClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

/* FTP server settings */
char ftp_server[] = "ip";
char ftp_user[]   = "id";
char ftp_pass[]   = "pw";
ESP32_FTPClient ftp (ftp_server,ftp_user,ftp_pass, 5000, 2);

/* Define NTP Client to get time */
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "kr.pool.ntp.org");

void setup() { 
    /* Boot :: put your setup code here, to run once: */
    Serial.begin(115200);
    beginWiFi();
    configCamera();
    beginFTP();
}

String getDateTime() {
    timeClient.begin();
    timeClient.setTimeOffset(32400); // GMT+9
    timeClient.update();

    // Get a time structure
    unsigned long epochTime = timeClient.getEpochTime();
    struct tm *ptm = gmtime ((time_t *)&epochTime);

    int monthDay = ptm->tm_mday;
    int currentMonth = ptm->tm_mon+1;
    int currentYear = ptm->tm_year+1900;
    String currentDate = String(currentYear) + "_" + String(currentMonth) + "_" + String(monthDay);
    String formattedTime = timeClient.getFormattedTime();

    // Print complete date: 2020_10_4_14:55:55
    String currentDateTime = currentDate + "_" + formattedTime;
    //Serial.print("Current date time: "); Serial.println(currentDateTime);
    return currentDateTime;
}

void beginFTP(){  
    camera_fb_t * fb = NULL;
    ftp.OpenConnection();

    // Image file name
    String currentDateTime = getDateTime();
    String tem_name = "cam_" + currentDateTime + ".jpg";
    const char *fname = tem_name.c_str();

    // Upload image
    fb = esp_camera_fb_get();  
    ftp.InitFile("Type I");
    ftp.NewFile(fname);
    ftp.WriteData(fb->buf, fb->len);
    ftp.CloseFile();
    Serial.print("uploaded:"); 
    Serial.println(tem_name);

    ftp.CloseConnection();
    esp_camera_fb_return(fb);
}

void loop() {}

 

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ์ด๋ฏธ์ง€๊ฐ€ ์—†์–ด์„œ ์˜ˆ์ œ๋ฅผ ๋Œ๋ฆฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒŒ์ผ ์—…๋กœ๋“œ๊ฐ€ ์ง„ํ–‰๋œ๋‹ค. TMP006 ์ด๋ผ๋Š” ์˜จ๋„์„ผ์„œ ์ธก์ •๊ณผ ํ•จ๊ป˜ FTP ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฝ์šฐ์ด๋‹ค. 


์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ

728x90
728x90