Skip to content

Commit 2a597f0

Browse files
committed
upd
1 parent 72a23f0 commit 2a597f0

File tree

22 files changed

+1196
-92
lines changed

22 files changed

+1196
-92
lines changed

README.md

Lines changed: 163 additions & 23 deletions
Large diffs are not rendered by default.

examples/OTA_spiffs/OTA_spiffs.ino

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
Пример обновления прошивки и SPIFFS из чата.
3+
Файл для FS генерируется через плагин для IDE:
4+
- esp32 data uploader https://github.com/lorol/arduino-esp32fs-plugin
5+
- esp8266 data uploader https://github.com/earlephilhower/arduino-esp8266littlefs-plugin
6+
При нажатии кнопки Data Upload (и отключенной плате) снизу в логе находим путь к папке билда. Например:
7+
- C:\Users\Alex\AppData\Local\Temp\arduino_build_232786
8+
оттуда берём bin файл с датой (spiffs). Закидываем в чат
9+
Если зайти на IP платы - там будет простенький файловый менеджер для проверки
10+
*/
11+
12+
#define WIFI_SSID "login"
13+
#define WIFI_PASS "pass"
14+
#define BOT_TOKEN "2654326546:asjhAsfAsfkllgUsaOuiz_axfkj_AsfkjhB"
15+
#define CHAT_ID "123456789"
16+
17+
#include <FastBot.h>
18+
FastBot bot(BOT_TOKEN);
19+
20+
// для проверки файлов (веб-файловый менеджер)
21+
#include <LittleFS.h>
22+
#ifdef ESP8266
23+
#include <ESP8266WebServer.h>
24+
ESP8266WebServer server(80);
25+
#else
26+
#include <WebServer.h>
27+
WebServer server(80);
28+
#endif
29+
30+
void setup() {
31+
connectWiFi();
32+
Serial.println(WiFi.localIP());
33+
bot.attach(newMsg);
34+
setWebface();
35+
}
36+
37+
// обработчик сообщений
38+
void newMsg(FB_msg& msg) {
39+
// выводим всю информацию о сообщении
40+
Serial.println(msg.toString());
41+
42+
// Обновить, если прислали bin файл.
43+
// В данном случае я вручную проверяю, к какому типу
44+
// относится загруженный бинарник:
45+
// если имя файла содержит mklittlefs (esp8266) или spiffs (esp32) -
46+
// даю команду на обновление SPIFFS, иначе - обновляем прошивку
47+
if (msg.OTA) {
48+
if (msg.fileName.indexOf("mklittlefs") > 0 ||
49+
msg.fileName.indexOf("spiffs") > 0) {
50+
bot.updateFS(); // обновить spiffs
51+
} else {
52+
bot.update(); // обновить прошивку
53+
}
54+
}
55+
}
56+
57+
void loop() {
58+
bot.tick();
59+
server.handleClient();
60+
}
61+
62+
// веб-сервер для проверки файлов (файловый менеджер)
63+
void setWebface() {
64+
// запускаем FS для проверки
65+
if (!LittleFS.begin()) {
66+
Serial.println("FS Error");
67+
return;
68+
}
69+
70+
server.begin();
71+
server.on("/", []() {
72+
String str;
73+
str += F("<!DOCTYPE html>\n");
74+
str += F("<html><body>\n");
75+
76+
#ifdef ESP8266
77+
Dir dir = LittleFS.openDir("/");
78+
while (dir.next()) {
79+
if (dir.isFile()) {
80+
str += F("<a href=\"/");
81+
str += dir.fileName();
82+
str += F("\">");
83+
str += dir.fileName();
84+
str += F("</a><br>\n");
85+
}
86+
}
87+
#else
88+
File root = LittleFS.open("/");
89+
File file = root.openNextFile();
90+
while (file) {
91+
if (!file.isDirectory()) {
92+
str += F("<a href=\"/");
93+
str += file.name();
94+
str += F("\">");
95+
str += file.name();
96+
str += F("</a><br>\n");
97+
}
98+
file = root.openNextFile();
99+
}
100+
#endif
101+
102+
str += F("</body></html>");
103+
server.send(200, "text/html", str);
104+
});
105+
106+
// выбор файла (url - имя файла) - выводит файл в браузере
107+
server.onNotFound([]() {
108+
File file = LittleFS.open(server.uri(), "r");
109+
if (!file) {
110+
server.send(200, "text/plain", "Error");
111+
return;
112+
}
113+
server.streamFile(file, "text/plain");
114+
file.close();
115+
});
116+
}
117+
118+
void connectWiFi() {
119+
delay(2000);
120+
Serial.begin(115200);
121+
Serial.println();
122+
123+
WiFi.begin(WIFI_SSID, WIFI_PASS);
124+
while (WiFi.status() != WL_CONNECTED) {
125+
delay(500);
126+
Serial.print(".");
127+
if (millis() > 15000) ESP.restart();
128+
}
129+
Serial.println("Connected");
130+
}

examples/OTA_spiffs/data/text3.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is text 3

examples/OTA_spiffs/data/text4.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is text 4

examples/editFile/data/test.png

15.6 KB
Loading

examples/editFile/data/test2.png

51.2 KB
Loading

