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