From 33414474aee784b53fcbcdbd7cf89d68bfa98081 Mon Sep 17 00:00:00 2001 From: frenchie71 Date: Mon, 9 Sep 2019 19:14:05 +0200 Subject: [PATCH 01/11] change hardware relay form for mutiple relays --- src/websrc/esprfid.htm | 34 +++++++++++++++++++++++++++------- src/websrc/js/esprfid.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/websrc/esprfid.htm b/src/websrc/esprfid.htm index 4a51bf36..6b77d8f6 100644 --- a/src/websrc/esprfid.htm +++ b/src/websrc/esprfid.htm @@ -253,6 +253,24 @@
Please refer the + + + + + + + + + +

@@ -832,7 +852,7 @@
-
+
+ diff --git a/src/websrc/js/esprfid.js b/src/websrc/js/esprfid.js index 1379d5e3..29b16602 100644 --- a/src/websrc/js/esprfid.js +++ b/src/websrc/js/esprfid.js @@ -9,7 +9,7 @@ var ajaxobj; var isOfficialBoard = false; var maxNumRelays=4; -var numRelays=3; +var numRelays=1; var theCurrentLogFile =""; @@ -128,12 +128,14 @@ function handleReader() { } } -function handleLock() { - var lType = parseInt(document.getElementById("lockType").value); +function handleLock(xnum) { + var xstr=""; + if (xnum>1) {xstr="" + xnum} + var lType = parseInt(document.getElementById("lockType"+xstr).value); if (lType === 0) { - document.getElementById("activateTimeForm").style.display = "block"; + document.getElementById("activateTimeForm"+xstr).style.display = "block"; } else if (lType === 1) { - document.getElementById("activateTimeForm").style.display = "none"; + document.getElementById("activateTimeForm"+xstr).style.display = "none"; } } @@ -165,9 +167,12 @@ function listhardware() { for (var i = 2; i<=numRelays; i++) { - // read the other relays - // downstream compat - add node if not there - } + + document.getElementById("gpiorly"+i).value = config.hardware["relay"+i].rpin; + document.getElementById("lockType"+i).value = config.hardware["relay"+i].ltype; + document.getElementById("typerly"+i).value = config.hardware["relay"+i].rtype; + document.getElementById("delay"+i).value = config.hardware["relay"+i].rtime; + } } handleReader(); handleLock(); @@ -217,6 +222,15 @@ function savehardware() { config.hardware.wifipin = parseInt(document.getElementById("wifipin").value); config.hardware.doorstatpin = parseInt(document.getElementById("doorstatpin").value); config.hardware.openlockpin = parseInt(document.getElementById("openlockpin").value); + config.hardware["numrelays"] = numRelays; + + for (var i = 2; i<=numRelays; i++) + { + config.hardware["relay"+i].rpin = document.getElementById("gpiorly"+i).value; + config.hardware["relay"+i].ltype = document.getElementById("lockType"+i).value; + config.hardware["relay"+i].rtype = document.getElementById("typerly"+i).value; + config.hardware["relay"+i].rtime = document.getElementById("delay"+i).value; + } uncommited(); } @@ -1296,8 +1310,6 @@ function initUserTable() { for (var i=2; i<= maxNumRelays; i++) { - var relayForm = $("#relayform"); - var relayparent= $("#relayformparent"); if (i<= numRelays) { //FooTable.get('#usertable').draw(); @@ -1525,6 +1537,13 @@ function clearlatest() { function changeRelayNumber(){ numRelays = $("#numrlys :selected").val(); + + // downstream config compatibility + + config.hardware["numrelays"] = numRelays; + + // add the missing form elements + updateRelayForm(); } @@ -1533,6 +1552,21 @@ function updateRelayForm(){ var i; for (i=2; i<= maxNumRelays; i++) { + + // downstream compatibility + if (!(config.hardware.hasOwnProperty("relay"+i))) + { + var relayJson = + { + "rtype": 1, + "ltype": 0, + "rpin": 4, + "rtime": 400, + }; + config.hardware["relay"+i] = relayJson; + } + + var relayForm = $("#relayform"); var relayparent= $("#relayformparent"); if (i<= numRelays) @@ -1541,12 +1575,19 @@ function updateRelayForm(){ if (!(existingRelayForm)) { var relayFormClone = relayForm.clone(true); + var cloneObj = relayFormClone[0]; relayFormClone.attr('id', 'relayform' + i); - var cloneTitle = relayFormClone[0].children[0]; - cloneTitle.innerText = 'Relay ' + i + " settings"; - //cloneTitle.innerHTML = 'relayform' + i; - // add class duplicate + + var str = cloneObj.innerHTML; + str=str.replace("Relay 1 Settings","Relay "+i + " settings"); + str=str.replace ("gpiorly","gpiorly" +i); + str=str.replace ("lockType","lockType" +i); + str=str.replace ("typerly","typerly" +i); + str=str.replace ("handleLock(1)","handleLock(" +i+")"); + str=str.replace ("activateTimeForm","activateTimeForm"+i); + cloneObj.innerHTML=str.replace ("delay","delay" +i); relayparent[0].appendChild(relayFormClone[0]); + handleLock(i); } } else { var removeRelayForm = document.getElementById("relayform" + i); From 211b08c45af6b6ca8f9658a21272853dd2e7d8dc Mon Sep 17 00:00:00 2001 From: frenchie71 Date: Mon, 9 Sep 2019 23:15:54 +0200 Subject: [PATCH 04/11] backend changes --- src/config.esp | 28 ++++++++++++++--- src/magicnumbers.h | 8 +++++ src/main.cpp | 66 ++++++++++++++++++++++------------------ src/mqtt.esp | 2 +- src/rfid.esp | 27 ++++++++++------ src/websocket.esp | 22 ++++++++++++-- src/websrc/esprfid.htm | 2 +- src/websrc/js/esprfid.js | 5 +-- 8 files changed, 110 insertions(+), 50 deletions(-) diff --git a/src/config.esp b/src/config.esp index 3da540a9..0046c01f 100644 --- a/src/config.esp +++ b/src/config.esp @@ -62,6 +62,14 @@ bool ICACHE_FLASH_ATTR loadConfiguration() openLockButton.interval(30); } } + + if (hardware.containsKey("numrelays")) + { + numRelays = hardware["numrelays"]; + } + else numRelays=1; + + #ifdef OFFICIALBOARD setupWiegandReader(5, 4); #endif @@ -110,12 +118,22 @@ bool ICACHE_FLASH_ATTR loadConfiguration() int ntpinter = ntp["interval"]; timeZone = ntp["timezone"]; activateTime = hardware["rtime"]; - lockType = hardware["ltype"]; - relayType = hardware["rtype"]; + lockType[0]= hardware["ltype"]; + relayType[0] = hardware["rtype"]; + #ifndef OFFICIALBOARD - relayPin = hardware["rpin"]; - pinMode(relayPin, OUTPUT); - digitalWrite(relayPin, !relayType); + relayPin[0] = hardware["rpin"]; + pinMode(relayPin[0], OUTPUT); + digitalWrite(relayPin[0], !relayType[0]); + + for (int i = 1; i WIEGAND wg; -int relayPin = 13; +int relayPin[MAX_NUM_RELAYS] = {13,13,13,13}; #endif @@ -64,7 +64,15 @@ RFID_Reader RFIDr; int rfidss; int readertype; -int relayPin; + +// relay specific variables + +int numRelays=1; +int relayPin[MAX_NUM_RELAYS]; +int lockType[MAX_NUM_RELAYS]; +int relayType[MAX_NUM_RELAYS]; +bool activateRelay [MAX_NUM_RELAYS]= {false,false,false,false}; +bool deactivateRelay [MAX_NUM_RELAYS]= {false,false,false,false}; #endif @@ -118,8 +126,6 @@ String currentInput = ""; unsigned long deltaTime = 0; unsigned long uptime = 0; bool shouldReboot = false; -bool activateRelay = false; -bool deactivateRelay = false; bool inAPMode = false; bool isWifiConnected = false; unsigned long autoRestartIntervalSeconds = 0; @@ -140,8 +146,6 @@ char *muser = NULL; char *mpas = NULL; int mport; -int lockType; -int relayType; unsigned long activateTime; int timeZone; @@ -246,7 +250,7 @@ void ICACHE_RAM_ATTR loop() Serial.println("Button has been pressed"); #endif writeLatest("", "(used open/close button)", 1); - activateRelay = true; + activateRelay[0] = true; } if (wifipin != 255 && configMode && !wmode) @@ -277,62 +281,64 @@ void ICACHE_RAM_ATTR loop() } // Continuous relay mode - if (lockType == 1) - { - if (activateRelay) + + for (int currentRelay = 0; currentRelay < numRelays ; currentRelay++){ + if (lockType[currentRelay] == LOCKTYPE_CONTINUOUS) + { + if (activateRelay[currentRelay]) { // currently OFF, need to switch ON - if (digitalRead(relayPin) == !relayType) + if (digitalRead(relayPin[currentRelay]) == !relayType[currentRelay]) { #ifdef DEBUG Serial.print("mili : "); Serial.println(millis()); - Serial.println("activating relay now"); + Serial.printf("activating relay %d now\n",currentRelay); #endif - digitalWrite(relayPin, relayType); + digitalWrite(relayPin[currentRelay], relayType[currentRelay]); } else // currently ON, need to switch OFF { #ifdef DEBUG Serial.print("mili : "); Serial.println(millis()); - Serial.println("deactivating relay now"); + Serial.printf("deactivating relay %d now\n",currentRelay); #endif - digitalWrite(relayPin, !relayType); + digitalWrite(relayPin[currentRelay], !relayType[currentRelay]); } - activateRelay = false; + activateRelay[currentRelay] = false; } - } - else if (lockType == 0) // momentary relay mode - { - if (activateRelay) + } + else if (lockType[currentRelay] == 0) // momentary relay mode + { + if (activateRelay[currentRelay]) { #ifdef DEBUG Serial.print("mili : "); Serial.println(millis()); - Serial.println("activating relay now"); + Serial.printf("activating relay %d now\n",currentRelay); #endif - digitalWrite(relayPin, relayType); + digitalWrite(relayPin[currentRelay], relayType[currentRelay]); previousMillis = millis(); - activateRelay = false; - deactivateRelay = true; + activateRelay[currentRelay] = false; + deactivateRelay[currentRelay] = true; } - else if ((currentMillis - previousMillis >= activateTime) && (deactivateRelay)) + else if ((currentMillis - previousMillis >= activateTime) && (deactivateRelay[currentRelay])) { #ifdef DEBUG Serial.println(currentMillis); Serial.println(previousMillis); - Serial.println(activateTime); - Serial.println(activateRelay); + Serial.println(activateTime[currentRelay]); + Serial.println(activateRelay[currentRelay]); Serial.println("deactivate relay after this"); Serial.print("mili : "); Serial.println(millis()); #endif - digitalWrite(relayPin, !relayType); - deactivateRelay = false; + digitalWrite(relayPin[currentRelay], !relayType[currentRelay]); + deactivateRelay[currentRelay] = false; } + } } - if (formatreq) { #ifdef DEBUG diff --git a/src/mqtt.esp b/src/mqtt.esp index 2d663609..b14b5b4a 100644 --- a/src/mqtt.esp +++ b/src/mqtt.esp @@ -238,7 +238,7 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties #ifdef DEBUG Serial.println("[ INFO ] Door open"); #endif - activateRelay = true; + activateRelay[0] = true; previousMillis = millis(); return; } diff --git a/src/rfid.esp b/src/rfid.esp index 50203acf..50d4a75b 100644 --- a/src/rfid.esp +++ b/src/rfid.esp @@ -242,7 +242,6 @@ void ICACHE_FLASH_ATTR rfidloop() return; } #endif - int AccType = 0; String filename = "/P/"; filename += uid; File f = SPIFFS.open(filename, "r"); @@ -255,8 +254,18 @@ void ICACHE_FLASH_ATTR rfidloop() JsonObject &json = jsonBuffer.parseObject(buf.get()); if (json.success()) { + + for (int currentRelay = 0; currentRelay < numRelays ; currentRelay++){ + + int AccType = ACCCESS_DENIED; + + String username = json["user"]; - AccType = json["acctype"]; + if (currentRelay == 0) + AccType = json["acctype"]; + else + AccType = json["acctype"+(currentRelay+1)]; + #ifdef DEBUG Serial.println(" = known PICC"); Serial.print("[ INFO ] User Name: "); @@ -265,17 +274,17 @@ void ICACHE_FLASH_ATTR rfidloop() else Serial.print(username); #endif - if (AccType == 1) + if (AccType == ACCESS_GRANTED) { unsigned long validL = json["validuntil"]; unsigned long nowL = now(); if (validL > nowL) { - activateRelay = true; + activateRelay[currentRelay] = true; ws.textAll("{\"command\":\"giveAccess\"}"); #ifdef DEBUG - Serial.println(" have access"); + Serial.printf(" has access relay %d\n",currentRelay); #endif if (mqttenabled == 1) { @@ -292,16 +301,15 @@ void ICACHE_FLASH_ATTR rfidloop() { mqtt_publish_access(now(), "true", "Expired", username, uid); } - AccType = 2; } } - else if (AccType == 99) + else if (AccType == ACCESS_ADMIN) { doEnableWifi = true; - activateRelay = true; + activateRelay[currentRelay] = true; ws.textAll("{\"command\":\"giveAccess\"}"); #ifdef DEBUG - Serial.println(" have admin access, enable wifi"); + Serial.println(" has admin access, enable wifi"); #endif if (mqttenabled == 1) { @@ -334,6 +342,7 @@ void ICACHE_FLASH_ATTR rfidloop() root.printTo((char *)buffer->get(), len + 1); ws.textAll(buffer); } + } } else { diff --git a/src/websocket.esp b/src/websocket.esp index 3fc91932..d5e44e84 100644 --- a/src/websocket.esp +++ b/src/websocket.esp @@ -109,9 +109,27 @@ void ICACHE_FLASH_ATTR procMsg(AsyncWebSocketClient *client, size_t sz) f.close(); ws.textAll("{\"command\":\"result\",\"resultof\":\"userfile\",\"result\": true}"); } - else if (strcmp(command, "testrelay") == 0) + else if (strcmp(command, "testrelay1") == 0) { - activateRelay = true; + activateRelay[0] = true; + previousMillis = millis(); + ws.textAll("{\"command\":\"giveAccess\"}"); + } + else if (strcmp(command, "testrelay2") == 0) + { + activateRelay[1] = true; + previousMillis = millis(); + ws.textAll("{\"command\":\"giveAccess\"}"); + } + else if (strcmp(command, "testrelay3") == 0) + { + activateRelay[2] = true; + previousMillis = millis(); + ws.textAll("{\"command\":\"giveAccess\"}"); + } + else if (strcmp(command, "testrelay4") == 0) + { + activateRelay[3] = true; previousMillis = millis(); ws.textAll("{\"command\":\"giveAccess\"}"); } diff --git a/src/websrc/esprfid.htm b/src/websrc/esprfid.htm index 23141566..1ae0e689 100644 --- a/src/websrc/esprfid.htm +++ b/src/websrc/esprfid.htm @@ -296,7 +296,7 @@
Please refer the - +
diff --git a/src/websrc/js/esprfid.js b/src/websrc/js/esprfid.js index 29b16602..690bf9d3 100644 --- a/src/websrc/js/esprfid.js +++ b/src/websrc/js/esprfid.js @@ -629,8 +629,8 @@ function builddata(obj) { data = data.concat(obj.list); } -function testRelay() { - websock.send("{\"command\":\"testrelay\"}"); +function testRelay(xnum) { + websock.send("{\"command\":\"testrelay" + xnum + "\"}"); } function colorStatusbar(ref) { @@ -1584,6 +1584,7 @@ function updateRelayForm(){ str=str.replace ("lockType","lockType" +i); str=str.replace ("typerly","typerly" +i); str=str.replace ("handleLock(1)","handleLock(" +i+")"); + str=str.replace ("testRelay(1)","testRelay(" +i+")"); str=str.replace ("activateTimeForm","activateTimeForm"+i); cloneObj.innerHTML=str.replace ("delay","delay" +i); relayparent[0].appendChild(relayFormClone[0]); From de87f93b5b656267227cd05bc6e165efcbb23a0c Mon Sep 17 00:00:00 2001 From: frenchie71 Date: Mon, 9 Sep 2019 23:23:31 +0200 Subject: [PATCH 05/11] bugfixes --- src/config.esp | 3 ++- src/main.cpp | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/config.esp b/src/config.esp index 0046c01f..65a9157e 100644 --- a/src/config.esp +++ b/src/config.esp @@ -117,7 +117,7 @@ bool ICACHE_FLASH_ATTR loadConfiguration() const char *ntpserver = ntp["server"]; int ntpinter = ntp["interval"]; timeZone = ntp["timezone"]; - activateTime = hardware["rtime"]; + activateTime[0] = hardware["rtime"]; lockType[0]= hardware["ltype"]; relayType[0] = hardware["rtype"]; @@ -127,6 +127,7 @@ bool ICACHE_FLASH_ATTR loadConfiguration() digitalWrite(relayPin[0], !relayType[0]); for (int i = 1; i = activateTime) && (deactivateRelay[currentRelay])) + else if ((currentMillis - previousMillis >= activateTime[currentRelay]) && (deactivateRelay[currentRelay])) { #ifdef DEBUG Serial.println(currentMillis); From c85dcc90d7f036fd7dd7b947d2d2ee3a82cb60df Mon Sep 17 00:00:00 2001 From: frenchie71 Date: Mon, 9 Sep 2019 23:29:42 +0200 Subject: [PATCH 06/11] bugfices --- src/config.esp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/config.esp b/src/config.esp index 65a9157e..0c39c035 100644 --- a/src/config.esp +++ b/src/config.esp @@ -127,10 +127,11 @@ bool ICACHE_FLASH_ATTR loadConfiguration() digitalWrite(relayPin[0], !relayType[0]); for (int i = 1; i Date: Tue, 10 Sep 2019 00:03:59 +0200 Subject: [PATCH 07/11] user modal form --- src/websrc/esprfid.htm | 2 +- src/websrc/js/esprfid.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/websrc/esprfid.htm b/src/websrc/esprfid.htm index 1ae0e689..4d267183 100644 --- a/src/websrc/esprfid.htm +++ b/src/websrc/esprfid.htm @@ -832,7 +832,7 @@
You can add new users by either scanning a RFID Tag or by clicking on "New U
-