Thank you for your patience AWOL!
Serial Monitor shold be seted to send newline.
#include <SPI.h>
#include <Ethernet.h>
#include <EEPROM.h>
EthernetServer server(8888);
byte mac[6];
byte ip[4];
byte gateway[4];
byte subnet[] = {255, 255, 255, 0};
int pins[] = {4, 5, 6, 7, 8, 9};
int pinState[] = {0, 0, 0, 0, 0, 0};
boolean newData = false;
char parLine[100];
void setup()
{
Serial.begin(9600);
for(int i=0;i<6; i++) {pinMode(pins[i], OUTPUT);}
setMAC();
setIP();
byte j = 0;
while(j<3)
{
gateway[j]= ip[j];
j++;
}
gateway[j] = 1;
byte x= EEPROM.read(0);
if(x!='#'){Serial.print("Restart device to set-up the network!");return;}
readEEPROM(mac, 1, 6);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
Serial.print("\n The network was successfuly initiated!");
Serial.print("\n The PC session is finished!");
}
void loop()
{
EthernetClient client= server.available();
if(client)
{
while(client.connected())
{
readHeader(client);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html><body>");
client.println("<h3> Arduino Control Panel</h3>");
client.println("<form method= 'GET'>");
setParams();
setPinStates();
for (int i=0; i<6; i++)
{
htmlPins(client, i);
}
client.println("<input type= 'submit' value= 'Update'/>");
client.println("</form>");
client.println("</body></html>");
delay(1);
client.stop();
}
}
}
void setMAC()
{
char tmac[18];
char tmp[4];
char tmp2[6];
Serial.print("\n Input the MAC address,then press Enter: ");
while(!getString(tmac, 18)){;}
newData= false;
byte i= 0, k= 0, l= strlen(tmac);
while(i<=l)
{
tmp2[0]= '0';
tmp2[1]= 'x';
tmp2[2]= '\0';
byte j= 0;
while(tmac[i]!=':' && tmac[i]!='-' && tmac[i]!='\0')
{
tmp[j++]= tmac[i++];
}
tmp[j]= '\0';
Serial.print(tmp);
if(k!=5)Serial.print(":");
strcat(tmp2, tmp);
mac[k++]= ((byte)strtol(tmp2,NULL,0));
i++;
}
EEPROM.write(0,'#');
byte x=0;
for(int i=1; i<7; i++)
{
byte b= mac[x++];
EEPROM.write(i,b);
delay(3);
}
}
void setIP()
{
char tip[16];
char tmp[4];
Serial.print("\n Input IP address then press Enter: ");
while(!getString(tip, 16)){;}
newData= false;
byte i= 0, k= 0, l=strlen(tip);
while(i<=l)
{
byte j= 0;
while(tip[i]!='.' && tip[i]!='\0')
{
tmp[j++]= tip[i++];
}
tmp[j]='\0';
ip[k++]= ((byte)strtol(tmp,NULL,0));
i++;
}
for(int i=0;i<4;i++)
{
Serial.print(ip[i]);
if(i!=3)Serial.print(".");
}
byte x= 0;
for(int i= 7;i<= 10; i++)
{
byte b= ip[x++];
EEPROM.write(i,b);
delay(3);
}
}
boolean getString(char *s, byte l)
{
static byte x= 0;
char n='\n';
char ch;
while(Serial.available()>0 && newData==false)
{
ch= Serial.read();
if(ch!= n)
{
s[x]= ch;
x++;
if(x>= l)
{
x= l-1;
}
}
else
{
s[x]='\0';
x= 0;
newData= true;
return true;
}
}
return false;
}
void readEEPROM(byte *arr,int s,int e)
{
byte b, x= 0;
int i=s;
while(i<=e)
{
b=EEPROM[i++];
arr[x++]= b;
}
}
void readHeader(EthernetClient client)
{
char ch;
int i = 0;
while (ch!='\n')
{
if (client.available())
{
ch = client.read();
parLine[i] = ch;
i ++;
}
}
parLine[i] ='\0';
}
void setParams()
{
if(parLine[5]=='?')
{
for (int i=0; i<6; i++)
{
pinState[i] = valueOfParam(i + '0');
}
}
}
int valueOfParam(char param)
{
int l= strlen(parLine);
for (int i = 6; i<l; i++)
{
if (parLine[i]==param && parLine[i+1]== '=')
{
return(parLine[i+2]- '0');
}
}
return 0;
}
void setPinStates()
{
for (int i=0; i<6; i++) {digitalWrite(pins[i], pinState[i]);}
}
void htmlPins(EthernetClient client,int i)
{
client.print("<p> D");
client.print(pins[i]);
client.print(" ");
client.print(" <select name='");
client.print(i);
client.println("'>");
client.print("<option value='0'");
if (pinState[i] == 0)
{
client.print(" selected");
}
client.println(">Off</option>");
client.print("<option value='1'");
if (pinState[i] == 1)
{
client.print(" selected");
}
client.println(">On</option>");
client.println("</select></p>");
}