Skip to content

Commit

Permalink
Some new Features:
Browse files Browse the repository at this point in the history
- 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")
  • Loading branch information
Tobias Kolb committed Apr 23, 2022
1 parent 97b9de0 commit 7fa6cd7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 9 deletions.
2 changes: 2 additions & 0 deletions data/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
- "%MQTT_ROOTTOPIC%/matchId"<br>
- "%MQTT_ROOTTOPIC%/matchName"<br>
- "%MQTT_ROOTTOPIC%/matchConfidence"<br>
- "%MQTT_ROOTTOPIC%/lastLogMessage"<br>
Subscribed Topics (=read)<br>
- "%MQTT_ROOTTOPIC%/ignoreTouchRing"
</small>
Expand Down Expand Up @@ -130,6 +131,7 @@
<div class="col-md-4">
<button id="btnFirmwareUpdate" name="btnFirmwareUpdate" class="btn btn-info" type="submit" formaction="update">Firmware-Update </button>
<button id="btnDoPairing" name="btnDoPairing" class="btn btn-warning" type="submit" formaction="pairing">Pairing a new sensor </button>
<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>
<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>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/FingerprintManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ void FingerprintManager::deleteFinger(int id) {
preferences.end();
Serial.println(String("Finger template #") + id + " deleted from sensor and prefs.");

// TODO update hash on sensor
}
}

Expand Down Expand Up @@ -472,6 +471,11 @@ bool FingerprintManager::deleteAll() {
if (rc)
rc = preferences.clear();
preferences.end();

for (int i=1; i<=200; i++) {
fingerList[i] = String("@empty");
};

return rc;
}
else
Expand Down
100 changes: 92 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ const long gmtOffset_sec = 0; // UTC Time
const int daylightOffset_sec = 0; // UTC Time
const int doorbellOutputPin = 19; // pin connected to the doorbell (when using hardware connection instead of mqtt to ring the bell)

#ifdef CUSTOM_GPIOS
const int customOutput1 = 18; // not used internally, but can be set over MQTT
const int customOutput2 = 26; // not used internally, but can be set over MQTT
const int customInput1 = 21; // not used internally, but changes are published over MQTT
const int customInput2 = 22; // not used internally, but changes are published over MQTT
bool customInput1Value = false;
bool customInput2Value = false;
#endif

const int logMessagesCount = 5;
String logMessages[logMessagesCount]; // log messages, 0=most recent log message
bool shouldReboot = false;
Expand Down Expand Up @@ -136,12 +145,13 @@ String processor(const String& var){

// send LastMessage to websocket clients
void notifyClients(String message) {
message = "[" + getTimestampString() + "]: " + message;
Serial.println(message);
addLogMessage(message);
String messageWithTimestamp = "[" + getTimestampString() + "]: " + message;
Serial.println(messageWithTimestamp);
addLogMessage(messageWithTimestamp);
events.send(getLogMessagesAsHtml().c_str(),"message",millis(),1000);
//Serial.println("New message: " + getLogMessagesAsHtml());


String mqttRootTopic = settingsManager.getAppSettings().mqttRootTopic;
mqttClient.publish((String(mqttRootTopic) + "/lastLogMessage").c_str(), message.c_str());
}

void updateClientsFingerlist(String fingerlist) {
Expand Down Expand Up @@ -391,6 +401,22 @@ void startWebserver(){
});


webServer.on("/deleteAllFingerprints", HTTP_GET, [](AsyncWebServerRequest *request){
if(request->hasArg("btnDeleteAllFingerprints"))
{
notifyClients("Deleting all fingerprints...");

if (!fingerManager.deleteAll())
notifyClients("Finger database could not be deleted.");

request->redirect("/");

} else {
request->send(SPIFFS, "/settings.html", String(), false, processor);
}
});


webServer.onNotFound([](AsyncWebServerRequest *request){
request->send(404);
});
Expand Down Expand Up @@ -443,6 +469,25 @@ void mqttCallback(char* topic, byte* message, unsigned int length) {
}
}

