18
18
* Ethernet shield attached to pins 10, 11, 12, 13
19
19
20
20
created 15 March 2010
21
- updated 27 Feb 2012
21
+ updated 16 Mar 2012
22
22
by Tom Igoe with input from Usman Haque and Joe Saavedra
23
23
24
24
http://arduino.cc/en/Tutorial/PachubeClientString
40
40
0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED };
41
41
// fill in an available IP address on your network here,
42
42
// for manual configuration:
43
- IPAddress ip (10 ,0 ,0 ,20 );
43
+ IPAddress ip (10 ,0 ,1 ,20 );
44
44
45
45
// initialize the library instance:
46
46
EthernetClient client;
47
47
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
51
56
52
57
void setup () {
53
58
// start serial port:
@@ -66,9 +71,9 @@ void loop() {
66
71
// read the analog sensor:
67
72
int sensorReading = analogRead (A0);
68
73
// convert the data to a String to send it:
69
-
74
+
70
75
String dataString = " sensor1," ;
71
- dataString += sensorReading;
76
+ dataString += sensorReading;
72
77
73
78
// you can append multiple readings to this String if your
74
79
// pachube feed is set up to handle multiple values:
@@ -93,7 +98,7 @@ void loop() {
93
98
}
94
99
95
100
// 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:
97
102
if (!client.connected () && (millis () - lastConnectionTime > postingInterval)) {
98
103
sendData (dataString);
99
104
}
@@ -105,38 +110,36 @@ void loop() {
105
110
// this method makes a HTTP connection to the server:
106
111
void sendData (String thisData) {
107
112
// if there's a successful connection:
108
- if (client.connect (" api.pachube.com " , 80 )) {
113
+ if (client.connect (server , 80 )) {
109
114
Serial.println (" connecting..." );
110
115
// send the HTTP PUT request:
111
116
client.print (" PUT /v2/feeds/" );
112
117
client.print (FEEDID);
113
118
client.println (" .csv HTTP/1.1" );
114
- client.print (" Host: api.pachube.com\n " );
119
+ client.println (" Host: api.pachube.com" );
115
120
client.print (" X-PachubeApiKey: " );
116
121
client.println (APIKEY);
117
122
client.print (" User-Agent: " );
118
123
client.println (USERAGENT);
119
124
client.print (" Content-Length: " );
120
- client.println (thisData.length (), DEC );
125
+ client.println (thisData.length ());
121
126
122
127
// 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 ();
125
131
126
132
// here's the actual content of the PUT request:
127
133
client.println (thisData);
128
-
129
- // note the time that the connection was made:
130
- lastConnectionTime = millis ();
131
134
}
132
135
else {
133
136
// if you couldn't make a connection:
134
137
Serial.println (" connection failed" );
135
138
Serial.println ();
136
139
Serial.println (" disconnecting." );
137
140
client.stop ();
138
- lastConnected = client.connected ();
139
141
}
142
+ // note the time that the connection was made or attempted:
143
+ lastConnectionTime = millis ();
140
144
}
141
145
142
-
0 commit comments