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

Updated the PachubeClient and PachubeClientString sketches to make HTTP calling consistent

This commit is contained in:
Tom Igoe 2012-03-09 21:43:49 -05:00
parent 44fb113fd3
commit fe4b5703f7
2 changed files with 28 additions and 34 deletions

View File

@ -15,17 +15,15 @@
* 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 4 March 2012 created 9 March 2012
by Tom Igoe by Tom Igoe
This code is in the public domain. This code is in the public domain.
*/ */
#include <SPI.h> #include <SPI.h>
#include <WiFi.h> #include <WiFi.h>
#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 Project" // user agent is the project name
@ -36,10 +34,12 @@ 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";
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 int postingInterval = 10000; //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);
@ -93,7 +93,7 @@ void loop() {
// this method makes a HTTP connection to the server: // this method makes a HTTP connection to the server:
void sendData(int thisData) { void sendData(int thisData) {
// if there's a successful connection: // if there's a successful connection:
if (client.connect("www.pachube.com", 80)) { if (client.connect(server, 80)) {
Serial.println("connecting..."); Serial.println("connecting...");
// send the HTTP PUT request: // send the HTTP PUT request:
client.print("PUT /v2/feeds/"); client.print("PUT /v2/feeds/");
@ -119,8 +119,6 @@ void sendData(int thisData) {
client.print("sensor1,"); client.print("sensor1,");
client.println(thisData); client.println(thisData);
// note the time that the connection was made:
lastConnectionTime = millis();
} }
else { else {
// if you couldn't make a connection: // if you couldn't make a connection:
@ -129,6 +127,8 @@ void sendData(int thisData) {
Serial.println("disconnecting."); Serial.println("disconnecting.");
client.stop(); client.stop();
} }
// note the time that the connection was made:
lastConnectionTime = millis();
lastConnected = client.connected(); lastConnected = client.connected();
} }
@ -169,8 +169,3 @@ 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 4 March 2012 created 9 March 2012
by Tom Igoe by Tom Igoe
This code is in the public domain. This code is in the public domain.
@ -34,14 +34,16 @@
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";
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 int postingInterval = 10000; //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: // start serial port:
@ -99,7 +101,7 @@ void loop() {
// this method makes a HTTP connection to the server: // this method makes a HTTP connection to the server:
void sendData(String thisData) { void sendData(String thisData) {
// if there's a successful connection: // if there's a successful connection:
if (client.connect("api.pachube.com", 80)) { if (client.connect(server, 80)) {
Serial.println("connecting..."); Serial.println("connecting...");
// send the HTTP PUT request: // send the HTTP PUT request:
client.print("PUT /v2/feeds/"); client.print("PUT /v2/feeds/");
@ -119,9 +121,6 @@ void sendData(String thisData) {
// here's the actual content of the PUT request: // here's the actual content of the PUT request:
client.println(thisData); client.println(thisData);
// note the time that the connection was made:
lastConnectionTime = millis();
} }
else { else {
// if you couldn't make a connection: // if you couldn't make a connection:
@ -129,8 +128,8 @@ void sendData(String thisData) {
Serial.println(); Serial.println();
Serial.println("disconnecting."); Serial.println("disconnecting.");
client.stop(); client.stop();
}
// note the time that the connection was made:
lastConnectionTime = millis();
lastConnected = client.connected(); lastConnected = client.connected();
} }
}