]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/xively/examples/WiFiMultipleDatastreamsUpload/WiFiMultipleDatastreamsUpload.ino
first commit
[arduino] / books / pdummies / Libraries / xively / examples / WiFiMultipleDatastreamsUpload / WiFiMultipleDatastreamsUpload.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 char bufferId[] = "info_message";
21 String stringId("random_string");
22 const int bufferSize = 140;
23 char bufferValue[bufferSize]; // enough space to store the string we're going to send
24 XivelyDatastream datastreams[] = {
25   XivelyDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
26   XivelyDatastream(bufferId, strlen(bufferId), DATASTREAM_BUFFER, bufferValue, bufferSize),
27   XivelyDatastream(stringId, DATASTREAM_STRING)
28 };
29 // Finally, wrap the datastreams into a feed
30 XivelyFeed feed(15552, datastreams, 3 /* number of datastreams */);
31
32 WiFiClient client;
33 XivelyClient xivelyclient(client);
34
35 void printWifiStatus() {
36   // print the SSID of the network you're attached to:
37   Serial.print("SSID: ");
38   Serial.println(WiFi.SSID());
39
40   // print your WiFi shield's IP address:
41   IPAddress ip = WiFi.localIP();
42   Serial.print("IP Address: ");
43   Serial.println(ip);
44
45   // print the received signal strength:
46   long rssi = WiFi.RSSI();
47   Serial.print("signal strength (RSSI):");
48   Serial.print(rssi);
49   Serial.println(" dBm");
50 }
51
52 void setup() {
53   // put your setup code here, to run once:
54   Serial.begin(9600);
55   
56   Serial.println("Starting multiple datastream upload to Xively...");
57   Serial.println();
58
59   // attempt to connect to Wifi network:
60   while ( status != WL_CONNECTED) { 
61     Serial.print("Attempting to connect to SSID: ");
62     Serial.println(ssid);
63     status = WiFi.begin(ssid, pass);
64     // wait 10 seconds for connection:
65     delay(10000);
66   } 
67   Serial.println("Connected to wifi");
68   printWifiStatus();
69 }
70
71 void loop() {
72   int sensorValue = analogRead(sensorPin);
73   datastreams[0].setFloat(sensorValue);
74
75   Serial.print("Read sensor value ");
76   Serial.println(datastreams[0].getFloat());
77
78   datastreams[1].setBuffer("a message to upload");
79   Serial.print("Setting buffer value to:\n    ");
80   Serial.println(datastreams[1].getBuffer());
81
82   // Pick a random number to send up in a string
83   String stringValue(random(100));
84   stringValue += " is a random number";
85   datastreams[2].setString(stringValue);
86   Serial.print("Setting string value to:\n    ");
87   Serial.println(datastreams[2].getString());
88
89   Serial.println("Uploading it to Xively");
90   int ret = xivelyclient.put(feed, xivelyKey);
91   Serial.print("xivelyclient.put returned ");
92   Serial.println(ret);
93
94   Serial.println();
95   delay(15000);
96 }