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:
parent
c2e9860cff
commit
90fb863c01
@ -14,6 +14,8 @@
|
||||
|
||||
created 19 Apr 2012
|
||||
by Tom Igoe
|
||||
modified 21 Jan 2014
|
||||
by Federico Vanzati
|
||||
|
||||
http://arduino.cc/en/Tutorial/WebClientRepeating
|
||||
This code is in the public domain.
|
||||
@ -30,7 +32,7 @@ byte mac[] = {
|
||||
};
|
||||
// fill in an available IP address on your network here,
|
||||
// for manual configuration:
|
||||
IPAddress ip(10, 0, 0, 20);
|
||||
IPAddress ip(192, 168, 1, 177);
|
||||
|
||||
// fill in your Domain Name Server address here:
|
||||
IPAddress myDns(1, 1, 1, 1);
|
||||
@ -39,15 +41,19 @@ IPAddress myDns(1, 1, 1, 1);
|
||||
EthernetClient client;
|
||||
|
||||
char server[] = "www.arduino.cc";
|
||||
//IPAddress server(64,131,82,241);
|
||||
|
||||
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 = 60L * 1000L; // delay between updates, in milliseconds
|
||||
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
|
||||
// the "L" is needed to use long type numbers
|
||||
|
||||
void setup() {
|
||||
// start serial port:
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for Leonardo only
|
||||
}
|
||||
|
||||
// give the ethernet module time to boot up:
|
||||
delay(1000);
|
||||
// start the Ethernet connection using a fixed IP address and DNS server:
|
||||
@ -63,29 +69,23 @@ void loop() {
|
||||
// purposes only:
|
||||
if (client.available()) {
|
||||
char c = client.read();
|
||||
Serial.print(c);
|
||||
Serial.write(c);
|
||||
}
|
||||
|
||||
// if there's no net connection, but there was one last time
|
||||
// through the loop, then stop the client:
|
||||
if (!client.connected() && lastConnected) {
|
||||
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)) {
|
||||
// if ten seconds have passed since your last connection,
|
||||
// then connect again and send data:
|
||||
if (millis() - lastConnectionTime > postingInterval) {
|
||||
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:
|
||||
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 (client.connect(server, 80)) {
|
||||
Serial.println("connecting...");
|
||||
@ -102,11 +102,7 @@ void httpRequest() {
|
||||
else {
|
||||
// if you couldn't make a connection:
|
||||
Serial.println("connection failed");
|
||||
Serial.println("disconnecting.");
|
||||
client.stop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
/*
|
||||
Repeating Wifi Web client
|
||||
Repeating Wifi Web Client
|
||||
|
||||
This sketch connects to a a web server and makes a request
|
||||
using an Arduino Wifi shield.
|
||||
|
||||
Circuit:
|
||||
* Wifi shield attached to pins 10, 11, 12, 13
|
||||
* WiFi shield attached to pins SPI pins and pin 7
|
||||
|
||||
created 23 April 2012
|
||||
modifide 31 May 2012
|
||||
modified 31 May 2012
|
||||
by Tom Igoe
|
||||
modified 13 Jan 2014
|
||||
by Federico Vanzati
|
||||
|
||||
http://arduino.cc/en/Tutorial/WifiWebClientRepeating
|
||||
This code is in the public domain.
|
||||
@ -17,7 +19,7 @@
|
||||
|
||||
#include <SPI.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
|
||||
char ssid[] = "yourNetwork"; // your network SSID (name)
|
||||
char pass[] = "secretPassword"; // your network password
|
||||
int keyIndex = 0; // your network key Index number (needed only for WEP)
|
||||
@ -31,9 +33,8 @@ WiFiClient client;
|
||||
char server[] = "www.arduino.cc";
|
||||
//IPAddress server(64,131,82,241);
|
||||
|
||||
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 = 10 * 1000; // delay between updates, in milliseconds
|
||||
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
|
||||
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
|
||||
|
||||
void setup() {
|
||||
//Initialize serial and wait for port to open:
|
||||
@ -76,33 +77,27 @@ void loop() {
|
||||
Serial.write(c);
|
||||
}
|
||||
|
||||
// if there's no net connection, but there was one last time
|
||||
// through the loop, then stop the client:
|
||||
if (!client.connected() && lastConnected) {
|
||||
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)) {
|
||||
// if ten seconds have passed since your last connection,
|
||||
// then connect again and send data:
|
||||
if (millis() - lastConnectionTime > postingInterval) {
|
||||
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:
|
||||
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 (client.connect(server, 80)) {
|
||||
Serial.println("connecting...");
|
||||
// send the HTTP PUT request:
|
||||
client.println("GET /latest.txt HTTP/1.1");
|
||||
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();
|
||||
|
||||
@ -112,8 +107,6 @@ void httpRequest() {
|
||||
else {
|
||||
// if you couldn't make a connection:
|
||||
Serial.println("connection failed");
|
||||
Serial.println("disconnecting.");
|
||||
client.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@ -136,7 +129,3 @@ void printWifiStatus() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user