-
Notifications
You must be signed in to change notification settings - Fork 1
/
arduino-eeprom.ino
132 lines (115 loc) · 4.22 KB
/
arduino-eeprom.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
/*
Info : Serial implementation for storing/retrieving
IP address information to/from EEPROM
Created : 19 June 2017
Author : Eddy Yanto
Board : Arduino Ethernet (1KB EEPROM)
Available serial commands EEPROM Address
--------------------------- --------------
WRITE:IP:192.168.1.50 IP : 0-3
WRITE:SUBNET:255.255.255.0 SUBNET : 4-7
WRITE:GATEWAY:192.168.1.254 GATEWAY : 8-11
WRITE:DNS:192.168.1.254 DNS : 12-15
READ:IP
READ:SUBNET
READ:GATEWAY
READ:DNS
RESTART:NET
*/
#include <EEPROM.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x20, 0xF8, 0x5E, 0xEF, 0xFE, 0xED };
char buff[32]; //buffer for sprintf
char byteSerial; //store incoming serial one byte at a time
String inputSerial, command, param; //command parsing variables
//EEPROM addresses
const int ipEEPROMAddress[] = {0, 1, 2, 3};
const int subnetEEPROMAddress[] = {4, 5, 6, 7};
const int gatewayEEPROMAddress[] = {8, 9, 10, 11};
const int dnsEEPROMAddress[] = {12, 13, 14, 15};
IPAddress ip(EEPROM.read(0), EEPROM.read(1), EEPROM.read(2), EEPROM.read(3));
IPAddress subnet(EEPROM.read(4), EEPROM.read(5), EEPROM.read(6), EEPROM.read(7));
IPAddress gateway(EEPROM.read(8), EEPROM.read(9), EEPROM.read(10), EEPROM.read(11));
IPAddress mdns(EEPROM.read(12), EEPROM.read(13), EEPROM.read(14), EEPROM.read(15));
EthernetServer tcpServer(7000);
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip, mdns, gateway, subnet);
tcpServer.begin();
}
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void loop() {
__processSerial();
}
void __processSerial() {
if (Serial.available() > 0) {
byteSerial = Serial.read();
inputSerial += byteSerial;
if (byteSerial == '\r') {
inputSerial.trim();
//parse COMMAND: WRITE or READ — eg: WRITE:IP:192.168.1.50
int i = inputSerial.indexOf(":"); //421
command = inputSerial.substring(0, i);
//trim COMMAND from input — eg:
//WRITE:IP:192.168.1.50
//IP:192.168.1.50
inputSerial = inputSerial.substring(i + 1, inputSerial.length());
//parse PARAM: IP or SUBNET or GATEWAY or DNS
//IP:192.168.1.50
//192.168.1.50
i = inputSerial.indexOf(":");
param = inputSerial.substring(0, i);
if (command == "WRITE") {
//trim PARAM from input
inputSerial = inputSerial.substring(i + 1, inputSerial.length()); //eg: 192.168.1.50
IPAddress tempIp;
int valid = tempIp.fromString(inputSerial);
if (valid) {
if (param == "IP") {
__writeEEPROM(ipEEPROMAddress, tempIp);
} else if (param == "SUBNET") {
__writeEEPROM(subnetEEPROMAddress, tempIp);
} else if (param == "GATEWAY") {
__writeEEPROM(gatewayEEPROMAddress, tempIp);
} else if (param == "DNS") {
__writeEEPROM(dnsEEPROMAddress, tempIp);
} else {
sprintf(buff, "\nUNKNOWN COMMAND");
}
} else {
sprintf(buff, "\nINVALID ADDRESS");
}
} else if (command == "READ") {
if (param == "IP") {
sprintf(buff, "\nIP : %d.%d.%d.%d", EEPROM.read(0), EEPROM.read(1), EEPROM.read(2), EEPROM.read(3));
} else if (param == "SUBNET") {
sprintf(buff, "\nSUBNET : %d.%d.%d.%d", EEPROM.read(4), EEPROM.read(5), EEPROM.read(6), EEPROM.read(7));
} else if (param == "GATEWAY") {
sprintf(buff, "\nGATEWAY : %d.%d.%d.%d", EEPROM.read(8), EEPROM.read(9), EEPROM.read(10), EEPROM.read(11));
} else if (param == "DNS") {
sprintf(buff, "\nDNS : %d.%d.%d.%d", EEPROM.read(12), EEPROM.read(13), EEPROM.read(14), EEPROM.read(15));
} else {
sprintf(buff, "\nUNKNOWN COMMAND");
}
} else if (command == "RESTART") {
sprintf(buff, "\nRESTART OK");
delay(50);
resetFunc();
} else {
sprintf(buff, "\nUNKNOWN COMMAND");
}
Serial.println(buff);
memset(buff, 0, sizeof buff);
inputSerial = "";
command = "";
param = "";
}
}
}
void __writeEEPROM(int add[4], IPAddress tempIp) {
for (int i = 0; i <= 3; i++) {
EEPROM.write(add[i], tempIp[i]);
}
Serial.println("\nWRITE OK");
}