Diferencia entre revisiones de «WebClientRepeating»

De ArduWiki
Saltar a: navegación, buscar
(Referencias)
(Codigo)
 
(No se muestran 4 ediciones intermedias del mismo usuario)
Línea 1: Línea 1:
== Web Client Repeating ==
 
 
 
Este ejemplo muestra cómo realizar solicitudes HTTP repetidas utilizando un escudo Ethernet. Este ejemplo utiliza DNS, asignando al cliente Ethernet una dirección MAC, una dirección IP y una dirección DNS. Se conecta a http://www.arduino.cc/latest.txt. El contenido de la página se puede ver a través de la ventana serie de Arduino.
 
Este ejemplo muestra cómo realizar solicitudes HTTP repetidas utilizando un escudo Ethernet. Este ejemplo utiliza DNS, asignando al cliente Ethernet una dirección MAC, una dirección IP y una dirección DNS. Se conecta a http://www.arduino.cc/latest.txt. El contenido de la página se puede ver a través de la ventana serie de Arduino.
  
 +
== Codigo ==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
/*
 
This sketch connects to a a web server and makes a request
 
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
 
the Adafruit Ethernet shield, either one will work, as long as it's got
 
a Wiznet Ethernet module on board.
 
 
This example uses DNS, by assigning the Ethernet client with a MAC address,
 
IP address, and DNS address.
 
 
Circuit:
 
* Ethernet shield attached to pins 10, 11, 12, 13
 
 
created 19 Apr 2012
 
by Tom Igoe
 
modified 21 Jan 2014
 
by Federico Vanzati
 
 
http://www.arduino.cc/en/Tutorial/WebClientRepeating
 
This code is in the public domain.
 
 
*/
 
 
 
#include <SPI.h>
 
#include <SPI.h>
 
#include <Ethernet.h>
 
#include <Ethernet.h>
  
// assign a MAC address for the ethernet controller.
+
//Ethernet shield attached to pins 10, 11, 12, 13
// fill in your address here:
+
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //Asigna una MAC al modulo
byte mac[] = {
+
IPAddress ip(192, 168, 1, 177);   //Asigna un IP libre
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
+
IPAddress myDns(1, 1, 1, 1);     //Asigna tu servidor DNS
};
 
// fill in an available IP address on your network here,
 
// for manual configuration:
 
IPAddress ip(192, 168, 1, 177);
 
 
 
// fill in your Domain Name Server address here:
 
IPAddress myDns(1, 1, 1, 1);
 
  
// initialize the library instance:
+
EthernetClient client;             //Inicializa cliente
EthernetClient client;
 
  
 
char server[] = "www.arduino.cc";
 
char server[] = "www.arduino.cc";
 
//IPAddress server(64,131,82,241);
 
//IPAddress server(64,131,82,241);
  
unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
+
unsigned long lastConnectionTime = 0;               //Ultima conexion
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
+
const unsigned long postingInterval = 10L * 1000L;   //Retardo entre actualizaciones
// the "L" is needed to use long type numbers
 
  
 
void setup() {
 
void setup() {
  // start serial port:
+
  Serial.begin(9600);
  Serial.begin(9600);
+
  delay(1000);
  while (!Serial) {
+
  Ethernet.begin(mac, ip, myDns);             //Inicia conexion Ethernet
    ; // wait for serial port to connect. Needed for native USB port only
+
  Serial.print("Mi direccion IP: ");
  }
+
  Serial.println(Ethernet.localIP());
 
 
  // give the ethernet module time to boot up:
 
  delay(1000);
 
  // start the Ethernet connection using a fixed IP address and DNS server:
 
  Ethernet.begin(mac, ip, myDns);
 
  // print the Ethernet board/shield's IP address:
 
  Serial.print("My IP address: ");
 
  Serial.println(Ethernet.localIP());
 
 
}
 
}
  
 
void loop() {
 
void loop() {
  // if there's incoming data from the net connection.
+
  if (client.available()) {
  // send it out the serial port.  This is for debugging
+
      char c = client.read();
  // purposes only:
+
      Serial.write(c);
  if (client.available()) {
+
  }
    char c = client.read();
+
  if (millis() - lastConnectionTime > postingInterval) {
    Serial.write(c);
+
      httpRequest();
  }
+
  }
 
 
  // if ten seconds have passed since your last connection,
 
  // then connect again and send data:
 
  if (millis() - lastConnectionTime > postingInterval) {
 
    httpRequest();
 
  }
 
 
 
 
}
 
}
  
// this method makes a HTTP connection to the server:
 
 
void httpRequest() {
 
void httpRequest() {
  // close any connection before send a new request.
+
  client.stop(); //Cierra conexion y libera socket
  // This will free the socket on the WiFi shield
+
  if (client.connect(server, 80)) {
  client.stop();
+
      Serial.println("conectando...");
 
+
      client.println("GET /latest.txt HTTP/1.1");   //Envia requerimiento HHTTP GET
  // if there's a successful connection:
+
      client.println("Host: www.arduino.cc");
  if (client.connect(server, 80)) {
+
      client.println("User-Agent: arduino-ethernet");
    Serial.println("connecting...");
+
      client.println("Connection: close");
    // send the HTTP GET request:
+
      client.println();
    client.println("GET /latest.txt HTTP/1.1");
+
      lastConnectionTime = millis();
    client.println("Host: www.arduino.cc");
+
  }else{
    client.println("User-Agent: arduino-ethernet");
+
      Serial.println("conexion fallo.");
    client.println("Connection: close");
+
  }
    client.println();
 
 
 
    // note the time that the connection was made:
 
    lastConnectionTime = millis();
 
  } else {
 
    // if you couldn't make a connection:
 
    Serial.println("connection failed");
 
  }
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== Vea también ==
 
== Vea también ==
 +
<categorytree mode=all>Libreria Ethernet</categorytree>
  
 
== Referencias externas ==
 
== Referencias externas ==
 +
* [http://www.arduino.cc/en/Tutorial/WebClientRepeating Web Client Repeating]
 +
 
[[Category:Ejemplos]]
 
[[Category:Ejemplos]]
 
[[Category:Libreria Ethernet]]
 
[[Category:Libreria Ethernet]]

Revisión actual del 18:55 30 may 2019

Este ejemplo muestra cómo realizar solicitudes HTTP repetidas utilizando un escudo Ethernet. Este ejemplo utiliza DNS, asignando al cliente Ethernet una dirección MAC, una dirección IP y una dirección DNS. Se conecta a http://www.arduino.cc/latest.txt. El contenido de la página se puede ver a través de la ventana serie de Arduino.

Codigo

#include <SPI.h>
#include <Ethernet.h>

//Ethernet shield attached to pins 10, 11, 12, 13
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};  //Asigna una MAC al modulo
IPAddress ip(192, 168, 1, 177);   //Asigna un IP libre
IPAddress myDns(1, 1, 1, 1);      //Asigna tu servidor DNS

EthernetClient client;             //Inicializa cliente

char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241);

unsigned long lastConnectionTime = 0;                //Ultima conexion
const unsigned long postingInterval = 10L * 1000L;   //Retardo entre actualizaciones

void setup() {
   Serial.begin(9600);
   delay(1000);
   Ethernet.begin(mac, ip, myDns);             //Inicia conexion Ethernet
   Serial.print("Mi direccion IP: ");
   Serial.println(Ethernet.localIP());
}

void loop() {
   if (client.available()) {
      char c = client.read();
      Serial.write(c);
   }
   if (millis() - lastConnectionTime > postingInterval) {
      httpRequest();
   }
}

void httpRequest() {
   client.stop();  //Cierra conexion y libera socket
   if (client.connect(server, 80)) {
      Serial.println("conectando...");
      client.println("GET /latest.txt HTTP/1.1");   //Envia requerimiento HHTTP GET
      client.println("Host: www.arduino.cc");
      client.println("User-Agent: arduino-ethernet");
      client.println("Connection: close");
      client.println();
      lastConnectionTime = millis();
   }else{
      Serial.println("conexion fallo.");
   }
}

Vea también


Referencias externas