]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/xively/examples/DatastreamUpload/DatastreamUpload.ino
first commit
[arduino] / books / pdummies / Libraries / xively / examples / DatastreamUpload / DatastreamUpload.ino
1 #include <SPI.h>
2 #include <Ethernet.h>
3 #include <HttpClient.h>
4 #include <Xively.h>
5
6 // MAC address for your Ethernet shield
7 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
8
9 // Your Xively key to let you upload data
10 char xivelyKey[] = "YOUR_XIVELY_API_KEY";
11
12 // Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
13 int sensorPin = 2;
14
15 // Define the strings for our datastream IDs
16 char sensorId[] = "sensor_reading";
17 XivelyDatastream datastreams[] = {
18   XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
19 };
20 // Finally, wrap the datastreams into a feed
21 XivelyFeed feed(15552, datastreams, 1 /* number of datastreams */);
22
23 EthernetClient client;
24 XivelyClient xivelyclient(client);
25
26 void setup() {
27   // put your setup code here, to run once:
28   Serial.begin(9600);
29   
30   Serial.println("Starting single datastream upload to Xively...");
31   Serial.println();
32
33   while (Ethernet.begin(mac) != 1)
34   {
35     Serial.println("Error getting IP address via DHCP, trying again...");
36     delay(15000);
37   }
38 }
39
40 void loop() {
41   int sensorValue = analogRead(sensorPin);
42   datastreams[0].setFloat(sensorValue);
43
44   Serial.print("Read sensor value ");
45   Serial.println(datastreams[0].getFloat());
46
47   Serial.println("Uploading it to Xively");
48   int ret = xivelyclient.put(feed, xivelyKey);
49   Serial.print("xivelyclient.put returned ");
50   Serial.println(ret);
51
52   Serial.println();
53   delay(15000);
54 }