2
Pachube sensor client with Strings
4
This sketch connects an analog sensor to Pachube (http://www.pachube.com)
5
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
6
the Adafruit Ethernet shield, either one will work, as long as it's got
7
a Wiznet Ethernet module on board.
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.
13
This example uses the String library, which is part of the Arduino core from
17
* Analog sensor attached to analog in 0
18
* Ethernet shield attached to pins 10, 11, 12, 13
22
by Tom Igoe with input from Usman Haque and Joe Saavedra
24
http://arduino.cc/en/Tutorial/PachubeClientString
25
This code is in the public domain.
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
37
// assign a MAC address for the ethernet controller.
38
// fill in your address here:
40
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
41
// fill in an available IP address on your network here,
42
// for manual configuration:
43
IPAddress ip(10,0,0,20);
45
// initialize the library instance:
46
EthernetClient client;
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
55
// give the ethernet module time to boot up:
57
// start the Ethernet connection:
58
if (Ethernet.begin(mac) == 0) {
59
Serial.println("Failed to configure Ethernet using DHCP");
60
// DHCP failed, so use a fixed IP address:
61
Ethernet.begin(mac, ip);
66
// read the analog sensor:
67
int sensorReading = analogRead(A0);
68
// convert the data to a String to send it:
70
String dataString = "sensor1,";
71
dataString += sensorReading;
73
// you can append multiple readings to this String if your
74
// pachube feed is set up to handle multiple values:
75
int otherSensorReading = analogRead(A1);
76
dataString += "\nsensor2,";
77
dataString += otherSensorReading;
79
// if there's incoming data from the net connection.
80
// send it out the serial port. This is for debugging
82
if (client.available()) {
83
char c = client.read();
87
// if there's no net connection, but there was one last time
88
// through the loop, then stop the client:
89
if (!client.connected() && lastConnected) {
91
Serial.println("disconnecting.");
95
// if you're not connected, and ten seconds have passed since
96
// your last connection, then connect again and send data:
97
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
100
// store the state of the connection for next time through
102
lastConnected = client.connected();
105
// this method makes a HTTP connection to the server:
106
void sendData(String thisData) {
107
// if there's a successful connection:
108
if (client.connect("api.pachube.com", 80)) {
109
Serial.println("connecting...");
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);
119
client.print("Content-Length: ");
120
client.println(thisData.length(), DEC);
122
// last pieces of the HTTP PUT request:
123
client.print("Content-Type: text/csv\n");
124
client.println("Connection: close\n");
126
// here's the actual content of the PUT request:
127
client.println(thisData);
129
// note the time that the connection was made:
130
lastConnectionTime = millis();
133
// if you couldn't make a connection:
134
Serial.println("connection failed");
136
Serial.println("disconnecting.");
138
lastConnected = client.connected();