5f3f10c7c5e8de1740196d0be62c3a13b09c26d4
[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_MODE(SEMI_AUTOMATIC)
8 SYSTEM_THREAD(ENABLED)
9
10 // System defines
11 // Arduino
12 #define DHTPIN                          2               // Data pin
13 #define PWRPIN                          D5              // Power pin
14 #define ERRPIN                          D7              // Error pin
15 #define DHTTYPE                         DHT22   // DHT 22  (AM2302)
16 // IoTCloud
17 #define SLEEP_TIME                      30              // Sleep time in seconds
18 #define RETRY_SLEEP_TIME        5               // Sleep time in seconds
19 #define RETRY_TIME                      100000  // stop trying after 5 seconds
20
21 // Initialize DHT sensor for normal 16mhz Arduino
22 DHT dht(DHTPIN, DHTTYPE);
23
24 // Globals
25 // IoTCloud
26 TimingSingleton *timer;
27 bool foundError;
28 MyVector<TransactionStatus *> * transStatusList;
29 char keyBuffer[80];
30 char dataBuffer[80];
31 Table *t1;
32 int64_t machineId;
33
34 void setup() {
35         // TODO: 
36         // 1) Before running PHOTON's with this firmware,
37         //    please go to the master branch and run Init.C
38         //    in iotcloud/version2/src/C to initialize 
39         //    a table on the cloud side.
40         //    If you want to read the committed keys and values 
41         //    on the cloud, then you can use Read.java in
42         //    iotcloud/version2/src/java/simple_test.
43         // 2) If you want to use the Serial library, 
44         //    please install "screen" on your machine and run
45         //    it on the serial port right after flashing the
46         //    firmware onto the Particle board.
47         //    e.g. sudo screen /dev/ttyACM0
48
49         // We use one I/O pin to power the sensor so
50         // that we can make it go to sleep when we go into
51         // deep sleep.
52         
53         // TODO: Profiling!
54         //Serial.begin();
55         //Serial.print("Time begin setup: ");
56         //Serial.println(micros());
57         Particle.connect();
58         // Wait for maximum 10 seconds - sleep if WiFi is not connected
59         // Wake up and try again after 5 seconds
60         if (!waitFor(Particle.connected, RETRY_TIME))
61                 System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME);
62                 
63         pinMode(PWRPIN, OUTPUT);
64         // Turn on sensor
65         digitalWrite(PWRPIN, HIGH);
66         // Arduino DHT
67         dht.begin();
68         // Prepare device key from MAC (just last 2 of 6 digits)
69         byte mac[6];
70         WiFi.macAddress(mac);
71
72         // TODO: Uncomment the following block to print MAC
73         //Serial.begin();
74         //for (int i=0; i<6; i++) {
75         //      Serial.printf("%02x%s", mac[i], i != 5 ? ":" : "");
76         //}
77         //Serial.println();
78         // Prepare machine ID
79         int64_t mac4 = (int64_t) mac[4];
80         int64_t mac5 = (int64_t) mac[5];
81         machineId = (mac4 * 256) +  mac5;
82         
83         // IoTCloud library
84         timer = TimingSingleton_getInstance();
85         foundError = false;
86         transStatusList = new MyVector<TransactionStatus *>();
87         IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
88         IoTString *password = new IoTString("reallysecret");
89         
90         //Serial.print("Time begin rebuilding table: ");
91         //Serial.println(micros());     
92         
93         t1 = new Table(baseurl, password, machineId, -1);
94         t1->rebuild();
95
96         baseurl->releaseRef();
97         password->releaseRef();
98         
99         //Serial.print("Time end setup: ");
100         //Serial.println(micros());
101 }
102
103 void loop() {
104         // Wait until sensor is ready
105         //delay(2000);
106         //Serial.print("Time begin loop: ");
107         //Serial.println(micros());
108         // Read humidity
109         float humid = dht.readHumidity();
110         // Read temperature as Fahrenheit
111         float tempF = dht.readTemperature(true);
112         // Check if any reads failed and exit early (to try again).
113         if (isnan(tempF))
114                 return;
115         // Humidity
116         // Key
117         sprintf(keyBuffer, "humid%d", machineId);
118         IoTString * iKeyHumid = new IoTString(keyBuffer);
119         // Do updates for the temperature
120         sprintf(dataBuffer, "%f", humid);
121         IoTString * iValueHumid = new IoTString(dataBuffer);
122
123         // Check and create a new key if it isn't created yet
124         t1->createNewKey(iKeyHumid, machineId);
125         t1->startTransaction();
126         t1->put(iKeyHumid, iValueHumid);
127         transStatusList->add(t1->commitTransaction());
128
129         // Temperature
130         // Key
131         sprintf(keyBuffer, "tempF%d", machineId);
132         IoTString * iKeyTempF = new IoTString(keyBuffer);
133         // Do updates for the temperature
134         sprintf(dataBuffer, "%f", tempF);
135         IoTString * iValueTempF = new IoTString(dataBuffer);
136
137         // Check and create a new key if it isn't created yet
138         t1->createNewKey(iKeyTempF, machineId);
139         t1->startTransaction();
140         t1->put(iKeyTempF, iValueTempF);
141         transStatusList->add(t1->commitTransaction());
142         t1->update();
143
144         iKeyHumid->releaseRef();
145         iValueHumid->releaseRef();
146         iKeyTempF->releaseRef();
147         iValueTempF->releaseRef();
148         
149         for (uint i = 0; i < transStatusList->size(); i++) {
150                 TransactionStatus * status = transStatusList->get(i);
151                 if (status->getStatus() != TransactionStatus_StatusCommitted) {
152                         foundError = true;
153                         digitalWrite(ERRPIN, HIGH);     // Turn on LED upon error
154                 }
155                 delete status;
156         }
157         
158         // Turn off sensor
159         digitalWrite(PWRPIN, LOW);
160
161         //Serial.print("Time end loop: ");
162         //Serial.println(micros());
163         System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);
164
165         //while(true) { }
166 }
167