c3be4ab5606c0dbf7b8e695a02b49a0ea472651b
[iotcloud.git] / version2 / src / others / ino / IR-Sensor / IR-Sensor_Particle-Cloud.ino
1 // ------------------------------------------------
2 // Controlling HC-SR501 through Particle Cloud
3 // @author Rahmadi Trimananda - UC Irvine
4 // ------------------------------------------------
5 // HC-SR501 Motion Detector
6 // Basically, this simple code detects motion and send the occurrences to the cloud
7 // We use the WKP port (port A7) to wake up the device when there is motion
8 //
9 // Based on tutorial: http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/arduino-hc-sr501-motion-sensor-tutorial/
10
11 SYSTEM_MODE(SEMI_AUTOMATIC)
12 SYSTEM_THREAD(ENABLED)
13
14 int pirValue; // Place to store read PIR Value
15 int64_t machineId;
16 // Key and data
17 char keyBuffer[80];
18 char dataBuffer[80];
19
20 // Setting up pins for input
21 void setup() {
22
23         // Get the MAC address
24         byte mac[6];
25         WiFi.macAddress(mac);
26         // Prepare machine ID
27         int64_t mac4 = (int64_t) mac[4];
28         int64_t mac5 = (int64_t) mac[5];
29         machineId = (mac4 * 256) +  mac5;
30 }
31
32 void loop() {
33
34         // Basically delay to give time to send updates to Particle cloud.
35         // If we do less than this then it would fail publishing.
36         delay(2000);    
37         // Machine ID
38         sprintf(keyBuffer, "%04x", machineId);
39         // Motion detection
40         sprintf(dataBuffer, "motion-detected");
41         Particle.publish(keyBuffer, dataBuffer);
42         delay(1000);
43
44         System.sleep(SLEEP_MODE_DEEP, 0);
45 }