mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-02 13:24:12 +01:00
Added option for DNS in all HTTP client examples
This commit is contained in:
parent
84403e2512
commit
01a0e7e3de
@ -15,7 +15,7 @@
|
||||
* Analog sensor attached to analog in 0
|
||||
* Wifi shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 9 March 2012
|
||||
created 13 March 2012
|
||||
by Tom Igoe
|
||||
|
||||
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 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 pass[] = "secretPassword"; // your network password
|
||||
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
// initialize the library instance:
|
||||
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
|
||||
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() {
|
||||
Serial.begin(9600);
|
||||
@ -48,9 +51,9 @@ void setup() {
|
||||
Serial.println(ssid);
|
||||
|
||||
status = WiFi.begin(ssid, pass);
|
||||
if ( status != WL_CONNECTED) {
|
||||
if ( status != WL_CONNECTED) {
|
||||
Serial.println("Couldn't get a wifi connection");
|
||||
// don't do anything else:
|
||||
// stop here and do nothing:
|
||||
while(true);
|
||||
}
|
||||
else {
|
||||
@ -99,7 +102,7 @@ void sendData(int thisData) {
|
||||
client.print("PUT /v2/feeds/");
|
||||
client.print(FEEDID);
|
||||
client.println(".csv HTTP/1.1");
|
||||
client.print("Host: api.pachube.com\n");
|
||||
client.println("Host: api.pachube.com");
|
||||
client.print("X-PachubeApiKey: ");
|
||||
client.println(APIKEY);
|
||||
client.print("User-Agent: ");
|
||||
@ -112,13 +115,14 @@ void sendData(int thisData) {
|
||||
client.println(thisLength);
|
||||
|
||||
// last pieces of the HTTP PUT request:
|
||||
client.print("Content-Type: text/csv\n");
|
||||
client.println("Connection: close\n");
|
||||
client.println("Content-Type: text/csv");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
|
||||
// here's the actual content of the PUT request:
|
||||
client.print("sensor1,");
|
||||
client.println(thisData);
|
||||
|
||||
|
||||
}
|
||||
else {
|
||||
// if you couldn't make a connection:
|
||||
@ -127,9 +131,8 @@ void sendData(int thisData) {
|
||||
Serial.println("disconnecting.");
|
||||
client.stop();
|
||||
}
|
||||
// note the time that the connection was made:
|
||||
// note the time that the connection was made or attempted:
|
||||
lastConnectionTime = millis();
|
||||
lastConnected = client.connected();
|
||||
}
|
||||
|
||||
|
||||
@ -169,3 +172,6 @@ void printWifiStatus() {
|
||||
Serial.print(rssi);
|
||||
Serial.println(" dBm");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
||||
* Analog sensor attached to analog in 0
|
||||
* Wifi shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 9 March 2012
|
||||
created 13 March 2012
|
||||
by Tom Igoe
|
||||
|
||||
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 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 pass[] = "secretPassword"; // your network password
|
||||
int status = WL_IDLE_STATUS
|
||||
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
// initialize the library instance:
|
||||
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
|
||||
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
|
||||
|
||||
void setup() {
|
||||
// start serial port:
|
||||
Serial.begin(9600);
|
||||
Serial.println("Attempting to connect to Wifi network...");
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(ssid);
|
||||
|
||||
status = WiFi.begin(ssid, pass);
|
||||
if ( status != WL_CONNECTED) {
|
||||
Serial.println("Couldn't get a wifi connection");
|
||||
// stop here and do nothing:
|
||||
while(true);
|
||||
}
|
||||
else {
|
||||
@ -107,17 +115,18 @@ void sendData(String thisData) {
|
||||
client.print("PUT /v2/feeds/");
|
||||
client.print(FEEDID);
|
||||
client.println(".csv HTTP/1.1");
|
||||
client.print("Host: api.pachube.com\n");
|
||||
client.println("Host: api.pachube.com");
|
||||
client.print("X-PachubeApiKey: ");
|
||||
client.println(APIKEY);
|
||||
client.print("User-Agent: ");
|
||||
client.println(USERAGENT);
|
||||
client.print("Content-Length: ");
|
||||
client.println(thisData.length(), DEC);
|
||||
client.println(thisData.length());
|
||||
|
||||
// last pieces of the HTTP PUT request:
|
||||
client.print("Content-Type: text/csv\n");
|
||||
client.println("Connection: close\n");
|
||||
client.println("Content-Type: text/csv");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
|
||||
// here's the actual content of the PUT request:
|
||||
client.println(thisData);
|
||||
@ -129,7 +138,7 @@ void sendData(String thisData) {
|
||||
Serial.println("disconnecting.");
|
||||
client.stop();
|
||||
}
|
||||
// note the time that the connection was made:
|
||||
// note the time that the connection was made or attempted:
|
||||
lastConnectionTime = millis();
|
||||
lastConnected = client.connected();
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@
|
||||
Circuit:
|
||||
* WiFi shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 9 Mar 2012
|
||||
created 13 Mar 2012
|
||||
by Tom Igoe
|
||||
|
||||
This code is in the public domain.
|
||||
@ -22,8 +22,9 @@
|
||||
#include <SPI.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
char ssid[] = "YourNetwork"; // your network SSID (name)
|
||||
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
|
||||
//char ssid[] = "YourNetwork"; // your network SSID (name)
|
||||
//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 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
|
||||
|
||||
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
|
||||
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 tweet = ""; // string to hold the tweet
|
||||
@ -54,25 +58,21 @@ void setup() {
|
||||
status = WiFi.begin(ssid, pass);
|
||||
if ( status != WL_CONNECTED) {
|
||||
Serial.println("Couldn't get a wifi connection");
|
||||
// stop here and do nothing:
|
||||
while(true);
|
||||
}
|
||||
else {
|
||||
else {
|
||||
Serial.println("Connected to wifi");
|
||||
printWifiStatus();
|
||||
connectToServer();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (client.connected()) {
|
||||
if (client.available()) {
|
||||
// read incoming bytes:
|
||||
char inChar = client.read();
|
||||
// print the incoming byte (for debugging):
|
||||
Serial.write(inChar);
|
||||
|
||||
// add incoming byte to end of line:
|
||||
currentLine += inChar;
|
||||
@ -118,7 +118,7 @@ void connectToServer() {
|
||||
if (client.connect(server, 80)) {
|
||||
Serial.println("making HTTP request...");
|
||||
// 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("Connection:close");
|
||||
client.println();
|
||||
@ -146,3 +146,4 @@ void printWifiStatus() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
created 13 July 2010
|
||||
by dlf (Metodo2 srl)
|
||||
modified 9 Mar 2012
|
||||
modified 13 Mar 2012
|
||||
by Tom Igoe
|
||||
*/
|
||||
|
||||
@ -26,12 +26,13 @@
|
||||
|
||||
char ssid[] = "YourNetwork"; // your network SSID (name)
|
||||
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 status = WL_IDLE_STATUS;
|
||||
IPAddress server(173,194,73,105); // Google
|
||||
//char server[] = "www.google.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(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
|
||||
// with the IP address and port of the server
|
||||
@ -47,10 +48,10 @@ void setup() {
|
||||
status = WiFi.begin(ssid, pass);
|
||||
if ( status != WL_CONNECTED) {
|
||||
Serial.println("Couldn't get a wifi connection");
|
||||
// don't do anything else:
|
||||
// stop here and do nothing:
|
||||
while(true);
|
||||
}
|
||||
else {
|
||||
else {
|
||||
Serial.println("Connected to wifi");
|
||||
printWifiStatus();
|
||||
Serial.println("\nStarting connection to server...");
|
||||
@ -60,7 +61,7 @@ void setup() {
|
||||
// Make a HTTP request:
|
||||
client.println("GET /search?q=arduino HTTP/1.1");
|
||||
client.println("Host:www.google.com");
|
||||
client.println("Connection:close");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user