-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgramm-Simpe-Device.ino
217 lines (184 loc) · 5.34 KB
/
Programm-Simpe-Device.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
#include "config.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <SPI.h>
#include "MFRC522.h"
/* wiring the MFRC522 to ESP8266 (ESP-12)
SDA(SS) = GPIO4 --> I2C
MOSI = GPIO13 --> SPI
MISO = GPIO12 --> SPI
SCK = GPIO14 --> SPI
GND = GND
3.3V = 3.3V
*/
#define RST_PIN 3 // RST-PIN für RC522 - RFID - SPI - Modul
#define SS_PIN 4 // SDA-PIN für RC522 - RFID - SPI - Modul
#define LED_RED 16
#define LED_YELLOW 0
#define LED_GREEN 2
#define SWITCH_PIN 15
#define SIGNAL_PIN 5
const char *ssid = WIFI_SSID;
const char *pass = WIFI_PASSWORD;
MFRC522 mfrc522(SS_PIN, RST_PIN);
String logedInID = "0";
int loginUpdateCounter = 0;
void setup() {
pinMode(SWITCH_PIN, INPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(SIGNAL_PIN, OUTPUT);
digitalWrite(SIGNAL_PIN, LOW);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, HIGH);
Serial.begin(115200);
delay(250);
Serial.println(F("Booting...."));
SPI.begin();
mfrc522.PCD_Init();
WiFi.begin(ssid, pass);
int retries = 0;
while ((WiFi.status() != WL_CONNECTED) && (retries < 10)) {
retries++;
delay(500);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(F("WiFi connected"));
digitalWrite(LED_YELLOW, LOW);
} else {
digitalWrite(LED_RED, HIGH);
while (true);
}
Serial.println(F("Ready!"));
Serial.println(F("======================================================"));
Serial.println(F("Scan for Card and print UID:"));
digitalWrite(LED_GREEN, LOW);
}
void logout() {
Serial.println("Loged out.");
logedInID = "0";
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
digitalWrite(SIGNAL_PIN, LOW);
}
void loop() {
while(digitalRead(SWITCH_PIN) == 0){
delay(100);
}
delay(250);
String uid = readID();
if(uid != "0") {
tryLoginID(uid);
delay(1000);
while(digitalRead(SWITCH_PIN) == 1){
updateLogin();
delay(1000);
}
logout();
} else {
delay(250);
}
}
String readID(){
// Look for new cards
for(int i = 0; i < 3; i++) {
if(mfrc522.PICC_IsNewCardPresent()){
if (mfrc522.PICC_ReadCardSerial()) {
String uid = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
if (i != 0) {
uid = uid + ":";
}
int b = mfrc522.uid.uidByte[i];
if (b >= 16) {
uid = uid + String(b, 16);
} else {
uid = uid + "0" + String(b, 16);
}
}
return uid;
}
}
delay(100);
}
return "0";
}
void tryLoginID(String uid) {
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, LOW);
HTTPClient http;
WiFiClient client;
Serial.print("[HTTP] begin...\n");
// configure traged server and url
String s = "";
s = s + "http://" + SERVER_IP + "/machine_try_login/" + AUTHENTICATION_TOKEN + "/" + MACHINE_NAME + "/" + MACHINE_ID + "/" + uid;
http.begin(client, s); //HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
if (payload.indexOf("true") >= 0) {
logedInID = uid;
Serial.println("Loged in: " + uid);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(SIGNAL_PIN, HIGH);
} else {
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, HIGH);
delay(2000);
digitalWrite(LED_RED, LOW);
}
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
digitalWrite(LED_RED, HIGH);
}
http.end();
}
void updateLogin() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_YELLOW, LOW);
loginUpdateCounter++;
if (loginUpdateCounter < 200) {
return;
}
loginUpdateCounter = 0;
HTTPClient http;
WiFiClient client;
Serial.print("[HTTP] begin...\n");
// configure traged server and url
String s = "";
s = s + "http://" + SERVER_IP + "/machine_extend_login/" + AUTHENTICATION_TOKEN + "/" + MACHINE_NAME;
http.begin(client, s); //HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_YELLOW, HIGH);
}
http.end();
}