]> git.piffa.net Git - arduino/blob - books/pdummies/APFD_Chapter12_Home_Sensing_Station/APFD_Chapter12_Home_Sensing_Station.ino
first commit
[arduino] / books / pdummies / APFD_Chapter12_Home_Sensing_Station / APFD_Chapter12_Home_Sensing_Station.ino
1 /* Arduino Projects for Dummies
2  * by Brock Craft 
3  *
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.
8  *
9  * Requires:
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
14  *
15  * v0.1 15.05.2013
16 */
17
18 #include <SPI.h>
19 #include <Ethernet.h>
20 #include <HttpClient.h>
21 #include <Xively.h>
22
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
27
28 // MAC address for your Ethernet shield
29 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
30
31 // Analog pins we're monitoring (0 and 1 are used by the Ethernet shield's SD card)
32
33 const int lightSensorPin=2;
34 const int tempPin1=3;
35 const int tempPin2=4;
36
37 unsigned long lastConnectionTime = 0;                // last time we connected to Xively
38 const unsigned long connectionInterval = 15000;      // delay between connecting to Xively in milliseconds
39
40 // Initialize the Xively library
41
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)
46  
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),
52 };
53
54 // Wrap the 3 datastreams into one feed
55 XivelyFeed feed(FEED_ID, datastreams, 3 /* number of datastreams */);
56
57 // Create the ethernet client and Xively client
58 EthernetClient client;
59 XivelyClient xivelyclient(client);
60
61 void setup() {
62   Serial.begin(9600);
63   Serial.println("Initializing network");
64   while (Ethernet.begin(mac) != 1) {
65     Serial.println("Error getting IP address via DHCP, trying again...");
66     delay(15000);
67   }
68   Serial.println("Network initialized");
69   Serial.println("Ready.");
70 }
71
72 void loop() {
73   // main program loop
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
79    
80     // read the datastream back from Xively
81     getData(0); // This is optional. Just prints what you sent to Xively to the Serial Monitor
82     
83     float temperature1 = ((getVoltage(tempPin1) -.5) * 100L);
84     //convert from 10 mv per degree with 500 mV offset, to degrees ((voltage - 500mV) * 100)
85  
86     // send it to Xively
87     sendData(1, temperature1); // the 1 is the index number of this stream in the datastreams[] array
88    
89     // read the datastream back from Xively
90     getData(1);
91     
92     float temperature2 = ((getVoltage(tempPin2) -.5) * 100L);
93     // send it to Xively
94     sendData(2, temperature2); // the 2 is the index number of this stream in the datastreams[] array
95
96     // read the datastream back from Xively
97     getData(2);
98     // update connection time so we wait before connecting again
99     lastConnectionTime = millis();
100  
101     Serial.println("Waiting for next reading");
102     Serial.println("========================");
103   }
104 }
105
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);~~~
109
110   Serial.print("Sensor value is: ");
111   Serial.println(datastreams[streamIndexNumber].getFloat());
112
113   Serial.println("Uploading to Xively");
114   int ret = xivelyclient.put(feed, API_KEY);
115   //Serial.print("PUT return code: ");
116   //Serial.println(ret);
117 }
118
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");
122
123   int request = xivelyclient.get(feed, API_KEY);
124   //Serial.print("GET return code: ");
125   //Serial.println(request);
126
127   if (request > 0) {
128     Serial.print("Datastream: ");
129     Serial.println(feed[stream]);
130
131     Serial.print("Sensor value: ");
132     Serial.println(feed[stream].getFloat());
133     Serial.println("========================");
134   }
135 }
136
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);
141 }