@@ -28,6 +28,15 @@ const long  gmtOffset_sec = 0; // UTC Time
2828const  int    daylightOffset_sec = 0 ; //  UTC Time
2929const  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+ 
3140const  int  logMessagesCount = 5 ;
3241String logMessages[logMessagesCount]; //  log messages, 0=most recent log message
3342bool  shouldReboot = false ;
@@ -136,12 +145,13 @@ String processor(const String& var){
136145
137146//  send LastMessage to websocket clients
138147void  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
147157void  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
448493void  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