3 #include "TimingSingleton.h"
4 #include "TransactionStatus.h"
9 #define DHTPIN 2 // Data pin
10 #define PWRPIN D5 // Power pin
11 #define ERRPIN D7 // Error pin
12 #define DHTTYPE DHT22 // DHT 22 (AM2302)
14 #define SLEEP_TIME 10 // Sleep time in seconds
16 // Initialize DHT sensor for normal 16mhz Arduino
17 DHT dht(DHTPIN, DHTTYPE);
21 TimingSingleton *timer;
23 MyVector<TransactionStatus *> * transStatusList;
32 // 1) Before running PHOTON's with this firmware,
33 // please go to the master branch and run Init.C
34 // to initialize a table on the cloud side.
35 // If you want to read the committed keys and values
36 // on the cloud, then you can use Read.java.
37 // 2) 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
43 // We use one I/O pin to power the sensor so
44 // that we can make it go to sleep when we go into
46 pinMode(PWRPIN, OUTPUT);
48 digitalWrite(PWRPIN, HIGH);
50 // Prepare device key from MAC (just last 2 of 6 digits)
54 // TODO: Uncomment the following block to print MAC
56 //for (int i=0; i<6; i++) {
57 // Serial.printf("%02x%s", mac[i], i != 5 ? ":" : "");
61 int64_t mac4 = (int64_t) mac[4];
62 int64_t mac5 = (int64_t) mac[5];
63 machineId = (mac4 * 256) + mac5;
66 timer = TimingSingleton_getInstance();
68 transStatusList = new MyVector<TransactionStatus *>();
69 IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
70 IoTString *password = new IoTString("reallysecret");
71 t1 = new Table(baseurl, password, machineId, -1);
74 baseurl->releaseRef();
75 password->releaseRef();
83 // Wait until sensor is ready
86 float humid = dht.readHumidity();
87 // Read temperature as Fahrenheit
88 float tempF = dht.readTemperature(true);
89 // Check if any reads failed and exit early (to try again).
94 sprintf(keyBuffer, "humid%d", machineId);
95 IoTString * iKeyHumid = new IoTString(keyBuffer);
96 // Do updates for the temperature
97 sprintf(dataBuffer, "%f", humid);
98 IoTString * iValueHumid = new IoTString(dataBuffer);
100 // Check and create a new key if it isn't created yet
101 t1->createNewKey(iKeyHumid, machineId);
102 t1->startTransaction();
103 t1->put(iKeyHumid, iValueHumid);
104 transStatusList->add(t1->commitTransaction());
108 sprintf(keyBuffer, "tempF%d", machineId);
109 IoTString * iKeyTempF = new IoTString(keyBuffer);
110 // Do updates for the temperature
111 sprintf(dataBuffer, "%f", tempF);
112 IoTString * iValueTempF = new IoTString(dataBuffer);
114 // Check and create a new key if it isn't created yet
115 t1->createNewKey(iKeyTempF, machineId);
116 t1->startTransaction();
117 t1->put(iKeyTempF, iValueTempF);
118 transStatusList->add(t1->commitTransaction());
121 iKeyHumid->releaseRef();
122 iValueHumid->releaseRef();
123 iKeyTempF->releaseRef();
124 iValueTempF->releaseRef();
126 for (uint i = 0; i < transStatusList->size(); i++) {
127 TransactionStatus * status = transStatusList->get(i);
128 if (status->getStatus() != TransactionStatus_StatusCommitted) {
130 digitalWrite(ERRPIN, HIGH); // Turn on LED upon error
136 digitalWrite(PWRPIN, LOW);
137 System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);