Skip to content

Instantly share code, notes, and snippets.

@cat-in-136
Last active October 10, 2022 15:51
Show Gist options
  • Select an option

  • Save cat-in-136/fc2bcd59883088c43a927cccb7b5fd93 to your computer and use it in GitHub Desktop.

Select an option

Save cat-in-136/fc2bcd59883088c43a927cccb7b5fd93 to your computer and use it in GitHub Desktop.

Revisions

  1. cat-in-136 revised this gist Oct 10, 2022. 1 changed file with 15 additions and 6 deletions.
    21 changes: 15 additions & 6 deletions FTPTest.cpp
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,12 @@
    #if defined(ARDUINO_ESP32_DEV)
    #include <Arduino.h>
    #include <FS.h>
    #include <SD.h>
    #include <SPI.h>
    #else
    #include <M5Stack.h>
    #include <M5StackUpdater.h>
    #endif
    #include <Preferences.h>
    #include <WiFi.h>
    #include <WiFiClient.h>
    @@ -16,8 +23,12 @@ static const String FTP_LOGIN_PASS = "esp32";
    FtpServer ftpServer;

    void setup() {
    //Serial.begin(115200); // start serial for output

    #if defined(ARDUINO_ESP32_DEV)
    Serial.begin(115200);
    if(!SD.begin()) {
    Serial.println("Card Mount Failed");
    }
    #else
    //---osmar
    M5.begin();
    if (digitalRead(BUTTON_A_PIN) == 0) {
    @@ -30,7 +41,7 @@ void setup() {
    // mute speaker
    M5.Speaker.begin();
    M5.Speaker.mute();

    #endif



    @@ -87,9 +98,7 @@ void setup() {
    for(;;) { __asm__ __volatile__ ("nop"); }
    }

    WiFi.setSleep(false);


    // WiFi.setSleep(false); // no affect

    ftpServer.setCallback([](FtpOperation ftpOperation, unsigned int freeSpace,
    unsigned int totalSpace) {
  2. cat-in-136 created this gist Jul 7, 2022.
    121 changes: 121 additions & 0 deletions FTPTest.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,121 @@
    #include <M5Stack.h>
    #include <M5StackUpdater.h>
    #include <Preferences.h>
    #include <WiFi.h>
    #include <WiFiClient.h>

    #include <SimpleFTPServer.h>

    static const String FTP_LOGIN_USER = "esp32";
    static const String FTP_LOGIN_PASS = "esp32";

    //#define WIFI_SSID
    //#define WIFI_SSID "AP_SSID"
    //#define WIFI_PASSWD "AP_PASSWD"

    FtpServer ftpServer;

    void setup() {
    //Serial.begin(115200); // start serial for output

    //---osmar
    M5.begin();
    if (digitalRead(BUTTON_A_PIN) == 0) {
    Serial.println("Will Load menu binary");
    updateFromFS(SD);
    ESP.restart();
    }
    Wire.begin();

    // mute speaker
    M5.Speaker.begin();
    M5.Speaker.mute();




    WiFiClient wifiClient;

    #ifdef WIFI_SSID
    String ssid = WIFI_SSID;
    String passwd = WIFI_PASSWD;
    #else
    Preferences preferences;
    preferences.begin("wifi-config");
    String ssid = preferences.getString("WIFI_SSID");
    String passwd = preferences.getString("WIFI_PASSWD");
    preferences.end();
    #endif

    Serial.printf("SSID:%s PASSWD:%s\n", ssid.c_str(), passwd.c_str());
    if (ssid == "" && passwd == "") {
    Serial.println("No WiFi Setting");
    for(;;) { __asm__ __volatile__ ("nop"); }
    }

    WiFi.onEvent(
    [](WiFiEvent_t event, WiFiEventInfo_t info) {
    Serial.print("WiFi connected IP address:");
    Serial.println(IPAddress(info.got_ip.ip_info.ip.addr));
    },
    SYSTEM_EVENT_STA_GOT_IP);
    WiFi.onEvent(
    [](WiFiEvent_t event, WiFiEventInfo_t info) {
    Serial.print("WiFi lost connection. Reason: ");
    Serial.println(info.disconnected.reason);
    },
    SYSTEM_EVENT_STA_DISCONNECTED);

    for (uint8_t i = 0; i < 30 * 2 * 3; i++) {
    if (i % (30 * 2) == 0) {
    WiFi.disconnect(true);
    delay(1);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid.c_str(), passwd.c_str());
    }
    delay(500);
    if (WiFi.status() == WL_CONNECTED) {
    break;
    }
    }

    if (WiFi.status() != WL_CONNECTED) {
    WiFi.disconnect(true);
    delay(1);
    WiFi.mode(WIFI_OFF);
    Serial.println("WiFi failed");
    for(;;) { __asm__ __volatile__ ("nop"); }
    }

    WiFi.setSleep(false);



    ftpServer.setCallback([](FtpOperation ftpOperation, unsigned int freeSpace,
    unsigned int totalSpace) {
    switch (ftpOperation) {
    case FTP_CONNECT:
    log_d("FTP: Connected!");
    break;
    case FTP_DISCONNECT:
    log_d("FTP: Disconnected!");
    break;
    case FTP_FREE_SPACE_CHANGE:
    log_d("FTP: Free space change, free %u of %u!\n", freeSpace,
    totalSpace);
    break;
    default:
    break;
    }
    });
    ftpServer.setTransferCallback([](FtpTransferOperation ftpOperation,
    const char *name,
    unsigned int transferredSize) {
    log_d("FTP: %s %u", name, transferredSize);
    });
    ftpServer.begin(FTP_LOGIN_USER.c_str(), FTP_LOGIN_PASS.c_str());
    }

    void loop() {
    ftpServer.handleFTP();
    }