Tweaked phone app for testbed experiment; tweaked testbed files
[iotcloud.git] / version2 / src / others / ino / Magnetic-Sensor / Magnetic-Sensor_Fidelius-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 #include "Table.h"
11 #include "IoTString.h"
12 #include "TimingSingleton.h"
13 #include "TransactionStatus.h"
14
15 // IoTCloud
16 #define SLEEP_TIME                      15              // Sleep time in seconds
17 #define RETRY_SLEEP_TIME        5               // Sleep time in seconds
18 #define RETRY_TIME                      10000   // stop trying after 10 seconds
19 #define CONNECTION_DELAY        2100000 // Need to delay after connecting WiFi to wait for sensor
20
21 SYSTEM_MODE(SEMI_AUTOMATIC)
22 SYSTEM_THREAD(ENABLED)
23
24 // Globals
25 // IoTCloud
26 bool foundError;
27 MyVector<TransactionStatus *> * transStatusList;
28 char keyBuffer[80];
29 char dataBuffer[80];
30 Table *t1;
31 int64_t machineId;
32 String wakeupTime;
33
34 // Setting up pins for input
35 void setup() {
36         // TODO: 
37         // 1) Before running PHOTON's with this firmware,
38         //    please go to the master branch and run Init.C
39         //    in iotcloud/version2/src/C to initialize 
40         //    a table on the cloud side.
41         //    If you want to read the committed keys and values 
42         //    on the cloud, then you can use Read.java in
43         //    iotcloud/version2/src/java/simple_test.
44         // 2) If you want to use the Serial library, 
45         //    please install "screen" on your machine and run
46         //    it on the serial port right after flashing the
47         //    firmware onto the Particle board.
48         //    e.g. sudo screen /dev/ttyACM0
49         
50         // Record the time when waking up!
51         wakeupTime = Time.timeStr();
52
53         // TODO: Profiling!
54         //Serial.begin();
55
56         // Connect only to WiFi
57         WiFi.on();
58         WiFi.connect();
59         if (!waitFor(WiFi.ready, RETRY_TIME))
60                 System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME);
61         while(!WiFi.localIP()); // Wait for valid IP
62     // Add delay if needed (for the humidity sensor we need ~2s delay)
63     // Delays are in millis but micros() returns microsecond time
64         if (micros() < CONNECTION_DELAY)
65                 delay((CONNECTION_DELAY - micros())/1000);
66
67         // Get the MAC address
68         byte mac[6];
69         WiFi.macAddress(mac);
70         // Prepare machine ID
71         int64_t mac4 = (int64_t) mac[4];
72         int64_t mac5 = (int64_t) mac[5];
73         machineId = (mac4 * 256) +  mac5;
74         
75         IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
76         IoTString *password = new IoTString("reallysecret");
77         
78         t1 = new Table(baseurl, password, machineId, -1);
79         t1->rebuild();
80
81         baseurl->releaseRef();
82         password->releaseRef();
83 }
84
85 void loop() {
86
87         // Machine ID
88         sprintf(keyBuffer, "m%04x", machineId);
89         IoTString * iKey = new IoTString(keyBuffer);
90         // Do updates for the magnetic action
91         //sprintf(dataBuffer, "%s -> closing-door-detected", wakeupTime.c_str());
92         sprintf(dataBuffer, "%s", "close");
93         IoTString * iValue = new IoTString(dataBuffer);
94
95         // Check and create a new key if it isn't created yet
96         t1->createNewKey(iKey, machineId);
97         t1->startTransaction();
98         t1->put(iKey, iValue);
99         transStatusList->add(t1->commitTransaction());
100
101         // TODO: Profiling
102         //Serial.print("Elapsed time: ");
103         //Serial.println(micros());
104         //while(true);
105
106         System.sleep(SLEEP_MODE_DEEP, 0);
107 }