From 127f4ea69c8bcfb68fe6ebf8c9f3809fec619dc2 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Tue, 17 Apr 2018 09:43:43 -0700 Subject: [PATCH] Fixing and completing the generic firmware for sensor reading (temperature and humidity) and storing the values on the cloud; we need to use Init.C in iotcloud/version2/src/C to initialize the table and Read.java (in iotcloud/version2/src/java/simple_test). --- version2/src/C/Sensor-Arduino.ino | 69 ++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 16 deletions(-) diff --git a/version2/src/C/Sensor-Arduino.ino b/version2/src/C/Sensor-Arduino.ino index 22d9105..6f5e3cf 100644 --- a/version2/src/C/Sensor-Arduino.ino +++ b/version2/src/C/Sensor-Arduino.ino @@ -8,9 +8,10 @@ // Arduino #define DHTPIN 2 // Data pin #define PWRPIN D5 // Power pin +#define ERRPIN D7 // Error pin #define DHTTYPE DHT22 // DHT 22 (AM2302) // IoTCloud -#define SLEEP_TIME 1800 // Sleep time in seconds +#define SLEEP_TIME 10 // Sleep time in seconds // Initialize DHT sensor for normal 16mhz Arduino DHT dht(DHTPIN, DHTTYPE); @@ -23,16 +24,21 @@ MyVector * transStatusList; char keyBuffer[80]; char dataBuffer[80]; Table *t1; -IoTString *iKeyA; -IoTString *iValueA; +int64_t machineId; 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 + // TODO: + // 1) Before running PHOTON's with this firmware, + // please go to the master branch and run Init.C + // to initialize a table on the cloud side. + // If you want to read the committed keys and values + // on the cloud, then you can use Read.java. + // 2) 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 // We use one I/O pin to power the sensor so // that we can make it go to sleep when we go into @@ -51,8 +57,11 @@ void setup() { // Serial.printf("%02x%s", mac[i], i != 5 ? ":" : ""); //} //Serial.println(); + // Prepare machine ID + int64_t mac4 = (int64_t) mac[4]; + int64_t mac5 = (int64_t) mac[5]; + machineId = (mac4 * 256) + mac5; - int64_t machineId = (int64_t) mac[4] + mac[5]; // IoTCloud library timer = TimingSingleton_getInstance(); foundError = false; @@ -73,27 +82,55 @@ void loop() { // Wait until sensor is ready delay(2000); + // Read humidity + float humid = dht.readHumidity(); // Read temperature as Fahrenheit float tempF = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(tempF)) return; - + // Humidity // Key - sprintf(keyBuffer, "sensor0"); - iKeyA = new IoTString(keyBuffer); + sprintf(keyBuffer, "humid%d", machineId); + IoTString * iKeyHumid = new IoTString(keyBuffer); + // Do updates for the temperature + sprintf(dataBuffer, "%f", humid); + IoTString * iValueHumid = new IoTString(dataBuffer); + // Check and create a new key if it isn't created yet + t1->createNewKey(iKeyHumid, machineId); + t1->startTransaction(); + t1->put(iKeyHumid, iValueHumid); + transStatusList->add(t1->commitTransaction()); + + // Temperature + // Key + sprintf(keyBuffer, "tempF%d", machineId); + IoTString * iKeyTempF = new IoTString(keyBuffer); // Do updates for the temperature sprintf(dataBuffer, "%f", tempF); - iValueA = new IoTString(dataBuffer); + IoTString * iValueTempF = new IoTString(dataBuffer); + // Check and create a new key if it isn't created yet + t1->createNewKey(iKeyTempF, machineId); t1->startTransaction(); - t1->put(iKeyA, iValueA); + t1->put(iKeyTempF, iValueTempF); transStatusList->add(t1->commitTransaction()); t1->update(); - iKeyA->releaseRef(); - iValueA->releaseRef(); + iKeyHumid->releaseRef(); + iValueHumid->releaseRef(); + iKeyTempF->releaseRef(); + iValueTempF->releaseRef(); + + for (uint i = 0; i < transStatusList->size(); i++) { + TransactionStatus * status = transStatusList->get(i); + if (status->getStatus() != TransactionStatus_StatusCommitted) { + foundError = true; + digitalWrite(ERRPIN, HIGH); // Turn on LED upon error + } + delete status; + } // Turn off sensor digitalWrite(PWRPIN, LOW); -- 2.34.1