Completing firmware for the temperature/humidity, IR, and magnetic sensors for both...
[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 int64_t machineId;
12 // Key and data
13 char keyBuffer[80];
14 char dataBuffer[80];
15
16 // Setting up pins for input
17 void setup() {
18
19         // Get the MAC address
20         byte mac[6];
21         WiFi.macAddress(mac);
22         // Prepare machine ID
23         int64_t mac4 = (int64_t) mac[4];
24         int64_t mac5 = (int64_t) mac[5];
25         machineId = (mac4 * 256) +  mac5;
26 }
27
28 void loop() {
29
30         // Basically delay to give time to send updates to Particle cloud.
31         // If we do less than this then it would fail publishing.
32         delay(2000);    
33         // Machine ID
34         sprintf(keyBuffer, "%04x", machineId);
35         // Motion detection
36         sprintf(dataBuffer, "%s -> motion-detected", Time.timeStr().c_str());
37         Particle.publish(keyBuffer, dataBuffer);
38         delay(1000);
39         
40         // TODO: Profiling
41         //String strTime(micros());
42         //Particle.publish("Time motion sensor", strTime.c_str());
43         //while(true);
44
45         System.sleep(SLEEP_MODE_DEEP, 0);
46 }