1 /* Arduino Projects for Dummies
4 * Chapter 12: Building a Home Sensing Station
5 * Reads values from Analog Sensors on Pins 2,3 and 4
6 * Posts these values as three datastreams to
7 * a single Xively feed.
10 * HttpClient library at: https://github.com/amcewen/HttpClient
11 * Xively library at: https://github.com/xively/xively-arduino
12 * Based on example code written by Adrian McEwen with
13 * modifications by Sam Mulube
20 #include <HttpClient.h>
23 // Replace the text below with your API_KEY and FEED_ID from Xively!
24 // Otherwise the code will not work!
25 #define API_KEY "***** YOUR API KEY GOES HERE *****" // your Xively API key goes here
26 #define FEED_ID ***** YOUR FEED ID GOES HERE ***** // your Xively feed ID goes here
28 // MAC address for your Ethernet shield
29 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
31 // Analog pins we're monitoring (0 and 1 are used by the Ethernet shield's SD card)
33 const int lightSensorPin=2;
37 unsigned long lastConnectionTime = 0; // last time we connected to Xively
38 const unsigned long connectionInterval = 15000; // delay between connecting to Xively in milliseconds
40 // Initialize the Xively library
42 // Define the strings for our datastream IDs
43 char sensorId1[] = "light"; // Light level (LDR)
44 char sensorId2[] = "temp1"; // Temperature sensor 1 (TMP36)
45 char sensorId3[] = "temp2"; // Temperature sensor 2 (TMP36)
47 // Create three datastreams for the feed
48 XivelyDatastream datastreams[] = {
49 XivelyDatastream(sensorId1, strlen(sensorId1), DATASTREAM_FLOAT),
50 XivelyDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
51 XivelyDatastream(sensorId3, strlen(sensorId3), DATASTREAM_FLOAT),
54 // Wrap the 3 datastreams into one feed
55 XivelyFeed feed(FEED_ID, datastreams, 3 /* number of datastreams */);
57 // Create the ethernet client and Xively client
58 EthernetClient client;
59 XivelyClient xivelyclient(client);
63 Serial.println("Initializing network");
64 while (Ethernet.begin(mac) != 1) {
65 Serial.println("Error getting IP address via DHCP, trying again...");
68 Serial.println("Network initialized");
69 Serial.println("Ready.");
74 if (millis() - lastConnectionTime > connectionInterval) {
75 // read a value from the Light sensor pin (2)
76 float lightLevel = map(analogRead(lightSensorPin),0,1023,0,100);
77 // send it to Xively (sensor ,
78 sendData(0, lightLevel); // the 0 is the index number of this stream in the datastreams[] array
80 // read the datastream back from Xively
81 getData(0); // This is optional. Just prints what you sent to Xively to the Serial Monitor
83 float temperature1 = ((getVoltage(tempPin1) -.5) * 100L);
84 //convert from 10 mv per degree with 500 mV offset, to degrees ((voltage - 500mV) * 100)
87 sendData(1, temperature1); // the 1 is the index number of this stream in the datastreams[] array
89 // read the datastream back from Xively
92 float temperature2 = ((getVoltage(tempPin2) -.5) * 100L);
94 sendData(2, temperature2); // the 2 is the index number of this stream in the datastreams[] array
96 // read the datastream back from Xively
98 // update connection time so we wait before connecting again
99 lastConnectionTime = millis();
101 Serial.println("Waiting for next reading");
102 Serial.println("========================");
106 // send the supplied value to Xively, printing some debug information as we go
107 void sendData(int streamIndexNumber, float sensorValue) {
108 datastreams[streamIndexNumber].setFloat(sensorValue);~~~
110 Serial.print("Sensor value is: ");
111 Serial.println(datastreams[streamIndexNumber].getFloat());
113 Serial.println("Uploading to Xively");
114 int ret = xivelyclient.put(feed, API_KEY);
115 //Serial.print("PUT return code: ");
116 //Serial.println(ret);
119 // get the value of the datastream from Xively, printing out the values we received
120 void getData(int stream) {
121 Serial.println("Reading the data back from Xively");
123 int request = xivelyclient.get(feed, API_KEY);
124 //Serial.print("GET return code: ");
125 //Serial.println(request);
128 Serial.print("Datastream: ");
129 Serial.println(feed[stream]);
131 Serial.print("Sensor value: ");
132 Serial.println(feed[stream].getFloat());
133 Serial.println("========================");
137 float getVoltage(int pin){
138 //Converts from a 0 to 1024 digital reading from 0 to 5 volts
139 //in ~ 5 millivolt increments
140 return (analogRead(pin) * .004882814);