Skip to content

Commit 7fa6cd7

Browse files
author
Tobias Kolb
committed
Some new Features:
- Option in settings to just delete all fingerprints without doing a factory reset - Log message is send to MqttTopic "/lastLogMessage" - Hidden feature to use 4 unused GPIOs as DI/DO over MQTT (has to be enabled in build by using compiler flag "CUSTOM_GPIOS")
1 parent 97b9de0 commit 7fa6cd7

File tree

3 files changed

+99
-9
lines changed

3 files changed

+99
-9
lines changed

data/settings.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
- "%MQTT_ROOTTOPIC%/matchId"<br>
9595
- "%MQTT_ROOTTOPIC%/matchName"<br>
9696
- "%MQTT_ROOTTOPIC%/matchConfidence"<br>
97+
- "%MQTT_ROOTTOPIC%/lastLogMessage"<br>
9798
Subscribed Topics (=read)<br>
9899
- "%MQTT_ROOTTOPIC%/ignoreTouchRing"
99100
</small>
@@ -130,6 +131,7 @@
130131
<div class="col-md-4">
131132
<button id="btnFirmwareUpdate" name="btnFirmwareUpdate" class="btn btn-info" type="submit" formaction="update">Firmware-Update </button>
132133
<button id="btnDoPairing" name="btnDoPairing" class="btn btn-warning" type="submit" formaction="pairing">Pairing a new sensor </button>
134+
<button id="btnDeleteAllFingerprints" name="btnDeleteAllFingerprints" class="btn btn-danger" type="submit" formaction="deleteAllFingerprints" onclick="return confirm('This will delete all fingerprints. Are you sure you wanna do that?')">Delete all Fingerprints</button>
133135
<button id="btnFactoryReset" name="btnFactoryReset" class="btn btn-danger" type="submit" formaction="factoryReset" onclick="return confirm('This will delete all fingerprints, your settings and your WiFi configuration. Are you sure you wanna do that?')">Factory-Reset</button>
134136
</div>
135137
</div>

src/FingerprintManager.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ void FingerprintManager::deleteFinger(int id) {
383383
preferences.end();
384384
Serial.println(String("Finger template #") + id + " deleted from sensor and prefs.");
385385

386-
// TODO update hash on sensor
387386
}
388387
}
389388

@@ -472,6 +471,11 @@ bool FingerprintManager::deleteAll() {
472471
if (rc)
473472
rc = preferences.clear();
474473
preferences.end();
474+
475+
for (int i=1; i<=200; i++) {
476+
fingerList[i] = String("@empty");
477+
};
478+
475479
return rc;
476480
}
477481
else

src/main.cpp

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ const long gmtOffset_sec = 0; // UTC Time
2828
const int daylightOffset_sec = 0; // UTC Time
2929
const int doorbellOutputPin = 19; // pin connected to the doorbell (when using hardware connection instead of mqtt to ring the bell)
3030

31+
#ifdef CUSTOM_GPIOS
32+
const int customOutput1 = 18; // not used internally, but can be set over MQTT
33+
const int customOutput2 = 26; // not used internally, but can be set over MQTT
34+
const int customInput1 = 21; // not used internally, but changes are published over MQTT
35+
const int customInput2 = 22; // not used internally, but changes are published over MQTT
36+
bool customInput1Value = false;
37+
bool customInput2Value = false;
38+
#endif
39+
3140
const int logMessagesCount = 5;
3241
String logMessages[logMessagesCount]; // log messages, 0=most recent log message
3342
bool shouldReboot = false;
@@ -136,12 +145,13 @@ String processor(const String& var){
136145

137146
// send LastMessage to websocket clients
138147
void notifyClients(String message) {
139-
message = "[" + getTimestampString() + "]: " + message;
140-
Serial.println(message);
141-
addLogMessage(message);
148+
String messageWithTimestamp = "[" + getTimestampString() + "]: " + message;
149+
Serial.println(messageWithTimestamp);
150+
addLogMessage(messageWithTimestamp);
142151
events.send(getLogMessagesAsHtml().c_str(),"message",millis(),1000);
143-
//Serial.println("New message: " + getLogMessagesAsHtml());
144-
152+
153+
String mqttRootTopic = settingsManager.getAppSettings().mqttRootTopic;
154+
mqttClient.publish((String(mqttRootTopic) + "/lastLogMessage").c_str(), message.c_str());
145155
}
146156

147157
void updateClientsFingerlist(String fingerlist) {
@@ -391,6 +401,22 @@ void startWebserver(){
391401
});
392402

393403

404+
webServer.on("/deleteAllFingerprints", HTTP_GET, [](AsyncWebServerRequest *request){
405+
if(request->hasArg("btnDeleteAllFingerprints"))
406+
{
407+
notifyClients("Deleting all fingerprints...");
408+
409+
if (!fingerManager.deleteAll())
410+
notifyClients("Finger database could not be deleted.");
411+
412+
request->redirect("/");
413+
414+
} else {
415+
request->send(SPIFFS, "/settings.html", String(), false, processor);
416+
}
417+
});
418+
419+
394420
webServer.onNotFound([](AsyncWebServerRequest *request){
395421
request->send(404);
396422
});
@@ -443,6 +469,25 @@ void mqttCallback(char* topic, byte* message, unsigned int length) {
443469
}
444470
}
445471