#ifdef CUSTOM_GPIOS
if (String(topic) == String(settingsManager.getAppSettings().mqttRootTopic) + "/customOutput1") {
if(messageTemp == "on"){
digitalWrite(customOutput1, HIGH);
}
else if(messageTemp == "off"){
digitalWrite(customOutput1, LOW);
}
}
if (String(topic) == String(settingsManager.getAppSettings().mqttRootTopic) + "/customOutput2") {
if(messageTemp == "on"){
digitalWrite(customOutput2, HIGH);
}
else if(messageTemp == "off"){
digitalWrite(customOutput2, LOW);
}
}
#endif

}

void connectMqttClient() {
Expand All @@ -452,7 +497,7 @@ void connectMqttClient() {
bool connectResult;

// connect with or witout authentication
String lastWillTopic = settingsManager.getAppSettings().mqttRootTopic + "/lastWill";
String lastWillTopic = settingsManager.getAppSettings().mqttRootTopic + "/lastLogMessage";
String lastWillMessage = "FingerprintDoorbell disconnected unexpectedly";
if (settingsManager.getAppSettings().mqttUsername.isEmpty() || settingsManager.getAppSettings().mqttPassword.isEmpty())
connectResult = mqttClient.connect(settingsManager.getWifiSettings().hostname.c_str(),lastWillTopic.c_str(), 1, false, lastWillMessage.c_str());
Expand All @@ -464,6 +509,13 @@ void connectMqttClient() {
Serial.println("connected");
// Subscribe
mqttClient.subscribe((settingsManager.getAppSettings().mqttRootTopic + "/ignoreTouchRing").c_str(), 1); // QoS = 1 (at least once)
#ifdef CUSTOM_GPIOS
mqttClient.subscribe((settingsManager.getAppSettings().mqttRootTopic + "/customOutput1").c_str(), 1); // QoS = 1 (at least once)
mqttClient.subscribe((settingsManager.getAppSettings().mqttRootTopic + "/customOutput2").c_str(), 1); // QoS = 1 (at least once)
#endif



} else {
if (mqttClient.state() == 4 || mqttClient.state() == 5) {
mqttConfigValid = false;
Expand Down Expand Up @@ -570,8 +622,14 @@ void setup()
while (!Serial); // For Yun/Leo/Micro/Zero/...
delay(100);

// initialize output pin
// initialize GPIOs
pinMode(doorbellOutputPin, OUTPUT);
#ifdef CUSTOM_GPIOS
pinMode(customOutput1, OUTPUT);
pinMode(customOutput2, OUTPUT);
pinMode(customInput1, INPUT_PULLDOWN);
pinMode(customInput2, INPUT_PULLDOWN);
#endif

settingsManager.loadWifiSettings();
settingsManager.loadAppSettings();
Expand Down Expand Up @@ -634,7 +692,6 @@ void loop()
reboot();
}


// Reconnect handling
if (currentMode != Mode::wificonfig)
{
Expand Down Expand Up @@ -685,5 +742,32 @@ void loop()
if (needMaintenanceMode)
currentMode = Mode::maintenance;

#ifdef CUSTOM_GPIOS
// read custom inputs and publish by MQTT
bool i1;
bool i2;
i1 = (digitalRead(customInput1) == HIGH);
i2 = (digitalRead(customInput2) == HIGH);

String mqttRootTopic = settingsManager.getAppSettings().mqttRootTopic;
if (i1 != customInput1Value) {
if (i1)
mqttClient.publish((String(mqttRootTopic) + "/customInput1").c_str(), "on");
else
mqttClient.publish((String(mqttRootTopic) + "/customInput1").c_str(), "off");
}

if (i2 != customInput2Value) {
if (i2)
mqttClient.publish((String(mqttRootTopic) + "/customInput2").c_str(), "on");
else
mqttClient.publish((String(mqttRootTopic) + "/customInput2").c_str(), "off");
}

customInput1Value = i1;
customInput2Value = i2;

#endif

}

0 comments on commit 7fa6cd7

Please sign in to comment.