From 0930022173655ad90a6e52927eae8cdc78cec440 Mon Sep 17 00:00:00 2001 From: Hendrik Friedel Date: Thu, 27 Oct 2022 09:26:19 +0200 Subject: [PATCH 1/4] Fixed compileerrors w/ ESP32 and ported to ESP8266 --- platformio.ini | 20 ++++++++++++-- src/FingerprintManager.cpp | 7 +++++ src/FingerprintManager.h | 6 +++++ src/SettingsManager.cpp | 2 +- src/main.cpp | 53 +++++++++++++++++++++++++++++++++++--- 5 files changed, 81 insertions(+), 7 deletions(-) diff --git a/platformio.ini b/platformio.ini index cb3cde5..d7198f8 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,14 +9,30 @@ ; https://docs.platformio.org/page/projectconf.html [env:esp32doit-devkit-v1] -platform = espressif32 +platform = espressif32@3.5.0 # @3.5.0 #otherwise Update.h is missing. See https://community.platformio.org/t/update-espressif-32-version-4-1-0-leads-to-error-collect2-exe-error-ld-returned-1-exit-status/27742/3 + # Other fix is to use a different version of the Asyncwebserver (https://github.com/khoih-prog/ESPAsyncWebServer/archive/refs/heads/master.zip.) board = esp32doit-devkit-v1 framework = arduino monitor_speed = 115200 +lib_deps = + me-no-dev/ESP Async WebServer@^1.2.3 # this should fix it https://github.com/khoih-prog/ESPAsyncWebServer/archive/refs/heads/master.zip. + https://github.com/ayushsharma82/AsyncElegantOTA/archive/refs/heads/master.zip # ayushsharma82/AsyncElegantOTA@^2.2.6 fails with update.h missing + knolleary/PubSubClient@^2.8 + adafruit/Adafruit Fingerprint Sensor Library@^2.0.7 + intrbiz/Crypto@^1.0.0 +lib_ldf_mode = deep + +[env:esp8622] +platform = espressif8266@2.3.2 +framework = arduino +board = esp12e +monitor_speed = 115200 lib_deps = me-no-dev/ESP Async WebServer@^1.2.3 ayushsharma82/AsyncElegantOTA@^2.2.6 knolleary/PubSubClient@^2.8 adafruit/Adafruit Fingerprint Sensor Library@^2.0.7 intrbiz/Crypto@^1.0.0 -lib_ldf_mode = deep + vshymanskyy/Preferences #otherwise Preferences.h was missing + ESP8266wifi +lib_ldf_mode = deep \ No newline at end of file diff --git a/src/FingerprintManager.cpp b/src/FingerprintManager.cpp index ee030fc..53ad5dd 100644 --- a/src/FingerprintManager.cpp +++ b/src/FingerprintManager.cpp @@ -6,7 +6,14 @@ bool FingerprintManager::connect() { // initialize input pins + #if defined(ESP32) pinMode(touchRingPin, INPUT_PULLDOWN); + #endif + + #if defined(ESP8266) + pinMode(touchRingPin,INPUT); + #endif + Serial.println("\n\nAdafruit finger detect test"); diff --git a/src/FingerprintManager.h b/src/FingerprintManager.h index d151510..f7b8882 100644 --- a/src/FingerprintManager.h +++ b/src/FingerprintManager.h @@ -5,7 +5,13 @@ #include #include "global.h" +#ifdef ESP32 #define mySerial Serial2 +#endif + +#ifdef ESP8266 +#define mySerial Serial1 +#endif #define FINGERPRINT_WRITENOTEPAD 0x18 // Write Notepad on sensor #define FINGERPRINT_READNOTEPAD 0x19 // Read Notepad from sensor diff --git a/src/SettingsManager.cpp b/src/SettingsManager.cpp index f5a8e50..251e8b2 100644 --- a/src/SettingsManager.cpp +++ b/src/SettingsManager.cpp @@ -104,7 +104,7 @@ String SettingsManager::generateNewPairingCode() { SHA256 hasher; /* Put some unique values as input in our new hash */ - hasher.doUpdate( String(esp_random()).c_str() ); // random number + hasher.doUpdate( String(rand()).c_str() ); // random number // esp_rand() does not exist for esp8266 hasher.doUpdate( String(millis()).c_str() ); // time since boot hasher.doUpdate(getTimestampString().c_str()); // current time (if NTP is available) hasher.doUpdate(appSettings.mqttUsername.c_str()); diff --git a/src/main.cpp b/src/main.cpp index 8d7db21..1d4d035 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,17 +2,43 @@ Main of FingerprintDoorbell ****************************************************/ -#include + + #include #include #include #include -#include +#if defined(ESP32) +#include "SPIFFS.h" +#include +#endif +#if defined(ESP8266) +#include +#include +#endif #include #include "FingerprintManager.h" #include "SettingsManager.h" #include "global.h" + +#if defined(ESP8266) +bool getLocalTime(struct tm * info, uint32_t ms = 5000) +{ + uint32_t start = millis(); + time_t now; + while((millis()-start) <= ms) { + time(&now); + localtime_r(&now, info); + if(info->tm_year > (2016 - 1900)){ + return true; + } + delay(10); + } + return false; +} +#endif + enum class Mode { scan, enroll, wificonfig, maintenance }; const char* VersionInfo = "0.4"; @@ -216,7 +242,12 @@ bool initWifi() { WifiSettings wifiSettings = settingsManager.getWifiSettings(); WiFi.mode(WIFI_STA); WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE); + #if defined(ESP32) WiFi.setHostname(wifiSettings.hostname.c_str()); //define hostname + #endif + #if defined(ESP8266) + WiFi.hostname(wifiSettings.hostname.c_str()); //define hostname + #endif WiFi.begin(wifiSettings.ssid.c_str(), wifiSettings.password.c_str()); int counter = 0; while (WiFi.status() != WL_CONNECTED) { @@ -250,7 +281,7 @@ void initWiFiAccessPointForConfiguration() { void startWebserver(){ // Initialize SPIFFS - if(!SPIFFS.begin(true)){ + if(!SPIFFS.begin()){ Serial.println("An Error has occurred while mounting SPIFFS"); return; } @@ -553,6 +584,14 @@ void doScan() mqttClient.publish((String(mqttRootTopic) + "/matchName").c_str(), match.matchName.c_str()); mqttClient.publish((String(mqttRootTopic) + "/matchConfidence").c_str(), String(match.matchConfidence).c_str()); Serial.println("MQTT message sent: Open the door!"); + #ifdef CUSTOM_GPIOS + if (strstr("GPIO", match.MatchName) != NULL){ + digitalWrite(customOutput1, HIGH); + delay(500); + digitalWrite(customOutput1, LOW); + Serial.println("Triggering Custom GPIO"); + } + #endif } else { notifyClients("Security issue! Match was not sent by MQTT because of invalid sensor pairing! This could potentially be an attack! If the sensor is new or has been replaced by you do a (re)pairing in settings page."); } @@ -626,9 +665,15 @@ void setup() pinMode(doorbellOutputPin, OUTPUT); #ifdef CUSTOM_GPIOS pinMode(customOutput1, OUTPUT); - pinMode(customOutput2, OUTPUT); + pinMode(customOutput2, OUTPUT); + #ifdef ESP32 pinMode(customInput1, INPUT_PULLDOWN); pinMode(customInput2, INPUT_PULLDOWN); + #endif + #ifdef ESP8266 + pinMode(customInput1); + pinMode(customInput2); + #endif #endif settingsManager.loadWifiSettings(); From 5aa4d2e494cb0cae1eac1fa158b739b6fa4215b6 Mon Sep 17 00:00:00 2001 From: Hendrik Friedel Date: Sun, 30 Oct 2022 16:58:10 +0100 Subject: [PATCH 2/4] Fix of SPIFFS error (formating) and preferences set to RW instead of RO --- src/FingerprintManager.cpp | 2 +- src/main.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/FingerprintManager.cpp b/src/FingerprintManager.cpp index 53ad5dd..9fc2b10 100644 --- a/src/FingerprintManager.cpp +++ b/src/FingerprintManager.cpp @@ -227,7 +227,7 @@ Match FingerprintManager::scanFingerprint() { // Preferences void FingerprintManager::loadFingerListFromPrefs() { Preferences preferences; - preferences.begin("fingerList", true); + preferences.begin("fingerList", false); int counter = 0; for (int i=1; i<=200; i++) { String key = String(i); diff --git a/src/main.cpp b/src/main.cpp index 1d4d035..0783bfc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -281,8 +281,8 @@ void initWiFiAccessPointForConfiguration() { void startWebserver(){ // Initialize SPIFFS - if(!SPIFFS.begin()){ - Serial.println("An Error has occurred while mounting SPIFFS"); + if(!SPIFFS.begin(true)){ + Serial.println("An Error has occurred while mounting SPIFFS. Possibly you need to format the FS. See https://github.com/espressif/arduino-esp32/issues/638"); return; } From 80393cc3cfa8a6a1a044b6eee0e2d91052a0107f Mon Sep 17 00:00:00 2001 From: Hendrik Friedel Date: Mon, 31 Oct 2022 20:32:21 +0100 Subject: [PATCH 3/4] Update of espressif version --- platformio.ini | 9 ++++++--- src/FingerprintManager.cpp | 6 +++++- src/main.cpp | 21 +++++++++++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/platformio.ini b/platformio.ini index d7198f8..7950522 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,8 @@ [env:esp32doit-devkit-v1] platform = espressif32@3.5.0 # @3.5.0 #otherwise Update.h is missing. See https://community.platformio.org/t/update-espressif-32-version-4-1-0-leads-to-error-collect2-exe-error-ld-returned-1-exit-status/27742/3 # Other fix is to use a different version of the Asyncwebserver (https://github.com/khoih-prog/ESPAsyncWebServer/archive/refs/heads/master.zip.) -board = esp32doit-devkit-v1 +board = nodemcu-32s +board_build.flash_mode= qio # fixes SPIFFS error framework = arduino monitor_speed = 115200 lib_deps = @@ -23,9 +24,10 @@ lib_deps = lib_ldf_mode = deep [env:esp8622] -platform = espressif8266@2.3.2 +platform = espressif8266@4.0.1 framework = arduino board = esp12e +board_build.filesystem = spiffs monitor_speed = 115200 lib_deps = me-no-dev/ESP Async WebServer@^1.2.3 @@ -35,4 +37,5 @@ lib_deps = intrbiz/Crypto@^1.0.0 vshymanskyy/Preferences #otherwise Preferences.h was missing ESP8266wifi -lib_ldf_mode = deep \ No newline at end of file +lib_ldf_mode = deep +build_flags = -Wl,-Teagle.flash.4m2m.ld diff --git a/src/FingerprintManager.cpp b/src/FingerprintManager.cpp index 9fc2b10..a857da7 100644 --- a/src/FingerprintManager.cpp +++ b/src/FingerprintManager.cpp @@ -227,7 +227,8 @@ Match FingerprintManager::scanFingerprint() { // Preferences void FingerprintManager::loadFingerListFromPrefs() { Preferences preferences; - preferences.begin("fingerList", false); + Serial.println("loadFingerListFromPrefs"); + preferences.begin("fingerList", true); int counter = 0; for (int i=1; i<=200; i++) { String key = String(i); @@ -385,6 +386,7 @@ void FingerprintManager::deleteFinger(int id) { } else { fingerList[id] = "@empty"; Preferences preferences; + Serial.println("deleteFinger"); preferences.begin("fingerList", false); preferences.remove (String(id).c_str()); preferences.end(); @@ -399,6 +401,7 @@ void FingerprintManager::deleteFinger(int id) { void FingerprintManager::renameFinger(int id, String newName) { if ((id > 0) && (id <= 200)) { Preferences preferences; + Serial.println("deleteFinger"); preferences.begin("fingerList", false); preferences.putString(String(id).c_str(), newName); preferences.end(); @@ -474,6 +477,7 @@ bool FingerprintManager::deleteAll() { { bool rc; Preferences preferences; + Serial.println("deletAll"); rc = preferences.begin("fingerList", false); if (rc) rc = preferences.clear(); diff --git a/src/main.cpp b/src/main.cpp index 0783bfc..56c0a6a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -239,6 +239,7 @@ bool checkPairingValid() { bool initWifi() { // Connect to Wi-Fi + Serial.println("Init Wifi"); WifiSettings wifiSettings = settingsManager.getWifiSettings(); WiFi.mode(WIFI_STA); WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE); @@ -266,6 +267,7 @@ bool initWifi() { } void initWiFiAccessPointForConfiguration() { + Serial.println("initWifiAP"); WiFi.softAPConfig(WifiConfigIp, WifiConfigIp, IPAddress(255, 255, 255, 0)); WiFi.softAP(WifiConfigSsid, WifiConfigPassword); @@ -279,12 +281,27 @@ void initWiFiAccessPointForConfiguration() { void startWebserver(){ - + Serial.println("startWebserver"); // Initialize SPIFFS - if(!SPIFFS.begin(true)){ + if(!SPIFFS.begin()){ Serial.println("An Error has occurred while mounting SPIFFS. Possibly you need to format the FS. See https://github.com/espressif/arduino-esp32/issues/638"); + //SPIFFS.format() return; } + String str = ""; + Serial.println("SPIFFS begin successful. Listing Directories"); + Dir dir = SPIFFS.openDir("/"); + while (dir.next()) { + str += dir.fileName(); + str += " / "; + str += dir.fileSize(); + str += "\r\n"; + } + Serial.print(str); + Serial.println("SPIFFS begin successful. Listing Directories END."); + + + // Init time by NTP Client configTime(gmtOffset_sec, daylightOffset_sec, settingsManager.getAppSettings().ntpServer.c_str()); From b84b50d2820ca139def312ae29e7c4cbc5944fc1 Mon Sep 17 00:00:00 2001 From: Hendrik Friedel Date: Mon, 31 Oct 2022 22:29:24 +0100 Subject: [PATCH 4/4] Added true return code in Settings-Manager if loadWifiSettings or loadAppSettings are successful --- src/SettingsManager.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/SettingsManager.cpp b/src/SettingsManager.cpp index 251e8b2..a82ee80 100644 --- a/src/SettingsManager.cpp +++ b/src/SettingsManager.cpp @@ -8,6 +8,7 @@ bool SettingsManager::loadWifiSettings() { wifiSettings.password = preferences.getString("password", String("")); wifiSettings.hostname = preferences.getString("hostname", String("FingerprintDoorbell")); preferences.end(); + return true; } else { return false; } @@ -25,6 +26,7 @@ bool SettingsManager::loadAppSettings() { appSettings.sensorPairingCode = preferences.getString("pairingCode", ""); appSettings.sensorPairingValid = preferences.getBool("pairingValid", false); preferences.end(); + return true; } else { return false; }