Skip to content

Commit 01a0e7e

Browse files
author
Tom Igoe
committed
Added option for DNS in all HTTP client examples
1 parent 84403e2 commit 01a0e7e

File tree

4 files changed

+61
-44
lines changed

4 files changed

+61
-44
lines changed

WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Analog sensor attached to analog in 0
1616
* Wifi shield attached to pins 10, 11, 12, 13
1717
18-
created 9 March 2012
18+
created 13 March 2012
1919
by Tom Igoe
2020
2121
This code is in the public domain.
@@ -26,20 +26,23 @@
2626

2727
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
2828
#define FEEDID 00000 // replace your feed ID
29-
#define USERAGENT "My Project" // user agent is the project name
29+
#define USERAGENT "My Arduino Project" // user agent is the project name
3030

3131
char ssid[] = "yourNetwork"; // your network SSID (name)
3232
char pass[] = "secretPassword"; // your network password
33+
3334
int status = WL_IDLE_STATUS;
3435

3536
// initialize the library instance:
3637
WiFiClient client;
37-
IPAddress server(216,52,233,122);
38-
//char server[] = "api.pachube.com";
38+
// if you don't want to use DNS (and reduce your sketch size)
39+
// use the numeric IP instead of the name for the server:
40+
IPAddress server(216,52,233,122); // numeric IP for api.pachube.com
41+
//char server[] = "api.pachube.com"; // name address for pachube API
3942

4043
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
4144
boolean lastConnected = false; // state of the connection last time through the main loop
42-
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
45+
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
4346

