// ------------------------------------------------ // 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, "m%04x", machineId); IoTString * iKey = new IoTString(keyBuffer); // Do updates for the magnetic action //sprintf(dataBuffer, "%s -> closing-door-detected", wakeupTime.c_str()); sprintf(dataBuffer, "%s", "close"); 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); }