472+
#ifdef CUSTOM_GPIOS
473+
if (String(topic) == String(settingsManager.getAppSettings().mqttRootTopic) + "/customOutput1") {
474+
if(messageTemp == "on"){
475+
digitalWrite(customOutput1, HIGH);
476+
}
477+
else if(messageTemp == "off"){
478+
digitalWrite(customOutput1, LOW);
479+
}
480+
}
481+
if (String(topic) == String(settingsManager.getAppSettings().mqttRootTopic) + "/customOutput2") {
482+
if(messageTemp == "on"){
483+
digitalWrite(customOutput2, HIGH);
484+
}
485+
else if(messageTemp == "off"){
486+
digitalWrite(customOutput2, LOW);
487+
}
488+
}
489+
#endif
490+
446491
}
447492

448493
void connectMqttClient() {
@@ -452,7 +497,7 @@ void connectMqttClient() {
452497
bool connectResult;
453498

454499
// connect with or witout authentication
455-
String lastWillTopic = settingsManager.getAppSettings().mqttRootTopic + "/lastWill";
500+
String lastWillTopic = settingsManager.getAppSettings().mqttRootTopic + "/lastLogMessage";
456501
String lastWillMessage = "FingerprintDoorbell disconnected unexpectedly";
457502
if (settingsManager.getAppSettings().mqttUsername.isEmpty() || settingsManager.getAppSettings().mqttPassword.isEmpty())
458503
connectResult = mqttClient.connect(settingsManager.getWifiSettings().hostname.c_str(),lastWillTopic.c_str(), 1, false, lastWillMessage.c_str());
@@ -464,6 +509,13 @@ void connectMqttClient() {
464509
Serial.println("connected");
465510
// Subscribe
466511
mqttClient.subscribe((settingsManager.getAppSettings().mqttRootTopic + "/ignoreTouchRing").c_str(), 1); // QoS = 1 (at least once)
512+
#ifdef CUSTOM_GPIOS
513+
mqttClient.subscribe((settingsManager.getAppSettings().mqttRootTopic + "/customOutput1").c_str(), 1); // QoS = 1 (at least once)
514+
mqttClient.subscribe((settingsManager.getAppSettings().mqttRootTopic + "/customOutput2").c_str(), 1); // QoS = 1 (at least once)
515+
#endif
516+
517+
518+
467519
} else {
468520
if (mqttClient.state() == 4 || mqttClient.state() == 5) {
469521
mqttConfigValid = false;
@@ -570,8 +622,14 @@ void setup()
570622
while (!Serial); // For Yun/Leo/Micro/Zero/...
571623
delay(100);
572624

573-
// initialize output pin
625+
// initialize GPIOs
574626
pinMode(doorbellOutputPin, OUTPUT);
627+
#ifdef CUSTOM_GPIOS
628+
pinMode(customOutput1, OUTPUT);
629+
pinMode(customOutput2, OUTPUT);
630+
pinMode(customInput1, INPUT_PULLDOWN);
631+
pinMode(customInput2, INPUT_PULLDOWN);
632+
#endif
575633

576634
settingsManager.loadWifiSettings();
577635
settingsManager.loadAppSettings();
@@ -634,7 +692,6 @@ void loop()
634692
reboot();
635693
}
636694

637-
638695
// Reconnect handling
639696
if (currentMode != Mode::wificonfig)
640697
{
@@ -685,5 +742,32 @@ void loop()
685742
if (needMaintenanceMode)
686743
currentMode = Mode::maintenance;
687744

745+
#ifdef CUSTOM_GPIOS
746+
// read custom inputs and publish by MQTT
747+
bool i1;
748+
bool i2;
749+
i1 = (digitalRead(customInput1) == HIGH);
750+
i2 = (digitalRead(customInput2) == HIGH);
751+
752+
String mqttRootTopic = settingsManager.getAppSettings().mqttRootTopic;
753+
if (i1 != customInput1Value) {
754+
if (i1)
755+
mqttClient.publish((String(mqttRootTopic) + "/customInput1").c_str(), "on");
756+
else
757+
mqttClient.publish((String(mqttRootTopic) + "/customInput1").c_str(), "off");
758+
}
759+
760+
if (i2 != customInput2Value) {
761+
if (i2)
762+
mqttClient.publish((String(mqttRootTopic) + "/customInput2").c_str(), "on");
763+
else
764+
mqttClient.publish((String(mqttRootTopic) + "/customInput2").c_str(), "off");
765+
}
766+
767+
customInput1Value = i1;
768+
customInput2Value = i2;
769+
770+
#endif
771+
688772
}
689773

0 commit comments

Comments
 (0)