Completing firmware for the temperature/humidity, IR, and magnetic sensors for both...
[iotcloud.git] / version2 / src / others / Magnetic-Sensor / Magnetic-Sensor_Particle-Cloud.ino
1 // ------------------------------------------------
2 // Controlling Arduino EK1656 through cloud
3 // @author Rahmadi Trimananda - UC Irvine
4 // ------------------------------------------------
5 // Gikfun MC-38 - Arduino EK1656 (magnetic sensor)
6 // Basically, this simple code detects sensor opening 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
10 int64_t machineId;
11 // Key and data
12 char keyBuffer[80];
13 char dataBuffer[80];
14
15 // Setting up pins for input
16 void setup() {
17
18         // Get the MAC address
19         byte mac[6];
20         WiFi.macAddress(mac);
21         // Prepare machine ID
22         int64_t mac4 = (int64_t) mac[4];
23         int64_t mac5 = (int64_t) mac[5];
24         machineId = (mac4 * 256) +  mac5;
25 }
26
27 void loop() {
28
29         // Basically delay to give time to send updates to Particle cloud.
30         // If we do less than this then it would fail publishing.
31         delay(2000);    
32         // Machine ID
33         sprintf(keyBuffer, "%04x", machineId);
34         // Motion detection
35         sprintf(dataBuffer, "%s -> closing-door-detected", Time.timeStr().c_str());
36         Particle.publish(keyBuffer, dataBuffer);
37         
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 }