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

Added option for DNS in all HTTP client examples

This commit is contained in:
Tom Igoe 2012-03-13 13:00:14 -04:00
parent 84403e2512
commit 01a0e7e3de
4 changed files with 61 additions and 44 deletions

View File

@ -15,7 +15,7 @@
* Analog sensor attached to analog in 0 * Analog sensor attached to analog in 0
* Wifi shield attached to pins 10, 11, 12, 13 * Wifi shield attached to pins 10, 11, 12, 13
created 9 March 2012 created 13 March 2012
by Tom Igoe by Tom Igoe
This code is in the public domain. This code is in the public domain.
@ -26,20 +26,23 @@
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here #define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
#define FEEDID 00000 // replace your feed ID #define FEEDID 00000 // replace your feed ID
#define USERAGENT "My Project" // user agent is the project name #define USERAGENT "My Arduino Project" // user agent is the project name
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password char pass[] = "secretPassword"; // your network password
int status = WL_IDLE_STATUS; int status = WL_IDLE_STATUS;
// initialize the library instance: // initialize the library instance:
WiFiClient client; WiFiClient client;
IPAddress server(216,52,233,122); // if you don't want to use DNS (and reduce your sketch size)
//char server[] = "api.pachube.com"; // use the numeric IP instead of the name for the server:
IPAddress server(216,52,233,122); // numeric IP for api.pachube.com
//char server[] = "api.pachube.com"; // name address for pachube API
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 boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
@ -50,7 +53,7 @@ void setup() {
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) { if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection"); Serial.println("Couldn't get a wifi connection");
// don't do anything else: // stop here and do nothing:
while(true); while(true);
} }
else { else {
@ -99,7 +102,7 @@ void sendData(int thisData) {
client.print("PUT /v2/feeds/"); client.print("PUT /v2/feeds/");
client.print(FEEDID); client.print(FEEDID);
client.println(".csv HTTP/1.1"); client.println(".csv HTTP/1.1");
client.print("Host: api.pachube.com\n"); client.println("Host: api.pachube.com");
client.print("X-PachubeApiKey: "); client.print("X-PachubeApiKey: ");
client.println(APIKEY); client.println(APIKEY);
client.print("User-Agent: "); client.print("User-Agent: ");
@ -112,8 +115,9 @@ void sendData(int thisData) {
client.println(thisLength); client.println(thisLength);
// last pieces of the HTTP PUT request: // last pieces of the HTTP PUT request:
client.print("Content-Type: text/csv\n"); client.println("Content-Type: text/csv");
client.println("Connection: close\n"); client.println("Connection: close");
client.println();
// here's the actual content of the PUT request: // here's the actual content of the PUT request:
client.print("sensor1,"); client.print("sensor1,");
@ -127,9 +131,8 @@ void sendData(int thisData) {
Serial.println("disconnecting."); Serial.println("disconnecting.");
client.stop(); client.stop();
} }
// note the time that the connection was made: // note the time that the connection was made or attempted:
lastConnectionTime = millis(); lastConnectionTime = millis();
lastConnected = client.connected();
} }
@ -169,3 +172,6 @@ void printWifiStatus() {
Serial.print(rssi); Serial.print(rssi);
Serial.println(" dBm"); Serial.println(" dBm");
} }

View File

@ -18,7 +18,7 @@
* Analog sensor attached to analog in 0 * Analog sensor attached to analog in 0
* Wifi shield attached to pins 10, 11, 12, 13 * Wifi shield attached to pins 10, 11, 12, 13
created 9 March 2012 created 13 March 2012
by Tom Igoe by Tom Igoe
This code is in the public domain. This code is in the public domain.
@ -30,27 +30,35 @@
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here #define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
#define FEEDID 00000 // replace your feed ID #define FEEDID 00000 // replace your feed ID
#define USERAGENT "My Project" // user agent is the project name #define USERAGENT "My Arduino Project" // user agent is the project name
char ssid[] = "yourNetwork"; // your network SSID (name) char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password char pass[] = "secretPassword"; // your network password
int status = WL_IDLE_STATUS
int status = WL_IDLE_STATUS;
// initialize the library instance: // initialize the library instance:
WiFiClient client; WiFiClient client;
IPAddress server(216,52,233,122);
//char server[] = "api.pachube.com"; // if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(216,52,233,122); // numeric IP for api.pachube.com
//char server[] = "api.pachube.com"; // name address for pachube API
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 boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
void setup() { void setup() {
// start serial port:
Serial.begin(9600); Serial.begin(9600);
Serial.println("Attempting to connect to Wifi network...");
Serial.print("SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) { if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection"); Serial.println("Couldn't get a wifi connection");
// stop here and do nothing:
while(true); while(true);
} }
else { else {
@ -107,17 +115,18 @@ void sendData(String thisData) {
client.print("PUT /v2/feeds/"); client.print("PUT /v2/feeds/");
client.print(FEEDID); client.print(FEEDID);
client.println(".csv HTTP/1.1"); client.println(".csv HTTP/1.1");
client.print("Host: api.pachube.com\n"); client.println("Host: api.pachube.com");
client.print("X-PachubeApiKey: "); client.print("X-PachubeApiKey: ");
client.println(APIKEY); client.println(APIKEY);
client.print("User-Agent: "); client.print("User-Agent: ");
client.println(USERAGENT); client.println(USERAGENT);
client.print("Content-Length: "); client.print("Content-Length: ");
client.println(thisData.length(), DEC); client.println(thisData.length());
// last pieces of the HTTP PUT request: // last pieces of the HTTP PUT request:
client.print("Content-Type: text/csv\n"); client.println("Content-Type: text/csv");
client.println("Connection: close\n"); client.println("Connection: close");
client.println();
// here's the actual content of the PUT request: // here's the actual content of the PUT request:
client.println(thisData); client.println(thisData);
@ -129,7 +138,7 @@ void sendData(String thisData) {
Serial.println("disconnecting."); Serial.println("disconnecting.");
client.stop(); client.stop();
} }
// note the time that the connection was made: // note the time that the connection was made or attempted:
lastConnectionTime = millis(); lastConnectionTime = millis();
lastConnected = client.connected();
} }

