commit c0c663bd076e5fd2ff3674591dec2bd2e93d2bf1 Author: Blue Fox Date: Thu Dec 29 09:43:33 2022 +0100 First commit diff --git a/ESafeP/ESafeP.ino b/ESafeP/ESafeP.ino new file mode 100644 index 0000000..9fe34bf --- /dev/null +++ b/ESafeP/ESafeP.ino @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include + +// define variables and structs + +String version = "ESafeP 1.3"; // 1.0: Web Interface finished; 1.1: Servo motor works now; 1.2: The 3x4 Keyboard functions; 1.3: Added Keyboard functionality: Change the Code + +int currentWiFiAP = 1; // 1: normal WiFi; 2: alternative WiFi; 3: softAP (ESP opens an access point) +char apSSID[10] = "ESafeP-AP"; +char apPSK[14] = "aA161616161Aa"; +bool apON = false; + +struct WiFiConfig { + char ssid[33]; + char pwd[64]; + char alternative_ssid[33]; + char alternative_pwd[64]; +}; +struct Config { + char code[9]; + char friendly_name[21]; +}; +int wiFiConfigAddress = 4; // this is a pointer where objects are stored on EEPROM (points to the end; points to free space); at 0 the state of establishment is stored (0 = not established or 1 established) +int configAddress = wiFiConfigAddress + sizeof(WiFiConfig); + +int doorServoPin = 12; // pin number where the control line of the servo motor is connected to +Servo doorServo; // servo object for the door +bool safeOpened = false; // value if the safe is opened or closed +int servo_open_angle = 90; +int servo_closed_angle = 135; + +// All pins for I/O (Keyboard, LEDs, etc.) +int keyboard3x4_column1 = 25; +int keyboard3x4_column2 = 26; +int keyboard3x4_column3 = 27; +int keyboard3x4_row1 = 33; +int keyboard3x4_row2 = 32; +int keyboard3x4_row3 = 35; +int keyboard3x4_row4 = 34; +int readyForInputLED = 2; +int wrongCodeLED = 4; +int codeChangeLED = 5; +int newCodeLED = 18; +int newCodeRepeatLED = 19; +int invalidCodeLED = 21; +int typeOldCodeLED = 22; + + +int webport = 80; // the port for the web interface +WebServer server(webport); // the webserver (port "webport") + +void setup() { + pinSetup(); // set up all needed pins + + doorServo.attach(doorServoPin); + + Serial.begin(115200); + EEPROM.begin(4 + sizeof(WiFiConfig) + sizeof(Config)); // load the data on EEPROM into the RAM + connectWiFi(); // connect to wifi on startup + webSetup(); // register the web paths and start the web server + if (MDNS.begin("esp32")) { + Serial.println("MDNS responder started"); // start the mDNS responder and print a message on success + } +} + +void loop() { + // reconnect to WiFi if connection was lost + connectWiFi(); + + // the keyboard functions... + if(check_3x4keyboard_clicked()) { + // show that now the code can be submitted + digitalWrite(readyForInputLED, HIGH); + + // read the code and make checks, to open or close the safe + String output = readCodeFrom3x4keyboard(true); + if (output == getCode() && safeOpened == false && output != "newCodeFinished") { + // open the safe + Serial.println("Opening the safe..."); + open(); + Serial.println("Opened the safe."); + } else if (safeOpened == true && output != "newCodeFinished") { + // close the safe + Serial.println("Closing the safe..."); + close(); + Serial.println("Closed the safe."); + } else if(output != "newCodeFinished") { // if the ouput was not the real code AND no new code was set, THEN show that the code was wrong + blinkLED(wrongCodeLED, 700); + + // debug message + Serial.printf("Wrong code submitted: %s\n", output); + } + + // now show that everything is done and no code will be read now + digitalWrite(readyForInputLED, LOW); + } + + // response a client if it's requesting something + server.handleClient(); +} diff --git a/ESafeP/keyboard.ino b/ESafeP/keyboard.ino new file mode 100644 index 0000000..7fe8ccb --- /dev/null +++ b/ESafeP/keyboard.ino @@ -0,0 +1,135 @@ +bool check_3x4keyboard_clicked() { // just a function that checks if some key on the keyboard was pressed + digitalWrite(keyboard3x4_column1, HIGH); + digitalWrite(keyboard3x4_column2, HIGH); + digitalWrite(keyboard3x4_column3, HIGH); + + if (analogRead(keyboard3x4_row1) >= 4000) { + return true; + } else if (analogRead(keyboard3x4_row2) >= 4000) { + return true; + } else if (analogRead(keyboard3x4_row3) >= 4000) { + return true; + } else if (analogRead(keyboard3x4_row4) >= 4000) { + return true; + } + + return false; +} + +String readCodeFrom3x4keyboard(bool codeChange) { // the parameter "codeChange" is just to prevent a loop, look further down + String input = ""; + int currentpower = 1; + + while (true) { + switch (currentpower) { + case 1: + digitalWrite(keyboard3x4_column3, LOW); + digitalWrite(keyboard3x4_column1, HIGH); + currentpower = 2; + + if (analogRead(keyboard3x4_row1) >= 4000) { + int timeAtClick = millis(); + while (analogRead(keyboard3x4_row1) >= 4000) {} + if(millis() - timeAtClick >= 2000 && codeChange) { // if the button was pressed longer than 2 seconds then... (here's the loop-preventing codeChange parameter) + Serial.println("Changing code..."); + digitalWrite(codeChangeLED, HIGH); // turn on the led that indicates code change + digitalWrite(typeOldCodeLED, HIGH); // turn on the led that prompts to type in the old code + + // read the old code and check if it's correct + String currentCode = readCodeFrom3x4keyboard(false); + while(currentCode != getCode()) { blinkLED(wrongCodeLED, 700); currentCode = readCodeFrom3x4keyboard(false); } + + // show that now the new code has to be submitted (only if the old code was right) + digitalWrite(typeOldCodeLED, LOW); + digitalWrite(newCodeLED, HIGH); + + // read the new code + String newCode = readCodeFrom3x4keyboard(false); + while(newCode.length() != 8) { blinkLED(invalidCodeLED, 700); newCode = readCodeFrom3x4keyboard(false); } // make sure the code is 8 numbers long + + // prompt the user to repeat the code + digitalWrite(newCodeLED, LOW); + digitalWrite(newCodeRepeatLED, HIGH); + + // read repeated new code + String newCodeRepeat = readCodeFrom3x4keyboard(false); + while(newCode != newCodeRepeat) { blinkLED(wrongCodeLED, 700); newCodeRepeat = readCodeFrom3x4keyboard(false); } + + // ...and finally set the code + setCode(newCode); + + // turn off the used leds + digitalWrite(newCodeRepeatLED, LOW); + digitalWrite(codeChangeLED, LOW); + + // debug message + Serial.println("Changed code."); + + return "newCodeFinished"; // statuscode "newCodeFinished" (just shows that a new code was set) + } else { // if pressed short, just remove one number from the read code + input = input.substring(0, input.length() - 1); + } + delay(10); + } else if (analogRead(keyboard3x4_row2) >= 4000) { + while (analogRead(keyboard3x4_row2) >= 4000) {} + delay(10); + input += "7"; + } else if (analogRead(keyboard3x4_row3) >= 4000) { + while (analogRead(keyboard3x4_row3) >= 4000) {} + delay(10); + input += "4"; + } else if (analogRead(keyboard3x4_row4) >= 4000) { + while (analogRead(keyboard3x4_row4) >= 4000) {} + delay(10); + input += "1"; + } + break; + case 2: + digitalWrite(keyboard3x4_column1, LOW); + digitalWrite(keyboard3x4_column2, HIGH); + currentpower = 3; + + if (analogRead(keyboard3x4_row1) >= 4000) { + while (analogRead(keyboard3x4_row1) >= 4000) {} + delay(10); + input += "0"; + } else if (analogRead(keyboard3x4_row2) >= 4000) { + while (analogRead(keyboard3x4_row2) >= 4000) {} + delay(10); + input += "8"; + } else if (analogRead(keyboard3x4_row3) >= 4000) { + while (analogRead(keyboard3x4_row3) >= 4000) {} + delay(10); + input += "5"; + } else if (analogRead(keyboard3x4_row4) >= 4000) { + while (analogRead(keyboard3x4_row4) >= 4000) {} + delay(10); + input += "2"; + } + break; + case 3: + digitalWrite(keyboard3x4_column2, LOW); + digitalWrite(keyboard3x4_column3, HIGH); + currentpower = 1; + + if (analogRead(keyboard3x4_row1) >= 4000) { + while (analogRead(keyboard3x4_row1) >= 4000) {} + delay(10); + return input; + } else if (analogRead(keyboard3x4_row2) >= 4000) { + while (analogRead(keyboard3x4_row2) >= 4000) {} + delay(10); + input += "9"; + } else if (analogRead(keyboard3x4_row3) >= 4000) { + while (analogRead(keyboard3x4_row3) >= 4000) {} + delay(10); + input += "6"; + } else if (analogRead(keyboard3x4_row4) >= 4000) { + while (analogRead(keyboard3x4_row4) >= 4000) {} + delay(10); + input += "3"; + } + break; + } + } +} diff --git a/ESafeP/utils.ino b/ESafeP/utils.ino new file mode 100644 index 0000000..832491b --- /dev/null +++ b/ESafeP/utils.ino @@ -0,0 +1,308 @@ +int biggest2thPotency(int number) { + int res = 1; + + while (res <= number) { + res <<= 1; // multiply res with 2 (res << 1) as long as res isn't bigger than the number, + } + // so that we have the nearest (bigger) 2th potency. + res >>= 1; // now divide the result once so that we have the nearest (but smaller or equal) 2th potency + return res; +} +bool checkCommitID(int commit_id, int searched_bit) { + // a function that checks if the searched_bit was ever set + // Technically: the commit_id is an Integer which is the sum of other numbers (related to the ext4 permissions). List of those numbers: + // *** 1: WiFi *** + // *** 2: Alternative WiFi *** + // *** 4: Code *** + // *** 8: Friendly name *** + + int b2thP; + while (commit_id > 0) { + b2thP = biggest2thPotency(commit_id); + if (b2thP == searched_bit) { + return true; + } + commit_id -= b2thP; + } + return false; +} +bool hasWiFi(int commit_id) { + return checkCommitID(commit_id, 1); +} +bool hasAlternativeWiFi(int commit_id) { + return checkCommitID(commit_id, 2); +} +bool hasCode(int commit_id) { + return checkCommitID(commit_id, 4); +} +bool hasFriendlyName(int commit_id) { + return checkCommitID(commit_id, 8); +} + +void setFriendlyName(String name) { + // get the configuration + Config config; EEPROM.get(configAddress, config); + + // convert the String to a char array + int nameLength = name.length(); + char nameArray[21]; + for (int i = 0; i <= 20; i++) { + if (nameLength >= i) { + nameArray[i] = name[i]; + } else { + nameArray[i] = (char) 0; + } + } + + // get the commit id + int commit_id; EEPROM.get(0, commit_id); + + if (hasFriendlyName(commit_id)) { + strncpy(config.friendly_name, nameArray, 21); + } + else { + strncpy(config.friendly_name, nameArray, 21); + EEPROM.put(0, commit_id + 8); + } + + // save the config + EEPROM.put(configAddress, config); + EEPROM.commit(); +} +void setCode(String code) { + // get the configuration + Config config; EEPROM.get(configAddress, config); + + // convert the String to a char array + int codeLength = code.length(); + char codeArray[9]; + for (int i = 0; i <= 8; i++) { + if (codeLength >= i) { + codeArray[i] = code[i]; + } else { + codeArray[i] = (char) 0; + } + } + + // get the commit id + int commit_id; EEPROM.get(0, commit_id); + + if (hasCode(commit_id)) { + strncpy(config.code, codeArray, 9); + } + else { + strncpy(config.code, codeArray, 9); + EEPROM.put(0, commit_id + 4); + } + + // save the config + EEPROM.put(configAddress, config); + EEPROM.commit(); +} +void setWiFi(String ssid, String password) { + // get the configuration + WiFiConfig config; EEPROM.get(wiFiConfigAddress, config); + + // convert the Strings to char arrays + int ssidLength = ssid.length(); int pwdLength = password.length(); + char ssidArray[33]; + char pwdArray[64]; + for (int i = 0; i <= 31; i++) { + if (ssidLength >= i) { + ssidArray[i] = ssid[i]; + } else { + ssidArray[i] = (char) 0; + } + } + for (int i = 0; i <= 62; i++) { + if (pwdLength >= i) { + pwdArray[i] = password[i]; + } else { + pwdArray[i] = (char) 0; + } + } + + // get the commit id + int commit_id; EEPROM.get(0, commit_id); + + if (hasWiFi(commit_id)) { + strncpy(config.ssid, ssidArray, 33); + strncpy(config.pwd, pwdArray, 64); + } + else { + strncpy(config.ssid, ssidArray, 33); + strncpy(config.pwd, pwdArray, 64); + EEPROM.put(0, commit_id + 1); + } + + // save the config + EEPROM.put(wiFiConfigAddress, config); + EEPROM.commit(); + + // reboot to go into the wifi + ESP.restart(); +} +void setAlternativeWiFi(String ssid, String password) { + // get the configuration + WiFiConfig config; EEPROM.get(wiFiConfigAddress, config); + + // convert the Strings to char arrays + int ssidLength = ssid.length(); int pwdLength = password.length(); + char ssidArray[33]; + char pwdArray[64]; + for (int i = 0; i <= 31; i++) { + if (ssidLength >= i) { + ssidArray[i] = ssid[i]; + } else { + ssidArray[i] = (char) 0; + } + } + for (int i = 0; i <= 62; i++) { + if (pwdLength >= i) { + pwdArray[i] = password[i]; + } else { + pwdArray[i] = (char) 0; + } + } + + // get the commit id + int commit_id; EEPROM.get(0, commit_id); + + if (hasAlternativeWiFi(commit_id)) { + strncpy(config.alternative_ssid, ssidArray, 33); + strncpy(config.alternative_pwd, pwdArray, 64); + } + else { + strncpy(config.alternative_ssid, ssidArray, 33); + strncpy(config.alternative_pwd, pwdArray, 64); + EEPROM.put(0, commit_id + 2); + } + + // save the config + EEPROM.put(wiFiConfigAddress, config); + EEPROM.commit(); + + // reboot to go into the wifi + ESP.restart(); +} +String getFriendlyName() { + Config config; EEPROM.get(configAddress, config); + int commitID; EEPROM.get(0, commitID); + if (hasFriendlyName(commitID)) { + return (String) config.friendly_name; + } + else { + setFriendlyName("ESafeP #1"); // set a default + return getFriendlyName(); + } +} +String getCode() { + Config config; EEPROM.get(configAddress, config); + int commitID; EEPROM.get(0, commitID); + if (hasCode(commitID)) { + return (String) config.code; + } + else { + setCode("12345678"); // set a default + return getCode(); + } +} +String getWiFiSSID() { + WiFiConfig config; EEPROM.get(wiFiConfigAddress, config); + int commitID; EEPROM.get(0, commitID); + if (hasWiFi(commitID)) { + return (String) config.ssid; + } + else { + return ""; + } +} +String getWiFiPassword() { + WiFiConfig config; EEPROM.get(wiFiConfigAddress, config); + int commitID; EEPROM.get(0, commitID); + if (hasWiFi(commitID)) { + return (String) config.pwd; + } + else { + return ""; + } +} +String getAlternativeWiFiSSID() { + WiFiConfig config; EEPROM.get(wiFiConfigAddress, config); + int commitID; EEPROM.get(0, commitID); + if (hasAlternativeWiFi(commitID)) { + return (String) config.alternative_ssid; + } + else { + return ""; + } +} +String getAlternativeWiFiPassword() { + WiFiConfig config; EEPROM.get(wiFiConfigAddress, config); + int commitID; EEPROM.get(0, commitID); + if (hasAlternativeWiFi(commitID)) { + return (String) config.alternative_pwd; + } + else { + return ""; + } +} +void reset() { + EEPROM.begin(4096); + + for (int i = 0 ; i <= 4096 ; i++) { + EEPROM.write(i, 0); + } + + EEPROM.commit(); +} +void open() { + doorServo.write(servo_open_angle); + safeOpened = true; + + // if this is not done, the ESP32Servo library would try to set the servo angle to 1 degree all the time. So this resets the connection to the servo motor + delay(400); + doorServo.detach(); + doorServo.attach(doorServoPin); +} +void close() { + doorServo.write(servo_closed_angle); + safeOpened = false; + + // if this is not done, the ESP32Servo library would try to set the servo angle to 1 degree all the time. So this resets the connection to the servo motor + delay(400); + doorServo.detach(); + doorServo.attach(doorServoPin); +} +bool isIn(String target, String list[], int listlength) { + for (int i = 0; i < listlength; i++) { + //list[i].toCharArray(listElmnt, list[i].length()); + if (list[i] == target) { + return true; + } + } + return false; +} +void blinkLED(int pin, int length) { + digitalWrite(pin, HIGH); + delay(length); + digitalWrite(pin, LOW); +} + +void pinSetup() { + pinMode(keyboard3x4_column1, OUTPUT); // The I/O Pins of the 3x4 Keyboard + pinMode(keyboard3x4_column2, OUTPUT); // The I/O Pins of the 3x4 Keyboard + pinMode(keyboard3x4_column3, OUTPUT); // The I/O Pins of the 3x4 Keyboard + pinMode(keyboard3x4_row1, INPUT); // The I/O Pins of the 3x4 Keyboard + pinMode(keyboard3x4_row2, INPUT); // The I/O Pins of the 3x4 Keyboard + pinMode(keyboard3x4_row3, INPUT); // The I/O Pins of the 3x4 Keyboard + pinMode(keyboard3x4_row4, INPUT); // The I/O Pins of the 3x4 Keyboard + + pinMode(readyForInputLED, OUTPUT); + pinMode(wrongCodeLED, OUTPUT); + pinMode(codeChangeLED, OUTPUT); + pinMode(newCodeLED, OUTPUT); + pinMode(newCodeRepeatLED, OUTPUT); + pinMode(invalidCodeLED, OUTPUT); + pinMode(typeOldCodeLED, OUTPUT); +} diff --git a/ESafeP/web.ino b/ESafeP/web.ino new file mode 100644 index 0000000..e39cead --- /dev/null +++ b/ESafeP/web.ino @@ -0,0 +1,329 @@ +void root() { + Serial.println("WEB: 200 - GET '/'"); + + String html = "Your safe!

