Adding DHT22/AM2302 sensor libraries (Arduino and PietteTech) but using Arduino for...
[iotcloud.git] / version2 / src / others / ino / Sensor-PietteTech.ino
1 #include "Table.h"
2 #include "IoTString.h"
3 #include "TimingSingleton.h"
4 #include "TransactionStatus.h"
5 #include "PietteTech_DHT.h"
6
7 // System defines
8 // PietteTech
9 #define DHTTYPE                                 DHT22   // Sensor type DHT11/21/22/AM2301/AM2302
10 #define DHTPIN                                  D2              // Digital pin for communications
11 #define DHT_SAMPLE_INTERVAL             2000    // Sample every two seconds
12 // IoTCloud
13 #define SLEEP_TIME                              30              // Sleep time in seconds
14
15 // PietteTech
16 // Declaration
17 void dht_wrapper(); // must be declared before the lib initialization
18 // Library instantiation
19 PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
20
21 // Globals
22 // PietteTech
23 unsigned int DHTnextSampleTime;     // Next time we want to start sample
24 bool bDHTstarted;           // flag to indicate we started acquisition
25 int n;                              // counter
26 // IoTCloud
27 TimingSingleton *timer;
28 bool foundError;
29 MyVector<TransactionStatus *> * transStatusList;
30 char keyBuffer[80];
31 char dataBuffer[80];
32 Table *t1;
33 IoTString *iKeyA;
34 IoTString *iValueA;
35
36 void setup() {
37         // TODO: 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         // IoTCloud library
44         timer = TimingSingleton_getInstance();
45         foundError = false;
46         transStatusList = new MyVector<TransactionStatus *>();
47         IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
48         IoTString *password = new IoTString("reallysecret");
49         t1 = new Table(baseurl, password, 321, -1);
50         t1->rebuild();
51
52         baseurl->releaseRef();
53         password->releaseRef();
54         
55         // PietteTech DHT library
56     DHTnextSampleTime = 0;  // Start the first sample immediately
57 }
58
59 // This function gets and prints sensor status for debugging
60 void getSensorStatus(int result) {
61
62     Serial.print("Read sensor: ");
63     switch (result) {
64     case DHTLIB_OK:
65         Serial.println("OK");
66         break;
67     case DHTLIB_ERROR_CHECKSUM:
68         Serial.println("Error\n\r\tChecksum error");
69         break;
70     case DHTLIB_ERROR_ISR_TIMEOUT:
71         Serial.println("Error\n\r\tISR time out error");
72         break;
73     case DHTLIB_ERROR_RESPONSE_TIMEOUT:
74         Serial.println("Error\n\r\tResponse time out error");
75         break;
76     case DHTLIB_ERROR_DATA_TIMEOUT:
77         Serial.println("Error\n\r\tData time out error");
78         break;
79     case DHTLIB_ERROR_ACQUIRING:
80         Serial.println("Error\n\r\tAcquiring");
81         break;
82     case DHTLIB_ERROR_DELTA:
83         Serial.println("Error\n\r\tDelta time to small");
84         break;
85     case DHTLIB_ERROR_NOTSTARTED:
86         Serial.println("Error\n\r\tNot started");
87         break;
88     default:
89         Serial.println("Unknown error");
90         break;
91     }
92 }
93
94 // This wrapper is in charge of calling
95 // must be defined like this for the library to work
96 void dht_wrapper() {
97     DHT.isrCallback();
98 }
99
100 void loop() {
101
102         // Wait until sensor is ready
103         delay(2000);
104         // Check if we need to start the next sample
105         if (millis() > DHTnextSampleTime) {
106
107                 if (!bDHTstarted) {     // Start the sample
108                         DHT.acquire();
109                         bDHTstarted = true;
110                 }
111
112                 // Has sample completed?
113                 if (!DHT.acquiring()) {
114                         // TODO: get DHT status (for debugging)
115                         //int result = DHT.getStatus();
116                         //getSensorStatus(result);
117
118                         // Key
119                         sprintf(keyBuffer, "sensor0");
120                         iKeyA = new IoTString(keyBuffer);
121
122                         float tempF = DHT.getFahrenheit();
123                         // Do updates for the temperature
124                         sprintf(dataBuffer, "%f", tempF);
125                         iValueA = new IoTString(dataBuffer);
126
127                         t1->startTransaction();
128                         t1->put(iKeyA, iValueA);
129                         transStatusList->add(t1->commitTransaction());
130                         t1->update();
131
132                         iKeyA->releaseRef();
133                         iValueA->releaseRef();
134                         System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);
135                 }
136         }
137 }
138