my English is bad, I'm using google translator.
But after reading enough, this is what we get done, basing on 3 things:
- Arduino example WebClienteRepeating
- One of the example presented here
- A web server, created by someone else (can not remember the name)
does the following, ClienteWeb a No-ip automatically updated every 5 minutes + web server
Now I have a problem, how to do that when physically Ethernet cable is disconnected and reconnected ..... Arduino lift both the web server and the web client and automatically reconnect.
had thought about doing externally:
use a transistor as electronic switch such that put the RESET pin to GND (resetting arduino) ....
but not how to do the function
//----------------------------------
// Servidor web + cliente No-Ip
//----------------------------------
#include <SPI.h>
#include <Ethernet.h>
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192, 168, 1, 50);
// fill in your Domain Name Server address here:
IPAddress myDns(192, 168, 1, 1);
// initialize the library instance:
EthernetClient client;
EthernetServer servidor(888);
//----------------------------------------------------------Servidor
int PIN_LED = 12;
String readString = String(30); //lee los caracteres de una secuencia en una cadena. ***no entiendo***
//Los strings se representan como arrays de caracteres (tipo char)
String state = String(3); //****tampoco entiendo****
//-----------------------------------------------------------//
unsigned long lastConnectionTime = 0; // última vez que se conecta al servidor, en milisegundos
const unsigned long postingInterval = 300L * 1000L; // Tiempo entre actualizacion, en milisegundos
// La "L" es necesaria para usar numeros de tipo Long
//----------------------------------------------------------------------------
void setup() {
// iniciar puerto serial:
Serial.begin(9600);
// dar al modulo ethernet tiempo para arrancar:
delay(3000);
// iniciar la conexion ethernet usando ip y dns del DHCP:
Ethernet.begin(mac, ip, myDns);
// print the Ethernet board/shield's IP address:
Serial.print("direccion IP Arduino: ");
Serial.println(Ethernet.localIP());
if (Ethernet.begin(mac) == 0) {
Serial.println("fallo conexion Ethernet usando DHCP");
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, LOW);
state = "OFF";
}
//---------------------------------------------------------------------------
void loop() {
// si hay datos entrantes de la conexión a la red.
// enviarlo por el puerto serie. Esto es para la depuración
// solo para ese proposito:
if (client.available()) {
char c = client.read();
Serial.write(c);
}
// si han transcurrido diez segundos desde su última conexión,
// Luego conecte de nuevo y enviar datos:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
//----------------------------------------------------------------------------
//---------------------Servidor-----------------------------------------------
//----------------------------------------------------------------------------
//EthernetClient Crea un cliente que se puede conectar a
//una dirección específica de Internet IP
EthernetClient cliente= servidor.available();
if(cliente) {
boolean lineaenblanco=true; //****no entiendo*****
while(cliente.connected()) {
if(cliente.available()) {
char c=cliente.read();
if(readString.length()<30) {
readString.concat(c);
//Cliente conectado
//Leemos petición HTTP caracter a caracter
//Almacenar los caracteres en la variable readString
}
if(c=='\n' && lineaenblanco) //Si la petición HTTP ha finalizado
{
int LED = readString.indexOf("LED=");
if(readString.substring(LED,LED+5)=="LED=T") {
digitalWrite(PIN_LED,LOW);
state="ON"; }
else if (readString.substring(LED,LED+5)=="LED=F") {
digitalWrite(PIN_LED,HIGH);
state="OFF";
}
//Cabecera HTTP estándar
cliente.println("HTTP/1.1 200 OK");
cliente.println("Content-Type: text/html");
cliente.println(); //Página Web en HTML
cliente.println("<html>");
cliente.println("<head>");
cliente.println("<title>LAMPARA ON/OFF</title>");
cliente.println("</head>");
cliente.println("<body width=100% height=100%>");
cliente.println("<center>");
cliente.println("<h1>LAMPARA ON/OFF</h1>");
cliente.print("
");
cliente.print("Estado de la lampara: ");
cliente.print(state);
cliente.print("
");
cliente.println("<input type=submit value=ON style=width:200px;height:75px onClick=location.href='./?LED=T\'>");
cliente.println("<input type=submit value=OFF style=width:200px;height:75px onClick=location.href='./?LED=F\'>");
cliente.println("</center>");
cliente.println("</body>");
cliente.println("</html>");
cliente.stop();
//Cierro conexión con el cliente
readString="";
}
}
}
}
}
//----------------------------------------------------------------------------
//--------este método hace una conexión HTTP con el servidor:-----------------
//----------------------------------------------------------------------------
void httpRequest() {
// cerrar cualquier conexión antes de enviar una nueva solicitud.
// Esto liberará la toma del escudo WiFi
client.stop();
// si hay una conexión con éxito:
if (client.connect("dynupdate.no-ip.com", 80)) {
Serial.println();
Serial.println();
Serial.println(">>>>>connectado a no-ip<<<<<");
// Make a HTTP request:
//replace yourhost.no-ip.org with your no-ip hostname
client.println("GET /nic/update?hostname=Tu Dominio de no-ip HTTP/1.0");
client.println("Host: dynupdate.no-ip.com");
//encode your username:password (make sure colon is between username and password)
//to base64 at http://www.opinionatedgeek.com/dotnet/tools/base64encode/
//and replace the string below after Basic with your encoded string
//clear text username:password is not accepted
//reemplazar Usuario@email.com:contraseña encriptados
//la pagina de mas arriva sirve para tal caso
client.println("Authorization: Basic Usuario@email.com:contraseña");
client.println("User-Agent: Arduino Sketch/1.0 user@host.com");
client.println();
// tenga en cuenta el momento en que se realizó la conexión:
lastConnectionTime = millis();
}
else {
// Si no se Logra hacer Una Conexión::
Serial.println("++++++++conneccion fallida a no-ip+++++++++");
}
}
in the code there things you do not understand