Curs 12
Curs 12
Departamentul Calculatoare
Proiectarea cu Micro-Procesoare
https://mihai.utcluj.ro/
ESP32 – moduri de funcționare Wi-Fi
Station (Client)
• Se conectează la o rețea Wi-Fi existentă
• SSID și parola trebuie cunoscute
• Poate funcționa ca și client sau server
https://randomnerdtutorials.com/esp32-access-point-ap-web-server/
2024 Cluj-Napoca 2
ESP32 – moduri de funcționare Wi-Fi
• Mod de funcționare Wi-Fi Punct de acces (Access Point – AP)
WiFi.softAP(ssid, password);
WiFi.softAP(const char* ssid, const char* password, int channel, int ssid_hidden, int
max_connection)
2024 Cluj-Napoca 3
ESP32 – moduri de funcționare Wi-Fi
• Conectarea la AP – configurație automată
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
2024 Cluj-Napoca 4
TCP: Server și client
• TCP Server
• TCP Client
WiFiClient client = server.available(); // on the server side
2024 Cluj-Napoca 5
TCP: Transfer date
client.read() // Read the next available byte received after the last call to read()
client.print(data, BASE) // optional, you can specify the base (DEC, HEX, OCT)
client.flush() // Discard any bytes that have been written to the client but not yet
read.
2024 Cluj-Napoca 6
Applicație Wi-Fi și server Web
#include <WiFi.h>
#include <WiFiAP.h>
#include <WiFiClient.h>
// MESSAGE STRINGS
const String SETUP_INIT = "SETUP: Initializing ESP32 dev board";
const String SETUP_ERROR = "!!ERROR!! SETUP: Unable to start SoftAP mode";
const String SETUP_SERVER_START = "SETUP: HTTP server started --> IP addr: ";
const String SETUP_SERVER_PORT = " on port: ";
const String INFO_NEW_CLIENT = "New client connected";
const String INFO_DISCONNECT_CLIENT = "Client disconnected";
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
const String HTTP_HEADER = "HTTP/1.1 200 OK\r\nContent-type:text/html\r\n\r\n";
const String HTML_WELCOME = "<h1>Welcome to your ESP32 Web Server!</h1>";
2024 Cluj-Napoca 7
Applicație Wi-Fi și server Web
// The default port (both TCP & UDP) of a WWW HTTP server number according to
// RFC1340 is 80
const int HTTP_PORT_NO = 80;
void setup() {
Serial.begin(9600);
if (!WiFi.softAP(SSID, PASS)) {
Serial.println(SETUP_ERROR);
// Lock system in infinite loop in order to prevent further execution
while (1)
;
}
2024 Cluj-Napoca 8
Applicație Wi-Fi și server Web
void loop() {
WiFiClient client = HttpServer.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println(INFO_NEW_CLIENT); // print a message out the serial port
String currentLine = ""; // make a String to hold request from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
const char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, that's the end of the client request
if (currentLine.length() == 0) {
// Send welcome page to the client
printWelcomePage(client);
break;
} else currentLine = "";
} else if (c != '\r') { // if not carriage return
currentLine += c; // add it to the end of the currentLine
}
}
}
// close the connection:
client.stop();
Serial.println(INFO_DISCONNECT_CLIENT);
Serial.println();
}
}
2024 Cluj-Napoca 9
Applicație Wi-Fi și server Web
2024 Cluj-Napoca 10
Cerere pagina web – pagină index
http://192.168.4.1
Welcome to the index page!
2024 Cluj-Napoca 11
Cerere pagina web – fără nume de fișier
GET / HTTP/1.1
Host: 192.168.4.1
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/120.0.0.0 Mobile Safari/537.36
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/ap
ng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Accept-Encoding: gzip, deflate
Accept-Language: ro-RO,ro;q=0.9,en-US;q=0.8,en;q=0.7,pt;q=0.6,de;q=0.5
2024 Cluj-Napoca 12
Cerere pagina web – cu nume fișier
2024 Cluj-Napoca 13
Sistemul de fișiere SPIFFS
• SPIFFS – SPI Flash File Storage
– Un sistem de fișiere pentru memoria flash a micro-controller-ului ESP32
– O parte a memoriei flash poate fi folosită pentru a stoca fișiere
– Adresa și dimensiunea memoriei flash se gasește in folder-ul ESP32 din
mediul de dezvoltare Arduino
/System/Volumes/Data/Users/admin/Library/Arduino15/packages/esp32/hard
ware/esp32/2.0.11/tools/partitions/default.csv
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x140000,
app1, app, ota_1, 0x150000,0x140000,
spiffs, data, spiffs, 0x290000,0x160000,
coredump, data, coredump,0x3F0000,0x10000,
2024 Cluj-Napoca 14
Sistemul de fișiere SPIFFS
• Se pot crea fișiere din programul ESP32
• Se pot muta fișiere de pe PC
– În acest caz trebuie creat un fișier imagine:
2024 Cluj-Napoca 15
Sistemul de fișiere SPIFFS
• După ce fișierul imagine a fost creat esptool este folosit pentru a il scrie in
memoria flash a ESP32
2024 Cluj-Napoca 16
Sistemul de fișiere SPIFFS
// list the files in the directory
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
File root = fs.open(dirname);
if(!root){
Serial.println("- failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println(" - not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : "); Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: "); Serial.print(file.name());
Serial.print("\tSIZE: "); Serial.println(file.size());
}
file = root.openNextFile();
} https://www.tutorialspoint.com/esp32_for_iot/esp32_for_iot_spiffs_storage.htm
}
2024 Cluj-Napoca 17
Sistemul de fișiere SPIFFS
String s;
File file = fs.open(path);
if(!file || file.isDirectory()){
return "";
}
while(file.available()){
s = s + (char)file.read();
}
file.close();
return s;
}
2024 Cluj-Napoca 18
Sistemul de fișiere SPIFFS
// write a String to a file
void writeFile(fs::FS &fs, const char * path, String message){
if(file.print(message)){
Serial.println("- file written");
} else {
Serial.println("- write failed");
}
file.close();
2024 Cluj-Napoca 19
Server Web folosind SPIFFS
#include "FS.h"
#include "SPIFFS.h"
#include <WiFi.h>
#include <WiFiAP.h>
#include <WiFiClient.h>
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
const String HTTP_HEADER = "HTTP/1.1 200 OK\r\nContent-type:text/html\r\n\r\n";
2024 Cluj-Napoca 20
Server Web folosind SPIFFS
// File system setting - do not format if fail
#define FORMAT_SPIFFS_IF_FAILED false
void setup(){
Serial.begin(115200);
Serial.println(webServerInfoMessage);
}
2024 Cluj-Napoca 21
Server Web folosind SPIFFS
void printPage(WiFiClient client, String page)
{
// Always start the response to the client with the proper headers
client.println(HTTP_HEADER);
if (page.indexOf("htm") >= 0)
fname = "/“ + page; // real file name if specified
2024 Cluj-Napoca 22
Server Web folosind SPIFFS
String getRequestFileName (String& request)
{
// parse request
int g = request.indexOf ("GET /");
if (g >= 0) {
String afterGet = request.substring(g+5);
if (h >= 0)
fname = afterGet.substring(0, h);
}
return fname;
}
GET / HTTP/1.1
2024 Cluj-Napoca 23
Server Web folosing SPIFFS
void loop(){
WiFiClient client = HttpServer.available(); // listen for incoming clients
if (client) { // if you get a client,
String request; // this contains the request from the client
Serial.println("New client connected"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
const char c = client.read(); // read a byte, then
request = request + c; // add it to the request string
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, end of request, send response.
if (currentLine.length() == 0) {
String fname = getRequestFileName(request);
Serial.println ("The client wants the file: "+ fname);
printPage(client, fname); // Send welcome page to the client
break;
} else currentLine = "";
} else if (c != '\r’) {
currentLine += c; // add read character to current line
}
}
}
client.stop(); // close the connection:
Serial.println("Client disconnected."); Serial.println();
}
2024 Cluj-Napoca 24
Capabilități CSS
index.html
style.css
2024 Cluj-Napoca 25
Capabilități CSS
if (page.indexOf("css") > 0)
client.println(CSS_HEADER);
else
client.println(HTTP_HEADER);
2024 Cluj-Napoca 26
Capabilități CSS
2024 Cluj-Napoca 27
Librăria ESP Async Web Sever
• ESP Async Web Server
– Ascultă după cereri de conexiune
– Încapsulează clienți noi într-un Request
– Ține evidența clienților și se ocupă de ”curățarea” memoriei
– Server-ul poate accepta noi conexiuni in timp ce deservește Request-urile existente
– Viteză mare de procesare
– API ușor de folosi, HTTP basic și authetificare Digest MD5
– Ușor de extins pentru a accepta orice tip de conținut
https://github.com/me-no-dev/ESPAsyncWebServer
https://github.com/me-no-dev/AsyncTCP
2024 Cluj-Napoca 28
Librăria ESP Async Web Sever
https://randomnerdtutorials.com/esp32-web-server-spiffs-spi-flash-file-system/
2024 Cluj-Napoca 29
Librăria ESP Async Web Sever
void setup(){
Serial.begin(115200); // Serial port for debugging purposes
pinMode(ledPin, OUTPUT);
if(!SPIFFS.begin(true)) {// Initialize SPIFFS
Serial.println("An Error has occurred while mounting SPIFFS"); return; }
// set up the access point
if (!WiFi.softAP(ssid)) {
Serial.println("Failed to set up access point");
while (1); }
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index.html", String(), false, processor); });
// Route to load style.css file
server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/style.css", "text/css"); });
// Route to set GPIO to HIGH
server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request){
digitalWrite(ledPin, HIGH);
request->send(SPIFFS, "/index.html", String(), false, processor); });
// Route to set GPIO to LOW
server.on("/off", HTTP_GET, [](AsyncWebServerRequest *request){
digitalWrite(ledPin, LOW);
request->send(SPIFFS, "/index.html", String(), false, processor); });
// Start server
server.begin();
}
2024 Cluj-Napoca 30
Librăria ESP Async Web Sever
// The processor() function is what will attribute a value to the placeholder
// we’ve created on the HTML file. It accepts as argument the placeholder and
// should return a String that will replace the placeholder.
void loop ()
{
// nothing in the loop
}
2024 Cluj-Napoca 31
Concluzii ESP32 Wi-Fi & SPIFFS
• ESP32 poate fi configurat atât ca AP cât și ca client Wi-Fi
• Un server TCP poate fi configurat pe un anumit port
• Un client TCP se conecteaza pe o adresă IP și un port specific
• Comunicarea este realizată prin intermediul clasei WiFiClient
• SPIFFS poate stoca fișiere oferă posibilitatea de a crea aplicații mai complexe
• Librăria ESP Async Web Server poate fi folosită pentru a seta pagini web
complexe care deservesc multiple conexiuni simultane
2024 Cluj-Napoca 32
Referințe
1. https://randomnerdtutorials.com/esp32-access-point-ap-web-server/
2. https://docs.espressif.com/projects/esp-idf/en/v4.2.5/esp32/api-
reference/storage/spiffs.html
3. https://www.tutorialspoint.com/esp32_for_iot/esp32_for_iot_spiffs_storage.htm
4. https://randomnerdtutorials.com/esp32-web-server-spiffs-spi-flash-file-system/
5. https://github.com/me-no-dev/ESPAsyncWebServer
6. https://github.com/me-no-dev/AsyncTCP
2024 Cluj-Napoca 33