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,31 +15,31 @@
* 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
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() {
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();
} }
@ -160,7 +160,7 @@ void printWifiStatus() {
// print your WiFi shield's IP address: // print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP(); IPAddress ip = WiFi.localIP();
Serial.print("IP Address: "); Serial.print("IP Address: ");
Serial.println(ip); Serial.println(ip);
// print the received signal strength: // print the received signal strength:
@ -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.
@ -29,19 +29,21 @@
#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
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:
@ -87,7 +89,7 @@ void loop() {
} }
// if you're not connected, and ten seconds have passed since // if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data: // your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(dataString); sendData(dataString);
} }
@ -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();
lastConnected = client.connected();
} }
// note the time that the connection was made:
lastConnectionTime = millis();
lastConnected = client.connected();
} }