From 67ae127cfc25efd2e9351c7ea1676ac24e38246c Mon Sep 17 00:00:00 2001 From: rtrimana Date: Tue, 8 May 2018 09:44:04 -0700 Subject: [PATCH] Publishing sensor values on PHOTON cloud instead of using Fidelius---needs more delay for their cloud. --- .../src/C/Sensor-Arduino_Photon-Cloud.ino | 117 ++++++++++++++++++ ...-Arduino.ino => Sensor-Arduino-simple.ino} | 0 .../ino/Sensor-Arduino_Fidelius.ino} | 44 ++++--- 3 files changed, 145 insertions(+), 16 deletions(-) create mode 100644 version2/src/C/Sensor-Arduino_Photon-Cloud.ino rename version2/src/others/ino/{Sensor-Arduino.ino => Sensor-Arduino-simple.ino} (100%) rename version2/src/{C/Sensor-Arduino.ino => others/ino/Sensor-Arduino_Fidelius.ino} (83%) 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.ino b/version2/src/others/ino/Sensor-Arduino-simple.ino similarity index 100% rename from version2/src/others/ino/Sensor-Arduino.ino rename to version2/src/others/ino/Sensor-Arduino-simple.ino diff --git a/version2/src/C/Sensor-Arduino.ino b/version2/src/others/ino/Sensor-Arduino_Fidelius.ino similarity index 83% rename from version2/src/C/Sensor-Arduino.ino rename to version2/src/others/ino/Sensor-Arduino_Fidelius.ino index 2521bae..c35a2bb 100644 --- a/version2/src/C/Sensor-Arduino.ino +++ b/version2/src/others/ino/Sensor-Arduino_Fidelius.ino @@ -14,8 +14,8 @@ SYSTEM_THREAD(ENABLED) #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 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 @@ -24,7 +24,6 @@ DHT dht(DHTPIN, DHTTYPE); // Globals // IoTCloud -TimingSingleton *timer; bool foundError; MyVector * transStatusList; char keyBuffer[80]; @@ -64,20 +63,19 @@ void setup() { // 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); +// 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()); + 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) - // Delays are in millis but micros() returns microsecond time delay((CONNECTION_DELAY - micros())/1000); // TODO: Profiling WiFi @@ -100,9 +98,8 @@ void setup() { //machineId = (int64_t) mac[5]; // Shorter version of machine ID // IoTCloud library - timer = TimingSingleton_getInstance(); - foundError = false; - transStatusList = new MyVector(); + //foundError = false; + //transStatusList = new MyVector(); IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"); IoTString *password = new IoTString("reallysecret"); // TODO: Profiling @@ -130,13 +127,13 @@ void loop() { // Read temperature as Fahrenheit float tempF = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). - if (isnan(tempF)) + if (isnan(humid) || isnan(tempF)) return; // Humidity // Key sprintf(keyBuffer, "h%04x", machineId); IoTString * iKeyHumid = new IoTString(keyBuffer); - // Do updates for the temperature + // Do updates for the humidity sprintf(dataBuffer, "%0.2f", humid); IoTString * iValueHumid = new IoTString(dataBuffer); @@ -166,14 +163,29 @@ void loop() { iKeyTempF->releaseRef(); iValueTempF->releaseRef(); - for (uint i = 0; i < transStatusList->size(); i++) { + // 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); -- 2.34.1