Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed compileerrors w/ ESP32 and ported to ESP8266 #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,33 @@
; https://docs.platformio.org/page/projectconf.html

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
platform = [email protected] # @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 = nodemcu-32s
board_build.flash_mode= qio # fixes SPIFFS error
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 = [email protected]
framework = arduino
board = esp12e
board_build.filesystem = spiffs
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
vshymanskyy/Preferences #otherwise Preferences.h was missing
ESP8266wifi
lib_ldf_mode = deep
build_flags = -Wl,-Teagle.flash.4m2m.ld
11 changes: 11 additions & 0 deletions src/FingerprintManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -220,6 +227,7 @@ Match FingerprintManager::scanFingerprint() {
// Preferences
void FingerprintManager::loadFingerListFromPrefs() {
Preferences preferences;
Serial.println("loadFingerListFromPrefs");
preferences.begin("fingerList", true);
int counter = 0;
for (int i=1; i<=200; i++) {
Expand Down Expand Up @@ -378,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();
Expand All @@ -392,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();
Expand Down Expand Up @@ -467,6 +477,7 @@ bool FingerprintManager::deleteAll() {
{
bool rc;
Preferences preferences;
Serial.println("deletAll");
rc = preferences.begin("fingerList", false);
if (rc)
rc = preferences.clear();
Expand Down
6 changes: 6 additions & 0 deletions src/FingerprintManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
#include <Preferences.h>
#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
Expand Down
4 changes: 3 additions & 1 deletion src/SettingsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -104,7 +106,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());
Expand Down
74 changes: 68 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,43 @@
Main of FingerprintDoorbell
****************************************************/

#include <WiFi.h>


#include <DNSServer.h>
#include <time.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <SPIFFS.h>
#if defined(ESP32)
#include "SPIFFS.h"
#include <WiFi.h>
#endif
#if defined(ESP8266)
#include <FS.h>
#include <ESP8266wifi.h>
#endif
#include <PubSubClient.h>
#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";
Expand Down Expand Up @@ -213,10 +239,16 @@ 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);
#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) {
Expand All @@ -235,6 +267,7 @@ bool initWifi() {
}

void initWiFiAccessPointForConfiguration() {
Serial.println("initWifiAP");
WiFi.softAPConfig(WifiConfigIp, WifiConfigIp, IPAddress(255, 255, 255, 0));
WiFi.softAP(WifiConfigSsid, WifiConfigPassword);

Expand All @@ -248,12 +281,27 @@ void initWiFiAccessPointForConfiguration() {


void startWebserver(){

Serial.println("startWebserver");
// Initialize SPIFFS
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
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());
Expand Down Expand Up @@ -553,6 +601,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.");
}
Expand Down Expand Up @@ -626,9 +682,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();
Expand Down