-
Notifications
You must be signed in to change notification settings - Fork 416
/
config.esp
212 lines (208 loc) · 5.21 KB
/
config.esp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
bool ICACHE_FLASH_ATTR loadConfiguration()
{
File configFile = SPIFFS.open("/config.json", "r");
if (!configFile)
{
#ifdef DEBUG
Serial.println(F("[ WARN ] Failed to open config file"));
#endif
return false;
}
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.parseObject(buf.get());
if (!json.success())
{
#ifdef DEBUG
Serial.println(F("[ WARN ] Failed to parse config file"));
#endif
return false;
}
#ifdef DEBUG
Serial.println(F("[ INFO ] Config file found"));
json.prettyPrintTo(Serial);
Serial.println();
#endif
JsonObject &network = json["network"];
JsonObject &hardware = json["hardware"];
JsonObject &general = json["general"];
JsonObject &mqtt = json["mqtt"];
JsonObject &ntp = json["ntp"];
#ifdef DEBUG
Serial.println(F("[ INFO ] Trying to setup RFID Hardware"));
#endif
if (hardware.containsKey("wifipin"))
{
wifipin = hardware["wifipin"];
if (wifipin!=255)
{
pinMode(wifipin, OUTPUT);
digitalWrite(wifipin, LEDoff);
}
}
if (hardware.containsKey("buttonpin"))
{
buttonPin = hardware["buttonpin"];
if (buttonPin != 255)
{
button = Bounce();
button.attach(buttonPin, INPUT_PULLUP);
button.interval(30);
}
}
#ifdef OFFICIALBOARD
setupWiegandReader(5, 4);
#endif
#ifndef OFFICIALBOARD
readerType = hardware["readerType"];
if (readerType == 1 || readerType == 5)
{
int wgd0pin = hardware["wgd0pin"];
int wgd1pin = hardware["wgd1pin"];
setupWiegandReader(wgd0pin, wgd1pin); // also some other settings like weather to use keypad or not, LED pin, BUZZER pin, Wiegand 26/34 version
}
else if (readerType == 0 || readerType == 4)
{
rfidss = 15;
if (hardware.containsKey("sspin"))
{
rfidss = hardware["sspin"];
}
int rfidgain = hardware["rfidgain"];
setupMFRC522Reader(rfidss, rfidgain);
}
else if (readerType == 2 || readerType == 6)
{
rfidss = hardware["sspin"];
setupPN532Reader(rfidss);
}
#ifndef DEBUG
if (readerType>2) Serial.begin(9600);
#endif
#endif
autoRestartIntervalSeconds = general["restart"];
wifiTimeout = network["offtime"];
const char *bssidmac = network["bssid"];
byte bssid[6];
parseBytes(bssidmac, ':', bssid, 6, 16);
deviceHostname = strdup(general["hostnm"]);
WiFi.hostname(deviceHostname);
if (!MDNS.begin(deviceHostname))
{
#ifdef DEBUG
Serial.println("[ WARN ]Error setting up MDNS responder!");
#endif
}
MDNS.addService("http", "tcp", 80);
const char *ntpserver = ntp["server"];
int ntpinter = ntp["interval"];
timeZone = ntp["timezone"];
activateTime = hardware["rtime"];
lockType = hardware["ltype"];
relayType = hardware["rtype"];
#ifndef OFFICIALBOARD
relayPin = hardware["rpin"];
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, !relayType);
#endif
const char *ssid = network["ssid"];
const char *password = network["pswd"];
wmode = network["wmode"];
http_pass = strdup(general["pswd"]);
ws.setAuthentication("admin", http_pass);
if (wmode == 1)
{
int hid = network["hide"];
#ifdef DEBUG
Serial.println(F("[ INFO ] ESP-RFID is running in AP Mode "));
#endif
const char *apipch;
if (network.containsKey("apip"))
{
apipch = network["apip"];
}
else
{
apipch = "192.168.4.1";
}
const char *apsubnetch;
if (network.containsKey("apsubnet"))
{
apsubnetch = network["apsubnet"];
}
else
{
apsubnetch = "255.255.255.0";
}
IPAddress apip;
IPAddress apsubnet;
apip.fromString(apipch);
apsubnet.fromString(apsubnetch);
return startAP(apip, apsubnet, hid, ssid, password);
}
else
{
if (network["dhcp"] == "0")
{
WiFi.mode(WIFI_STA);
const char *clientipch = network["ip"];
const char *subnetch = network["subnet"];
const char *gatewaych = network["gateway"];
const char *dnsch = network["dns"];
IPAddress clientip;
IPAddress subnet;
IPAddress gateway;
IPAddress dns;
clientip.fromString(clientipch);
subnet.fromString(subnetch);
gateway.fromString(gatewaych);
dns.fromString(dnsch);
WiFi.config(clientip, gateway, subnet, dns);
}
if (!connectSTA(ssid, password, bssid))
{
return false;
}
}
#ifdef DEBUG
Serial.println("[ INFO ] Trying to setup NTP Server");
#endif
/*
IPAddress timeserverip;
WiFi.hostByName(ntpserver, timeserverip);
String ip = printIP(timeserverip);
writeEvent("INFO", "ntp", "Connecting NTP Server", ip);
*/
NTP.Ntp(ntpserver, timeZone, ntpinter * 60);
mqttenabled = mqtt["enabled"];
if (mqttenabled == 1)
{
#ifdef DEBUG
Serial.println("[ INFO ] Trying to setup MQTT");
#endif
String mhsString = mqtt["host"];
mhs = strdup(mhsString.c_str());
mport = mqtt["port"];
String muserString = mqtt["user"];
muser = strdup(muserString.c_str());
String mpasString = mqtt["pswd"];
mpas = strdup(mpasString.c_str());
String mqttTopicString = mqtt["topic"];
mqttTopic = strdup(mqttTopicString.c_str());
mqttClient.setServer(mhs, mport);
mqttClient.setCredentials(muser, mpas);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onPublish(onMqttPublish);
mqttClient.onConnect(onMqttConnect);
#ifdef DEBUG
Serial.println("[ INFO ] try to call mqttconnect ");
#endif
connectToMqtt();
}
#ifdef DEBUG
Serial.println(F("[ INFO ] Configuration done."));
#endif
return true;
}