Skip to content

Commit 9f755d1

Browse files
author
Tom Igoe
committed
Updated Ethernet PachubeClient and PachubeClientString examples to use Pachube API 2.0
1 parent 2f29f89 commit 9f755d1

File tree

2 files changed

+71
-39
lines changed

2 files changed

+71
-39
lines changed

libraries/Ethernet/examples/PachubeClient/PachubeClient.ino

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,33 @@
66
the Adafruit Ethernet shield, either one will work, as long as it's got
77
a Wiznet Ethernet module on board.
88
9+
This example has been updated to use version 2.0 of the Pachube.com API.
10+
To make it work, create a feed with a datastream, and give it the ID
11+
sensor1. Or change the code below to match your feed.
12+
13+
914
Circuit:
1015
* Analog sensor attached to analog in 0
1116
* Ethernet shield attached to pins 10, 11, 12, 13
1217
1318
created 15 March 2010
14-
updated 26 Oct 2011
15-
by Tom Igoe
19+
updated 27 Feb 2012
20+
by Tom Igoe with input from Usman Haque and Joe Saavedra
1621
17-
http://www.tigoe.net/pcomp/code/category/arduinowiring/873
22+
http://arduino.cc/en/Tutorial/PachubeClient
1823
This code is in the public domain.
1924
2025
*/
2126

2227
#include <SPI.h>
2328
#include <Ethernet.h>
2429

30+
31+
32+
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
33+
#define FEEDID 00000 // replace your feed ID
34+
#define USERAGENT "My Project" // user agent is the project name
35+
2536
// assign a MAC address for the ethernet controller.
2637
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
2738
// fill in your address here:
@@ -41,19 +52,10 @@ const int postingInterval = 10000; //delay between updates to Pachube.com
4152
void setup() {
4253
// start serial port:
4354
Serial.begin(9600);
44-
// start the Ethernet connection:
55+
// start the Ethernet connection:
4556
if (Ethernet.begin(mac) == 0) {
4657
Serial.println("Failed to configure Ethernet using DHCP");
47-
// no point in carrying on, so do nothing forevermore:
48-
for(;;)
49-
;
50-
}
51-
// give the ethernet module time to boot up:
52-
delay(1000);
53-
// start the Ethernet connection:
54-
if (Ethernet.begin(mac) == 0) {
55-
Serial.println("Failed to configure Ethernet using DHCP");
56-
// Configure manually:
58+
// DHCP failed, so use a fixed IP address:
5759
Ethernet.begin(mac, ip);
5860
}
5961
}
@@ -93,31 +95,40 @@ void sendData(int thisData) {
9395
// if there's a successful connection:
9496
if (client.connect("www.pachube.com", 80)) {
9597
Serial.println("connecting...");
96-
// send the HTTP PUT request.
97-
// fill in your feed address here:
98-
client.print("PUT /api/YOUR_FEED_HERE.csv HTTP/1.1\n");
99-
client.print("Host: www.pachube.com\n");
100-
// fill in your Pachube API key here:
101-
client.print("X-PachubeApiKey: YOUR_KEY_HERE\n");
98+
// send the HTTP PUT request:
99+
client.print("PUT /v2/feeds/");
100+
client.print(FEEDID);
101+
client.println(".csv HTTP/1.1");
102+
client.print("Host: api.pachube.com\n");
103+
client.print("X-PachubeApiKey: ");
104+
client.println(APIKEY);
105+
client.print("User-Agent: ");
106+
client.println(USERAGENT);
102107
client.print("Content-Length: ");
103108

104109
// calculate the length of the sensor reading in bytes:
105-
int thisLength = getLength(thisData);
106-
client.println(thisLength, DEC);
110+
// 8 bytes for "sensor1," + number of digits of the data:
111+
int thisLength = 8 + getLength(thisData);
112+
client.println(thisLength);
107113

108114
// last pieces of the HTTP PUT request:
109115
client.print("Content-Type: text/csv\n");
110116
client.println("Connection: close\n");
111117

112118
// here's the actual content of the PUT request:
113-
client.println(thisData, DEC);
119+
client.print("sensor1,");
120+
client.println(thisData);
114121

115122
// note the time that the connection was made:
116123
lastConnectionTime = millis();
117124
}
118125
else {
119-
// if you couldn't make a connection:
126+
// if you couldn't make a connection:
120127
Serial.println("connection failed");
128+
Serial.println();
129+
Serial.println("disconnecting.");
130+
client.stop();
131+
lastConnected = client.connected();
121132
}
122133
}
123134

libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
the Adafruit Ethernet shield, either one will work, as long as it's got
77
a Wiznet Ethernet module on board.
88
9+
This example has been updated to use version 2.0 of the Pachube.com API.
10+
To make it work, create a feed with two datastreams, and give them the IDs
11+
sensor1 and sensor2. Or change the code below to match your feed.
12+
913
This example uses the String library, which is part of the Arduino core from
1014
version 0019.
1115
@@ -14,23 +18,29 @@
1418
* Ethernet shield attached to pins 10, 11, 12, 13
1519
1620
created 15 March 2010
17-
updated 26 Oct 2011
18-
by Tom Igoe
21+
updated 27 Feb 2012
22+
by Tom Igoe with input from Usman Haque and Joe Saavedra
1923
24+
http://arduino.cc/en/Tutorial/PachubeClientString
2025
This code is in the public domain.
2126
2227
*/
2328

2429
#include <SPI.h>
2530
#include <Ethernet.h>
2631

32+
33+
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
34+
#define FEEDID 00000 // replace your feed ID
35+
#define USERAGENT "My Project" // user agent is the project name
36+
2737
// assign a MAC address for the ethernet controller.
2838
// fill in your address here:
29-
byte mac[] = {
39+
byte mac[] = {
3040
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
3141
// fill in an available IP address on your network here,
3242
// for manual configuration:
33-
IPAddress ip(10,0,1,20);
43+
IPAddress ip(10,0,0,20);
3444

3545
// initialize the library instance:
3646
EthernetClient client;
@@ -47,7 +57,7 @@ void setup() {
4757
// start the Ethernet connection:
4858
if (Ethernet.begin(mac) == 0) {
4959
Serial.println("Failed to configure Ethernet using DHCP");
50-
// Configure manually:
60+
// DHCP failed, so use a fixed IP address:
5161
Ethernet.begin(mac, ip);
5262
}
5363
}
@@ -56,13 +66,15 @@ void loop() {
5666
// read the analog sensor:
5767
int sensorReading = analogRead(A0);
5868
// convert the data to a String to send it:
59-
String dataString = String(sensorReading);
69+
70+
String dataString = "sensor1,";
71+
dataString += sensorReading;
6072

6173
// you can append multiple readings to this String if your
6274
// pachube feed is set up to handle multiple values:
6375
int otherSensorReading = analogRead(A1);
64-
dataString += ",";
65-
dataString += String(otherSensorReading);
76+
dataString += "\nsensor2,";
77+
dataString += otherSensorReading;
6678

6779
// if there's incoming data from the net connection.
6880
// send it out the serial port. This is for debugging
@@ -93,14 +105,17 @@ void loop() {
93105
// this method makes a HTTP connection to the server:
94106
void sendData(String thisData) {
95107
// if there's a successful connection:
96-
if (client.connect("www.pachube.com", 80)) {
108+
if (client.connect("api.pachube.com", 80)) {
97109
Serial.println("connecting...");
98-
// send the HTTP PUT request.
99-
// fill in your feed address here:
100-
client.print("PUT /api/YOUR_FEED_HERE.csv HTTP/1.1\n");
101-
client.print("Host: www.pachube.com\n");
102-
// fill in your Pachube API key here:
103-
client.print("X-PachubeApiKey: YOUR_KEY_HERE\n");
110+
// send the HTTP PUT request:
111+
client.print("PUT /v2/feeds/");
112+
client.print(FEEDID);
113+
client.println(".csv HTTP/1.1");
114+
client.print("Host: api.pachube.com\n");
115+
client.print("X-PachubeApiKey: ");
116+
client.println(APIKEY);
117+
client.print("User-Agent: ");
118+
client.println(USERAGENT);
104119
client.print("Content-Length: ");
105120
client.println(thisData.length(), DEC);
106121

@@ -117,5 +132,11 @@ void sendData(String thisData) {
117132
else {
118133
// if you couldn't make a connection:
119134
Serial.println("connection failed");
135+
Serial.println();
136+
Serial.println("disconnecting.");
137+
client.stop();
138+
lastConnected = client.connected();
120139
}
121140
}
141+
142+

0 commit comments

Comments
 (0)