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
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:
3646EthernetClient 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 += " \n sensor2 ," ;
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:
94106void 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