ESafeP

"; + html += getFriendlyName(); + html += "

"; + html += safeOpened ? "Offen" : "Zu"; + html += "


"; + html += safeOpened ? + "
" + : "
"; + html += "


" + version + "
"; + server.send(200, "text/html", html); +} + +void all_css(){ + Serial.println("WEB: 200 - GET '/all.css'"); + + String css = "td{text-align:center;padding:3pt;}div,fieldset,input,select{padding:5px;font-size:1em}p{margin:.5em 0}input{width:100%;background:#fff;color:#2a2a2e;border-radius:20pt;text-align:center;border:0;line-height:2rem;font-size:15.5pt;}input[type=checkbox],input[type=radio]{width:1em;margin-right:6px;vertical-align:-1px}input[type=range]{width:99%}select{width:100%;background:#fff;color:#000}textarea{resize:vertical;width:98%;height:318px;padding:5px;overflow:auto;background:#fff;color:#000}body{text-align:center;font-family:verdana,sans-serif;background:#2a2a2e;color:#ffffff;}button{border:0;border-radius:20pt;background:#0dcaf0;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;-webkit-transition-duration:.2s;transition-duration:.2s;cursor:pointer}button:hover{background:#0d6efd}.bred{background:#fd7e14}.bred:hover{background:#dc3545}.bgrn{background:#47c266}.bgrn:hover{background:#5aaf6f}a{color:#1fa3ec;text-decoration:none}.p{float:left;text-align:left}.q{float:right;text-align:right}.r{border-radius:.3em;padding:2px;margin:6px 2px}"; + server.send(200, "text/css", css); +} + +void favicon_svg() { + Serial.println("WEB: 200 - GET '/favicon.svg'"); + + String svg = ""; + server.send(200, "image/svg+xml", svg); +} + +void settings() { + Serial.println("WEB: 200 - GET '/settings'"); + + String html = "Einstellungen



Einstellungen





" + version + "
"; + server.send(200, "text/html", html); +} + +void settings_chpwdform() { + Serial.println("WEB: 200 - GET '/settings/chpwdform'"); + + String html = ""; + if(server.hasArg("codes-not-equal")) { html += ""; } + html += "Einstellungen



Code ändern







" + version + "
"; + + server.send(200, "text/html", html); +} + +void settings_chpwd() { + Serial.println("WEB: 200 - GET '/settings/chpwd'"); + + if(server.hasArg("code_old") && server.hasArg("code_new")) { + String code_old = server.arg("code_old"); + String code_new = server.arg("code_new"); + if(code_old.length() != 8 || code_new.length() != 8) { + server.sendHeader("Location", "/settings/chpwdform?", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/settings/chpwdform?'

"); + } + if(code_old == getCode()) { + setCode(code_new); + } else { + server.sendHeader("Location", "/settings/chpwdform?codes-not-equal=1", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/settings/chpwdform?codes-not-equal=1'

"); + } + server.sendHeader("Location", "/", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/'

"); + } else { + server.sendHeader("Location", "/settings/chpwdform", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/settings/chpwdform'

"); + } +} + +void settings_chfriendlynameform() { + Serial.println("WEB: 200 - GET '/settings/chfriendlynameform'"); + + String html = "Einstellungen



Namen ändern






" + version + "
"; + server.send(200, "text/html", html); +} + +void settings_chfrdyname() { + Serial.println("WEB: 200 - GET '/settings/change_frdlyname'"); + if(server.hasArg("name")) { + String name = server.arg("name"); + if(name.length() <= 20) { + setFriendlyName(name); + } + } + server.sendHeader("Location", "/", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/'

"); +} + +void settings_chwificonfigform() { + Serial.println("WEB: 200 - GET '/settings/chwificonfigform'"); + + // get commit id (for wifi password) + int commit_id; EEPROM.get(0, commit_id); + + // do the wifi scan + int total_network_count = WiFi.scanNetworks(); + String unique[total_network_count]; int unique_counter = 0; + + String html = ""; + if(server.hasArg("ssidandpwdneeded")) { html += ""; } + html = "WLAN-Konfiguration



"; + html += "WLAN-Konfiguration"; + html += "


MAC-Addresse des Tresors

"; + html += WiFi.macAddress(); + html += "


Verfügbare WLAN-Netzwerke

"; + for(int i = 0; i < total_network_count; i++) { + if(!isIn(WiFi.SSID(i), unique, unique_counter)) { // duplications are not displayed + html += ""; + + unique[unique_counter] = WiFi.SSID(i); + unique_counter++; + } + } + html += "
SSIDRSSI
"; + html += WiFi.SSID(i); + html += ""; + html += WiFi.encryptionType(i) == WIFI_AUTH_OPEN ? "Offen" : ""; + html += ""; + html += WiFi.RSSI(i); + html += "










" + version + "
"; + server.send(302, "text/html", html); +} + +void settings_chwifi() { + Serial.println("WEB: 200 - GET '/settings/chwifi'"); + + if(!server.hasArg("wifissid") && !server.hasArg("wifipwd") && !server.hasArg("alternativewifissid") && !server.hasArg("alternativewifipwd")) { // no data to use + server.sendHeader("Location", "/settings/chwificonfigform", true); // Redirect to our html web page + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/settings/chwificonfigform'

"); + } + if((server.hasArg("wifissid") && !server.hasArg("wifipwd") || (!server.hasArg("wifissid") && server.hasArg("wifipwd"))) || ((server.hasArg("alternativewifissid") && !server.hasArg("alternativewifipwd")) || (!server.hasArg("alternativewifissid") && server.hasArg("alternativewifipwd")))) { // only if ssid OR pwd were typed in + server.sendHeader("Location", "/settings/chwificonfigform?ssidandpwdneeded=1", true); // Redirect to our html web page + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/settings/chwificonfigform&ssidandpwdneeded=1'

"); + } + if(server.hasArg("wifissid") && server.hasArg("wifipwd")) { + if(server.arg("wifissid") != "" && server.arg("wifipwd") != "") { + String ssid = server.arg("wifissid"); + String pwd = server.arg("wifipwd"); + + setWiFi(ssid, pwd); + } + } + if(server.hasArg("alternativewifissid") && server.hasArg("alternativewifipwd")) { + if(server.arg("alternativewifissid") != "" && server.arg("alternativewifipwd") != "") { + String alternative_ssid = server.arg("alternativewifissid"); + String alternative_pwd = server.arg("alternativewifipwd"); + + setAlternativeWiFi(alternative_ssid, alternative_pwd); + } + } + + server.sendHeader("Location", "/", true); // Redirect to our html web page + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/'

"); +} + +void info() { + Serial.println("WEB: 200 - GET '/info'"); + + String html = ""; + server.send(200, "text/html", html); +} + +void reboot() { // restart the ESP8266 when this is requested + Serial.println("WEB: 200 - GET '/reboot'"); + + String html = "Please wait




Neustart



Bitte warten.



"; + server.send(200, "text/html", html); + Serial.println("----- REBOOTING -----"); + delay(2000); + ESP.restart(); +} + +void settings_reset() { + Serial.println("WEB: 200 - GET '/reset'"); + + if(server.hasArg("code")) { + if(server.arg("code") == getCode()) { + Serial.println("----- RESETTING -----"); + reset(); + Serial.println("... RESET!"); + server.sendHeader("Location", "/settings", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/settings'

"); + } else { + server.sendHeader("Location", "/check?false=1&reset=1", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/check?false=1&reset=1'

"); + } + } else { + server.sendHeader("Location", "/check?reset=1", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/check?reset=1'

"); + } +} + +void open_web() { + Serial.println("WEB: 200 - GET '/open'"); + + if(server.hasArg("code")) { + if(server.arg("code") == getCode()) { + Serial.println("----- OPENING -----"); + open(); + Serial.println("... OPENED"); + server.sendHeader("Location", "/", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/'

"); + } else { + server.sendHeader("Location", "/check?false=1&opensafe=1", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/check?false=1&opensafe=1'

"); + } + } else { + server.sendHeader("Location", "/check?opensafe=1", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/check?opensafe=1'

"); + } +} + +void close_web() { + Serial.println("----- CLOSING -----"); + close(); + Serial.println("... CLOSED"); + server.sendHeader("Location", "/", true); + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/'

"); +} + +void check() { + Serial.println("WEB: GET - 200 '/check'"); + + String html = ""; + html += server.hasArg("false") ? "" : ""; + html += "Einstellungen



Bist du das?






" + version + "
"; + server.send(200, "text/html", html); +} + + +void webSetup() { + server.on("/", root); + server.on("/all.css", all_css); + server.on("/favicon.svg", favicon_svg); + server.on("/check", check); + server.on("/open", open_web); + server.on("/close", close_web); + + server.on("/settings", settings); + server.on("/settings/chpwdform", settings_chpwdform); + server.on("/settings/chpwd", settings_chpwd); + server.on("/settings/chfriendlynameform", settings_chfriendlynameform); + server.on("/settings/change_frdlyname", settings_chfrdyname); + server.on("/settings/chwificonfigform", settings_chwificonfigform); + server.on("/settings/chwifi", settings_chwifi); + + server.on("/info", info); + + server.on("/reboot", reboot); + server.on("/reset", settings_reset); + + server.onNotFound([]() { + Serial.println("WEB: 404: Redirecting to '/'."); + server.sendHeader("Location", "/", true); // Redirect to our html web page + server.send(302, "text/html", "Code 302

302 Redirect

Redirect to '/'

"); + }); + + Serial.println("Starting web server on port " + String(webport)); + server.begin(); + Serial.println("Started web server"); +} +void connectWiFi() { // connect to the wifi with the above defined credentials + if(WiFi.status() == WL_CONNECTED) { return; } // return if not connected + switch(currentWiFiAP) { + case 1: { + Serial.println("Connecting to WiFi..."); + String WiFiSSIDstr = getWiFiSSID(); + String WiFiPSKstr = getWiFiPassword(); + char WiFiSSID[33]; + char WiFiPSK[64]; + + for(int i = 0; i<33; i++) { if(WiFiSSIDstr.length() >= i) {WiFiSSID[i] = WiFiSSIDstr[i];} else {WiFiSSID[i] = (char) 0;} } + for(int i = 0; i<64; i++) { if(WiFiPSKstr.length() >= i) {WiFiPSK[i] = WiFiPSKstr[i];} else {WiFiPSK[i] = (char) 0;} } + + WiFi.begin(WiFiSSID, WiFiPSK); + delay(5000); // pause the program 5 secs + if(WiFi.status() != WL_CONNECTED) { + currentWiFiAP = 2; + break; + } + Serial.printf("Connected to wifi %s, got IP-Adress ", WiFiSSID); + Serial.println(WiFi.localIP()); + } + break; + case 2: { + Serial.println("Connecting to alternative WiFi"); + String alternativeWiFiSSIDstr = getAlternativeWiFiSSID(); + String alternativeWiFiPSKstr = getAlternativeWiFiPassword(); + char alternativeWiFiSSID[33]; + char alternativeWiFiPSK[64]; + + for(int i = 0; i<33; i++) { if(alternativeWiFiSSIDstr.length() >= i) {alternativeWiFiSSID[i] = alternativeWiFiSSIDstr[i];} else {alternativeWiFiSSID[i] = (char) 0;} } + for(int i = 0; i<64; i++) { if(alternativeWiFiPSKstr.length() >= i) {alternativeWiFiPSK[i] = alternativeWiFiPSKstr[i];} else {alternativeWiFiPSK[i] = (char) 0;} } + + WiFi.begin(alternativeWiFiSSID, alternativeWiFiPSK); + delay(5000); // pause the program 5 secs + if(WiFi.status() != WL_CONNECTED) { + currentWiFiAP = 3; + } + } + break; + case 3: { + if(!apON) { + Serial.println("Opening AP"); + WiFi.softAP(apSSID, apPSK); + apON = true; + Serial.println("Opened AP"); + } + } + break; + } +} diff --git a/HTML/all.css b/HTML/all.css new file mode 100644 index 0000000..4c57bc9 --- /dev/null +++ b/HTML/all.css @@ -0,0 +1 @@ +div,fieldset,input,select{padding:5px;font-size:1em}p{margin:.5em 0}input{width:100%;background:#fff;color:#2a2a2e;border-radius:20pt;text-align:center;border:0;line-height:2rem;font-size:15.5pt;}input[type=checkbox],input[type=radio]{width:1em;margin-right:6px;vertical-align:-1px}input[type=range]{width:99%}select{width:100%;background:#fff;color:#000}textarea{resize:vertical;width:98%;height:318px;padding:5px;overflow:auto;background:#fff;color:#000}body{text-align:center;font-family:verdana,sans-serif;background:#2a2a2e;color:#ffffff;}td{padding:0}button{border:0;border-radius:20pt;background:#0dcaf0;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;-webkit-transition-duration:.2s;transition-duration:.2s;cursor:pointer}button:hover{background:#0d6efd}.bred{background:#fd7e14}.bred:hover{background:#dc3545}.bgrn{background:#47c266}.bgrn:hover{background:#5aaf6f}a{color:#1fa3ec;text-decoration:none}.p{float:left;text-align:left}.q{float:right;text-align:right}.r{border-radius:.3em;padding:2px;margin:6px 2px} diff --git a/HTML/chfriendlynameform b/HTML/chfriendlynameform new file mode 100644 index 0000000..3b1c742 --- /dev/null +++ b/HTML/chfriendlynameform @@ -0,0 +1,26 @@ + + + + + + + Einstellungen + + +
+

+ + + + +
+
+ Namen ändern +
+

+
+ +
+
+ + diff --git a/HTML/chfriendlynameform.html b/HTML/chfriendlynameform.html new file mode 100644 index 0000000..84c102a --- /dev/null +++ b/HTML/chfriendlynameform.html @@ -0,0 +1,34 @@ + + + + + + + Einstellungen + + +
+

+ + + + +
+
+ Namen ändern +
+

+
+
+
+
+ +

+ +
+
+
+ ESafeP 1.0 (ESP 8266) +
+ + diff --git a/HTML/favicon.svg b/HTML/favicon.svg new file mode 100644 index 0000000..a99e482 --- /dev/null +++ b/HTML/favicon.svg @@ -0,0 +1,10 @@ + + + + + diff --git a/HTML/index.html b/HTML/index.html new file mode 100644 index 0000000..7c018ff --- /dev/null +++ b/HTML/index.html @@ -0,0 +1,46 @@ + + + + + + + + Your safe! + + + +
+
+

ESafeP

+

%s

+
+
+
+ + + + + + + +
%s
+
+
+
+
+
+

+
+

+
+

+

+
+

+ +
+ +