From 0ae32c0609f51bb0cf4a65533967ba65613a3e24 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Fri, 11 May 2018 09:25:06 -0700 Subject: [PATCH] Completing firmware for the temperature/humidity, IR, and magnetic sensors for both Particle and Fidelius clouds. --- version2/src/C/Makefile | 17 +++ version2/src/C/README.txt | 6 + .../src/C/Sensor-Arduino_Photon-Cloud.ino | 117 ------------------ .../IR-Sensor/IR-Sensor_Particle-Cloud.ino | 5 + .../Magnetic-Sensor_Particle-Cloud.ino | 5 + version2/src/others/RPi/LightsController.ino | 40 +++++- version2/src/others/RPi/Makefile | 2 - .../IR-Sensor/IR-Sensor_Fidelius-Cloud.ino | 15 ++- .../IR-Sensor/IR-Sensor_Particle-Cloud.ino | 11 +- .../Magnetic-Sensor_Fidelius-Cloud.ino | 106 ++++++++++++++++ .../Magnetic-Sensor_Particle-Cloud.ino | 46 +++++++ .../Temp-Sensor_Fidelius-Cloud.ino | 21 ++-- .../Temp-Sensor_Particle-Cloud.ino | 2 +- 13 files changed, 252 insertions(+), 141 deletions(-) create mode 100644 version2/src/C/README.txt delete mode 100644 version2/src/C/Sensor-Arduino_Photon-Cloud.ino create mode 100644 version2/src/others/ino/Magnetic-Sensor/Magnetic-Sensor_Fidelius-Cloud.ino create mode 100644 version2/src/others/ino/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino diff --git a/version2/src/C/Makefile b/version2/src/C/Makefile index ab535de..ba09b7a 100644 --- a/version2/src/C/Makefile +++ b/version2/src/C/Makefile @@ -37,6 +37,23 @@ flash6: flash7: particle flash IoT-7 photon_firmware*.bin +flash8: + particle flash IoT-8 photon_firmware*.bin + +flash9: + particle flash IoT-9 photon_firmware*.bin + +flash10: + particle flash IoT-10 photon_firmware*.bin + +flash11: + particle flash IoT-11 photon_firmware*.bin + +flash12: + particle flash IoT-12 photon_firmware*.bin + +flash13: + particle flash IoT-13 photon_firmware*.bin PHONY += clean clean: diff --git a/version2/src/C/README.txt b/version2/src/C/README.txt new file mode 100644 index 0000000..505a6ae --- /dev/null +++ b/version2/src/C/README.txt @@ -0,0 +1,6 @@ + +- This is the codebase for Fidelius cloud application that we build on Particle (PHOTON) devices. + +- We can replace the .ino file with any of .ino files for Fidelius cloud from the iotcloud/version2/src/others/ino directory. + +- INO files that only need Particle API's can be compiled separately and it does not have to be together with this Fidelius API source code, unless it needs some other library, e.g. DHT library. diff --git a/version2/src/C/Sensor-Arduino_Photon-Cloud.ino b/version2/src/C/Sensor-Arduino_Photon-Cloud.ino deleted file mode 100644 index 742ccc8..0000000 --- a/version2/src/C/Sensor-Arduino_Photon-Cloud.ino +++ /dev/null @@ -1,117 +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 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/IR-Sensor/IR-Sensor_Particle-Cloud.ino b/version2/src/others/IR-Sensor/IR-Sensor_Particle-Cloud.ino index 1e74a7a..c51c30f 100644 --- a/version2/src/others/IR-Sensor/IR-Sensor_Particle-Cloud.ino +++ b/version2/src/others/IR-Sensor/IR-Sensor_Particle-Cloud.ino @@ -36,6 +36,11 @@ void loop() { sprintf(dataBuffer, "%s -> motion-detected", Time.timeStr().c_str()); Particle.publish(keyBuffer, dataBuffer); delay(1000); + + // TODO: Profiling + //String strTime(micros()); + //Particle.publish("Time motion sensor", strTime.c_str()); + //while(true); System.sleep(SLEEP_MODE_DEEP, 0); } diff --git a/version2/src/others/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino b/version2/src/others/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino index 3f4c9c8..855c567 100644 --- a/version2/src/others/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino +++ b/version2/src/others/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino @@ -37,5 +37,10 @@ void loop() { delay(1000); + // TODO: Profiling + //String strTime(micros()); + //Particle.publish("Time motion sensor", strTime.c_str()); + //while(true); + System.sleep(SLEEP_MODE_DEEP, 0); } diff --git a/version2/src/others/RPi/LightsController.ino b/version2/src/others/RPi/LightsController.ino index b47d15d..a5fccef 100644 --- a/version2/src/others/RPi/LightsController.ino +++ b/version2/src/others/RPi/LightsController.ino @@ -43,8 +43,15 @@ void setup() llb2->init(); pinMode(led1, OUTPUT); - Particle.function("lifx1",lifx1Toggle); - Particle.function("lifx2",lifx2Toggle); + // Argument needed ("on" or "off") for these functions + //Particle.function("lifx1",lifxb1Toggle); + //Particle.function("lifx2",lifxb2Toggle); + + // Argument not needed for these functions + Particle.function("turnOn_1",turnOnLiFX_1); + Particle.function("turnOff_1",turnOffLiFX_1); + Particle.function("turnOn_2",turnOnLiFX_2); + Particle.function("turnOff_2",turnOffLiFX_2); } @@ -84,3 +91,32 @@ int lifx2Toggle(String command) { return -1; } } + + +int turnOnLiFX_1(String command) { + + llb1->turnOn(); + return 1; +} + + +int turnOffLiFX_1(String command) { + + llb1->turnOff(); + return 1; +} + + +int turnOnLiFX_2(String command) { + + llb2->turnOn(); + return 1; +} + + +int turnOffLiFX_2(String command) { + + llb2->turnOff(); + return 1; +} + diff --git a/version2/src/others/RPi/Makefile b/version2/src/others/RPi/Makefile index 948d8e1..b5d2c53 100644 --- a/version2/src/others/RPi/Makefile +++ b/version2/src/others/RPi/Makefile @@ -16,8 +16,6 @@ compile-c: compile: rm -f *.bin particle compile raspberry-pi - #particle compile photon - flash: particle flash Pi-1 raspberry-pi*.bin diff --git a/version2/src/others/ino/IR-Sensor/IR-Sensor_Fidelius-Cloud.ino b/version2/src/others/ino/IR-Sensor/IR-Sensor_Fidelius-Cloud.ino index 84831d0..cf27477 100644 --- a/version2/src/others/ino/IR-Sensor/IR-Sensor_Fidelius-Cloud.ino +++ b/version2/src/others/ino/IR-Sensor/IR-Sensor_Fidelius-Cloud.ino @@ -29,6 +29,7 @@ char keyBuffer[80]; char dataBuffer[80]; Table *t1; int64_t machineId; +String wakeupTime; // Setting up pins for input void setup() { @@ -46,10 +47,11 @@ void setup() { // firmware onto the Particle board. // e.g. sudo screen /dev/ttyACM0 + // Record the time when waking up! + wakeupTime = Time.timeStr(); + // TODO: Profiling! //Serial.begin(); - //Serial.print("Time begin setup: "); - //Serial.println(micros()); // Connect only to WiFi WiFi.on(); @@ -88,8 +90,8 @@ void loop() { // Machine ID sprintf(keyBuffer, "ir%04x", machineId); IoTString * iKey = new IoTString(keyBuffer); - // Do updates for the humidity - sprintf(dataBuffer, "motion-detected"); + // Do updates for the motion detection + sprintf(dataBuffer, "%s -> motion-detected", wakeupTime.c_str()); IoTString * iValue = new IoTString(dataBuffer); // Check and create a new key if it isn't created yet @@ -99,8 +101,9 @@ void loop() { transStatusList->add(t1->commitTransaction()); // TODO: Profiling - //Serial.print("Time end loop: "); + //Serial.print("Elapsed time: "); //Serial.println(micros()); - //while(true) { } + //while(true); + System.sleep(SLEEP_MODE_DEEP, 0); } diff --git a/version2/src/others/ino/IR-Sensor/IR-Sensor_Particle-Cloud.ino b/version2/src/others/ino/IR-Sensor/IR-Sensor_Particle-Cloud.ino index c3be4ab..c51c30f 100644 --- a/version2/src/others/ino/IR-Sensor/IR-Sensor_Particle-Cloud.ino +++ b/version2/src/others/ino/IR-Sensor/IR-Sensor_Particle-Cloud.ino @@ -8,10 +8,6 @@ // // Based on tutorial: http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/arduino-hc-sr501-motion-sensor-tutorial/ -SYSTEM_MODE(SEMI_AUTOMATIC) -SYSTEM_THREAD(ENABLED) - -int pirValue; // Place to store read PIR Value int64_t machineId; // Key and data char keyBuffer[80]; @@ -37,9 +33,14 @@ void loop() { // Machine ID sprintf(keyBuffer, "%04x", machineId); // Motion detection - sprintf(dataBuffer, "motion-detected"); + sprintf(dataBuffer, "%s -> motion-detected", Time.timeStr().c_str()); Particle.publish(keyBuffer, dataBuffer); delay(1000); + + // TODO: Profiling + //String strTime(micros()); + //Particle.publish("Time motion sensor", strTime.c_str()); + //while(true); System.sleep(SLEEP_MODE_DEEP, 0); } diff --git a/version2/src/others/ino/Magnetic-Sensor/Magnetic-Sensor_Fidelius-Cloud.ino b/version2/src/others/ino/Magnetic-Sensor/Magnetic-Sensor_Fidelius-Cloud.ino new file mode 100644 index 0000000..e9afd62 --- /dev/null +++ b/version2/src/others/ino/Magnetic-Sensor/Magnetic-Sensor_Fidelius-Cloud.ino @@ -0,0 +1,106 @@ +// ------------------------------------------------ +// Controlling HC-SR501 through Particle Cloud +// @author Rahmadi Trimananda - UC Irvine +// ------------------------------------------------ +// HC-SR501 Motion Detector +// Basically, this simple code detects motion and send the occurrences to the cloud +// We use the WKP port (port A7) to wake up the device when there is motion +// +// Based on tutorial: http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/arduino-hc-sr501-motion-sensor-tutorial/ +#include "Table.h" +#include "IoTString.h" +#include "TimingSingleton.h" +#include "TransactionStatus.h" + +// 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 + +SYSTEM_MODE(SEMI_AUTOMATIC) +SYSTEM_THREAD(ENABLED) + +// Globals +// IoTCloud +bool foundError; +MyVector * transStatusList; +char keyBuffer[80]; +char dataBuffer[80]; +Table *t1; +int64_t machineId; +String wakeupTime; + +// Setting up pins for input +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 + + // Record the time when waking up! + wakeupTime = Time.timeStr(); + + // TODO: Profiling! + //Serial.begin(); + + // 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); + + // Get the MAC address + byte mac[6]; + WiFi.macAddress(mac); + // Prepare machine ID + int64_t mac4 = (int64_t) mac[4]; + int64_t mac5 = (int64_t) mac[5]; + machineId = (mac4 * 256) + mac5; + + IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"); + IoTString *password = new IoTString("reallysecret"); + + t1 = new Table(baseurl, password, machineId, -1); + t1->rebuild(); + + baseurl->releaseRef(); + password->releaseRef(); +} + +void loop() { + + // Machine ID + sprintf(keyBuffer, "mag%04x", machineId); + IoTString * iKey = new IoTString(keyBuffer); + // Do updates for the magnetic action + sprintf(dataBuffer, "%s -> closing-door-detected", wakeupTime.c_str()); + IoTString * iValue = new IoTString(dataBuffer); + + // Check and create a new key if it isn't created yet + t1->createNewKey(iKey, machineId); + t1->startTransaction(); + t1->put(iKey, iValue); + transStatusList->add(t1->commitTransaction()); + + // TODO: Profiling + //Serial.print("Elapsed time: "); + //Serial.println(micros()); + //while(true); + + System.sleep(SLEEP_MODE_DEEP, 0); +} diff --git a/version2/src/others/ino/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino b/version2/src/others/ino/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino new file mode 100644 index 0000000..855c567 --- /dev/null +++ b/version2/src/others/ino/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino @@ -0,0 +1,46 @@ +// ------------------------------------------------ +// Controlling Arduino EK1656 through cloud +// @author Rahmadi Trimananda - UC Irvine +// ------------------------------------------------ +// Gikfun MC-38 - Arduino EK1656 (magnetic sensor) +// Basically, this simple code detects sensor opening and send the occurrences to the cloud +// We use the WKP port (port A7) to wake up the device when there is motion +// + +int64_t machineId; +// Key and data +char keyBuffer[80]; +char dataBuffer[80]; + +// Setting up pins for input +void setup() { + + // Get the MAC address + byte mac[6]; + WiFi.macAddress(mac); + // Prepare machine ID + int64_t mac4 = (int64_t) mac[4]; + int64_t mac5 = (int64_t) mac[5]; + machineId = (mac4 * 256) + mac5; +} + +void loop() { + + // Basically delay to give time to send updates to Particle cloud. + // If we do less than this then it would fail publishing. + delay(2000); + // Machine ID + sprintf(keyBuffer, "%04x", machineId); + // Motion detection + sprintf(dataBuffer, "%s -> closing-door-detected", Time.timeStr().c_str()); + Particle.publish(keyBuffer, dataBuffer); + + delay(1000); + + // TODO: Profiling + //String strTime(micros()); + //Particle.publish("Time motion sensor", strTime.c_str()); + //while(true); + + System.sleep(SLEEP_MODE_DEEP, 0); +} diff --git a/version2/src/others/ino/Temp-Sensor/Temp-Sensor_Fidelius-Cloud.ino b/version2/src/others/ino/Temp-Sensor/Temp-Sensor_Fidelius-Cloud.ino index c35a2bb..3e4441e 100644 --- a/version2/src/others/ino/Temp-Sensor/Temp-Sensor_Fidelius-Cloud.ino +++ b/version2/src/others/ino/Temp-Sensor/Temp-Sensor_Fidelius-Cloud.ino @@ -63,9 +63,9 @@ 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(); @@ -129,6 +129,9 @@ void loop() { // Check if any reads failed and exit early (to try again). if (isnan(humid) || isnan(tempF)) return; + + // TODO: Update values separately to different keys + /* // Humidity // Key sprintf(keyBuffer, "h%04x", machineId); @@ -162,12 +165,13 @@ void loop() { iValueHumid->releaseRef(); iKeyTempF->releaseRef(); iValueTempF->releaseRef(); + */ // TODO: Collapse temperature and humidity into one key -/* sprintf(keyBuffer, "%04x", machineId); + sprintf(keyBuffer, "humtemp%04x", machineId); IoTString * iKey = new IoTString(keyBuffer); // Do updates for the temperature - sprintf(dataBuffer, "%0.2f%0.2f", humid, tempF); + sprintf(dataBuffer, "%0.2f-%0.2f", humid, tempF); IoTString * iValue = new IoTString(dataBuffer); t1->createNewKey(iKey, machineId); @@ -176,7 +180,7 @@ void loop() { transStatusList->add(t1->commitTransaction()); iKey->releaseRef(); - iValue->releaseRef();*/ + iValue->releaseRef(); /*for (uint i = 0; i < transStatusList->size(); i++) { TransactionStatus * status = transStatusList->get(i); @@ -191,9 +195,10 @@ void loop() { digitalWrite(PWRPIN, LOW); // TODO: Profiling - //Serial.print("Time end loop: "); + //Serial.print("Elapsed time: "); //Serial.println(micros()); - //while(true) { } + //while(true); + System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME); } diff --git a/version2/src/others/ino/Temp-Sensor/Temp-Sensor_Particle-Cloud.ino b/version2/src/others/ino/Temp-Sensor/Temp-Sensor_Particle-Cloud.ino index 9674a47..ce216c4 100644 --- a/version2/src/others/ino/Temp-Sensor/Temp-Sensor_Particle-Cloud.ino +++ b/version2/src/others/ino/Temp-Sensor/Temp-Sensor_Particle-Cloud.ino @@ -91,7 +91,7 @@ void loop() { return; // Machine ID - sprintf(keyBuffer, "%04x", machineId); + sprintf(keyBuffer, "humtemp%04x", machineId); // Humidity + Temperature sprintf(dataBuffer, "%0.2f-%0.2f", humid, tempF); -- 2.34.1