View File

@ -13,7 +13,7 @@
Circuit: Circuit:
* WiFi shield attached to pins 10, 11, 12, 13 * WiFi shield attached to pins 10, 11, 12, 13
created 9 Mar 2012 created 13 Mar 2012
by Tom Igoe by Tom Igoe
This code is in the public domain. This code is in the public domain.
@ -22,8 +22,9 @@
#include <SPI.h> #include <SPI.h>
#include <WiFi.h> #include <WiFi.h>
char ssid[] = "YourNetwork"; // your network SSID (name) //char ssid[] = "YourNetwork"; // your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) //char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS; // status of the wifi connection int status = WL_IDLE_STATUS; // status of the wifi connection
@ -32,10 +33,13 @@ WiFiClient client;
const unsigned long requestInterval = 30*1000; // delay between requests; 30 seconds const unsigned long requestInterval = 30*1000; // delay between requests; 30 seconds
IPAddress server(199,59,149,200); // api.twitter.com // if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(199,59,149,200); // numeric IP for api.twitter.com
//char server[] = "api.twitter.com"; // name address for twitter API
boolean requested; // whether you've made a request since connecting boolean requested; // whether you've made a request since connecting
unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
String currentLine = ""; // string to hold the text from server String currentLine = ""; // string to hold the text from server
String tweet = ""; // string to hold the tweet String tweet = ""; // string to hold the tweet
@ -54,25 +58,21 @@ void setup() {
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) { if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection"); Serial.println("Couldn't get a wifi connection");
// stop here and do nothing:
while(true); while(true);
} }
else { else {
Serial.println("Connected to wifi"); Serial.println("Connected to wifi");
printWifiStatus(); printWifiStatus();
connectToServer(); connectToServer();
} }
} }
void loop() void loop()
{ {
if (client.connected()) { if (client.connected()) {
if (client.available()) { if (client.available()) {
// read incoming bytes: // read incoming bytes:
char inChar = client.read(); char inChar = client.read();
// print the incoming byte (for debugging):
Serial.write(inChar);
// add incoming byte to end of line: // add incoming byte to end of line:
currentLine += inChar; currentLine += inChar;
@ -118,7 +118,7 @@ void connectToServer() {
if (client.connect(server, 80)) { if (client.connect(server, 80)) {
Serial.println("making HTTP request..."); Serial.println("making HTTP request...");
// make HTTP GET request to twitter: // make HTTP GET request to twitter:
client.println("GET /1/statuses/user_timeline.xml?screen_name=arduinoteam HTTP/1.1"); client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino HTTP/1.1");
client.println("Host:api.twitter.com"); client.println("Host:api.twitter.com");
client.println("Connection:close"); client.println("Connection:close");
client.println(); client.println();
@ -146,3 +146,4 @@ void printWifiStatus() {
} }

View File

@ -16,7 +16,7 @@
created 13 July 2010 created 13 July 2010
by dlf (Metodo2 srl) by dlf (Metodo2 srl)
modified 9 Mar 2012 modified 13 Mar 2012
by Tom Igoe by Tom Igoe
*/ */
@ -26,12 +26,13 @@
char ssid[] = "YourNetwork"; // your network SSID (name) char ssid[] = "YourNetwork"; // your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS; int status = WL_IDLE_STATUS;
IPAddress server(173,194,73,105); // Google // if you don't want to use DNS (and reduce your sketch size)
//char server[] = "www.google.com"; // use the numeric IP instead of the name for the server:
IPAddress server(173,194,73,105); // numeric IP for Google (no DNS)
//char server[] = "www.google.com"; // name address for Google (using DNS)
// Initialize the Ethernet client library // Initialize the Ethernet client library
// with the IP address and port of the server // with the IP address and port of the server
@ -47,10 +48,10 @@ void setup() {
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) { if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection"); Serial.println("Couldn't get a wifi connection");
// don't do anything else: // stop here and do nothing:
while(true); while(true);
} }
else { else {
Serial.println("Connected to wifi"); Serial.println("Connected to wifi");
printWifiStatus(); printWifiStatus();
Serial.println("\nStarting connection to server..."); Serial.println("\nStarting connection to server...");
@ -60,7 +61,7 @@ void setup() {
// Make a HTTP request: // Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1"); client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host:www.google.com"); client.println("Host:www.google.com");
client.println("Connection:close"); client.println("Connection: close");
client.println(); client.println();
} }
} }