simple wifi

This commit is contained in:
2025-03-05 08:10:50 +02:00
commit 87b25e1f09
2 changed files with 174 additions and 0 deletions

82
WiFiProv/WiFiProv.ino Normal file
View File

@@ -0,0 +1,82 @@
/*
Please read README.md file in this folder, or on the web:
https://github.com/espressif/arduino-esp32/tree/master/libraries/WiFiProv/examples/WiFiProv
Note: This sketch takes up a lot of space for the app and may not be able to flash with default setting on some chips.
If you see Error like this: "Sketch too big"
In Arduino IDE go to: Tools > Partition scheme > chose anything that has more than 1.4MB APP
- for example "No OTA (2MB APP/2MB SPIFFS)"
*/
#include "sdkconfig.h"
#if CONFIG_ESP_WIFI_REMOTE_ENABLED
#error "WiFiProv is only supported in SoCs with native Wi-Fi support"
#endif
#include "WiFiProv.h"
#include "WiFi.h"
// #define USE_SOFT_AP // Uncomment if you want to enforce using the Soft AP method instead of BLE
const char *pop = "abcd1234"; // Proof of possession - otherwise called a PIN - string provided by the device, entered by the user in the phone app
const char *service_name = "PROV_123"; // Name of your device (the Espressif apps expects by default device name starting with "Prov_")
const char *service_key = NULL; // Password used for SofAP method (NULL = no password needed)
bool reset_provisioned = true; // When true the library will automatically delete previously provisioned data.
// WARNING: SysProvEvent is called from a separate FreeRTOS task (thread)!
void SysProvEvent(arduino_event_t *sys_event) {
switch (sys_event->event_id) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.print("\nConnected IP address : ");
Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr));
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: Serial.println("\nDisconnected. Connecting to the AP again... "); break;
case ARDUINO_EVENT_PROV_START: Serial.println("\nProvisioning started\nGive Credentials of your access point using smartphone app"); break;
case ARDUINO_EVENT_PROV_CRED_RECV:
{
Serial.println("\nReceived Wi-Fi credentials");
Serial.print("\tSSID : ");
Serial.println((const char *)sys_event->event_info.prov_cred_recv.ssid);
Serial.print("\tPassword : ");
Serial.println((char const *)sys_event->event_info.prov_cred_recv.password);
break;
}
case ARDUINO_EVENT_PROV_CRED_FAIL:
{
Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n");
if (sys_event->event_info.prov_fail_reason == NETWORK_PROV_WIFI_STA_AUTH_ERROR) {
Serial.println("\nWi-Fi AP password incorrect");
} else {
Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()");
}
break;
}
case ARDUINO_EVENT_PROV_CRED_SUCCESS: Serial.println("\nProvisioning Successful"); break;
case ARDUINO_EVENT_PROV_END: Serial.println("\nProvisioning Ends"); break;
default: break;
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(); // no SSID/PWD - get it from the Provisioning APP or from NVS (last successful connection)
WiFi.onEvent(SysProvEvent);
// BLE Provisioning using the ESP SoftAP Prov works fine for any BLE SoC, including ESP32, ESP32S3 and ESP32C3.
#if CONFIG_BLUEDROID_ENABLED && !defined(USE_SOFT_AP)
Serial.println("Begin Provisioning using BLE");
// Sample uuid that user can pass during provisioning using BLE
uint8_t uuid[16] = {0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf, 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02};
WiFiProv.beginProvision(
NETWORK_PROV_SCHEME_BLE, NETWORK_PROV_SCHEME_HANDLER_FREE_BLE, NETWORK_PROV_SECURITY_1, pop, service_name, service_key, uuid, reset_provisioned
);
log_d("ble qr");
WiFiProv.printQR(service_name, pop, "ble");
#else
Serial.println("Begin Provisioning using Soft AP");
WiFiProv.beginProvision(NETWORK_PROV_SCHEME_SOFTAP, NETWORK_PROV_SCHEME_HANDLER_NONE, NETWORK_PROV_SECURITY_1, pop, service_name, service_key);
log_d("wifi qr");
WiFiProv.printQR(service_name, pop, "softap");
#endif
}
void loop() {}

92
simple-wifi. Normal file
View File

@@ -0,0 +1,92 @@
#include <WiFi.h>
#include <WebServer.h>
#include <EEPROM.h>
const char* ap_ssid = "ESP32_Config";
const char* ap_password = "12345678";
WebServer server(80);
String ssid = "";
String password = "";
String static_ip = "";
String hostname = "";
void setup() {
Serial.begin(115200);
EEPROM.begin(512);
// Load saved settings
loadSettings();
// If settings are not set, enter AP mode
if (ssid == "" || password == "") {
WiFi.softAP(ap_ssid, ap_password);
Serial.println("AP Mode: Connect to the network and configure settings.");
server.on("/", handleRoot);
server.on("/save", handleSave);
server.begin();
} else {
// Connect to WiFi in client mode
WiFi.mode(WIFI_STA);
WiFi.config(IPAddress(static_ip.c_str()), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
WiFi.begin(ssid.c_str(), password.c_str());
WiFi.setHostname(hostname.c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("MAC Address: ");
Serial.println(WiFi.macAddress());
}
}
void loop() {
if (WiFi.getMode() == WIFI_AP) {
server.handleClient();
}
}
void handleRoot() {
String html = "<form action='/save' method='POST'>";
html += "SSID: <input type='text' name='ssid'><br>";
html += "Password: <input type='text' name='password'><br>";
html += "Static IP: <input type='text' name='static_ip'><br>";
html += "Hostname: <input type='text' name='hostname'><br>";
html += "<input type='submit' value='Save'>";
html += "</form>";
server.send(200, "text/html", html);
}
void handleSave() {
ssid = server.arg("ssid");
password = server.arg("password");
static_ip = server.arg("static_ip");
hostname = server.arg("hostname");
saveSettings();
server.send(200, "text/html", "Settings saved. Rebooting...");
delay(2000);
ESP.restart();
}
void saveSettings() {
EEPROM.writeString(0, ssid);
EEPROM.writeString(32, password);
EEPROM.writeString(64, static_ip);
EEPROM.writeString(96, hostname);
EEPROM.commit();
}
void loadSettings() {
ssid = EEPROM.readString(0);
password = EEPROM.readString(32);
static_ip = EEPROM.readString(64);
hostname = EEPROM.readString(96);
}