examples/editFile/editFile.ino

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// тест изменения файла в чате (меняем одну картинку на другую)
2+
// загрузи данные в SPIFFS (лежат в папке data)
3+
// - esp32 data uploader https://github.com/lorol/arduino-esp32fs-plugin
4+
// - esp8266 data uploader https://github.com/earlephilhower/arduino-esp8266littlefs-plugin
5+
6+
#define WIFI_SSID "login"
7+
#define WIFI_PASS "pass"
8+
#define BOT_TOKEN "2654326546:asjhAsfAsfkllgUsaOuiz_axfkj_AsfkjhB"
9+
#define CHAT_ID "123456789"
10+
11+
#include <LittleFS.h>
12+
#include <FastBot.h>
13+
FastBot bot(BOT_TOKEN);
14+
15+
void setup() {
16+
connectWiFi();
17+
18+
if (!LittleFS.begin()) {
19+
Serial.println("FS Error");
20+
return;
21+
}
22+
23+
// отправляем картинку в чат, это будет сообщение с номером bot.lastBotMsg()
24+
File file = LittleFS.open("/test.png", "r");
25+
bot.sendFile(file, FB_PHOTO, "test.png", CHAT_ID);
26+
file.close();
27+
}
28+
29+
void loop() {
30+
// меняем на вторую
31+
File file;
32+
file = LittleFS.open("/test2.png", "r");
33+
bot.editFile(file, FB_PHOTO, "test2.png", bot.lastBotMsg(), CHAT_ID);
34+
file.close();
35+
36+
// снова меняем на первую
37+
file = LittleFS.open("/test.png", "r");
38+
bot.editFile(file, FB_PHOTO, "test.png", bot.lastBotMsg(), CHAT_ID);
39+
file.close();
40+
}
41+
42+
void connectWiFi() {
43+
delay(2000);
44+
Serial.begin(115200);
45+
Serial.println();
46+
47+
WiFi.begin(WIFI_SSID, WIFI_PASS);
48+
while (WiFi.status() != WL_CONNECTED) {
49+
delay(500);
50+
Serial.print(".");
51+
if (millis() > 15000) ESP.restart();
52+
}
53+
Serial.println("Connected");
54+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
Запись файлов из чата в SPIFFS при помощи LittleFS.
3+
Перейди на IP адрес платы, чтобы попасть в файловый менеджер.
4+
Закинь файл в чат, он скачается и будет доступен для просмотра
5+
*/
6+
7+
#define WIFI_SSID "login"
8+
#define WIFI_PASS "pass"
9+
#define BOT_TOKEN "2654326546:asjhAsfAsfkllgUsaOuiz_axfkj_AsfkjhB"
10+
#define CHAT_ID "123456789"
11+
12+
#include <FastBot.h>
13+
FastBot bot(BOT_TOKEN);
14+
15+
// для проверки файлов (веб-файловый менеджер)
16+
#include <LittleFS.h>
17+
18+
#ifdef ESP8266
19+
#include <ESP8266WebServer.h>
20+
#include <WiFiClientSecure.h>
21+
#include <WiFiClientSecureBearSSL.h>
22+
ESP8266WebServer server(80);
23+
#else
24+
#include <WebServer.h>
25+
WebServer server(80);
26+
#endif
27+
28+
void setup() {
29+
connectWiFi();
30+
Serial.println(WiFi.localIP());
31+
bot.attach(newMsg);
32+
setWebface();
33+
}
34+
35+
// обработчик сообщений
36+
void newMsg(FB_msg& msg) {
37+
// выводим всю информацию о сообщении
38+
Serial.println(msg.toString());
39+
40+
if (msg.isFile) { // это файл
41+
Serial.print("Downloading ");
42+
Serial.println(msg.fileName);
43+
44+
String path = '/' + msg.fileName; // вида /filename.xxx
45+
File f = LittleFS.open(path, "w"); // открываем для записи
46+
bool status = bot.downloadFile(f, msg.fileUrl);
47+
Serial.println(status ? "OK" : "Error");
48+
}
49+
}
50+
51+
void loop() {
52+
bot.tick();
53+
server.handleClient();
54+
}
55+
56+
// веб-сервер для проверки файлов (файловый менеджер)
57+
void setWebface() {
58+
// запускаем FS для проверки
59+
if (!LittleFS.begin()) {
60+
Serial.println("FS Error");
61+
return;
62+
}
63+
64+
server.begin();
65+
server.on("/", []() {
66+
String str;
67+
str += F("<!DOCTYPE html>\n");
68+
str += F("<html><body>\n");
69+
70+
#ifdef ESP8266
71+
Dir dir = LittleFS.openDir("/");
72+
while (dir.next()) {
73+
if (dir.isFile()) {
74+
str += F("<a href=\"/");
75+
str += dir.fileName();
76+
str += F("\">");
77+
str += dir.fileName();
78+
str += F("</a><br>\n");
79+
}
80+
}
81+
#else
82+
File root = LittleFS.open("/");
83+
File file = root.openNextFile();
84+
while (file) {
85+
if (!file.isDirectory()) {
86+
str += F("<a href=\"/");
87+
str += file.name();
88+
str += F("\">");
89+
str += file.name();
90+
str += F("</a><br>\n");
91+
}
92+
file = root.openNextFile();
93+
}
94+
#endif
95+
96+
str += F("</body></html>");
97+
server.send(200, "text/html", str);
98+
});
99+
100+
// выбор файла (url - имя файла) - выводит файл в браузере
101+
server.onNotFound([]() {
102+
File file = LittleFS.open(server.uri(), "r");
103+
if (!file) {
104+
server.send(200, "text/plain", "Error");
105+
return;
106+
}
107+
server.streamFile(file, "text/plain");
108+
file.close();
109+
});
110+
}
111+
112+
void connectWiFi() {
113+
delay(2000);
114+
Serial.begin(115200);
115+
Serial.println();
116+
117+
WiFi.begin(WIFI_SSID, WIFI_PASS);
118+
while (WiFi.status() != WL_CONNECTED) {
119+
delay(500);
120+
Serial.print(".");
121+
if (millis() > 15000) ESP.restart();
122+
}
123+
Serial.println("Connected");
124+
}

0 commit comments

Comments
 (0)