From: rtrimana Date: Tue, 8 May 2018 16:44:04 +0000 (-0700) Subject: Publishing sensor values on PHOTON cloud instead of using Fidelius---needs more delay... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=67ae127cfc25efd2e9351c7ea1676ac24e38246c;p=iotcloud.git Publishing sensor values on PHOTON cloud instead of using Fidelius---needs more delay for their cloud. --- diff --git a/version2/src/C/Sensor-Arduino.ino b/version2/src/C/Sensor-Arduino.ino deleted file mode 100644 index 2521bae..0000000 --- a/version2/src/C/Sensor-Arduino.ino +++ /dev/null @@ -1,187 +0,0 @@ -#include "Table.h" -#include "IoTString.h" -#include "TimingSingleton.h" -#include "TransactionStatus.h" -#include "DHT.h" - -SYSTEM_MODE(SEMI_AUTOMATIC) -SYSTEM_THREAD(ENABLED) - -// System defines -// 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 120 // Sleep time in seconds -#define RETRY_SLEEP_TIME 15 // Sleep time in seconds -#define RETRY_TIME 10000 // stop trying after 10 seconds -#define CONNECTION_DELAY 2100000 // Need to delay after connecting WiFi to wait for sensor - -// 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; -int64_t machineId; - -void setup() { - // TODO: - // 1) Before running PHOTON's with this firmware, - // please go to the master branch and run Init.C - // in iotcloud/version2/src/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 in - // iotcloud/version2/src/java/simple_test. - // 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 - // deep sleep. - - // TODO: Profiling! - //Serial.begin(); - //Serial.print("Time begin setup: "); - //Serial.println(micros()); - // Turn on sensor - pinMode(PWRPIN, OUTPUT); - digitalWrite(PWRPIN, HIGH); - // Arduino DHT - dht.begin(); - // Connect to WiFi and Particle cloud - // Wait for a maximum amount of time - sleep if WiFi is not connected - // Wake up and try again afterwards - // TODO: either use this or just WiFi connection (below) - //Particle.connect(); - //if (!waitFor(Particle.connected, RETRY_TIME)) - // System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME); - - // Connect only to WiFi - WiFi.on(); - WiFi.connect(); - if (!waitFor(WiFi.ready, RETRY_TIME)) - System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME); - // Check if we already got an IP from the router - while(!WiFi.localIP()); - // Add delay if needed (for the humidity sensor we need ~2s delay) - if (micros() < CONNECTION_DELAY) - // Delays are in millis but micros() returns microsecond time - delay((CONNECTION_DELAY - micros())/1000); - - // TODO: Profiling WiFi - //Serial.println(micros()); - //while(true) { } - - // 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 - //for (int i=0; i<6; i++) { - // 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; - //machineId = (int64_t) mac[5]; // Shorter version of machine ID - - // 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"); - // TODO: Profiling - //Serial.print("Time begin rebuilding table: "); - //Serial.println(micros()); - - t1 = new Table(baseurl, password, machineId, -1); - t1->rebuild(); - - baseurl->releaseRef(); - password->releaseRef(); - // TODO: Profiling - //Serial.print("Time end setup: "); - //Serial.println(micros()); -} - -void loop() { - // Wait until sensor is ready - //delay(2000); - // TODO: Profiling - //Serial.print("Time begin loop: "); - //Serial.println(micros()); - // 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, "h%04x", machineId); - IoTString * iKeyHumid = new IoTString(keyBuffer); - // Do updates for the temperature - sprintf(dataBuffer, "%0.2f", 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, "t%04x", machineId); - IoTString * iKeyTempF = new IoTString(keyBuffer); - // Do updates for the temperature - sprintf(dataBuffer, "%0.2f", tempF); - 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(iKeyTempF, iValueTempF); - transStatusList->add(t1->commitTransaction()); - t1->update(); - - 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); - - // TODO: Profiling - //Serial.print("Time end loop: "); - //Serial.println(micros()); - //while(true) { } - System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME); -} - diff --git a/version2/src/C/Sensor-Arduino_Photon-Cloud.ino b/version2/src/C/Sensor-Arduino_Photon-Cloud.ino new file mode 100644 index 0000000..742ccc8 --- /dev/null +++ b/version2/src/C/Sensor-Arduino_Photon-Cloud.ino @@ -0,0 +1,117 @@ +#include "Table.h" +#include "IoTString.h" +#include "TimingSingleton.h" +#include "TransactionStatus.h" +#include "DHT.h" + +SYSTEM_MODE(SEMI_AUTOMATIC) +SYSTEM_THREAD(ENABLED) + +// System defines +// 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 15 // Sleep time in seconds +#define RETRY_SLEEP_TIME 5 // Sleep time in seconds +#define RETRY_TIME 10000 // stop trying after 10 seconds +#define CONNECTION_DELAY 2100000 // Need to delay after connecting WiFi to wait for sensor + +// Initialize DHT sensor for normal 16mhz Arduino +DHT dht(DHTPIN, DHTTYPE); + +// Globals +// IoTCloud +bool foundError; +MyVector * transStatusList; +char keyBuffer[80]; +char dataBuffer[80]; +Table *t1; +int64_t machineId; + +void setup() { + // TODO: + // 1) This code reads from the sensor and publishes + // the results on the cloud. + // 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 + // deep sleep. + + // TODO: Profiling! + //Serial.begin(); + //Serial.print("Time begin setup: "); + //Serial.println(micros()); + // Turn on sensor + pinMode(PWRPIN, OUTPUT); + digitalWrite(PWRPIN, HIGH); + // Arduino DHT + dht.begin(); + // Connect to WiFi and Particle cloud + // Wait for a maximum amount of time - sleep if WiFi is not connected + // Wake up and try again afterwards + // TODO: either use this or just WiFi connection (below) + Particle.connect(); + if (!waitFor(Particle.connected, RETRY_TIME)) + System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME); + + // 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 + //for (int i=0; i<6; i++) { + // 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; + //machineId = (int64_t) mac[5]; // Shorter version of machine ID + + // TODO: Profiling + //Serial.print("Time end setup: "); + //Serial.println(micros()); +} + +void loop() { + // Wait until sensor is ready + delay(1500); + // TODO: Profiling + //Serial.print("Time begin loop: "); + //Serial.println(micros()); + // 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(humid) || isnan(tempF)) + return; + + // Machine ID + sprintf(keyBuffer, "%04x", machineId); + // Humidity + Temperature + sprintf(dataBuffer, "%0.2f-%0.2f", humid, tempF); + + // Publish on PHOTON's cloud + Particle.publish(keyBuffer, dataBuffer); + delay(1000); + + // Turn off sensor + digitalWrite(PWRPIN, LOW); + + // TODO: Profiling + //Serial.print("Time end loop: "); + //Serial.println(micros()); + //while(true) { } + System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME); +} + diff --git a/version2/src/others/ino/Sensor-Arduino-simple.ino b/version2/src/others/ino/Sensor-Arduino-simple.ino new file mode 100644 index 0000000..99bacdc --- /dev/null +++ b/version2/src/others/ino/Sensor-Arduino-simple.ino @@ -0,0 +1,91 @@ +#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); +} + diff --git a/version2/src/others/ino/Sensor-Arduino.ino b/version2/src/others/ino/Sensor-Arduino.ino deleted file mode 100644 index 99bacdc..0000000 --- a/version2/src/others/ino/Sensor-Arduino.ino +++ /dev/null @@ -1,91 +0,0 @@ -#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); -} - diff --git a/version2/src/others/ino/Sensor-Arduino_Fidelius.ino b/version2/src/others/ino/Sensor-Arduino_Fidelius.ino new file mode 100644 index 0000000..c35a2bb --- /dev/null +++ b/version2/src/others/ino/Sensor-Arduino_Fidelius.ino @@ -0,0 +1,199 @@ +#include "Table.h" +#include "IoTString.h" +#include "TimingSingleton.h" +#include "TransactionStatus.h" +#include "DHT.h" + +SYSTEM_MODE(SEMI_AUTOMATIC) +SYSTEM_THREAD(ENABLED) + +// System defines +// 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 15 // Sleep time in seconds +#define RETRY_SLEEP_TIME 5 // Sleep time in seconds +#define RETRY_TIME 10000 // stop trying after 10 seconds +#define CONNECTION_DELAY 2100000 // Need to delay after connecting WiFi to wait for sensor + +// Initialize DHT sensor for normal 16mhz Arduino +DHT dht(DHTPIN, DHTTYPE); + +// Globals +// IoTCloud +bool foundError; +MyVector * transStatusList; +char keyBuffer[80]; +char dataBuffer[80]; +Table *t1; +int64_t machineId; + +void setup() { + // TODO: + // 1) Before running PHOTON's with this firmware, + // please go to the master branch and run Init.C + // in iotcloud/version2/src/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 in + // iotcloud/version2/src/java/simple_test. + // 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 + // deep sleep. + + // TODO: Profiling! + //Serial.begin(); + //Serial.print("Time begin setup: "); + //Serial.println(micros()); + // Turn on sensor + pinMode(PWRPIN, OUTPUT); + digitalWrite(PWRPIN, HIGH); + // Arduino DHT + dht.begin(); + // Connect to WiFi and Particle cloud + // Wait for a maximum amount of time - sleep if WiFi is not connected + // Wake up and try again afterwards + // TODO: either use this or just WiFi connection (below) +// Particle.connect(); +// if (!waitFor(Particle.connected, RETRY_TIME)) +// System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME); + + // Connect only to WiFi + WiFi.on(); + WiFi.connect(); + if (!waitFor(WiFi.ready, RETRY_TIME)) + System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME); + while(!WiFi.localIP()); // Wait for valid IP + // Add delay if needed (for the humidity sensor we need ~2s delay) + // Delays are in millis but micros() returns microsecond time + if (micros() < CONNECTION_DELAY) + delay((CONNECTION_DELAY - micros())/1000); + + // TODO: Profiling WiFi + //Serial.println(micros()); + //while(true) { } + + // 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 + //for (int i=0; i<6; i++) { + // 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; + //machineId = (int64_t) mac[5]; // Shorter version of machine ID + + // IoTCloud library + //foundError = false; + //transStatusList = new MyVector(); + IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"); + IoTString *password = new IoTString("reallysecret"); + // TODO: Profiling + //Serial.print("Time begin rebuilding table: "); + //Serial.println(micros()); + + t1 = new Table(baseurl, password, machineId, -1); + t1->rebuild(); + + baseurl->releaseRef(); + password->releaseRef(); + // TODO: Profiling + //Serial.print("Time end setup: "); + //Serial.println(micros()); +} + +void loop() { + // Wait until sensor is ready + //delay(2000); + // TODO: Profiling + //Serial.print("Time begin loop: "); + //Serial.println(micros()); + // 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(humid) || isnan(tempF)) + return; + // Humidity + // Key + sprintf(keyBuffer, "h%04x", machineId); + IoTString * iKeyHumid = new IoTString(keyBuffer); + // Do updates for the humidity + sprintf(dataBuffer, "%0.2f", 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, "t%04x", machineId); + IoTString * iKeyTempF = new IoTString(keyBuffer); + // Do updates for the temperature + sprintf(dataBuffer, "%0.2f", tempF); + 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(iKeyTempF, iValueTempF); + transStatusList->add(t1->commitTransaction()); + t1->update(); + + iKeyHumid->releaseRef(); + iValueHumid->releaseRef(); + iKeyTempF->releaseRef(); + iValueTempF->releaseRef(); + + // TODO: Collapse temperature and humidity into one key +/* sprintf(keyBuffer, "%04x", machineId); + IoTString * iKey = new IoTString(keyBuffer); + // Do updates for the temperature + sprintf(dataBuffer, "%0.2f%0.2f", humid, tempF); + IoTString * iValue = new IoTString(dataBuffer); + + t1->createNewKey(iKey, machineId); + t1->startTransaction(); + t1->put(iKey, iValue); + transStatusList->add(t1->commitTransaction()); + + iKey->releaseRef(); + iValue->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); + + // TODO: Profiling + //Serial.print("Time end loop: "); + //Serial.println(micros()); + //while(true) { } + System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME); +} +