Skip to content

Commit fe4b570

Browse files
committed
Updated the PachubeClient and PachubeClientString sketches to make HTTP calling consistent
1 parent 44fb113 commit fe4b570

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,31 @@
1515
* Analog sensor attached to analog in 0
1616
* Wifi shield attached to pins 10, 11, 12, 13
1717
18-
created 4 March 2012
18+
created 9 March 2012
1919
by Tom Igoe
2020
2121
This code is in the public domain.
2222
2323
*/
24-
2524
#include <SPI.h>
2625
#include <WiFi.h>
2726

28-
2927
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
30-
#define FEEDID 00000 // replace your feed ID
31-
#define USERAGENT "My Project" // user agent is the project name
28+
#define FEEDID 00000 // replace your feed ID
29+
#define USERAGENT "My Project" // user agent is the project name
3230

33-
char ssid[] = "yourNetwork"; // your network SSID (name)
31+
char ssid[] = "yourNetwork"; // your network SSID (name)
3432
char pass[] = "secretPassword"; // your network password
3533
int status = WL_IDLE_STATUS;
3634

3735
// initialize the library instance:
3836
WiFiClient client;
37+
IPAddress server(216,52,233,122);
38+
//char server[] = "api.pachube.com";
3939

40-
long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
41-
boolean lastConnected = false; // state of the connection last time through the main loop
42-
const int postingInterval = 10000; //delay between updates to Pachube.com
40+
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
41+
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
4343

4444
void setup() {
4545
Serial.begin(9600);
@@ -93,7 +93,7 @@ void loop() {
9393
// this method makes a HTTP connection to the server:
9494
void sendData(int thisData) {
9595
// if there's a successful connection:
96-
if (client.connect("www.pachube.com", 80)) {
96+
if (client.connect(server, 80)) {
9797
Serial.println("connecting...");
9898
// send the HTTP PUT request:
9999
client.print("PUT /v2/feeds/");
@@ -119,8 +119,6 @@ void sendData(int thisData) {
119119
client.print("sensor1,");
120120
client.println(thisData);
121121

122-
// note the time that the connection was made:
123-
lastConnectionTime = millis();
124122
}
125123
else {
126124
// if you couldn't make a connection:
@@ -129,6 +127,8 @@ void sendData(int thisData) {
129127
Serial.println("disconnecting.");
130128
client.stop();
131129
}
130+
// note the time that the connection was made:
131+
lastConnectionTime = millis();
132132
lastConnected = client.connected();
133133
}
134134

@@ -160,7 +160,7 @@ void printWifiStatus() {
160160

161161
// print your WiFi shield's IP address:
162162
IPAddress ip = WiFi.localIP();
163-
Serial.print("IP Address: ");
163+
Serial.print("IP Address: ");
164164
Serial.println(ip);
165165

166166
// print the received signal strength:
@@ -169,8 +169,3 @@ void printWifiStatus() {
169169
Serial.print(rssi);
170170
Serial.println(" dBm");
171171
}
172-
173-
174-
175-
176-

WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino

Lines changed: 15 additions & 16 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 4 March 2012
21+
created 9 March 2012
2222
by Tom Igoe
2323
2424
This code is in the public domain.
@@ -29,19 +29,21 @@
2929
#include <WiFi.h>
3030

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

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

3939
// initialize the library instance:
4040
WiFiClient client;
41+
IPAddress server(216,52,233,122);
42+
//char server[] = "api.pachube.com";
4143

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

4648
void setup() {
4749
// start serial port:
@@ -87,7 +89,7 @@ void loop() {
8789
}
8890

8991
// if you're not connected, and ten seconds have passed since
90-
// your last connection, then connect again and send data:
92+
// your last connection, then connect again and send data:
9193
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
9294
sendData(dataString);
9395
}
@@ -99,7 +101,7 @@ void loop() {
99101
// this method makes a HTTP connection to the server:
100102
void sendData(String thisData) {
101103
// if there's a successful connection:
102-
if (client.connect("api.pachube.com", 80)) {
104+
if (client.connect(server, 80)) {
103105
Serial.println("connecting...");
104106
// send the HTTP PUT request:
105107
client.print("PUT /v2/feeds/");
@@ -119,18 +121,15 @@ void sendData(String thisData) {
119121

120122
// here's the actual content of the PUT request:
121123
client.println(thisData);
122-
123-
// note the time that the connection was made:
124-
lastConnectionTime = millis();
125124
}
126125
else {
127126
// if you couldn't make a connection:
128127
Serial.println("connection failed");
129128
Serial.println();
130129
Serial.println("disconnecting.");
131130
client.stop();
132-
lastConnected = client.connected();
133131
}
132+
// note the time that the connection was made:
133+
lastConnectionTime = millis();
134+
lastConnected = client.connected();
134135
}
135-
136-

0 commit comments

Comments
 (0)