4447
void setup() {
4548
Serial.begin(9600);
@@ -48,9 +51,9 @@ void setup() {
4851
Serial.println(ssid);
4952

5053
status = WiFi.begin(ssid, pass);
51-
if ( status != WL_CONNECTED) {
54+
if ( status != WL_CONNECTED) {
5255
Serial.println("Couldn't get a wifi connection");
53-
// don't do anything else:
56+
// stop here and do nothing:
5457
while(true);
5558
}
5659
else {
@@ -99,7 +102,7 @@ void sendData(int thisData) {
99102
client.print("PUT /v2/feeds/");
100103
client.print(FEEDID);
101104
client.println(".csv HTTP/1.1");
102-
client.print("Host: api.pachube.com\n");
105+
client.println("Host: api.pachube.com");
103106
client.print("X-PachubeApiKey: ");
104107
client.println(APIKEY);
105108
client.print("User-Agent: ");
@@ -112,13 +115,14 @@ void sendData(int thisData) {
112115
client.println(thisLength);
113116

114117
// last pieces of the HTTP PUT request:
115-
client.print("Content-Type: text/csv\n");
116-
client.println("Connection: close\n");
118+
client.println("Content-Type: text/csv");
119+
client.println("Connection: close");
120+
client.println();
117121

118122
// here's the actual content of the PUT request:
119123
client.print("sensor1,");
120124
client.println(thisData);
121-
125+
122126
}
123127
else {
124128
// if you couldn't make a connection:
@@ -127,9 +131,8 @@ void sendData(int thisData) {
127131
Serial.println("disconnecting.");
128132
client.stop();
129133
}
130-
// note the time that the connection was made:
134+
// note the time that the connection was made or attempted:
131135
lastConnectionTime = millis();
132-
lastConnected = client.connected();
133136
}
134137

135138

@@ -169,3 +172,6 @@ void printWifiStatus() {
169172
Serial.print(rssi);
170173
Serial.println(" dBm");
171174
}
175+
176+
177+

WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Analog sensor attached to analog in 0
1919
* Wifi shield attached to pins 10, 11, 12, 13
2020
21-
created 9 March 2012
21+
created 13 March 2012
2222
by Tom Igoe
2323
2424
This code is in the public domain.
@@ -30,27 +30,35 @@
3030

3131
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
3232
#define FEEDID 00000 // replace your feed ID
33-
#define USERAGENT "My Project" // user agent is the project name
33+
#define USERAGENT "My Arduino Project" // user agent is the project name
3434

3535
char ssid[] = "yourNetwork"; // your network SSID (name)
3636
char pass[] = "secretPassword"; // your network password
37-
int status = WL_IDLE_STATUS
37+
38+
int status = WL_IDLE_STATUS;
3839

3940
// initialize the library instance:
4041
WiFiClient client;
41-
IPAddress server(216,52,233,122);
42-
//char server[] = "api.pachube.com";
42+
43+
// if you don't want to use DNS (and reduce your sketch size)
44+
// use the numeric IP instead of the name for the server:
45+
IPAddress server(216,52,233,122); // numeric IP for api.pachube.com
46+
//char server[] = "api.pachube.com"; // name address for pachube API
4347

4448
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
4549
boolean lastConnected = false; // state of the connection last time through the main loop
4650
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
4751

4852
void setup() {
49-
// start serial port:
5053
Serial.begin(9600);
54+
Serial.println("Attempting to connect to Wifi network...");
55+
Serial.print("SSID: ");
56+
Serial.println(ssid);
57+
5158
status = WiFi.begin(ssid, pass);
5259
if ( status != WL_CONNECTED) {
5360
Serial.println("Couldn't get a wifi connection");
61+
// stop here and do nothing:
5462
while(true);
5563
}
5664
else {
@@ -107,17 +115,18 @@ void sendData(String thisData) {
107115
client.print("PUT /v2/feeds/");
108116
client.print(FEEDID);
109117
client.println(".csv HTTP/1.1");
110-
client.print("Host: api.pachube.com\n");
118+
client.println("Host: api.pachube.com");
111119
client.print("X-PachubeApiKey: ");
112120
client.println(APIKEY);
113121
client.print("User-Agent: ");
114122
client.println(USERAGENT);
115123
client.print("Content-Length: ");
116-
client.println(thisData.length(), DEC);
124+
client.println(thisData.length());
117125

118126
// last pieces of the HTTP PUT request:
119-
client.print("Content-Type: text/csv\n");
120-
client.println("Connection: close\n");
127+
client.println("Content-Type: text/csv");
128+
client.println("Connection: close");
129+
client.println();
121130

122131
// here's the actual content of the PUT request:
123132
client.println(thisData);
@@ -129,7 +138,7 @@ void sendData(String thisData) {
129138
Serial.println("disconnecting.");
130139
client.stop();
131140
}
132-
// note the time that the connection was made:
141+
// note the time that the connection was made or attempted:
133142
lastConnectionTime = millis();
134-
lastConnected = client.connected();
135143
}
144+

WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Circuit:
1414
* WiFi shield attached to pins 10, 11, 12, 13
1515
16-
created 9 Mar 2012
16+
created 13 Mar 2012
1717
by Tom Igoe
1818
1919
This code is in the public domain.
@@ -22,8 +22,9 @@
2222
#include <SPI.h>
2323
#include <WiFi.h>
2424

25-
char ssid[] = "YourNetwork"; // your network SSID (name)
26-
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
25+
//char ssid[] = "YourNetwork"; // your network SSID (name)
26+
//char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
27+
2728
int keyIndex = 0; // your network key Index number (needed only for WEP)
2829
int status = WL_IDLE_STATUS; // status of the wifi connection
2930

@@ -32,10 +33,13 @@ WiFiClient client;
3233

3334
const unsigned long requestInterval = 30*1000; // delay between requests; 30 seconds
3435

35-
IPAddress server(199,59,149,200); // api.twitter.com
36+
// if you don't want to use DNS (and reduce your sketch size)
37+
// use the numeric IP instead of the name for the server:
38+
IPAddress server(199,59,149,200); // numeric IP for api.twitter.com
39+
//char server[] = "api.twitter.com"; // name address for twitter API
3640

3741
boolean requested; // whether you've made a request since connecting
38-
unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
42+
unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
3943

4044
String currentLine = ""; // string to hold the text from server
4145
String tweet = ""; // string to hold the tweet
@@ -54,25 +58,21 @@ void setup() {
5458
status = WiFi.begin(ssid, pass);
5559
if ( status != WL_CONNECTED) {
5660
Serial.println("Couldn't get a wifi connection");
61+
// stop here and do nothing:
5762
while(true);
5863
}
59-
else {
64+
else {
6065
Serial.println("Connected to wifi");
6166
printWifiStatus();
6267
connectToServer();
6368
}
6469
}
65-
66-
67-
6870
void loop()
6971
{
7072
if (client.connected()) {
7173
if (client.available()) {
7274
// read incoming bytes:
7375
char inChar = client.read();
74-
// print the incoming byte (for debugging):
75-
Serial.write(inChar);
7676

7777
// add incoming byte to end of line:
7878
currentLine += inChar;
@@ -118,7 +118,7 @@ void connectToServer() {
118118
if (client.connect(server, 80)) {
119119
Serial.println("making HTTP request...");
120120
// make HTTP GET request to twitter:
121-
client.println("GET /1/statuses/user_timeline.xml?screen_name=arduinoteam HTTP/1.1");
121+
client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino HTTP/1.1");
122122
client.println("Host:api.twitter.com");
123123
client.println("Connection:close");
124124
client.println();
@@ -146,3 +146,4 @@ void printWifiStatus() {
146146
}
147147

148148

149+

WiFi/examples/WifiWebClient/WifiWebClient.ino

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
created 13 July 2010
1818
by dlf (Metodo2 srl)
19-
modified 9 Mar 2012
19+
modified 13 Mar 2012
2020
by Tom Igoe
2121
*/
2222

@@ -26,12 +26,13 @@
2626

2727
char ssid[] = "YourNetwork"; // your network SSID (name)
2828
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
29-
3029
int keyIndex = 0; // your network key Index number (needed only for WEP)
3130

3231
int status = WL_IDLE_STATUS;
33-
IPAddress server(173,194,73,105); // Google
34-
//char server[] = "www.google.com";
32+
// if you don't want to use DNS (and reduce your sketch size)
33+
// use the numeric IP instead of the name for the server:
34+
IPAddress server(173,194,73,105); // numeric IP for Google (no DNS)
35+
//char server[] = "www.google.com"; // name address for Google (using DNS)
3536

3637
// Initialize the Ethernet client library
3738
// with the IP address and port of the server
@@ -47,10 +48,10 @@ void setup() {
4748
status = WiFi.begin(ssid, pass);
4849
if ( status != WL_CONNECTED) {
4950
Serial.println("Couldn't get a wifi connection");
50-
// don't do anything else:
51+
// stop here and do nothing:
5152
while(true);
5253
}
53-
else {
54+
else {
5455
Serial.println("Connected to wifi");
5556
printWifiStatus();
5657
Serial.println("\nStarting connection to server...");
@@ -60,7 +61,7 @@ void setup() {
6061
// Make a HTTP request:
6162
client.println("GET /search?q=arduino HTTP/1.1");
6263
client.println("Host:www.google.com");
63-
client.println("Connection:close");
64+
client.println("Connection: close");
6465
client.println();
6566
}
6667
}

0 commit comments

Comments
 (0)