22d9105c7d72653b17ca65ee148c09bf6b9e9915
[iotcloud.git] / version2 / src / C / Sensor-Arduino.ino
1 #include "Table.h"
2 #include "IoTString.h"
3 #include "TimingSingleton.h"
4 #include "TransactionStatus.h"
5 #include "DHT.h"
6
7 // System defines
8 // Arduino
9 #define DHTPIN                  2               // Data pin
10 #define PWRPIN                  D5              // Power pin
11 #define DHTTYPE                 DHT22   // DHT 22  (AM2302)
12 // IoTCloud
13 #define SLEEP_TIME              1800    // Sleep time in seconds
14
15 // Initialize DHT sensor for normal 16mhz Arduino
16 DHT dht(DHTPIN, DHTTYPE);
17
18 // Globals
19 // IoTCloud
20 TimingSingleton *timer;
21 bool foundError;
22 MyVector<TransactionStatus *> * transStatusList;
23 char keyBuffer[80];
24 char dataBuffer[80];
25 Table *t1;
26 IoTString *iKeyA;
27 IoTString *iValueA;
28
29
30 void setup() {
31         // TODO: If you want to use the Serial library, 
32         // please install "screen" on your machine and run
33         // it on the serial port right after flashing the
34         // firmware onto the Particle board.
35         // e.g. sudo screen /dev/ttyACM0
36
37         // We use one I/O pin to power the sensor so
38         // that we can make it go to sleep when we go into
39         // deep sleep.
40         pinMode(PWRPIN, OUTPUT);
41         // Turn on sensor
42         digitalWrite(PWRPIN, HIGH);
43
44         // Prepare device key from MAC (just last 2 of 6 digits)
45         byte mac[6];
46         WiFi.macAddress(mac);
47
48         // TODO: Uncomment the following block to print MAC
49         //Serial.begin();
50         //for (int i=0; i<6; i++) {
51         //      Serial.printf("%02x%s", mac[i], i != 5 ? ":" : "");
52         //}
53         //Serial.println();
54         
55         int64_t machineId = (int64_t) mac[4] + mac[5];
56         // IoTCloud library
57         timer = TimingSingleton_getInstance();
58         foundError = false;
59         transStatusList = new MyVector<TransactionStatus *>();
60         IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
61         IoTString *password = new IoTString("reallysecret");
62         t1 = new Table(baseurl, password, machineId, -1);
63         t1->rebuild();
64
65         baseurl->releaseRef();
66         password->releaseRef();
67         
68         // Arduino DHT
69         dht.begin();
70 }
71
72 void loop() {
73
74         // Wait until sensor is ready
75         delay(2000);
76         // Read temperature as Fahrenheit
77         float tempF = dht.readTemperature(true);
78         // Check if any reads failed and exit early (to try again).
79         if (isnan(tempF))
80                 return;
81
82         // Key
83         sprintf(keyBuffer, "sensor0");
84         iKeyA = new IoTString(keyBuffer);
85
86         // Do updates for the temperature
87         sprintf(dataBuffer, "%f", tempF);
88         iValueA = new IoTString(dataBuffer);
89
90         t1->startTransaction();
91         t1->put(iKeyA, iValueA);
92         transStatusList->add(t1->commitTransaction());
93         t1->update();
94
95         iKeyA->releaseRef();
96         iValueA->releaseRef();
97         
98         // Turn off sensor
99         digitalWrite(PWRPIN, LOW);
100         System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);
101 }
102