08-02-2025, 20:41
Nie działa mi kod ktory ma sie wywołac po wysłaniu zadania get na /index.html i /main.html.
Nie mam pojęcia jak zrobić ktoś coś pomoże?
Cały kod:
#include <SPI.h>
#include <SD.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <time.h>
const char* ssid = "";
const char* password = "";
const int pin_karty_SD = 5;
String username = "";
String pass = "";
String token = "";
AsyncWebServer server(80);
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
String czas()
{
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Nie udało się pobrać czasu.");
return "";
}
timeinfo.tm_hour += 1;
mktime(&timeinfo);
char expires[100];
strftime(expires, sizeof(expires), "%a, %d %b %Y %H:%M:%S GMT", &timeinfo);
return String(expires);
}
String generateToken() {
return String(random(100000, 999999));
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(". ");
}
Serial.println("Połączono z Wifi.");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
while (!time(nullptr)) {
delay(1000);
Serial.print("Syn. czasu");
}
Serial.println("Czas zsynchronizowany.");
String expireTime = czas();
Serial.println("Czas wygaśnięcia ciasteczka: " + expireTime);
SPI.begin(18, 19, 23, pin_karty_SD);
if (!SD.begin(pin_karty_SD)) {
Serial.print("Nie udało się zamontować karty SD.");
return;
}
Serial.print("Karta SD zamontowana.");
server.serveStatic("/", SD, "/www").setDefaultFile("index.html");
server.on("/main.html", HTTP_GET, [](AsyncWebServerRequest *request) {
request->redirect("/index.html");
});
// Obsługa logowania
server.on("/login", HTTP_POST, [](AsyncWebServerRequest *request) {
String login;
String haslo;
if (request->hasParam("user", true) && request->hasParam("pass", true)) {
login = request->getParam("user", true)->value();
haslo = request->getParam("pass", true)->value();
if (username == login && pass == haslo) {
token = generateToken(); // Używamy aktualnego tokenu
String expires = czas();
AsyncWebServerResponse *response = request->beginResponse(302, "text/plain", "Zalogowano!");
response->addHeader("Location", "/main.html");
response->addHeader("Set-Cookie", "session_token=" + token + "; expires=" + expires + "; Path=/; HttpOnly");
request->send(response);
} else {
AsyncWebServerResponse *response = request->beginResponse(302, "text/plain", "Bledna dane!");
response->addHeader("Location", "/index.html?error=true");
request->send(response);
}
} else {
request->send(400, "text/plain", "Brak danych logowania!");
}
});
server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request) {
if (request->hasHeader("Cookie")) {
String cookie = request->header("Cookie");
if (cookie.indexOf("session_token=") >= 0) {
// Sprawdzamy, czy token sesji jest poprawny
String tokenFromCookie = cookie.substring(cookie.indexOf("session_token=") + 14);
Serial.print("Token z ciastka: " + tokenFromCookie + "Token zapisany: " + token);
if (tokenFromCookie == token) { // Porównujemy z aktualnym tokenem
request->redirect("/main.html");
return;
}
}
}
else
{
request->redirect("/index.html");
}
});
server.begin();
}
void loop() {
}
Nie mam pojęcia jak zrobić ktoś coś pomoże?
Cały kod:
#include <SPI.h>
#include <SD.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <time.h>
const char* ssid = "";
const char* password = "";
const int pin_karty_SD = 5;
String username = "";
String pass = "";
String token = "";
AsyncWebServer server(80);
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
String czas()
{
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Nie udało się pobrać czasu.");
return "";
}
timeinfo.tm_hour += 1;
mktime(&timeinfo);
char expires[100];
strftime(expires, sizeof(expires), "%a, %d %b %Y %H:%M:%S GMT", &timeinfo);
return String(expires);
}
String generateToken() {
return String(random(100000, 999999));
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(". ");
}
Serial.println("Połączono z Wifi.");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
while (!time(nullptr)) {
delay(1000);
Serial.print("Syn. czasu");
}
Serial.println("Czas zsynchronizowany.");
String expireTime = czas();
Serial.println("Czas wygaśnięcia ciasteczka: " + expireTime);
SPI.begin(18, 19, 23, pin_karty_SD);
if (!SD.begin(pin_karty_SD)) {
Serial.print("Nie udało się zamontować karty SD.");
return;
}
Serial.print("Karta SD zamontowana.");
server.serveStatic("/", SD, "/www").setDefaultFile("index.html");
server.on("/main.html", HTTP_GET, [](AsyncWebServerRequest *request) {
request->redirect("/index.html");
});
// Obsługa logowania
server.on("/login", HTTP_POST, [](AsyncWebServerRequest *request) {
String login;
String haslo;
if (request->hasParam("user", true) && request->hasParam("pass", true)) {
login = request->getParam("user", true)->value();
haslo = request->getParam("pass", true)->value();
if (username == login && pass == haslo) {
token = generateToken(); // Używamy aktualnego tokenu
String expires = czas();
AsyncWebServerResponse *response = request->beginResponse(302, "text/plain", "Zalogowano!");
response->addHeader("Location", "/main.html");
response->addHeader("Set-Cookie", "session_token=" + token + "; expires=" + expires + "; Path=/; HttpOnly");
request->send(response);
} else {
AsyncWebServerResponse *response = request->beginResponse(302, "text/plain", "Bledna dane!");
response->addHeader("Location", "/index.html?error=true");
request->send(response);
}
} else {
request->send(400, "text/plain", "Brak danych logowania!");
}
});
server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request) {
if (request->hasHeader("Cookie")) {
String cookie = request->header("Cookie");
if (cookie.indexOf("session_token=") >= 0) {
// Sprawdzamy, czy token sesji jest poprawny
String tokenFromCookie = cookie.substring(cookie.indexOf("session_token=") + 14);
Serial.print("Token z ciastka: " + tokenFromCookie + "Token zapisany: " + token);
if (tokenFromCookie == token) { // Porównujemy z aktualnym tokenem
request->redirect("/main.html");
return;
}
}
}
else
{
request->redirect("/index.html");
}
});
server.begin();
}
void loop() {
}

