// ------------------------------------------------ // 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/ 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 -> 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); }