#include "Table.h" #include "IoTString.h" #include "TimingSingleton.h" #include "TransactionStatus.h" #include "DHT.h" // System defines // Arduino #define DHTPIN 2 // what pin we're connected to #define DHTTYPE DHT22 // DHT 22 (AM2302) // IoTCloud #define SLEEP_TIME 30 // Sleep time in seconds // Initialize DHT sensor for normal 16mhz Arduino DHT dht(DHTPIN, DHTTYPE); // Globals // 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 // Prepare device key from MAC (just last 2 of 6 digits) byte mac[6]; WiFi.macAddress(mac); // TODO: Uncomment the following block to print MAC //Serial.begin(); //for (int i=0; i<6; i++) { // Serial.printf("%02x%s", mac[i], i != 5 ? ":" : ""); //} //Serial.println(); int deviceKey = (int) mac[4] + mac[5]; // 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, deviceKey, -1); t1->rebuild(); baseurl->releaseRef(); password->releaseRef(); // Arduino DHT dht.begin(); } void loop() { // Wait until sensor is ready delay(2000); // Read temperature as Fahrenheit float tempF = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(tempF)) return; // Key sprintf(keyBuffer, "sensor0"); iKeyA = new IoTString(keyBuffer); // 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); }