--- /dev/null
+// ------------------------------------------------
+// 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/
+
+int pirValue; // Place to store read PIR Value
+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, "motion-detected");
+ Particle.publish(keyBuffer, dataBuffer);
+ delay(1000);
+
+ System.sleep(SLEEP_MODE_DEEP, 0);
+}
--- /dev/null
+include common.mk
+
+all: compile
+
+# To run Particle, we need to install Particle CLI
+# Please see:
+# https://docs.particle.io/guide/getting-started/connect/photon/#install-the-particle-cli
+monitor:
+ sudo chmod 666 /dev/ttyACM0
+ particle serial monitor
+
+compile:
+ rm -f *.bin
+ particle compile photon
+
+flash0:
+ particle flash IoT-0 photon*.bin
+
+flash1:
+ particle flash IoT-1 photon*.bin
+
+PHONY += clean
+clean:
+ rm -f *.bin
+
+PHONY += mrclean
+mrclean: clean
+ rm -rf ../docs
+
+tabbing:
+ uncrustify -c C.cfg --no-backup *.cpp
+ uncrustify -c C.cfg --no-backup *.h
+
+wc:
+ wc *.cpp *.h
+
+.PHONY: $(PHONY)
--- /dev/null
+# A few common Makefile items
+
+CC := gcc
+CXX := g++
+
+UNAME := $(shell uname)
+
+LIB_NAME := iotcloud
+LIB_SO := lib_$(LIB_NAME).so
+
+CPPFLAGS += -Wall -g -O0
+
+# Mac OSX options
+ifeq ($(UNAME), Darwin)
+CPPFLAGS += -D_XOPEN_SOURCE -DMAC
+endif
--- /dev/null
+/* DHT library
+
+MIT license
+written by Adafruit Industries
+*/
+
+#include "DHT.h"
+
+DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
+ _pin = pin;
+ _type = type;
+ _count = count;
+ firstreading = true;
+}
+
+void DHT::begin(void) {
+ // set up the pins!
+ pinMode(_pin, INPUT);
+ digitalWrite(_pin, HIGH);
+ _lastreadtime = 0;
+}
+
+//boolean S == Scale. True == Farenheit; False == Celcius
+float DHT::readTemperature(bool S) {
+ float f;
+
+ if (read()) {
+ switch (_type) {
+ case DHT11:
+ f = data[2];
+ if(S)
+ f = convertCtoF(f);
+
+ return f;
+ case DHT22:
+ case DHT21:
+ f = data[2] & 0x7F;
+ f *= 256;
+ f += data[3];
+ f /= 10;
+ if (data[2] & 0x80)
+ f *= -1;
+ if(S)
+ f = convertCtoF(f);
+
+ return f;
+ }
+ }
+ return NAN;
+}
+
+float DHT::convertCtoF(float c) {
+ return c * 9 / 5 + 32;
+}
+
+float DHT::convertFtoC(float f) {
+ return (f - 32) * 5 / 9;
+}
+
+float DHT::readHumidity(void) {
+ float f;
+ if (read()) {
+ switch (_type) {
+ case DHT11:
+ f = data[0];
+ return f;
+ case DHT22:
+ case DHT21:
+ f = data[0];
+ f *= 256;
+ f += data[1];
+ f /= 10;
+ return f;
+ }
+ }
+ return NAN;
+}
+
+float DHT::computeHeatIndex(float tempFahrenheit, float percentHumidity) {
+ // Adapted from equation at: https://github.com/adafruit/DHT-sensor-library/issues/9 and
+ // Wikipedia: http://en.wikipedia.org/wiki/Heat_index
+ return -42.379 +
+ 2.04901523 * tempFahrenheit +
+ 10.14333127 * percentHumidity +
+ -0.22475541 * tempFahrenheit*percentHumidity +
+ -0.00683783 * pow(tempFahrenheit, 2) +
+ -0.05481717 * pow(percentHumidity, 2) +
+ 0.00122874 * pow(tempFahrenheit, 2) * percentHumidity +
+ 0.00085282 * tempFahrenheit*pow(percentHumidity, 2) +
+ -0.00000199 * pow(tempFahrenheit, 2) * pow(percentHumidity, 2);
+}
+
+
+boolean DHT::read(void) {
+ uint8_t laststate = HIGH;
+ uint8_t counter = 0;
+ uint8_t j = 0, i;
+ unsigned long currenttime;
+
+ // Check if sensor was read less than two seconds ago and return early
+ // to use last reading.
+ currenttime = millis();
+ if (currenttime < _lastreadtime) {
+ // ie there was a rollover
+ _lastreadtime = 0;
+ }
+ if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
+ return true; // return last correct measurement
+ //delay(2000 - (currenttime - _lastreadtime));
+ }
+ firstreading = false;
+ /*
+ Serial.print("Currtime: "); Serial.print(currenttime);
+ Serial.print(" Lasttime: "); Serial.print(_lastreadtime);
+ */
+ _lastreadtime = millis();
+
+ data[0] = data[1] = data[2] = data[3] = data[4] = 0;
+
+ // pull the pin high and wait 250 milliseconds
+ digitalWrite(_pin, HIGH);
+ delay(250);
+
+ // now pull it low for ~20 milliseconds
+ pinMode(_pin, OUTPUT);
+ digitalWrite(_pin, LOW);
+ delay(20);
+ noInterrupts();
+ digitalWrite(_pin, HIGH);
+ delayMicroseconds(40);
+ pinMode(_pin, INPUT);
+
+ // read in timings
+ for ( i=0; i< MAXTIMINGS; i++) {
+ counter = 0;
+ while (digitalRead(_pin) == laststate) {
+ counter++;
+ delayMicroseconds(1);
+ if (counter == 255) {
+ break;
+ }
+ }
+ laststate = digitalRead(_pin);
+
+ if (counter == 255) break;
+
+ // ignore first 3 transitions
+ if ((i >= 4) && (i%2 == 0)) {
+ // shove each bit into the storage bytes
+ data[j/8] <<= 1;
+ if (counter > _count)
+ data[j/8] |= 1;
+ j++;
+ }
+
+ }
+
+ interrupts();
+
+ /*
+ Serial.println(j, DEC);
+ Serial.print(data[0], HEX); Serial.print(", ");
+ Serial.print(data[1], HEX); Serial.print(", ");
+ Serial.print(data[2], HEX); Serial.print(", ");
+ Serial.print(data[3], HEX); Serial.print(", ");
+ Serial.print(data[4], HEX); Serial.print(" =? ");
+ Serial.println(data[0] + data[1] + data[2] + data[3], HEX);
+ */
+
+ // check we read 40 bits and that the checksum matches
+ if ((j >= 40) &&
+ (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
+ return true;
+ }
+
+
+ return false;
+
+}
--- /dev/null
+#ifndef DHT_H\r
+#define DHT_H\r
+#if ARDUINO >= 100\r
+ #include "Arduino.h"\r
+#else\r
+ #include "WProgram.h"\r
+#endif\r
+\r
+/* DHT library \r
+\r
+MIT license\r
+written by Adafruit Industries\r
+*/\r
+\r
+// how many timing transitions we need to keep track of. 2 * number bits + extra\r
+#define MAXTIMINGS 85\r
+\r
+#define DHT11 11\r
+#define DHT22 22\r
+#define DHT21 21\r
+#define AM2301 21\r
+\r
+class DHT {\r
+ private:\r
+ uint8_t data[6];\r
+ uint8_t _pin, _type, _count;\r
+ unsigned long _lastreadtime;\r
+ boolean firstreading;\r
+\r
+ public:\r
+ DHT(uint8_t pin, uint8_t type, uint8_t count=6);\r
+ void begin(void);\r
+ float readTemperature(bool S=false);\r
+ float convertCtoF(float);\r
+ float convertFtoC(float);\r
+ float computeHeatIndex(float tempFahrenheit, float percentHumidity);\r
+ float readHumidity(void);\r
+ boolean read(void);\r
+\r
+};\r
+#endif\r
--- /dev/null
+include common.mk
+
+all: compile
+
+# To run Particle, we need to install Particle CLI
+# Please see:
+# https://docs.particle.io/guide/getting-started/connect/photon/#install-the-particle-cli
+monitor:
+ sudo chmod 666 /dev/ttyACM0
+ particle serial monitor
+
+compile:
+ rm -f *.bin
+ particle compile photon
+
+flash0:
+ particle flash IoT-0 photon*.bin
+
+flash1:
+ particle flash IoT-1 photon*.bin
+
+PHONY += clean
+clean:
+ rm -f *.bin
+
+PHONY += mrclean
+mrclean: clean
+ rm -rf ../docs
+
+tabbing:
+ uncrustify -c C.cfg --no-backup *.cpp
+ uncrustify -c C.cfg --no-backup *.h
+
+wc:
+ wc *.cpp *.h
+
+.PHONY: $(PHONY)
--- /dev/null
+#include "application.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
+char keyBuffer[80];
+char dataBuffer[80];
+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);
+}
+
--- /dev/null
+# A few common Makefile items
+
+CC := gcc
+CXX := g++
+
+UNAME := $(shell uname)
+
+LIB_NAME := iotcloud
+LIB_SO := lib_$(LIB_NAME).so
+
+CPPFLAGS += -Wall -g -O0
+
+# Mac OSX options
+ifeq ($(UNAME), Darwin)
+CPPFLAGS += -D_XOPEN_SOURCE -DMAC
+endif
--- /dev/null
+// ------------------------------------------------
+// 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<TransactionStatus *> * transStatusList;
+char keyBuffer[80];
+char dataBuffer[80];
+Table *t1;
+int64_t machineId;
+
+// 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
+
+ // TODO: Profiling!
+ //Serial.begin();
+ //Serial.print("Time begin setup: ");
+ //Serial.println(micros());
+
+ // 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");
+ // TODO: Profiling
+ //Serial.print("Time begin rebuilding table: ");
+ //Serial.println(micros());
+
+ t1 = new Table(baseurl, password, machineId, -1);
+ t1->rebuild();
+
+ baseurl->releaseRef();
+ password->releaseRef();
+}
+
+void loop() {
+
+ // Machine ID
+ sprintf(keyBuffer, "ir%04x", machineId);
+ IoTString * iKey = new IoTString(keyBuffer);
+ // Do updates for the humidity
+ sprintf(dataBuffer, "motion-detected");
+ 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("Time end loop: ");
+ //Serial.println(micros());
+ //while(true) { }
+ System.sleep(SLEEP_MODE_DEEP, 0);
+}
--- /dev/null
+// ------------------------------------------------
+// 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/
+
+int pirValue; // Place to store read PIR Value
+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, "motion-detected");
+ Particle.publish(keyBuffer, dataBuffer);
+ delay(1000);
+
+ System.sleep(SLEEP_MODE_DEEP, 0);
+}
--- /dev/null
+// ------------------------------------------------
+// Controlling LifxLightBulb through Particle Cloud
+// @author Rahmadi Trimananda - UC Irvine
+// ------------------------------------------------
+
+#include "LifxLightBulb.h"
+
+LifxLightBulb *llb1;
+LifxLightBulb *llb2;
+
+int led1 = D7;
+
+void setup()
+{
+ // Bulb 1
+ char macAddress1[8];
+ macAddress1[0] = 0xD0;
+ macAddress1[1] = 0x73;
+ macAddress1[2] = 0xD5;
+ macAddress1[3] = 0x12;
+ macAddress1[4] = 0x8E;
+ macAddress1[5] = 0x30;
+ macAddress1[6] = 0x00;
+ macAddress1[7] = 0x00;
+ IPAddress devIPAddress1(192, 168, 1, 126);
+
+ llb1 = new LifxLightBulb(devIPAddress1, macAddress1, 12345);
+ llb1->init();
+
+ // Bulb 2
+ char macAddress2[8];
+ macAddress2[0] = 0xD0;
+ macAddress2[1] = 0x73;
+ macAddress2[2] = 0xD5;
+ macAddress2[3] = 0x02;
+ macAddress2[4] = 0x41;
+ macAddress2[5] = 0xDA;
+ macAddress2[6] = 0x00;
+ macAddress2[7] = 0x00;
+ IPAddress devIPAddress2(192, 168, 1, 232);
+
+ llb2 = new LifxLightBulb(devIPAddress2, macAddress2, 12346);
+ llb2->init();
+
+ pinMode(led1, OUTPUT);
+ Particle.function("lifx1",lifx1Toggle);
+ Particle.function("lifx2",lifx2Toggle);
+}
+
+
+void loop() {
+ // Nothing to do here
+}
+
+
+// Toggling Lifx light bulb 1
+int lifx1Toggle(String command) {
+
+ if (command=="on") {
+ llb1->turnOn();
+ return 1;
+ }
+ else if (command=="off") {
+ llb1->turnOff();
+ return 0;
+ }
+ else {
+ return -1;
+ }
+}
+
+// Toggling Lifx light bulb 2
+int lifx2Toggle(String command) {
+
+ if (command=="on") {
+ llb2->turnOn();
+ return 1;
+ }
+ else if (command=="off") {
+ llb2->turnOff();
+ return 0;
+ }
+ else {
+ return -1;
+ }
+}
+++ /dev/null
-#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<TransactionStatus *> * 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<TransactionStatus *>();
- 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);
-}
-
--- /dev/null
+#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<TransactionStatus *> * 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<TransactionStatus *>();
+ 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);
+}
+
--- /dev/null
+#include "application.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
+char keyBuffer[80];
+char dataBuffer[80];
+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);
+}
+