Právě se nacházíte: Home / vyvoj /
ESP32 test WiFi nebo LAN využití SPIFFS na ukládání nastavení
Třeba to bude někomu k užitku - ke stažení
Jediná ne/výhoda pro web stránky, že jedou pouze přes WiFi síť
IP adresa WiFi je možno zadat do prohlížeče pro nastavení
// http://EASUN.eu , http://POWMR.eu -------------------------------------------
//------------------------------------------------------------------------------
// © 2023 - Autor: Pavel Kunor - fanet@email.cz , obchod@fanet.cz --------------
//------------------------------------------------------------------------------
//Není dovoleno šířit tento software bez výslovného písemného souhlasu vlastníka.
//------------------------------------------------------------------------------#include
#include
#include
#include //knihovna pro I2C komunikaci (s LCD displejem)
#include //knihovna pro LCD displej
//------- prace s SPIFFS pameti ESP32 --------
#include
//Systém SPIFFS je třeba naformátovat pouze při prvním spuštění
//#define FORMAT_SPIFFS_IF_FAILED true
//------- WebServer ----------------------------------------
#include
#include
//----------------------------------------------------------
//NASTAVENÍ MAC pro LAN --------------------------------------------------------
byte mac[] = {0x00, 0x06, 0xBB, 0x06, 0x06, 0x06};
//------------------------------------------------------------------------------
AsyncWebServer server(80);
//---- Nastaveni WiFi zakladni parametry
const char* parameter_string = "VstupniData";
const char* parameter_string2 = "VstupniData2";
//------- Nastaveni pro WiFi -----------------------------
const char* ssid = "";const char* password = "";
int JakLAN; int JakWiFi;
//------------------------------------------------------------------------------
#include "wifi.h"//web vkladani dat zakladni nastaveni WiFi
#include "wifi2.h"//web vkladani dat editace WiFi
//------------------------------------------------------------------------------
LiquidCrystal_I2C lcd(0x27,20,4);//název,I2C adresa,počet znaků,počet řádků
EthernetClient eclient;//LAN komunikace
void setup() {
Serial.begin(115200);
lcd.init();//inicializace displeje
lcd.backlight();//zapnutí podsvícení
//------------------------------------------------------------------------------
//vytisknutí první hlášky na displej
lcd.clear();
lcd.setCursor(0, 0);lcd.print("(c) 2023 Autor");
lcd.setCursor(0, 1);lcd.print(" Pavel Kunor");
lcd.setCursor(0, 3);lcd.print("EASUN.eu PowMR.eu");
//delay(500);
//------------------------------------------------------------------------------if(!SPIFFS.begin(true)){
Serial.println("Chyba nastaveni SPIFFS");
return;
}//Test na vlozene hodnoty pro WiFi ---------------------------------------------
String myString = read_file(SPIFFS, "/VstupniData.txt");
Serial.print("WiFiSSID: ");
if(myString == "null"){ssid = "null";Serial.println(myString);}
else{ssid = myString.c_str();Serial.println(myString);}
String myString2 = read_file(SPIFFS, "/VstupniData2.txt");
Serial.print("WiFiPasswd: ");
if(myString2 == "null"){password = "null";Serial.println(myString2);}
else{password = myString2.c_str();Serial.println(myString2);}
//Test na vlozene hodnoty pro WiFi ---------------------------------------------
Serial.print(F("TEST: "));Serial.print(ssid);
Serial.print(F(", "));Serial.print(password);
Serial.println();Serial.println();
//Test na vlozene hodnoty pro WiFi ---------------------------------------------//Zapinam WiFi WSP jako Access Point pro web nastaveni hodnot-------------------
if(ssid == "null" || password == "null"){// || broker == "null"){
const char* ssid = "EASUN";const char* password = "";
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print(F("SSID: "));Serial.println(ssid);
Serial.print(F("WiFi IP address: "));Serial.println(IP);
lcd.clear();
lcd.setCursor(0, 0);lcd.print("Nastavte system ....");
lcd.setCursor(0, 1);lcd.print("IP: ");lcd.print(IP);
lcd.setCursor(0, 2);lcd.print("SSID: ");lcd.print(ssid);
lcd.setCursor(0, 3);lcd.print("EaSun.eu PowMr.eu");
tcpip_adapter_init();
//---- Prvotni nastaveni WiFi web --------------------------
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", wifi_html, processor);
});
server.on("/getwifi", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;String inputMessage2;//String inputMessage3;
if (request->hasParam(parameter_string)) {
inputMessage = request->getParam(parameter_string)->value();write_file(SPIFFS, "/VstupniData.txt", inputMessage.c_str());
inputMessage2 = request->getParam(parameter_string2)->value();write_file(SPIFFS, "/VstupniData2.txt", inputMessage2.c_str());
HlaskaRestart();//Restartuji a hlasim na LCD
}
else {inputMessage = "No message sent";}
Serial.println(inputMessage);
request->send(200, "text/text", inputMessage);
});
server.onNotFound(notFound);
server.begin();
//---- Prvotni nastaveni WiFi web --------------------------
while(1);//Pokud je chyba na WiFi nastavuje se znovu--------
}
//Zapinam WiFi WSP jako Access Point pro web nastaveni hodnot-------------------
else{
//--- Testuji LAN ------------------------------------------------------------
Ethernet.init(5);// MKR ETH Shield
JakLAN = TestLan();//Nastavuji LAN
delay(2000);
Serial.print("Test LAN: ");Serial.println(JakLAN);
if(JakLAN == 1){
}
else{//-- Testuji WiFi ------------------------------------------------------------
//---------------------------------------------------------------------------------
JakWiFi = initWiFi();//Nastavuji WiFi
Serial.print("Test WiFi: ");Serial.println(JakWiFi);
if(JakWiFi == 1){
// --- web stranky -----------------------------------------
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", wifi2_html, processor);
});
server.on("/getwifi", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;String inputMessage2;String inputMessage3;
if (request->hasParam(parameter_string)) {
inputMessage = request->getParam(parameter_string)->value();write_file(SPIFFS, "/VstupniData.txt", inputMessage.c_str());
inputMessage2 = request->getParam(parameter_string2)->value();write_file(SPIFFS, "/VstupniData2.txt", inputMessage2.c_str());
HlaskaRestart();//Restartuji a hlasim na LCD
}
else {inputMessage = "Zadna vstupni data";}
Serial.println(inputMessage);request->send(200, "text/text", inputMessage);
});
server.onNotFound(notFound);
server.begin();
}
}
}
}void loop(){
}int TestLan() {
auto link = Ethernet.linkStatus();
Serial.println("Test ETH a WiFi");
lcd.clear();
lcd.setCursor(0, 0);lcd.print("Test ETH a WiFi ");
delay(500);
if (link==Unknown) {
lcd.clear();
lcd.setCursor(0, 0);lcd.print("LAN karta nefunguje");
Serial.println("LAN karta nefunguje");
return 0;
}
if (link==LinkON) {
Serial.println();
if (Ethernet.begin(mac)){
Serial.println("DHCP OK!");
IPAddress ip = Ethernet.localIP();
IPAddress subnet = Ethernet.subnetMask();
IPAddress gateway = Ethernet.gatewayIP();
IPAddress dns = Ethernet.dnsServerIP();
Ethernet.begin(mac, ip, subnet, gateway, dns);
//--- DHCP info -----------------------------------------
lcd.clear();
lcd.setCursor(0, 0);lcd.print("LAN:");lcd.print(ip);
lcd.setCursor(0, 1);lcd.print("M:");lcd.print(subnet);
lcd.setCursor(0, 2);lcd.print("G:");lcd.print(gateway);
lcd.setCursor(0, 3);lcd.print("DNS:");lcd.print(dns);Serial.print("LAN: ");Serial.println(ip);
Serial.print("Subnet: ");Serial.println(subnet);
Serial.print("Gateway: ");Serial.println(gateway);
Serial.print("DNS: ");Serial.println(dns);
return 1;
}
delay(500);
}
if (link==LinkOFF) {
lcd.clear();
lcd.setCursor(0, 0);lcd.print("ETH nefunguje!");
lcd.setCursor(0, 2);lcd.print("Testuji WiFi ...");
Serial.println("Testuji WiFi ...");
delay(500);
return 0;
}
}
int initWiFi() {
int TestX = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println(F("Pripojuji WiFi "));
lcd.clear();
lcd.setCursor(0, 0);lcd.print("Pripojuji WiFi");
lcd.setCursor(0, 3);lcd.print("EaSun.eu PowMr.eu");
while (WiFi.status() != WL_CONNECTED){
Serial.print(F("."));lcd.print(".");
delay(500);
if(TestX >= 30){//Pokud nefunguje smazu data a restartuji
write_file(SPIFFS, "/VstupniData.txt", "null");
write_file(SPIFFS, "/VstupniData2.txt", "null");
lcd.clear();
lcd.setCursor(0, 1);lcd.print("Chyba WiFi");
delay(1000);
lcd.setCursor(0, 2);lcd.print("Mazu nastaveni");
delay(1000);
bool formatted = SPIFFS.format();
if(formatted){Serial.println(F(" Success formatting"));}
else{Serial.println(F(" Error formatting"));}
ESP.restart();
break;
}
TestX = TestX + 1;
}
//WiFI funguje tak pokracuji
lcd.clear();
lcd.setCursor(0, 0);lcd.print("WiFi:");lcd.print(WiFi.localIP());
lcd.setCursor(0, 1);lcd.print("S:");lcd.print(WiFi.subnetMask());
lcd.setCursor(0, 2);lcd.print("G:");lcd.print(WiFi.gatewayIP());
//lcd.setCursor(0, 3);lcd.print("M:");lcd.print(WiFi.macAddress());
lcd.setCursor(0, 3);lcd.print("D:");lcd.print(WiFi.dnsIP());
Serial.print(F("RRSI: "));
Serial.println(WiFi.RSSI());
IPAddress ip = WiFi.localIP();
IPAddress subnet = WiFi.subnetMask();
IPAddress gateway = WiFi.gatewayIP();
IPAddress dns = WiFi.dnsIP();Serial.print("WiFi: ");Serial.println(ip);
Serial.print("Subnet: ");Serial.println(subnet);
Serial.print("Gateway: ");Serial.println(gateway);
Serial.print("DNS: ");Serial.println(dns);
Serial.print("Mac: ");Serial.println(WiFi.macAddress());
delay(500);
return 1;
}
void HlaskaRestart(){
lcd.clear();lcd.setCursor (0, 0);lcd.print("Aktualizuji data");
delay(2000);
lcd.clear();lcd.setCursor (0, 2);lcd.print("... restartuji");
delay(500);
lcd.clear();lcd.setCursor (0, 3);lcd.print("... vypinam");
delay(500);
ESP.restart();
}
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}String processor(const String& var){
if(var == "VstupniData"){return read_file(SPIFFS, "/VstupniData.txt");}
if(var == "VstupniData2"){return read_file(SPIFFS, "/VstupniData2.txt");}
return String();
}String read_file(fs::FS &fs, const char * path){
//Serial.printf("Reading file: %s ", path);
File file = fs.open(path, "r");
if(!file || file.isDirectory()){return "null";}
String fileContent;String testfile;
testfile = file.size();
while(file.available()){
fileContent+=String((char)file.read());
}
file.close();
if(testfile != "0"){return fileContent;}
else{return "null";}
}
void write_file(fs::FS &fs, const char * path, const char * message){
Serial.printf("Nahran soubor: %s ", path);
File file = fs.open(path, "w");
if(!file){
Serial.println(F("Chyba - nahravani souboru"));
return;
}
if(file.print(message)){Serial.println(F("OK - soubor je nahran"));}
else{Serial.println(F("CHYBA - nahravani souboru"));}
file.close();
}