diff --git a/WiFiProv/WiFiProv.ino b/WiFiProv/WiFiProv.ino deleted file mode 100644 index 76025d7..0000000 --- a/WiFiProv/WiFiProv.ino +++ /dev/null @@ -1,82 +0,0 @@ -/* -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() {} diff --git a/simple-wifi. b/simple-wifi. deleted file mode 100644 index 9a316e7..0000000 --- a/simple-wifi. +++ /dev/null @@ -1,92 +0,0 @@ -#include -#include -#include - -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 = "
"; - html += "SSID:
"; - html += "Password:
"; - html += "Static IP:
"; - html += "Hostname:
"; - html += ""; - html += "
"; - 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); -} \ No newline at end of file diff --git a/sketch_test_button/sketch_test_button.ino b/sketch_test_button/sketch_test_button.ino new file mode 100644 index 0000000..b28b04f --- /dev/null +++ b/sketch_test_button/sketch_test_button.ino @@ -0,0 +1,3 @@ + + + diff --git a/sketch_wifi/sketch_wifi.ino b/sketch_wifi/sketch_wifi.ino new file mode 100644 index 0000000..b269ab7 --- /dev/null +++ b/sketch_wifi/sketch_wifi.ino @@ -0,0 +1,199 @@ +#include +#include +#include +#include "esp_mac.h" // required - exposes esp_mac_type_t values + +const char* ap_ssid = "ESP32-AP"; +const char* ap_password = "12345678"; +const int userLedPin = 8; // Define the pin for the user LED +const int buttonPin = 0; // Define the pin for the button + +WebServer server(80); + +String ssid, password, static_ip, hostname; +bool isAPMode = false; + +void handleRoot() { + String macAddress = getDefaultMacAddress(); + String html = "Configuration"; + html += ""; + html += "

Board Configuration

"; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + 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. Device will restart in client mode."); + delay(2000); + ESP.restart(); +} + +void handleInfo() { + String html = "Info"; + html += ""; + html += "

Connection Info

"; + if (WiFi.status() == WL_CONNECTED) { + html += "

Status: Connected

"; + html += "

IP Address: " + WiFi.localIP().toString() + "

"; + } else { + html += "

Status: Not Connected

"; + } + html += "
"; + server.send(200, "text/html", html); +} + +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); +} + +void setup() { + Serial.begin(115200); + Serial.println("Setup started"); + EEPROM.begin(128); + + pinMode(userLedPin, OUTPUT); + digitalWrite(userLedPin, LOW); + + pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor + + loadSettings(); + + // Stop AP mode if it was previously started + WiFi.softAPdisconnect(true); + + // Check if the button is pressed at startup + if (digitalRead(buttonPin) == LOW) { + Serial.println("Button pressed at startup. Starting in AP mode."); + startAPMode(); + } else { + if (ssid.length() > 0 && password.length() > 0) { + Serial.println("Attempting to connect to WiFi with saved credentials:"); + Serial.print("SSID: "); + Serial.println(ssid); + Serial.print("Password: "); + Serial.println(password); + WiFi.begin(ssid.c_str(), password.c_str()); + Serial.print("Connecting to WiFi"); + unsigned long startTime = millis(); + while (WiFi.status() != WL_CONNECTED) { + if (millis() - startTime >= 10000) { // 10 seconds timeout + Serial.println("Failed to connect to WiFi."); + startAPMode(); + return; + } + Serial.print("."); + delay(500); + } + Serial.println(); + Serial.println("Connected to WiFi."); + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + Serial.print("Hostname: "); + Serial.println(hostname); + digitalWrite(userLedPin, HIGH); // Turn on the LED if connection is successful + isAPMode = false; + } else { + Serial.println("No saved WiFi credentials found. Starting in AP mode."); + startAPMode(); + } + } + Serial.println("Setup completed"); +} + +void loop() { + server.handleClient(); + blinkLed(); +} + +void blinkLed() { + static unsigned long lastBlinkTime = 0; + static bool ledState = LOW; + unsigned long interval = isAPMode ? 1000 : 3000; // 1 second interval for AP mode, 3 seconds for client mode + + if (millis() - lastBlinkTime >= interval) { + ledState = !ledState; + digitalWrite(userLedPin, ledState); + lastBlinkTime = millis(); + } +} + +void startAPMode() { + WiFi.softAP(ap_ssid, ap_password); + Serial.println("Access Point Started"); + Serial.print("IP Address: "); + Serial.println(WiFi.softAPIP()); + server.on("/", handleRoot); + server.on("/save", handleSave); + server.on("/info", handleInfo); + server.begin(); + isAPMode = true; +} + +String getDefaultMacAddress() { + String mac = ""; + unsigned char mac_base[6] = {0}; + + if (esp_efuse_mac_get_default(mac_base) == ESP_OK) { + char buffer[18]; // 6*2 characters for hex + 5 characters for colons + 1 character for null terminator + sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", mac_base[0], mac_base[1], mac_base[2], mac_base[3], mac_base[4], mac_base[5]); + mac = buffer; + } + + return mac; +} + +String getInterfaceMacAddress(esp_mac_type_t interface) { + String mac = ""; + unsigned char mac_base[6] = {0}; + + if (esp_read_mac(mac_base, interface) == ESP_OK) { + char buffer[18]; // 6*2 characters for hex + 5 characters for colons + 1 character for null terminator + sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", mac_base[0], mac_base[1], mac_base[2], mac_base[3], mac_base[4], mac_base[5]); + mac = buffer; + } + + return mac; +} \ No newline at end of file diff --git a/test_but/test_but.ino b/test_but/test_but.ino new file mode 100644 index 0000000..37e1922 --- /dev/null +++ b/test_but/test_but.ino @@ -0,0 +1,24 @@ +const int buttonPin = 0; // Assuming BUT1 is connected to GPIO 0 +const int ledPin = 8; // Assuming the user LED is connected to GPIO 8 +//test1 +void setup() { + Serial.begin(115200); + Serial.println("Setup started"); + pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor + pinMode(ledPin, OUTPUT); // Set the LED pin as output + Serial.println("Setup completed"); +} + +void loop() { + int buttonState = digitalRead(buttonPin); // Read the state of the button + + if (buttonState == LOW) { + Serial.println("Button is pressed"); + digitalWrite(ledPin, HIGH); // Turn on the LED + } else { + Serial.println("Button is not pressed"); + digitalWrite(ledPin, LOW); // Turn off the LED + } + + delay(2000); // Add a 2-second delay between readings +} \ No newline at end of file diff --git a/wifi_set_and_server/wifi_set_and_server.ino b/wifi_set_and_server/wifi_set_and_server.ino new file mode 100644 index 0000000..76ddb2e --- /dev/null +++ b/wifi_set_and_server/wifi_set_and_server.ino @@ -0,0 +1,318 @@ +#include +#include +#include +#include // https://github.com/tzapu/WiFiManager +#include "esp_mac.h" // required - exposes esp_mac_type_t values +//ver 0.0.2 +const char* ap_ssid = "ESP32-AP"; // SSID for the Access Point mode +const char* ap_password = "12345678"; // Password for the Access Point mode +const int userLedPin = 8; // Define the pin for the user LED +const int buttonPin = 0; // Define the pin for the button + +const int relayPins[4] = {10, 11, 22, 23}; // Define the pins for the relays +const int inputPins[4] = {1, 2, 3, 15}; // Define the pins for the inputs + +WebServer server(80); // Create a web server on port 80 + +String ssid, password, static_ip, netmask, gateway, hostname; // Variables to store WiFi settings +bool isAPMode = false; // Flag to indicate if the board is in AP mode + +unsigned long lastStatusPrintTime = 0; // Variable to keep track of the last status print time + +// Handle the root URL ("/") and serve the configuration page +void handleRoot() { + String macAddress = getDefaultMacAddress(); + String html = "Configuration"; + html += ""; // Refresh the page every second + html += ""; + + if (isAPMode) { + html += "

Board Configuration

"; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + html += ""; + html += "
"; + } + + // Add WiFi connection information if connected + if (WiFi.status() == WL_CONNECTED) { + html += "

Connection Info

"; + html += "

Status: Connected

"; + html += "

IP Address: " + WiFi.localIP().toString() + "

"; + html += "

Hostname: " + hostname + "

"; + html += "
"; + + // Add relay and input status with control buttons + html += "

Relay and Input Status

"; + for (int i = 0; i < 4; i++) { + html += "

Relay " + String(i + 1) + ": " + (digitalRead(relayPins[i]) == HIGH ? "ON" : "OFF") + "

"; + html += "
"; + html += ""; + html += ""; + html += ""; + html += "
"; + html += "

Input " + String(i + 1) + ": " + (digitalRead(inputPins[i]) == LOW ? "Pressed" : "Not Pressed") + "

"; + } + html += "
"; + } else { + html += "

Connection Info

"; + html += "

Status: Not Connected

"; + html += "
"; + } + + html += ""; + + server.send(200, "text/html", html); +} + +// Handle the save URL ("/save") and save the WiFi settings +void handleSave() { + ssid = server.arg("ssid"); + password = server.arg("password"); + static_ip = server.arg("static_ip"); + netmask = server.arg("netmask"); + gateway = server.arg("gateway"); + hostname = server.arg("hostname"); + + saveSettings(); + + server.send(200, "text/html", "Settings saved. Device will restart in client mode."); + delay(2000); + ESP.restart(); +} + +// Handle the relay control URL ("/relay") and control the relays +void handleRelay() { + int relay = server.arg("relay").toInt(); + String action = server.arg("action"); + + if (relay >= 0 && relay < 4) { + if (action == "on") { + digitalWrite(relayPins[relay], HIGH); + } else if (action == "off") { + digitalWrite(relayPins[relay], LOW); + } + } + + server.sendHeader("Location", "/"); + server.send(303); +} + +// Save the WiFi settings to EEPROM +void saveSettings() { + EEPROM.writeString(0, ssid); + EEPROM.writeString(32, password); + EEPROM.writeString(64, static_ip); + EEPROM.writeString(96, netmask); + EEPROM.writeString(128, gateway); + EEPROM.writeString(160, hostname); + EEPROM.commit(); +} + +// Load the WiFi settings from EEPROM +void loadSettings() { + ssid = EEPROM.readString(0); + password = EEPROM.readString(32); + static_ip = EEPROM.readString(64); + netmask = EEPROM.readString(96); + gateway = EEPROM.readString(128); + hostname = EEPROM.readString(160); +} + +void setup() { + Serial.begin(115200); + Serial.println("Setup started"); + EEPROM.begin(192); + + pinMode(userLedPin, OUTPUT); + digitalWrite(userLedPin, LOW); + + pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor + + for (int i = 0; i < 4; i++) { + pinMode(relayPins[i], OUTPUT); + digitalWrite(relayPins[i], LOW); // Turn off all relays at startup + pinMode(inputPins[i], INPUT_PULLUP); // Set input pins as input with internal pull-up resistors + } + + loadSettings(); + + // Stop AP mode if it was previously started + WiFi.softAPdisconnect(true); + + // Check if the button is pressed at startup + if (digitalRead(buttonPin) == LOW) { + Serial.println("Button pressed at startup. Starting in AP mode."); + startAPMode(); + } else { + if (ssid.length() > 0 && password.length() > 0) { + Serial.println("Attempting to connect to WiFi with saved credentials:"); + Serial.print("SSID: "); + Serial.println(ssid); + Serial.print("Password: "); + Serial.println(password); + + // Convert Strings to IPAddress objects + IPAddress ip, gw, nm; + if (!ip.fromString(static_ip) || !gw.fromString(gateway) || !nm.fromString(netmask)) { + Serial.println("Invalid IP configuration. Starting in AP mode."); + startAPMode(); + return; + } + + // Set hostname and static IP configuration + WiFi.config(ip, gw, nm); + WiFi.setHostname(hostname.c_str()); + + WiFi.begin(ssid.c_str(), password.c_str()); + unsigned long startTime = millis(); + while (WiFi.status() != WL_CONNECTED) { + if (millis() - startTime >= 10000) { // 10 seconds timeout + Serial.println("Failed to connect to WiFi. Starting in AP mode."); + startAPMode(); + return; + } + delay(500); + } + + Serial.println("Connected to WiFi."); + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + Serial.print("Hostname: "); + Serial.println(WiFi.getHostname()); + digitalWrite(userLedPin, HIGH); // Turn on the LED if connection is successful + isAPMode = false; + startWebServer(); + } else { + Serial.println("No saved WiFi credentials found. Starting in AP mode."); + startAPMode(); + } + } + Serial.println("Setup completed"); +} + +void loop() { + server.handleClient(); + blinkLed(); + printStatus(); +} + +// Blink the LED to indicate the mode (AP mode or client mode) +void blinkLed() { + static unsigned long lastBlinkTime = 0; + static bool ledState = LOW; + unsigned long interval = isAPMode ? 1000 : 3000; // 1 second interval for AP mode, 3 seconds for client mode + + if (millis() - lastBlinkTime >= interval) { + ledState = !ledState; + digitalWrite(userLedPin, ledState); + lastBlinkTime = millis(); + } +} + +// Print the status of the board every 20 seconds +void printStatus() { + if (millis() - lastStatusPrintTime >= 20000) { // 20 seconds interval + Serial.println("Board Status:"); + if (isAPMode) { + Serial.println("Mode: Access Point"); + Serial.print("AP SSID: "); + Serial.println(ap_ssid); + Serial.print("AP IP Address: "); + Serial.println(WiFi.softAPIP()); + } else { + Serial.println("Mode: Client"); + Serial.print("Connected to SSID: "); + Serial.println(ssid); + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + Serial.print("Hostname: "); + Serial.println(WiFi.getHostname()); + } + for (int i = 0; i < 4; i++) { + Serial.print("Relay "); + Serial.print(i + 1); + Serial.print(": "); + Serial.println(digitalRead(relayPins[i]) == HIGH ? "ON" : "OFF"); + Serial.print("Input "); + Serial.print(i + 1); + Serial.print(": "); + Serial.println(digitalRead(inputPins[i]) == LOW ? "Pressed" : "Not Pressed"); + } + Serial.println(); + lastStatusPrintTime = millis(); + } +} + +// Start the Access Point mode +void startAPMode() { + WiFi.softAP(ap_ssid, ap_password); + Serial.println("Access Point Started"); + Serial.print("IP Address: "); + Serial.println(WiFi.softAPIP()); + server.on("/", handleRoot); + server.on("/save", handleSave); + server.on("/relay", handleRelay); // Add handler for relay control + server.begin(); + isAPMode = true; +} + +// Start the web server in client mode +void startWebServer() { + server.on("/", handleRoot); + server.on("/save", handleSave); + server.on("/relay", handleRelay); // Add handler for relay control + server.begin(); +} + +// Get the default MAC address of the ESP32 +String getDefaultMacAddress() { + String mac = ""; + unsigned char mac_base[6] = {0}; + + if (esp_efuse_mac_get_default(mac_base) == ESP_OK) { + char buffer[18]; // 6*2 characters for hex + 5 characters for colons + 1 character for null terminator + sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", mac_base[0], mac_base[1], mac_base[2], mac_base[3], mac_base[4], mac_base[5]); + mac = buffer; + } + + return mac; +} + +// Get the MAC address of a specific interface +String getInterfaceMacAddress(esp_mac_type_t interface) { + String mac = ""; + unsigned char mac_base[6] = {0}; + + if (esp_read_mac(mac_base, interface) == ESP_OK) { + char buffer[18]; // 6*2 characters for hex + 5 characters for colons + 1 character for null terminator + sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X", mac_base[0], mac_base[1], mac_base[2], mac_base[3], mac_base[4], mac_base[5]); + mac = buffer; + } + + return mac; +}