2-um botão onde habilito o dhcp e salva essa opção na eeprom - ok (dá para acompanhar alguns prints na saida serial)
OBS. Quando reset a placa ela volta a ter um ip fixo, está assim porque mandei escrever 0 na eeprom sempre que reinicia, mas isso vai virar um botão reset no futuro
Dúvida, alguém tem ideia de como ler essa URL e jogar para um array para que eu possa escrever na eeprom?
Segue código...Abraços e obrigado a todos...
#include <EtherCard.h>
#include <EEPROM.h>
String versao = "--- VERSAO 3_4";
byte VarDHCP = 0; //IP INICIAL E FIXO SE POR 1 O IP SERA POR DHCP
// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[1000]; // tcp/ip send and receive buffer
BufferFiller bfill;
const char http_OK[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n\r\n";
const char http_Found[] PROGMEM =
"HTTP/1.0 302 Found\r\n"
"Location: /\r\n\r\n";
const char http_Unauthorized[] PROGMEM =
"HTTP/1.0 401 Unauthorized\r\n"
"Content-Type: text/html\r\n\r\n"
"<h1>401 Unauthorized</h1>";
static word ip() {
//$D = word data type
//$L = long data type
//$S = c string
//$F = progmem string
//$E = byte from the eeprom.
char* DHCP;
VarDHCP = EEPROM.read(0);
if ( EEPROM.read(0) == 0 ) {
DHCP = "FIXO";
} else {
DHCP = "DHCP";
}
bfill.emit_p(PSTR(
"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">"
"<html><head><title>---</title>"
"<meta http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"">"
"</head>"
"<body bgcolor=""#99CCFF"">"
"<center>"
"REDE:"
"<hr />"
"<form>"
"<p>ENDERECO IP:<BR>"
"<input name=ip1 type=text value=""192"" size=2 maxlength=""3"">"
"<input name=ip2 type=text value=""168"" size=2 maxlength=""3"">"
"<input name=ip3 type=text value=""1"" size=2 maxlength=""3"">"
"<input name=ip4 type=text value=""155"" size=2 maxlength=""3"">"
"<BR>"
"MASCARA: <BR>"
"<input name=mk1 type=text value=""255"" size=2 maxlength=""3"">"
"<input name=mk2 type=text value=""255"" size=2 maxlength=""3"">"
"<input name=mk3 type=text value=""255"" size=2 maxlength=""3"">"
"<input name=mk4 type=text value=""0"" size=2 maxlength=""3""><br>"
"<BR>"
"<input name=""ipfixo"" type=""submit"" id=""ipfixo"" value=""FIXO"">"
"</form>"
"<form><input type=""button"" value=""DHCP"" onClick=""document.location.href='/?dhcp';""></form>"
"<hr />"
"<p>ATUAL: <font color=""#FF0000""><b> $S</b></font></p>"
"<hr />"
"</center></body></html>"),
DHCP);
return bfill.position();
}
//"<form><input name=""dhcp"" type=""submit"" id=""dhcp"" value=""DHCP""></form>"
//"<form><input type=""button"" value=""DHCP"" onClick=""document.location.href='/?dhcp';""></form>"
//"<form><input type=""button"" value=""Voltar"" onClick=""document.location.href='/?menu';""></form>"
void setup(){
EEPROM.write(0, VarDHCP); //GRAVA 0 NA EEPROM, SEMPRE QUE REINICIAR SERA IP FIXO, NO FUTURO ESSA LINHA SAI
Serial.begin(9600);
Serial.print("\n\n[REDE]");
Serial.println(versao);
if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
Serial.println( "Failed to access Ethernet controller" );
VarDHCP = EEPROM.read(0);
// Serial.println(VarDHCP);
switch (VarDHCP) {
case 0:
Serial.println("IP FIXO");
static byte myip[] = { 192,168,1,155 };
static byte mymask[] = { 255,255,255,0 };
ether.staticSetup(myip, mymask);
break;
case 1:
if (!ether.dhcpSetup()){
Serial.println("DHCP failed");
} else {
ether.printIp("IP: ", ether.myip);
ether.printIp("MASK: ", ether.mymask);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
}
break;
default:
Serial.println("ERRO NA REDE");
}
}
void loop(){
// Serial.print("--- VarDHCP inicial: ");
VarDHCP = EEPROM.read(0);
// Serial.println(VarDHCP);
// delay(1000);
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if (pos) { // check if valid tcp data is received // data received from ethernet
delay(1);
bfill = ether.tcpOffset();
Serial.println("-- solicitacao de pagina positiva");
char *data = (char *) Ethernet::buffer + pos;
if (strncmp("GET /", data, 5) != 0) {
bfill.emit_p(http_Unauthorized);
}
else
{
data += 5;
if (data[0] == ' ') {
// Return home page
ip();
}
else if (strncmp( "?dhcp" , data, 5 ) == 0)
{
VarDHCP = 1;
EEPROM.write(0, VarDHCP);
Serial.print("--- VarDHCP: ");
Serial.println(EEPROM.read(0));
delay(1000);
// bfill.emit_p(http_Found);
}
}
bfill = ether.tcpOffset();
ether.httpServerReply(ip());
delay(1000);
}
/*
if (ether.packetLoop(ether.packetReceive())) {
memcpy_P(ether.tcpOffset(), page, sizeof page);
ether.httpServerReply(sizeof page - 1);
}
*/
}