6
6
the Adafruit Ethernet shield, either one will work, as long as it's got
7
7
a Wiznet Ethernet module on board.
8
8
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
+
9
13
This example uses the String library, which is part of the Arduino core from
10
14
version 0019.
11
15
14
18
* Ethernet shield attached to pins 10, 11, 12, 13
15
19
16
20
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
19
23
24
+ http://arduino.cc/en/Tutorial/PachubeClientString
20
25
This code is in the public domain.
21
26
22
27
*/
23
28
24
29
#include < SPI.h>
25
30
#include < Ethernet.h>
26
31
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
+
27
37
// assign a MAC address for the ethernet controller.
28
38
// fill in your address here:
29
- byte mac[] = {
39
+ byte mac[] = {
30
40
0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED };
31
41
// fill in an available IP address on your network here,
32
42
// for manual configuration:
33
- IPAddress ip (10 ,0 ,1 ,20 );
43
+ IPAddress ip (10 ,0 ,0 ,20 );
34
44
35
45
// initialize the library instance:
36
46
EthernetClient client;
@@ -47,7 +57,7 @@ void setup() {
47
57
// start the Ethernet connection:
48
58
if (Ethernet.begin (mac) == 0 ) {
49
59
Serial.println (" Failed to configure Ethernet using DHCP" );
50
- // Configure manually :
60
+ // DHCP failed, so use a fixed IP address :
51
61
Ethernet.begin (mac, ip);
52
62
}
53
63
}
@@ -56,13 +66,15 @@ void loop() {
56
66
// read the analog sensor:
57
67
int sensorReading = analogRead (A0);
58
68
// convert the data to a String to send it:
59
- String dataString = String (sensorReading);
69
+
70
+ String dataString = " sensor1," ;
71
+ dataString += sensorReading;
60
72
61
73
// you can append multiple readings to this String if your
62
74
// pachube feed is set up to handle multiple values:
63
75
int otherSensorReading = analogRead (A1);
64
- dataString += " ," ;
65
- dataString += String ( otherSensorReading) ;
76
+ dataString += " \n sensor2 ," ;
77
+ dataString += otherSensorReading;
66
78
67
79
// if there's incoming data from the net connection.
68
80
// send it out the serial port. This is for debugging
@@ -93,14 +105,17 @@ void loop() {
93
105
// this method makes a HTTP connection to the server:
94
106
void sendData (String thisData) {
95
107
// if there's a successful connection:
96
- if (client.connect (" www .pachube.com" , 80 )) {
108
+ if (client.connect (" api .pachube.com" , 80 )) {
97
109
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);
104
119
client.print (" Content-Length: " );
105
120
client.println (thisData.length (), DEC);
106
121
@@ -117,5 +132,11 @@ void sendData(String thisData) {
117
132
else {
118
133
// if you couldn't make a connection:
119
134
Serial.println (" connection failed" );
135
+ Serial.println ();
136
+ Serial.println (" disconnecting." );
137
+ client.stop ();
138
+ lastConnected = client.connected ();
120
139
}
121
140
}
141
+
142
+
0 commit comments