]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/xively/examples/WiFiDatastreamDownload/WiFiDatastreamDownload.ino
first commit
[arduino] / books / pdummies / Libraries / xively / examples / WiFiDatastreamDownload / WiFiDatastreamDownload.ino
1 #include <SPI.h>
2 #include <WiFi.h>
3 #include <HttpClient.h>
4 #include <Xively.h>
5
6 char ssid[] = "YourNetwork"; //  your network SSID (name) 
7 char pass[] = "password";    // your network password (use for WPA, or use as key for WEP)
8 int keyIndex = 0;            // your network key Index number (needed only for WEP)
9
10 int status = WL_IDLE_STATUS;
11
12 // Your Xively key to let you upload data
13 char xivelyKey[] = "YOUR_XIVELY_API_KEY";
14
15 // Define the string for our datastream ID
16 char temperatureId[] = "temperature";
17
18 XivelyDatastream datastreams[] = {
19   XivelyDatastream(temperatureId, strlen(temperatureId), DATASTREAM_FLOAT),
20 };
21 // Finally, wrap the datastreams into a feed
22 XivelyFeed feed(15552, datastreams, 1 /* number of datastreams */);
23
24 WiFiClient client;
25 XivelyClient xivelyclient(client);
26
27 void printWifiStatus() {
28   // print the SSID of the network you're attached to:
29   Serial.print("SSID: ");
30   Serial.println(WiFi.SSID());
31
32   // print your WiFi shield's IP address:
33   IPAddress ip = WiFi.localIP();
34   Serial.print("IP Address: ");
35   Serial.println(ip);
36
37   // print the received signal strength:
38   long rssi = WiFi.RSSI();
39   Serial.print("signal strength (RSSI):");
40   Serial.print(rssi);
41   Serial.println(" dBm");
42 }
43
44 void setup() {
45   // put your setup code here, to run once:
46   Serial.begin(9600);
47   
48   Serial.println("Reading from Xively example");
49   Serial.println();
50
51   // attempt to connect to Wifi network:
52   while ( status != WL_CONNECTED) { 
53     Serial.print("Attempting to connect to SSID: ");
54     Serial.println(ssid);
55     status = WiFi.begin(ssid, pass);
56     // wait 10 seconds for connection:
57     delay(10000);
58   } 
59   Serial.println("Connected to wifi");
60   printWifiStatus();
61 }
62
63 void loop() {
64   int ret = xivelyclient.get(feed, xivelyKey);
65   Serial.print("xivelyclient.get returned ");
66   Serial.println(ret);
67
68   if (ret > 0)
69   {
70     Serial.println("Datastream is...");
71     Serial.println(feed[0]);
72
73     Serial.print("Temperature is: ");
74     Serial.println(feed[0].getFloat());
75   }
76
77   Serial.println();
78   delay(15000UL);
79 }