-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathESP-RFID.ino
409 lines (352 loc) · 15.7 KB
/
ESP-RFID.ino
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#include <FS.h>
#include <ArduinoJson.h>
#include <EEPROM.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
ADC_MODE(ADC_VCC);
#define RF_PIN D1
#define ACTIVITY_LED D4
static char DEBUGMODE=0;
//helpers for debugging timings
int32_t starttime, stoptime;
static inline int32_t asm_ccount(void) {
int32_t r; asm volatile ("rsr %0, ccount" : "=r"(r)); return r; }
static inline void asm_nop() {
asm volatile ("nop"); }
// Variables used for temporary storage
static uint8_t data[64];
static uint8_t value[10];
static String webResponse;
// Variables used for state keeping
static uint8_t delayed_setup = 1;
static uint8_t state=1;
static uint8_t active_id=0;
static uint8_t network_count=0;
uint8_t vendor_active=0;
uint32_t ID_active=0;
void prepare_data(uint32_t ID, uint32_t VENDOR){
value[0] = (VENDOR>>4) & 0XF;
value[1] = VENDOR & 0XF;
for (int i=1; i<8; i++){
value[i+2] = (ID>>(28-i*4)) &0xF;
}
for (int i=0; i<9; i++) data[i]=1; //header
for (int i=0; i<10; i++) { //data
for (int j=0; j<4; j++) {
data[9 + i*5 +j] = value[i] >> (3-j) & 1;
}
data[9 + i*5 + 4] = ( data[9 + i*5 + 0]
+ data[9 + i*5 + 1]
+ data[9 + i*5 + 2]
+ data[9 + i*5 + 3]) % 2;
}
for (int i=0; i<4; i++) { //checksum
int checksum=0;
for (int j=0; j<10; j++) {
checksum += data[9 + i + j*5];
}
data[i+59] = checksum%2;
}
data[63] = 0; //footer
/*
delay(10);
Serial.println();
for (int i=0; i<64; i++) {
Serial.printf("%d", data[i]);
if (i>=8 && (i+2)%5==0) Serial.printf("\r\n ");
}
Serial.println();
delay(10);
*/
}
ESP8266WiFiMulti WiFiMulti;
ESP8266WebServer server(80);
void handleRoot() {
webResponse = "<!DOCTYPE html><meta charset=UTF-8><title>RFID Ugly panel</title><script>function httpGetAsync(e,t){var n=new XMLHttpRequest;n.onreadystatechange=function(){4==n.readyState&&200==n.status&&t&&t(n.responseText)},n.open(\"GET\",e,!0),n.send(null)}window.onload=function(){httpGetAsync(\"/api\",setup)};function setup(e){e=JSON.parse(e);for(var t=document.getElementById(\"available\"),n=0;n<e.rfid.length;n++){var o=document.createElement(\"option\");o.text=e.rfid[n].name+\" (\"+e.rfid[n].vendor+\"|\"+e.rfid[n].uid+\")\",o.value=e.rfid[n].id,t.options.add(o,o.value)}for(n=0;n<t.length;n++)if(t.options[n].value==e.active_id){t.options.selectedIndex=n;break}}function update(){var e=document.getElementById(\"available\");return console.log(e.options[e.options.selectedIndex].value),httpGetAsync(\"/api?rfid=\"+e.options[e.options.selectedIndex].value),!1}</script><style>button,input,select{display:block;margin:1px}button,input{min-width:200px}body{background-color:#eee}</style><h1>Welcome to<br><strike>Admin</strike>Ugly panel</h1><form action=/api onsubmit=\"return update()\"><select id=available><option value=0>Disabled</select><input type=submit value=Update></form><button id=wifi onclick='location.href=\"/wifi\"'>WiFi Setup</button> <button id=rfid onclick='location.href=\"/rfid\"'>RFID Setup</button>";
server.send(200, "text/html", webResponse.c_str());
}
void handleWifi() {
webResponse = "<!DOCTYPE html><meta charset=UTF-8><title>RFID Ugly panel</title><script>function httpGetAsync(e,t){var n=new XMLHttpRequest;n.onreadystatechange=function(){4==n.readyState&&200==n.status&&t&&t(n.responseText)},n.open(\"GET\",e,!0),n.send(null)}window.onload=function(){httpGetAsync(\"/api\",setup)};function reload(){location.reload()}function setup(e){e=JSON.parse(e);for(var t=document.getElementById(\"wifis\"),n=0;n<e.wifi.length;n++){var o=document.createElement(\"option\");o.text=e.wifi[n].ssid+\" (\"+e.wifi[n].pass.length+\")\",o.value=e.wifi[n].id,t.options.add(o,o.value)}}function remove(){var e=document.getElementById(\"wifis\");return console.log(e.options[e.options.selectedIndex].value),httpGetAsync(\"/api?remove=wifi&id=\"+e.options[e.options.selectedIndex].value),e.options.remove(e.options.selectedIndex),!1}function add(){return console.log(\"/api?add=wifi&ssid=\"+document.getElementById(\"ssid\").value+\"&pass=\"+document.getElementById(\"pass\").value),httpGetAsync(\"/api?add=wifi&ssid=\"+document.getElementById(\"ssid\").value+\"&pass=\"+document.getElementById(\"pass\").value,reload),!1}</script><style>button,input,select{display:block;margin:1px}button,input{min-width:200px}body{background-color:#eee}</style><form action=/api onsubmit=\"return remove()\"><select id=wifis></select><input type=submit value=Delete></form><form action=/api onsubmit=\"return add()\"><span>SSID: </span><input id=ssid> <span>PASS: </span><input type=password id=pass> <input type=submit value=Add></form><button id=wifi onclick='location.href=\"/\"'>Home</button> <button id=rfid onclick='location.href=\"/rfid\"'>RFID Setup</button>";
server.send(200, "text/html", webResponse.c_str());
}
void handleRfid() {
webResponse = "<!DOCTYPE html><meta charset=UTF-8><title>Control Room</title><script>function httpGetAsync(e,t){var n=new XMLHttpRequest;n.onreadystatechange=function(){4==n.readyState&&200==n.status&&t&&t(n.responseText)},n.open(\"GET\",e,!0),n.send(null)}window.onload=function(){httpGetAsync(\"/api\",setup)};function reload(){location.reload()}function setup(e){e=JSON.parse(e);for(var t=document.getElementById(\"rfids\"),n=0;n<e.rfid.length;n++){var d=document.createElement(\"option\");d.text=e.rfid[n].name+\"(\"+e.rfid[n].vendor+\"|\"+e.rfid[n].uid+\")\",d.value=e.rfid[n].id,t.options.add(d,d.value)}}function remove(){var e=document.getElementById(\"rfids\");return console.log(e.options[e.options.selectedIndex].value),httpGetAsync(\"/api?remove=rfid&id=\"+e.options[e.options.selectedIndex].value),e.options.remove(e.options.selectedIndex),!1}function add(){return console.log(\"/api?add=rfid&uid=\"+document.getElementById(\"uid\").value+\"&vendor=\"+document.getElementById(\"vendor\").value+\"&name=\"+document.getElementById(\"name\").value),httpGetAsync(\"/api?add=rfid&uid=\"+document.getElementById(\"uid\").value+\"&vendor=\"+document.getElementById(\"vendor\").value+\"&name=\"+document.getElementById(\"name\").value,reload),!1}</script><style>button,input,select{display:block;margin:1px}button,input{min-width:200px}body{background-color:#eee}</style><form action=/api onsubmit=\"return remove()\"><select id=rfids></select><input type=submit value=Delete></form><form action=/api onsubmit=\"return add()\"><span>Vendor (can be 0): </span><input id=vendor> <span>ID: </span><input id=uid> <span>Name: </span><input id=name> <input type=submit value=Add></form><button id=home onclick='location.href=\"/\"'>Home</button> <button id=wifi onclick='location.href=\"/wifi\"'>WiFi Setup</button>";
server.send(200, "text/html", webResponse.c_str());
}
void handleApi() {
File configFile = SPIFFS.open("/rfid.conf", "r");
DynamicJsonBuffer jsonBuffer;
JsonObject &config = jsonBuffer.parseObject(configFile);
// config.printTo(Serial);
if (server.hasArg("debug")){
DEBUGMODE = (server.arg("debug")=="true" || server.arg("debug")=="1") ? 1:0;
config["debug"]=DEBUGMODE;
configFile.close();
configFile = SPIFFS.open("/rfid.conf", "w");
config.printTo(configFile);
}
if (server.hasArg("rfid")){
if (server.arg("rfid")=="temp"){
ID_active=server.arg("uid").toInt();
vendor_active=server.arg("vendor").toInt();
prepare_data(ID_active, vendor_active);
}
else {
active_id = server.arg("rfid").toInt();
if(active_id){
int i=0;
for (i=0; i<config["rfid"].size(); i++){
if (config["rfid"][i]["id"]==active_id){
ID_active=config["rfid"][i]["uid"];
vendor_active=config["rfid"][i]["vendor"];
prepare_data(ID_active, vendor_active);
break;
}
}
if (i==config["rfid"].size()){
active_id=0;
}
}
config["active_id"]=active_id;
configFile.close();
configFile = SPIFFS.open("/rfid.conf", "w");
config.printTo(configFile);
}
}
if (server.hasArg("add"))
{
if (server.arg("add")=="wifi"){
if (server.hasArg("ssid") && server.hasArg("pass") && server.arg("pass").length()>7 ){
JsonArray &wifis = config["wifi"];
int i=0, idmax=0;
for (i=0; i< wifis.size(); i++){
if (wifis[i]["ssid"] == server.arg("ssid")) break;
idmax = (wifis[i]["id"] < idmax)? idmax:wifis[i]["id"];
}
if (i==wifis.size()){
JsonObject &newWiFi = jsonBuffer.createObject();
newWiFi["id"]=idmax+1;
newWiFi["ssid"]=server.arg("ssid");
newWiFi["pass"]=server.arg("pass");
wifis.add(newWiFi);
configFile.close();
configFile = SPIFFS.open("/rfid.conf", "w");
config.printTo(configFile);
}
}
}
else if (server.arg("add")=="rfid"){
if (server.hasArg("uid") && server.hasArg("vendor")){
JsonArray &rfids = config["rfid"];
int i=0, idmax=0;
for (i=0; i< rfids.size(); i++){
if (rfids[i]["uid"] == server.arg("uid").toInt()) break;
idmax = (rfids[i]["id"] < idmax)? idmax:rfids[i]["id"];
}
if (i==rfids.size()){
JsonObject &newRFID = jsonBuffer.createObject();
newRFID["id"] = idmax+1;
newRFID["uid"] = server.arg("uid").toInt();
newRFID["vendor"] = server.arg("vendor").toInt();
newRFID["name"] = server.hasArg("name") ? server.arg("name"):"";
rfids.add(newRFID);
configFile.close();
configFile = SPIFFS.open("/rfid.conf", "w");
config.printTo(configFile);
}
}
}
}
if (server.hasArg("remove"))
{
if (server.arg("remove")=="wifi"){
if (server.hasArg("id")){
JsonArray &wifis = config["wifi"];
int i=0;
for (i=0; i< wifis.size(); i++){
if (wifis[i]["id"] == server.arg("id")){
wifis.remove(i);
configFile.close();
configFile = SPIFFS.open("/rfid.conf", "w");
config.printTo(configFile);
}
}
}
}
else if (server.arg("remove")=="rfid"){
if (server.hasArg("id")){
JsonArray &rfids = config["rfid"];
int i=0;
for (i=0; i< rfids.size(); i++){
if (rfids[i]["id"] == server.arg("id")){
if (rfids[i]["id"]==active_id) { config["active_id"] = 0; active_id = 0; }
rfids.remove(i);
configFile.close();
configFile = SPIFFS.open("/rfid.conf", "w");
config.printTo(configFile);
}
}
}
}
}
webResponse = "";
config["battery"] = ESP.getVcc()/901.515;
config.prettyPrintTo(webResponse);
configFile.close();
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "application/json", webResponse.c_str());
}
void handleReboot() {
webResponse="Rebooting...";
server.send(200, "text/html", webResponse.c_str());
delay(5000);
SPIFFS.end();
ESP.reset();
delay(1000);
}
void handleFormat() {
webResponse="Formatting! All data will be lost!";
server.send(200, "text/html", webResponse.c_str());
delay(5000);
SPIFFS.end();
SPIFFS.format();
ESP.reset();
delay(1000);
}
void setup() {
EEPROM.begin(1);
state = EEPROM.read(0) & 1;
if (ESP.getResetReason() == "Deep-Sleep Wake" | ESP.getResetReason() == "External System") {
state = !state;
EEPROM.write(0, state);
EEPROM.commit();
}
if (!state) {
ESP.deepSleep(0);
}
EEPROM.end();
pinMode(RF_PIN, INPUT);
pinMode(ACTIVITY_LED, OUTPUT);
// pinMode(1, OUTPUT);
digitalWrite(ACTIVITY_LED, LOW);
// digitalWrite (1, LOW);
WiFi.disconnect(true); //true = WiFi.mode(WIFI_OFF);
delay(50);
if (DEBUGMODE) Serial.begin(115200);
// WiFi.persistent(false); //Do NOT write to flash;
WiFi.mode(WIFI_STA);
SPIFFS.begin();
if (SPIFFS.exists("/rfid.conf")){
File configFile = SPIFFS.open("/rfid.conf", "r");
DynamicJsonBuffer jsonBuffer;
JsonObject &config = jsonBuffer.parseObject(configFile);
DEBUGMODE = config["debug"];
active_id = config["active_id"];
network_count=config["wifi"].size();
for (int i=0; i<config["wifi"].size(); i++) {
WiFiMulti.addAP(config["wifi"][i]["ssid"], config["wifi"][i]["pass"]);
}
for (int i=0; active_id && i<config["rfid"].size(); i++) {
if (active_id == config["rfid"][i]["id"]) {
vendor_active = config["rfid"][i]["vendor"];
ID_active = config["rfid"][i]["uid"];
}
}
}
else {
File configFile = SPIFFS.open("/rfid.conf", "w");
DynamicJsonBuffer jsonBuffer;
JsonObject &config = jsonBuffer.createObject();
config["debug"] = DEBUGMODE;
config["active_id"] = 0;
JsonArray &rfconfig = config.createNestedArray("rfid");
JsonArray &wificonfig = config.createNestedArray("wifi");
config.printTo(configFile);
configFile.close();
}
server.on("/", handleRoot);
server.on("/api", handleApi);
server.on("/wifi", handleWifi);
server.on("/rfid", handleRfid);
server.on("/reboot", handleReboot);
server.on("/format", handleFormat);
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
if (DEBUGMODE) Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
if (DEBUGMODE) Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
if (DEBUGMODE) Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
if (DEBUGMODE){
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
}
});
// MDNS.begin("rfid");
// MDNS.addService("http", "tcp", 80);
// MDNS.update();
prepare_data(ID_active, vendor_active);
if (DEBUGMODE) Serial.println("\r\nBoot complete!!!1!");
}
void loop() {
if (delayed_setup) {
if(WiFiMulti.run() == WL_CONNECTED){
server.begin();
ArduinoOTA.begin();
delayed_setup=0;
if (DEBUGMODE) Serial.printf("WiFi connected, IP: %s\r\n", WiFi.localIP().toString().c_str());
}
else if (millis()>15000 || !network_count){
if (DEBUGMODE) Serial.println("WiFi failed, Hotspot up...");
WiFi.scanDelete();
WiFi.disconnect();
WiFi.mode(WIFI_AP);
WiFi.softAP("RFID");
server.begin();
ArduinoOTA.begin();
delayed_setup=0;
}
}
else {
ArduinoOTA.handle();
server.handleClient();
}
digitalWrite (ACTIVITY_LED, LOW);
delay(10);
int i=0, j=0;
//Manchester
if (active_id){
for (i=0; i<15; i++){
for (j=0; j<64; j++){
// data[j]? pinMode(RF_PIN, OUTPUT):pinMode(RF_PIN, INPUT);
// data[j]? (GPE |= 0b00000100):(GPE &= ~0b00000100);
data[j]? (GPE |= (1<<RF_PIN)):(GPE &= ~(1<<RF_PIN));
delayMicroseconds(255);
for (int k=0; k<14; k++) asm_nop(); //fine tuning
// data[j]? pinMode(RF_PIN, INPUT):pinMode(RF_PIN, OUTPUT);
// data[j]? (GPE &= ~0b00000100):(GPE |= 0b00000100);
data[j]? (GPE &= ~(1<<RF_PIN)):(GPE |= (1<<RF_PIN));
delayMicroseconds(255);
for (int k=0; k<13; k++) asm_nop(); //fine tuning
}
}
digitalWrite (ACTIVITY_LED, HIGH);
}
}