Skip to content

Commit c9ded42

Browse files
author
Tom Igoe
committed
Updated PachubeClientString and PachubeClient examples for Ethernet
1 parent 13e0b93 commit c9ded42

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

libraries/Ethernet/examples/PachubeClient/PachubeClient.ino

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Ethernet shield attached to pins 10, 11, 12, 13
1717
1818
created 15 March 2010
19-
updated 27 Feb 2012
19+
updated 16 Mar 2012
2020
by Tom Igoe with input from Usman Haque and Joe Saavedra
2121
2222
http://arduino.cc/en/Tutorial/PachubeClient
@@ -27,8 +27,6 @@ http://arduino.cc/en/Tutorial/PachubeClient
2727
#include <SPI.h>
2828
#include <Ethernet.h>
2929

30-
31-
3230
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
3331
#define FEEDID 00000 // replace your feed ID
3432
#define USERAGENT "My Project" // user agent is the project name
@@ -45,9 +43,14 @@ IPAddress ip(10,0,1,20);
4543
// initialize the library instance:
4644
EthernetClient client;
4745

48-
long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
49-
boolean lastConnected = false; // state of the connection last time through the main loop
50-
const int postingInterval = 10000; //delay between updates to Pachube.com
46+
// if you don't want to use DNS (and reduce your sketch size)
47+
// use the numeric IP instead of the name for the server:
48+
IPAddress server(216,52,233,122); // numeric IP for api.pachube.com
49+
//char server[] = "api.pachube.com"; // name address for pachube API
50+
51+
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
52+
boolean lastConnected = false; // state of the connection last time through the main loop
53+
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
5154

5255
void setup() {
5356
// start serial port:
@@ -93,13 +96,13 @@ void loop() {
9396
// this method makes a HTTP connection to the server:
9497
void sendData(int thisData) {
9598
// if there's a successful connection:
96-
if (client.connect("www.pachube.com", 80)) {
99+
if (client.connect(server, 80)) {
97100
Serial.println("connecting...");
98-
// send the HTTP PUT request:
101+
// send the HTTP PUT request:
99102
client.print("PUT /v2/feeds/");
100103
client.print(FEEDID);
101104
client.println(".csv HTTP/1.1");
102-
client.print("Host: api.pachube.com\n");
105+
client.println("Host: api.pachube.com");
103106
client.print("X-PachubeApiKey: ");
104107
client.println(APIKEY);
105108
client.print("User-Agent: ");
@@ -112,24 +115,24 @@ void sendData(int thisData) {
112115
client.println(thisLength);
113116

114117
// last pieces of the HTTP PUT request:
115-
client.print("Content-Type: text/csv\n");
116-
client.println("Connection: close\n");
118+
client.println("Content-Type: text/csv");
119+
client.println("Connection: close");
120+
client.println();
117121

118122
// here's the actual content of the PUT request:
119-
client.print("sensor1,");
123+
client.print("sensor1,");
120124
client.println(thisData);
121-
122-
// note the time that the connection was made:
123-
lastConnectionTime = millis();
125+
124126
}
125127
else {
126-
// if you couldn't make a connection:
128+
// if you couldn't make a connection:
127129
Serial.println("connection failed");
128130
Serial.println();
129131
Serial.println("disconnecting.");
130132
client.stop();
131-
lastConnected = client.connected();
132133
}
134+
// note the time that the connection was made or attempted:
135+
lastConnectionTime = millis();
133136
}
134137

135138

libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Ethernet shield attached to pins 10, 11, 12, 13
1919
2020
created 15 March 2010
21-
updated 27 Feb 2012
21+
updated 16 Mar 2012
2222
by Tom Igoe with input from Usman Haque and Joe Saavedra
2323
2424
http://arduino.cc/en/Tutorial/PachubeClientString
@@ -40,14 +40,19 @@
4040
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
4141
// fill in an available IP address on your network here,
4242
// for manual configuration:
43-
IPAddress ip(10,0,0,20);
43+
IPAddress ip(10,0,1,20);
4444

4545
// initialize the library instance:
4646
EthernetClient client;
4747

48-
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
49-
boolean lastConnected = false; // state of the connection last time through the main loop
50-
const unsigned long postingInterval = 10000; //delay between updates to Pachube.com
48+
// if you don't want to use DNS (and reduce your sketch size)
49+
// use the numeric IP instead of the name for the server:
50+
//IPAddress server(216,52,233,122); // numeric IP for api.pachube.com
51+
char server[] = "api.pachube.com"; // name address for pachube API
52+
53+
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
54+
boolean lastConnected = false; // state of the connection last time through the main loop
55+
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
5156

5257
void setup() {
5358
// start serial port:
@@ -66,9 +71,9 @@ void loop() {
6671
// read the analog sensor:
6772
int sensorReading = analogRead(A0);
6873
// convert the data to a String to send it:
69-
74+
7075
String dataString = "sensor1,";
71-
dataString += sensorReading;
76+
dataString += sensorReading;
7277

7378
// you can append multiple readings to this String if your
7479
// pachube feed is set up to handle multiple values:
@@ -93,7 +98,7 @@ void loop() {
9398
}
9499

95100
// if you're not connected, and ten seconds have passed since
96-
// your last connection, then connect again and send data:
101+
// your last connection, then connect again and send data:
97102
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
98103
sendData(dataString);
99104
}
@@ -105,38 +110,36 @@ void loop() {
105110
// this method makes a HTTP connection to the server:
106111
void sendData(String thisData) {
107112
// if there's a successful connection:
108-
if (client.connect("api.pachube.com", 80)) {
113+
if (client.connect(server, 80)) {
109114
Serial.println("connecting...");
110115
// send the HTTP PUT request:
111116
client.print("PUT /v2/feeds/");
112117
client.print(FEEDID);
113118
client.println(".csv HTTP/1.1");
114-
client.print("Host: api.pachube.com\n");
119+
client.println("Host: api.pachube.com");
115120
client.print("X-PachubeApiKey: ");
116121
client.println(APIKEY);
117122
client.print("User-Agent: ");
118123
client.println(USERAGENT);
119124
client.print("Content-Length: ");
120-
client.println(thisData.length(), DEC);
125+
client.println(thisData.length());
121126

122127
// last pieces of the HTTP PUT request:
123-
client.print("Content-Type: text/csv\n");
124-
client.println("Connection: close\n");
128+
client.println("Content-Type: text/csv");
129+
client.println("Connection: close");
130+
client.println();
125131

126132
// here's the actual content of the PUT request:
127133
client.println(thisData);
128-
129-
// note the time that the connection was made:
130-
lastConnectionTime = millis();
131134
}
132135
else {
133136
// if you couldn't make a connection:
134137
Serial.println("connection failed");
135138
Serial.println();
136139
Serial.println("disconnecting.");
137140
client.stop();
138-
lastConnected = client.connected();
139141
}
142+
// note the time that the connection was made or attempted:
143+
lastConnectionTime = millis();
140144
}
141145

142-

0 commit comments

Comments
 (0)