Skip to content

Instantly share code, notes, and snippets.

@cat-in-136
Last active October 10, 2022 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cat-in-136/fc2bcd59883088c43a927cccb7b5fd93 to your computer and use it in GitHub Desktop.
Save cat-in-136/fc2bcd59883088c43a927cccb7b5fd93 to your computer and use it in GitHub Desktop.
#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>
#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() {
#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) {
Serial.println("Will Load menu binary");
updateFromFS(SD);
ESP.restart();
}
Wire.begin();
// mute speaker
M5.Speaker.begin();
M5.Speaker.mute();
#endif
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); // no affect
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment