1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

better Client.stop() handling in WebClientRepeating and WifiWebClientRepeating examples

This commit is contained in:
Fede85 2014-01-21 20:38:52 +01:00
parent c2e9860cff
commit 90fb863c01
2 changed files with 35 additions and 50 deletions

View File

@ -14,6 +14,8 @@
created 19 Apr 2012 created 19 Apr 2012
by Tom Igoe by Tom Igoe
modified 21 Jan 2014
by Federico Vanzati
http://arduino.cc/en/Tutorial/WebClientRepeating http://arduino.cc/en/Tutorial/WebClientRepeating
This code is in the public domain. This code is in the public domain.
@ -30,7 +32,7 @@ byte mac[] = {
}; };
// fill in an available IP address on your network here, // fill in an available IP address on your network here,
// for manual configuration: // for manual configuration:
IPAddress ip(10, 0, 0, 20); IPAddress ip(192, 168, 1, 177);
// fill in your Domain Name Server address here: // fill in your Domain Name Server address here:
IPAddress myDns(1, 1, 1, 1); IPAddress myDns(1, 1, 1, 1);
@ -39,15 +41,19 @@ IPAddress myDns(1, 1, 1, 1);
EthernetClient client; EthernetClient client;
char server[] = "www.arduino.cc"; char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241);
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
const unsigned long postingInterval = 60L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers // the "L" is needed to use long type numbers
void setup() { void setup() {
// start serial port: // start serial port:
Serial.begin(9600); Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// give the ethernet module time to boot up: // give the ethernet module time to boot up:
delay(1000); delay(1000);
// start the Ethernet connection using a fixed IP address and DNS server: // start the Ethernet connection using a fixed IP address and DNS server:
@ -63,29 +69,23 @@ void loop() {
// purposes only: // purposes only:
if (client.available()) { if (client.available()) {
char c = client.read(); char c = client.read();
Serial.print(c); Serial.write(c);
} }
// if there's no net connection, but there was one last time // if ten seconds have passed since your last connection,
// through the loop, then stop the client: // then connect again and send data:
if (!client.connected() && lastConnected) { if (millis() - lastConnectionTime > postingInterval) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
httpRequest(); httpRequest();
} }
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
} }
// this method makes a HTTP connection to the server: // this method makes a HTTP connection to the server:
void httpRequest() { void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection: // if there's a successful connection:
if (client.connect(server, 80)) { if (client.connect(server, 80)) {
Serial.println("connecting..."); Serial.println("connecting...");
@ -102,11 +102,7 @@ void httpRequest() {
else { else {
// if you couldn't make a connection: // if you couldn't make a connection:
Serial.println("connection failed"); Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
} }
} }

View File

@ -1,15 +1,17 @@
/* /*
Repeating Wifi Web client Repeating Wifi Web Client
This sketch connects to a a web server and makes a request This sketch connects to a a web server and makes a request
using an Arduino Wifi shield. using an Arduino Wifi shield.
Circuit: Circuit:
* Wifi shield attached to pins 10, 11, 12, 13 * WiFi shield attached to pins SPI pins and pin 7
created 23 April 2012 created 23 April 2012
modifide 31 May 2012 modified 31 May 2012
by Tom Igoe by Tom Igoe
modified 13 Jan 2014
by Federico Vanzati
http://arduino.cc/en/Tutorial/WifiWebClientRepeating http://arduino.cc/en/Tutorial/WifiWebClientRepeating
This code is in the public domain. This code is in the public domain.
@ -32,8 +34,7 @@ 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; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
const unsigned long postingInterval = 10 * 1000; // delay between updates, in milliseconds
void setup() { void setup() {
//Initialize serial and wait for port to open: //Initialize serial and wait for port to open:
@ -76,33 +77,27 @@ void loop() {
Serial.write(c); Serial.write(c);
} }
// if there's no net connection, but there was one last time // if ten seconds have passed since your last connection,
// through the loop, then stop the client: // then connect again and send data:
if (!client.connected() && lastConnected) { if (millis() - lastConnectionTime > postingInterval) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
httpRequest(); httpRequest();
} }
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
} }
// this method makes a HTTP connection to the server: // this method makes a HTTP connection to the server:
void httpRequest() { void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection: // if there's a successful connection:
if (client.connect(server, 80)) { if (client.connect(server, 80)) {
Serial.println("connecting..."); Serial.println("connecting...");
// send the HTTP PUT request: // send the HTTP PUT request:
client.println("GET /latest.txt HTTP/1.1"); client.println("GET /latest.txt HTTP/1.1");
client.println("Host: www.arduino.cc"); client.println("Host: www.arduino.cc");
client.println("User-Agent: arduino-ethernet"); client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close"); client.println("Connection: close");
client.println(); client.println();
@ -112,8 +107,6 @@ void httpRequest() {
else { else {
// if you couldn't make a connection: // if you couldn't make a connection:
Serial.println("connection failed"); Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
} }
} }
@ -136,7 +129,3 @@ void printWifiStatus() {
} }