Skip to content

Commit

Permalink
Merge pull request #318 from frenchie71/two-relays
Browse files Browse the repository at this point in the history
Support for multiple relays
  • Loading branch information
omersiar authored Sep 13, 2019
2 parents be9cb05 + 26e157f commit 879eee5
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 75 deletions.
32 changes: 26 additions & 6 deletions src/config.esp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -109,13 +117,25 @@ bool ICACHE_FLASH_ATTR loadConfiguration()
const char *ntpserver = ntp["server"];
int ntpinter = ntp["interval"];
timeZone = ntp["timezone"];
activateTime = hardware["rtime"];
lockType = hardware["ltype"];
relayType = hardware["rtype"];
activateTime[0] = hardware["rtime"];
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 <numRelays ; i++){
JsonObject &relay = hardware["relay"+String((i+1))];
activateTime[i] = relay["rtime"];
lockType[i]= relay["ltype"];
relayType[i] = relay["rtype"];
relayPin[i] = relay["rpin"];
pinMode(relayPin[i], OUTPUT);
digitalWrite(relayPin[i], !relayType[i]);
}

#endif
const char *ssid = network["ssid"];
const char *password = network["pswd"];
Expand Down
13 changes: 13 additions & 0 deletions src/magicnumbers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@
// user related numbers

#define ACCESS_GRANTED 1
#define ACCESS_ADMIN 99
#define ACCCESS_DENIED 0

// Reader defines

#define WIEGANDTYPE_KEYPRESS 4
#define WIEGANDTYPE_PICC26 26
#define WIEGANDTYPE_PICC34 34

// hardware defines

#ifdef OFFICIALBOARD
#define MAX_NUM_RELAYS 1
#else
#define MAX_NUM_RELAYS 4
#endif

#define LOCKTYPE_MOMENTARY 0
#define LOCKTYPE_CONTINUOUS 1
73 changes: 42 additions & 31 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ SOFTWARE.

// #define DEBUG

int numRelays=1;

#ifdef OFFICIALBOARD

#include <Wiegand.h>

WIEGAND wg;
int relayPin = 13;
int relayPin[MAX_NUM_RELAYS] = {13};
bool activateRelay [MAX_NUM_RELAYS]= {false};
bool deactivateRelay [MAX_NUM_RELAYS]= {false};

#endif

Expand All @@ -64,10 +68,20 @@ RFID_Reader RFIDr;

int rfidss;
int readertype;
int relayPin;

// relay specific variables

int relayPin[MAX_NUM_RELAYS];
bool activateRelay [MAX_NUM_RELAYS]= {false,false,false,false};
bool deactivateRelay [MAX_NUM_RELAYS]= {false,false,false,false};

#endif

int lockType[MAX_NUM_RELAYS];
int relayType[MAX_NUM_RELAYS];
unsigned long activateTime[MAX_NUM_RELAYS];


// these are from vendors
#include "webh/glyphicons-halflings-regular.woff.gz.h"
#include "webh/required.css.gz.h"
Expand Down Expand Up @@ -118,8 +132,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;
Expand All @@ -140,9 +152,6 @@ char *muser = NULL;
char *mpas = NULL;
int mport;

int lockType;
int relayType;
unsigned long activateTime;
int timeZone;

unsigned long nextbeat = 0;
Expand Down Expand Up @@ -246,7 +255,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)
Expand Down Expand Up @@ -277,62 +286,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] == LOCKTYPE_MOMENTARY) // 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[currentRelay]) && (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
Expand Down
2 changes: 1 addition & 1 deletion src/mqtt.esp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
27 changes: 18 additions & 9 deletions src/rfid.esp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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"+String(currentRelay+1)];

#ifdef DEBUG
Serial.println(" = known PICC");
Serial.print("[ INFO ] User Name: ");
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -334,6 +342,7 @@ void ICACHE_FLASH_ATTR rfidloop()
root.printTo((char *)buffer->get(), len + 1);
ws.textAll(buffer);
}
}
}
else
{
Expand Down
33 changes: 31 additions & 2 deletions src/websocket.esp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ void ICACHE_FLASH_ATTR procMsg(AsyncWebSocketClient *client, size_t sz)
}
else if (strcmp(command, "userfile") == 0)
{
#ifdef DEBUG
Serial.println(F("[ DEBUG ] userfile received"));
String st = String();
root.printTo(st);
Serial.println(st);
#endif
const char *uid = root["uid"];
String filename = "/P/";
filename += uid;
Expand All @@ -105,16 +111,39 @@ void ICACHE_FLASH_ATTR procMsg(AsyncWebSocketClient *client, size_t sz)
{
//f.print(msg);
root.printTo(f);
#ifdef DEBUG
Serial.println(F("[ DEBUG ] userfile saved"));
#endif
}
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\"}");
}
#ifndef OFFICIALBOARD
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\"}");
}
#endif
else if (strcmp(command, "scan") == 0)
{
WiFi.scanNetworksAsync(printScanResult, true);
Expand Down
Loading

0 comments on commit 879eee5

Please sign in to comment.