Browse code

Create PW-switch-arduino_micro-Towitek-eeprom.ino

Stefan Höhn authored on18/01/2017 00:33:50 • GitHub committed on18/01/2017 00:33:50
Showing1 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,218 @@
1
+#include <EEPROM.h>
2
+
3
+const int EEPROM_MIN_ADDR = 0;
4
+const int EEPROM_MAX_ADDR = 511;
5
+//----------------------
6
+
7
+//-------------pw2--------------
8
+#include "Keyboard.h"
9
+int i;
10
+int z = 0;
11
+int key[5];
12
+String keyvalue;
13
+int bytes_from_RFID = 5;
14
+String Password_from_eeprom;
15
+String Password1_from_eeprom = "12345678901234";
16
+//---------------------------------
17
+
18
+
19
+boolean eeprom_is_addr_ok(int addr) {
20
+return ((addr >= EEPROM_MIN_ADDR) && (addr <= EEPROM_MAX_ADDR));
21
+}
22
+//----------------------
23
+boolean eeprom_write_bytes(int startAddr, const byte* array, int numBytes) {
24
+// counter
25
+int i;
26
+if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {
27
+return false;
28
+}
29
+for (i = 0; i < numBytes; i++) {
30
+EEPROM.write(startAddr + i, array[i]);
31
+}
32
+return true;
33
+}
34
+//-----------------------------
35
+boolean eeprom_write_string(int addr, const char* string) {
36
+
37
+int numBytes; // actual number of bytes to be written
38
+
39
+numBytes = strlen(string) + 1;
40
+return eeprom_write_bytes(addr, (const byte*)string, numBytes);
41
+}
42
+//------------------------------
43
+boolean eeprom_read_string(int addr, char* buffer, int bufSize) {
44
+byte ch; // byte read from eeprom
45
+int bytesRead; // number of bytes read so far
46
+if (!eeprom_is_addr_ok(addr)) { // check start address
47
+return false;
48
+}
49
+
50
+if (bufSize == 0) { // how can we store bytes in an empty buffer ?
51
+return false;
52
+}
53
+
54
+if (bufSize == 1) {
55
+buffer[0] = 0;
56
+return true;
57
+}
58
+bytesRead = 0; // initialize byte counter
59
+ch = EEPROM.read(addr + bytesRead); // read next byte from eeprom
60
+buffer[bytesRead] = ch; // store it into the user buffer
61
+bytesRead++; // increment byte counter
62
+
63
+while ( (ch != 0x00) && (bytesRead < bufSize) && ((addr + bytesRead) <= EEPROM_MAX_ADDR) ) {
64
+// if no stop condition is met, read the next byte from eeprom
65
+ch = EEPROM.read(addr + bytesRead);
66
+buffer[bytesRead] = ch; // store it into the user buffer
67
+bytesRead++; // increment byte counter
68
+}
69
+// make sure the user buffer has a string terminator, (0x00) as its last byte
70
+if ((ch != 0x00) && (bytesRead >= 1)) {
71
+buffer[bytesRead - 1] = 0;
72
+}
73
+return true;
74
+}
75
+//-------------------------------------------
76
+const int BUFSIZE = 15;
77
+char buf[BUFSIZE];
78
+char buf1[BUFSIZE];
79
+String myString; 
80
+String myString1 = "";
81
+char myStringChar[BUFSIZE];
82
+char myStringChar1[BUFSIZE];
83
+//boolean passwort_eingabe = false;
84
+boolean endlos = false;
85
+unsigned long start = millis();
86
+char ch ="";
87
+//-----------------------------------------------------------------
88
+
89
+
90
+void setup()
91
+{
92
+//------------------PW2---------------------
93
+Serial1.begin(9600); // start serial to RFID reader
94
+Keyboard.begin();  
95
+//-----------------------------------------
96
+
97
+Serial.begin(9600);
98
+
99
+passwort_eingabe ();
100
+//--------------------------------------
101
+pinMode(2, INPUT_PULLUP);
102
+  pinMode(13, OUTPUT); 
103
+
104
+// Serial.println("Saving variable string to address 15");
105
+//myString="12345678901234";
106
+//myString.toCharArray(myStringChar, BUFSIZE); //convert string to char array
107
+//strcpy(buf, myStringChar);
108
+//12345678901234
109
+//eeprom_write_string(15, buf); 
110
+
111
+//Serial.print("Reading string from address 0: "); 
112
+eeprom_read_string(0, buf, BUFSIZE);
113
+Password_from_eeprom = buf;
114
+//Serial.println(buf);
115
+//Serial.print("Reading string from address 15: ");
116
+//eeprom_read_string(15, buf1, BUFSIZE);
117
+//Password1_from_eeprom = buf1;
118
+//Serial.println(buf);
119
+
120
+}
121
+
122
+
123
+
124
+//---------------------------------------------------------------------
125
+void loop()
126
+{
127
+ int sensorVal = digitalRead(2);
128
+
129
+ if (sensorVal == HIGH) {
130
+    digitalWrite(13, LOW);
131
+  } else {
132
+    digitalWrite(13, HIGH);
133
+  }
134
+
135
+
136
+
137
+  
138
+//-------------PW2-------------------
139
+if (Serial1.available() > 0)
140
+ {
141
+i = Serial1.read();
142
+z ++;
143
+// -------------6 = 5+1(bytes)
144
+if (z < (bytes_from_RFID + 1) )
145
+  {
146
+Serial.print(i, DEC);
147
+Serial.print(" ");
148
+key[z] =i;
149
+// -----------------5 bytes from RFID
150
+if (z == bytes_from_RFID)
151
+   {
152
+ Serial.println();
153
+ Serial1.end();
154
+
155
+ keyvalue = (key[1] );
156
+ keyvalue = (keyvalue + key[2]);
157
+ keyvalue = (keyvalue + key[3]);
158
+ keyvalue = (keyvalue + key[4]);
159
+ keyvalue = (keyvalue + key[5]);
160
+ if (keyvalue == "12409413828")
161
+   {
162
+//------- \n after password means Newline or ENTER
163
+if (sensorVal == HIGH) {
164
+    Keyboard.print(Password_from_eeprom +"\n");
165
+  } else {
166
+    Keyboard.print(Password1_from_eeprom +"\n");
167
+  }
168
+ 
169
+   }
170
+// -----------------5 Seconds lock
171
+ delay(5000);
172
+//--------------------Thats not nice but it works.
173
+ Serial1.begin(9600);
174
+ 
175
+z=0;
176
+   }
177
+  }
178
+ }
179
+}
180
+
181
+//------------------------------------
182
+
183
+
184
+boolean passwort_eingabe()
185
+{
186
+// while( endlos == false)
187
+while (millis() - start <= 15000)
188
+ {
189
+  myString1.reserve(16);
190
+if( Serial.available())
191
+ {   
192
+   
193
+ch = Serial.read();
194
+if (ch != 10)
195
+{
196
+myString1 = (myString1 + ch);
197
+//---------------------------Warum dieser delay?---Keine Ahnung---------------
198
+delay(10);
199
+}
200
+    if (ch == 10)
201
+   { 
202
+      
203
+      
204
+       if (myString1 != "")
205
+          {
206
+            
207
+          myString1.toCharArray(myStringChar1, BUFSIZE); //convert string to char array
208
+          strcpy(buf, myStringChar1);
209
+          eeprom_write_string(0, buf);
210
+          break;
211
+          }
212
+       }
213
+ }
214
+ }
215
+return true ;
216
+}
217
+
218
+