-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfMODEM.ino
918 lines (723 loc) · 20.4 KB
/
fMODEM.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/*
fMODEM v1.41 Sep 2019
A Fake modem for retro-computers.
Connects to wifi, and passes data over RS232.
Requires Arduino UNO WiFi with TTL to RS232 Serial port card on TX/RX pins.
Configure WiFi:
AT ssid=Your WiFI Name e.g. AT ssid=MYHOTSPOT
AT password=Your Wifi password e.g. AT passowrd=SecretPW
AT password=OPEN is a special case for networks with no password
AT SAVE
Connect to WiFi:
AT WIFI
(Once you have done an AT SAVE it will store details in EEPROM and use them next power up)
Connect to server:
AT TELNET=telnet.server.address e.g. AT TELNET=towel.blinkenlights.nl
AT TELNET=telnet.server.address,port e.g. AT TELNET=towel.blinkenlights.nl,23
Other commands
AT LOGOUT to cancel connection
AT DROP to drop WiFi connection
AT LIST to list available access points
Based on Ardunio examples..
This example connects to an unencrypted Wifi network.
Then it prints the MAC address of the Wifi module,
the IP address obtained, and other network details.
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
Note: When compiling you may see a warning related to fuse5. This is apparently a known issue.
*/
#include <SPI.h>
#include <WiFiNINA.h>
#include <EEPROM.h>
#include <WiFiUdp.h>
// I wired up four LEDs, for power, RX, TX and AUX.
// AUX is used for active WiFi connection.
#define LED_POWER 7
#define LED_RX 4
#define LED_TX 6
#define LED_AUX 5
// Used to store user input
char buffer[80];
char buffer_count = 0;
// Comment this out when you deploy to the device, otherwise output will be sent to Serial, which is the USB Serial port.
// Great for debugging, not so great when you want the RS232 connected computer to see what is going on.
#define DEBUG
// A pointer to the serial port, to make a debug and deploy version.
HardwareSerial *SerialPort;
// Used by the Network Time Service. Yes, now your retro computer can get access to a RTC
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// WiFi connection stuff
int status = WL_IDLE_STATUS; // the Wifi radio's status
char ssid[16]; // your network SSID (name)
char pass[16]; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;
WiFiUDP Udp;
WiFiClient client; //client.connect
char server[] = "towel.blinkenlights.nl";
// Status
bool connected_to_web = false;
bool connected_to_wifi = false;
// LED blink counter
int TX_COUNT = 0;
int RX_COUNT = 0;
int BLINKLIMIT = 20;
void LED_BLINK_MANAGER()
{
if (TX_COUNT != 0)
{
TX_COUNT++;
if (TX_COUNT > BLINKLIMIT)
{
LED_TX_OFF();
TX_COUNT = 0;
}
}
if (RX_COUNT != 0)
{
RX_COUNT++;
if (RX_COUNT > BLINKLIMIT)
{
LED_RX_OFF();
RX_COUNT = 0;
}
}
}
void setup() {
#ifdef DEBUG
SerialPort = &Serial;
#else
SerialPort = &Serial1;
#endif
clearBuffer();
pinMode(LED_POWER, OUTPUT);
pinMode(LED_RX, OUTPUT);
pinMode(LED_TX, OUTPUT);
pinMode(LED_AUX, OUTPUT);
digitalWrite(LED_POWER, HIGH);
LED_TX_ON();
LED_RX_ON();
digitalWrite(LED_AUX, HIGH);
//Initialize serial and wait for port to open:
#ifdef DEBUG
SerialPort->begin(9600);
while (!SerialPort) {
; // wait for serial port to connect. Needed for native USB port only
}
#else
SerialPort->begin(9600);
#endif
SerialPort->println("");
SerialPort->println( " ## ## ####### ######## ######## ## ## ");
SerialPort->println( " #### ### ### ## ## ## ## ## ### ### ");
SerialPort->println( "## #### #### ## ## ## ## ## #### #### ");
SerialPort->println( "###### ## ### ## ## ## ## ## ###### ## ### ## ");
SerialPort->println( "## ## ## ## ## ## ## ## ## ## ");
SerialPort->println( "## ## ## ## ## ## ## ## ## ## ");
SerialPort->println( "## ## ## ####### ######## ######## ## ## ");
SerialPort->println( "## ");
SerialPort->println( " fMODEM v0.41 September 2019");
SerialPort->println("");
digitalWrite(LED_POWER, HIGH);
digitalWrite(LED_AUX, LOW);
SerialPort->println("fMODEM is active. AT HELP for options.");
SerialPort->println("");
SerialPort->print("Restoring settings..");
// Read previously stored settings
if (ReadData())
{
SerialPort->print("Loaded SSID: [");
SerialPort->print(ssid);
SerialPort->println("] and Password: [********]");
SerialPort->println("OK");
ConnectToWifi();
}
else
{
SerialPort->println("No settings have been saved.");
// Clear SSID and Passwords to zero
for (int i = 0; i < 16; i++)
{
pass[i] = 0;
ssid[i] = 0;
}
}
}
void loop() {
// LED Blink stuff
LED_BLINK_MANAGER();
// Process user input
EnterLine();
// Display content from server
if (connected_to_web)
{
// if the server's disconnected, stop the client:
if (!client.connected()) {
SerialPort->println("Disconnecting from server.");
client.stop();
client.flush();
connected_to_web = false;
}
while (client.available()) {
LED_RX_ON();
char c = client.read();
SerialPort->write(c);
}
}
}
// Process what the user just typed.
void EnterLine()
{
if (SerialPort->available())
{
LED_RX_ON();
int inByte = SerialPort->read();
buffer[buffer_count] = inByte;
SerialPort->write(inByte);
bool found_command = false;
if (inByte == 8) // User pressed delete
{
buffer_count--;
if (buffer_count < 0)
{
buffer_count = 0;
}
}
else
{
buffer_count++;
if (buffer_count >= 80)
{
buffer_count = 79;
}
}
if (inByte == 13) // User pressed return
{
// Eat next char, it will be 10
int extraByte = SerialPort->read();
// Check for commmands
// remove extra line feeds
if (buffer[0] == 10)
{
for (int i = 0; i < buffer_count; i++)
{
buffer[i] = buffer[i + 1];
}
}
buffer[buffer_count - 1] = 0; // Get rid of the newline character in there
// Process general AT commands
if (strncasecmp(buffer, "AT ", 3) == 0)
{
AT(String(buffer));
found_command = true;
}
// Process ATx commands.
if (strncasecmp(buffer, "ATI", 3) == 0)
{
printWifiData();
printCurrentNet();
found_command = true;
}
// All commands that have been recognized have been processed.
if (found_command == true)
{
buffer_count = 0;
for (int i = 0; i < 80; i++)
buffer[i] = 0;
//LED_TX_OFF();
return;
}
// Take a new line on the terminal, so the user knows they pressed return
SerialPort->println("");
// Send the string to the internet if connected
if (connected_to_web == true)
{
client.write(buffer);
LED_TX_ON();
}
clearBuffer();
}
}
}
// Process the various pseudo AT messages
void AT(const String& message) {
char option[80];
message.toCharArray(option, 80);
SerialPort->println("");
if (strncasecmp(option, "AT LIST", 7) == 0)
{
listNetworks();
}
if (strncasecmp(buffer, "AT SAVE", 7) == 0)
{
SerialPort->print("Saving..");
WriteData();
SerialPort->println("OK");
}
if (strncasecmp(buffer, "AT LOAD", 7) == 0)
{
//AT("HELP");
SerialPort->print("Loading..");
if (ReadData())
{
printWifiData();
printCurrentNet();
}
else
{
SerialPort->println("Unable to read data.");
}
SerialPort->println("OK");
}
if (strncasecmp(buffer, "AT DROP", 7) == 0)
{
AT_drop();
}
if (strncasecmp(buffer, "AT TIME", 7) == 0)
{
AT_getTime();
}
if (strncasecmp(buffer, "AT HELP", 7) == 0)
{
SerialPort->println("");
SerialPort->println("Welcome to fMODEM");
SerialPort->println("");
SerialPort->println("Configure WiFi:");
SerialPort->println("AT ssid=Your WiFI Name e.g. AT ssid=MYHOTSPOT");
SerialPort->println("AT password=Your Wifi password e.g. AT passowrd=SecretPW");
SerialPort->println("AT password=OPEN is a special case for networks with no password");
SerialPort->println("AT SAVE ");
SerialPort->println("");
SerialPort->println("Connect to WiFi:");
SerialPort->println("AT WIFI");
SerialPort->println("(Once you have done an AT SAVE it will store details in EEPROM and use them next power up)");
SerialPort->println("");
SerialPort->println("Connect to server:");
SerialPort->println("AT TELNET=telnet.server.address e.g. AT TELNET=towel.blinkenlights.nl");
SerialPort->println("AT TELNET=telnet.server.address,port e.g. AT TELNET=towel.blinkenlights.nl,port");
SerialPort->println("");
SerialPort->println("Other commands");
SerialPort->println("AT LOGOUT to cancel connection");
SerialPort->println("AT DROP to drop WiFi connection");
SerialPort->println("AT LIST to list available access points");
}
if ((strncasecmp(option, "AT WIFI", 7) == 0) || (strncasecmp(option, "AT BRIDGE", 9) == 0))
{
SerialPort->println("Looking for WiFi..");
ConnectToWifi();
}
if (strncasecmp(option, "AT SSID=", 8) == 0)
{
String(option).substring(8).toCharArray(ssid, 16);
SerialPort->print("OK, SSID defined as: ");
SerialPort->println(ssid);
}
if (strncasecmp(option, "AT PASSWORD=", 12) == 0)
{
String(option).substring(12).toCharArray(pass, 16);
SerialPort->print("OK, Password defined as: ");
SerialPort->println(pass);
}
if (strncasecmp(option, "AT TELNET=", 10) == 0)
{
AT_Telnet(String(option).substring(10));
}
if (strncasecmp(option, "AT LOGIN", 8) == 0)
{
if (connected_to_wifi)
{
AT_Login();
}
else
{
SerialPort->println("Connect to WiFi first.");
}
}
if ((strncasecmp(option, "AT LOGOUT", 9) == 0) || (strncasecmp(option, "AT HANGUP", 9) == 0))
{
if (!connected_to_wifi)
{
SerialPort->println("Connect to WiFi first.");
}
else
{
if (connected_to_web)
{
AT_Logout();
}
else
{
SerialPort->println("Log in first.");
}
}
}
SerialPort->println("OK");
}
void LED_TX_ON()
{
digitalWrite(LED_TX, HIGH);
TX_COUNT = 1;
}
void LED_TX_OFF()
{
digitalWrite(LED_TX, LOW);
TX_COUNT = 0;
}
void LED_RX_ON()
{
digitalWrite(LED_RX, HIGH);
RX_COUNT = 1;
}
void LED_RX_OFF()
{
digitalWrite(LED_RX, LOW);
RX_COUNT = 0;
}
void ConnectToWifi()
{
LED_TX_ON();
LED_RX_ON();
digitalWrite(LED_AUX, LOW);
int timeout = 0;
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
SerialPort->println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
SerialPort->println("Please upgrade the firmware");
}
digitalWrite(LED_AUX, LOW);
SerialPort->print("Attempting to connect to WiFi..");
// attempt to connect to Wifi network:
while (status != WL_CONNECTED && timeout < 2) {
if (pass == "OPEN")
{
status = WiFi.begin(ssid);
}
else
{
status = WiFi.begin(ssid, pass);
}
timeout++;
// Blink the LEDs for no reason other than it's cool.
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 3; i++)
{
if (i == 0)
{
LED_TX_OFF();
LED_RX_OFF();
digitalWrite(LED_AUX, LOW);
}
if (i == 1)
{
LED_TX_OFF();
LED_RX_ON();
digitalWrite(LED_AUX, LOW);
}
if (i == 2)
{
LED_TX_ON();
LED_RX_ON();
digitalWrite(LED_AUX, LOW);
}
delay(400);
}
}
}
digitalWrite(LED_AUX, HIGH);
LED_TX_OFF();
LED_RX_OFF();
if (status == WL_CONNECTED)
{
connected_to_wifi = true;
// you're connected now, so print out the data:
SerialPort->println("Connected to the network.");
printWifiData();
printCurrentNet();
}
else
{
SerialPort->println("Could not connect. Check SSID and PASSWORD settings.");
digitalWrite(LED_AUX, LOW);
}
SerialPort->println("OK");
}
void AT_Telnet(String server) {
for (int i = 0; i < server.length(); i++)
if (server[i] == 13) server[i] = 0;
server.toCharArray(buffer, 80);
// Check for a comma, indicating a port has been specified
int comma = server.indexOf(",");
if ( comma > 0)
{
String address = server.substring(0, comma);
int port = server.substring(comma + 1).toInt();
address.toCharArray(buffer, comma + 1);
if (client.connect(buffer, port))
{
SerialPort->println("Connected to server");
connected_to_web = true;
}
else
{
SerialPort->println("Connection failed (1!)");
}
}
else // no port specified, use default for telnet - 23
{
if (client.connect(buffer, 23)) {
SerialPort->println("connected to server (2)");
connected_to_web = true;
}
else
{
SerialPort->println("Connection failed (2)");
}
}
}
void AT_Login() {
SerialPort->println("Opening connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 464)) {
SerialPort->println("connected to server");
connected_to_web = true;
}
}
// Drop Wifi
void AT_drop()
{
if (connected_to_wifi)
{
WiFi.disconnect();
SerialPort->println("Disconnected from WiFi.");
connected_to_wifi = false;
digitalWrite(LED_AUX, LOW);
}
}
void AT_Logout()
{
if (connected_to_web)
{
client.stop();
client.flush();
SerialPort->println("Disconnected");
connected_to_web = false;
}
else
{
SerialPort->println("Not currently connected.");
}
}
void listNetworks() {
// scan for nearby networks:
if (!connected_to_wifi)
{
SerialPort->println("Must be connected to WiFi first.");
return;
}
SerialPort->println("Scanning for WiFi networks..");
int numSsid = WiFi.scanNetworks();
if (numSsid == -1) {
SerialPort->println("Couldn't get a wifi connection");
while (true);
}
// print the list of networks seen:
SerialPort->print("number of available networks:");
SerialPort->println(numSsid);
// print the network number and name for each network found:
for (int thisNet = 0; thisNet < numSsid; thisNet++) {
SerialPort->print(thisNet);
SerialPort->print(") ");
SerialPort->print(WiFi.SSID(thisNet));
SerialPort->print("\tSignal: ");
SerialPort->print(WiFi.RSSI(thisNet));
SerialPort->print(" dBm");
SerialPort->print("\tEncryption: ");
byte encryption = WiFi.encryptionType(thisNet);
SerialPort->println(String(encryption));
}
}
void AT_getTime() {
if (!connected_to_wifi)
{
SerialPort->println("Must be connected to WiFi first.");
return;
}
SerialPort->println("Connecting to time server..");
int numSsid = WiFi.scanNetworks(); // Seems to require some network access first. This works..
IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
unsigned int localPort = 2390; // local port to listen for UDP packets
Udp.begin(localPort);
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if (Udp.parsePacket()) {
// We've received a packet, read the data from it
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
unsigned long secsSince1900 = highWord << 16 | lowWord;
SerialPort->print("Seconds since Jan 1 1900 = ");
SerialPort->println(secsSince1900);
// now convert NTP time into everyday time:
SerialPort->print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
SerialPort->println(epoch);
// print the hour, minute and second:
SerialPort->print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
SerialPort->print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
SerialPort->print(':');
if (((epoch % 3600) / 60) < 10) {
// In the first 10 minutes of each hour, we'll want a leading '0'
SerialPort->print('0');
}
SerialPort->print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
SerialPort->print(':');
if ((epoch % 60) < 10) {
// In the first 10 seconds of each minute, we'll want a leading '0'
SerialPort->print('0');
}
SerialPort->println(epoch % 60); // print the second
}
Udp.stop();
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address) {
memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
void printWifiData() {
digitalWrite(LED_TX, HIGH);
// Af we connected to the Wifi?
SerialPort->println();
if (connected_to_wifi)
{
// print your board's IP address:
IPAddress ip = WiFi.localIP();
SerialPort->print("IP Address: ");
SerialPort->println(ip);
}
else
{
SerialPort->println("IP Address: (not currently assigned)");
}
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
SerialPort->print("MAC address: ");
printMacAddress(mac);
digitalWrite(LED_TX, LOW);
}
void printCurrentNet() {
digitalWrite(LED_TX, HIGH);
if (connected_to_wifi)
{
// print the SSID of the network you're attached to:
SerialPort->print("SSID: ");
SerialPort->println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
SerialPort->print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
SerialPort->print("signal strength (RSSI):");
SerialPort->println(String(rssi));
// print the encryption type:
byte encryption = WiFi.encryptionType();
SerialPort->print("Encryption Type: ");
SerialPort->println(String(encryption));
}
else
{
SerialPort->print("SSID: [");
SerialPort->print(ssid);
SerialPort->println("] (not currently connected)");
SerialPort->print("Password: [");
SerialPort->print(pass);
SerialPort->println("] (not currently connected)");
}
digitalWrite(LED_TX, LOW);
}
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
SerialPort->print("0");
}
SerialPort->print(String(mac[i]));
if (i > 0) {
SerialPort->print(":");
}
}
SerialPort->println();
}
void clearBuffer()
{
// Clear buffer
for (int i = 0; i < 80; i++)
buffer[i] = 0;
buffer_count = 0;
}
bool ReadData()
{
// 1. Read and check for magic value (42) to say we have something
// 2. Read SSID and Password if there is something
byte value = EEPROM.read(0);
char buf_s[16];
char buf_p[16];
if (value != 42) return false;
for (int i = 0; i < 16; i++)
{
buf_s[i] = EEPROM.read(i + 1);
if (buf_s[i] == 13) buf_s[i] = 0;
}
for (int i = 0; i < 16; i++)
{
buf_p[i] = EEPROM.read(i + 1 + 16);
if (buf_p[i] == 13) buf_p[i] = 0;
}
String str_s = String(buf_s);
String str_p = String(buf_p);
int l_s = str_s.length() + 1;
int l_p = str_p.length() + 1;
str_s.toCharArray(ssid, l_s);
str_p.toCharArray(pass, l_p);
return true;
}
void WriteData()
{
// 1. Write magic value (42) to say we have something
// 2. Write SSID () and Password()
// Erase
for (int i = 0; i < 33; i++)
{
EEPROM.write(i, 0);
}
EEPROM.write(0, 42);
for (int i = 0; i < 16; i++)
EEPROM.write(i + 1, ssid[i]);
for (int i = 0; i < 16; i++)
EEPROM.write(i + 1 + 16, pass[i]);
}