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
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:
4646EthernetClient 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
5257void 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:
106111void 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