3 #include "TimingSingleton.h"
4 #include "TransactionStatus.h"
5 #include "PietteTech_DHT.h"
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
13 #define SLEEP_TIME 30 // Sleep time in seconds
17 void dht_wrapper(); // must be declared before the lib initialization
18 // Library instantiation
19 PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
23 unsigned int DHTnextSampleTime; // Next time we want to start sample
24 bool bDHTstarted; // flag to indicate we started acquisition
27 TimingSingleton *timer;
29 MyVector<TransactionStatus *> * transStatusList;
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
44 timer = TimingSingleton_getInstance();
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);
52 baseurl->releaseRef();
53 password->releaseRef();
55 // PietteTech DHT library
56 DHTnextSampleTime = 0; // Start the first sample immediately
59 // This function gets and prints sensor status for debugging
60 void getSensorStatus(int result) {
62 Serial.print("Read sensor: ");
67 case DHTLIB_ERROR_CHECKSUM:
68 Serial.println("Error\n\r\tChecksum error");
70 case DHTLIB_ERROR_ISR_TIMEOUT:
71 Serial.println("Error\n\r\tISR time out error");
73 case DHTLIB_ERROR_RESPONSE_TIMEOUT:
74 Serial.println("Error\n\r\tResponse time out error");
76 case DHTLIB_ERROR_DATA_TIMEOUT:
77 Serial.println("Error\n\r\tData time out error");
79 case DHTLIB_ERROR_ACQUIRING:
80 Serial.println("Error\n\r\tAcquiring");
82 case DHTLIB_ERROR_DELTA:
83 Serial.println("Error\n\r\tDelta time to small");
85 case DHTLIB_ERROR_NOTSTARTED:
86 Serial.println("Error\n\r\tNot started");
89 Serial.println("Unknown error");
94 // This wrapper is in charge of calling
95 // must be defined like this for the library to work
102 // Wait until sensor is ready
104 // Check if we need to start the next sample
105 if (millis() > DHTnextSampleTime) {
107 if (!bDHTstarted) { // Start the sample
112 // Has sample completed?
113 if (!DHT.acquiring()) {
114 // TODO: get DHT status (for debugging)
115 //int result = DHT.getStatus();
116 //getSensorStatus(result);
119 sprintf(keyBuffer, "sensor0");
120 iKeyA = new IoTString(keyBuffer);
122 float tempF = DHT.getFahrenheit();
123 // Do updates for the temperature
124 sprintf(dataBuffer, "%f", tempF);
125 iValueA = new IoTString(dataBuffer);
127 t1->startTransaction();
128 t1->put(iKeyA, iValueA);
129 transStatusList->add(t1->commitTransaction());
133 iValueA->releaseRef();
134 System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);