]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/xively/examples/WiFiDatastreamUpload/WiFiDatastreamUpload.ino
first commit
[arduino] / books / pdummies / Libraries / xively / examples / WiFiDatastreamUpload / WiFiDatastreamUpload.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_API_KEY";
14
15 // Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
16 int sensorPin = 2;
17
18 // Define the strings for our datastream IDs
19 char sensorId[] = "sensor_reading";
20 XivelyDatastream datastreams[] = {
21   XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
22 };
23 // Finally, wrap the datastreams into a feed
24 XivelyFeed feed(15552, datastreams, 1 /* number of datastreams */);
25
26 WiFiClient client;
27 XivelyClient xivelyclient(client);
28
29 void printWifiStatus() {
30   // print the SSID of the network you're attached to:
31   Serial.print("SSID: ");
32   Serial.println(WiFi.SSID());
33
34   // print your WiFi shield's IP address:
35   IPAddress ip = WiFi.localIP();
36   Serial.print("IP Address: ");
37   Serial.println(ip);
38
39   // print the received signal strength:
40   long rssi = WiFi.RSSI();
41   Serial.print("signal strength (RSSI):");
42   Serial.print(rssi);
43   Serial.println(" dBm");
44 }
45 void setup() {
46   // put your setup code here, to run once:
47   Serial.begin(9600);
48   
49   Serial.println("Starting single datastream upload to Xively...");
50   Serial.println();
51
52   // attempt to connect to Wifi network:
53   while ( status != WL_CONNECTED) { 
54     Serial.print("Attempting to connect to SSID: ");
55     Serial.println(ssid);
56     status = WiFi.begin(ssid, pass);
57     // wait 10 seconds for connection:
58     delay(10000);
59   } 
60   Serial.println("Connected to wifi");
61   printWifiStatus();
62 }
63
64 void loop() {
65   int sensorValue = analogRead(sensorPin);
66   datastreams[0].setFloat(sensorValue);
67
68   Serial.print("Read sensor value ");
69   Serial.println(datastreams[0].getFloat());
70
71   Serial.println("Uploading it to Xively");
72   int ret = xivelyclient.put(feed, xivelyKey);
73   Serial.print("xivelyclient.put returned ");
74   Serial.println(ret);
75
76   Serial.println();
77   delay(15000);
78 }