3 #include "TimingSingleton.h"
4 #include "TransactionStatus.h"
7 SYSTEM_MODE(SEMI_AUTOMATIC)
12 #define DHTPIN 2 // Data pin
13 #define PWRPIN D5 // Power pin
14 #define ERRPIN D7 // Error pin
15 #define DHTTYPE DHT22 // DHT 22 (AM2302)
17 #define SLEEP_TIME 30 // Sleep time in seconds
18 #define RETRY_SLEEP_TIME 15 // Sleep time in seconds
19 #define RETRY_TIME 10000 // stop trying after 10 seconds
21 // Initialize DHT sensor for normal 16mhz Arduino
22 DHT dht(DHTPIN, DHTTYPE);
26 TimingSingleton *timer;
28 MyVector<TransactionStatus *> * transStatusList;
36 // 1) Before running PHOTON's with this firmware,
37 // please go to the master branch and run Init.C
38 // in iotcloud/version2/src/C to initialize
39 // a table on the cloud side.
40 // If you want to read the committed keys and values
41 // on the cloud, then you can use Read.java in
42 // iotcloud/version2/src/java/simple_test.
43 // 2) If you want to use the Serial library,
44 // please install "screen" on your machine and run
45 // it on the serial port right after flashing the
46 // firmware onto the Particle board.
47 // e.g. sudo screen /dev/ttyACM0
49 // We use one I/O pin to power the sensor so
50 // that we can make it go to sleep when we go into
55 //Serial.print("Time begin setup: ");
56 //Serial.println(micros());
58 pinMode(PWRPIN, OUTPUT);
59 digitalWrite(PWRPIN, HIGH);
64 // Wait for a maximum amount of time - sleep if WiFi is not connected
65 // Wake up and try again afterwards
66 if (!waitFor(Particle.connected, RETRY_TIME))
67 System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME);
69 // Prepare device key from MAC (just last 2 of 6 digits)
73 // TODO: Uncomment the following block to print MAC
75 //for (int i=0; i<6; i++) {
76 // Serial.printf("%02x%s", mac[i], i != 5 ? ":" : "");
80 int64_t mac4 = (int64_t) mac[4];
81 int64_t mac5 = (int64_t) mac[5];
82 machineId = (mac4 * 256) + mac5;
85 timer = TimingSingleton_getInstance();
87 transStatusList = new MyVector<TransactionStatus *>();
88 IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
89 IoTString *password = new IoTString("reallysecret");
91 //Serial.print("Time begin rebuilding table: ");
92 //Serial.println(micros());
94 t1 = new Table(baseurl, password, machineId, -1);
97 baseurl->releaseRef();
98 password->releaseRef();
100 //Serial.print("Time end setup: ");
101 //Serial.println(micros());
105 // Wait until sensor is ready
107 //Serial.print("Time begin loop: ");
108 //Serial.println(micros());
110 float humid = dht.readHumidity();
111 // Read temperature as Fahrenheit
112 float tempF = dht.readTemperature(true);
113 // Check if any reads failed and exit early (to try again).
118 sprintf(keyBuffer, "humid%d", machineId);
119 IoTString * iKeyHumid = new IoTString(keyBuffer);
120 // Do updates for the temperature
121 sprintf(dataBuffer, "%f", humid);
122 IoTString * iValueHumid = new IoTString(dataBuffer);
124 // Check and create a new key if it isn't created yet
125 t1->createNewKey(iKeyHumid, machineId);
126 t1->startTransaction();
127 t1->put(iKeyHumid, iValueHumid);
128 transStatusList->add(t1->commitTransaction());
132 sprintf(keyBuffer, "tempF%d", machineId);
133 IoTString * iKeyTempF = new IoTString(keyBuffer);
134 // Do updates for the temperature
135 sprintf(dataBuffer, "%f", tempF);
136 IoTString * iValueTempF = new IoTString(dataBuffer);
138 // Check and create a new key if it isn't created yet
139 t1->createNewKey(iKeyTempF, machineId);
140 t1->startTransaction();
141 t1->put(iKeyTempF, iValueTempF);
142 transStatusList->add(t1->commitTransaction());
145 iKeyHumid->releaseRef();
146 iValueHumid->releaseRef();
147 iKeyTempF->releaseRef();
148 iValueTempF->releaseRef();
150 for (uint i = 0; i < transStatusList->size(); i++) {
151 TransactionStatus * status = transStatusList->get(i);
152 if (status->getStatus() != TransactionStatus_StatusCommitted) {
154 digitalWrite(ERRPIN, HIGH); // Turn on LED upon error
160 digitalWrite(PWRPIN, LOW);
162 //Serial.print("Time end loop: ");
163 //Serial.println(micros());
165 System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);