#include "Table.h" #include "IoTString.h" #include "TimingSingleton.h" #include "TransactionStatus.h" #include "PietteTech_DHT.h" // System defines // PietteTech #define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302 #define DHTPIN D2 // Digital pin for communications #define DHT_SAMPLE_INTERVAL 2000 // Sample every two seconds // IoTCloud #define SLEEP_TIME 30 // Sleep time in seconds // PietteTech // Declaration void dht_wrapper(); // must be declared before the lib initialization // Library instantiation PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper); // Globals // PietteTech unsigned int DHTnextSampleTime; // Next time we want to start sample bool bDHTstarted; // flag to indicate we started acquisition int n; // counter // IoTCloud TimingSingleton *timer; bool foundError; MyVector * transStatusList; char keyBuffer[80]; char dataBuffer[80]; Table *t1; IoTString *iKeyA; IoTString *iValueA; void setup() { // TODO: If you want to use the Serial library, // please install "screen" on your machine and run // it on the serial port right after flashing the // firmware onto the Particle board. // e.g. sudo screen /dev/ttyACM0 // IoTCloud library timer = TimingSingleton_getInstance(); foundError = false; transStatusList = new MyVector(); IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"); IoTString *password = new IoTString("reallysecret"); t1 = new Table(baseurl, password, 321, -1); t1->rebuild(); baseurl->releaseRef(); password->releaseRef(); // PietteTech DHT library DHTnextSampleTime = 0; // Start the first sample immediately } // This function gets and prints sensor status for debugging void getSensorStatus(int result) { Serial.print("Read sensor: "); switch (result) { case DHTLIB_OK: Serial.println("OK"); break; case DHTLIB_ERROR_CHECKSUM: Serial.println("Error\n\r\tChecksum error"); break; case DHTLIB_ERROR_ISR_TIMEOUT: Serial.println("Error\n\r\tISR time out error"); break; case DHTLIB_ERROR_RESPONSE_TIMEOUT: Serial.println("Error\n\r\tResponse time out error"); break; case DHTLIB_ERROR_DATA_TIMEOUT: Serial.println("Error\n\r\tData time out error"); break; case DHTLIB_ERROR_ACQUIRING: Serial.println("Error\n\r\tAcquiring"); break; case DHTLIB_ERROR_DELTA: Serial.println("Error\n\r\tDelta time to small"); break; case DHTLIB_ERROR_NOTSTARTED: Serial.println("Error\n\r\tNot started"); break; default: Serial.println("Unknown error"); break; } } // This wrapper is in charge of calling // must be defined like this for the library to work void dht_wrapper() { DHT.isrCallback(); } void loop() { // Wait until sensor is ready delay(2000); // Check if we need to start the next sample if (millis() > DHTnextSampleTime) { if (!bDHTstarted) { // Start the sample DHT.acquire(); bDHTstarted = true; } // Has sample completed? if (!DHT.acquiring()) { // TODO: get DHT status (for debugging) //int result = DHT.getStatus(); //getSensorStatus(result); // Key sprintf(keyBuffer, "sensor0"); iKeyA = new IoTString(keyBuffer); float tempF = DHT.getFahrenheit(); // Do updates for the temperature sprintf(dataBuffer, "%f", tempF); iValueA = new IoTString(dataBuffer); t1->startTransaction(); t1->put(iKeyA, iValueA); transStatusList->add(t1->commitTransaction()); t1->update(); iKeyA->releaseRef(); iValueA->releaseRef(); System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME); } } }