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