84831d0f6e875a23b199557f74687b33576d8881
[iotcloud.git] / version2 / src / others / ino / IR-Sensor / IR-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
33 // Setting up pins for input
34 void setup() {
35         // TODO: 
36         // 1) Before running PHOTON's with this firmware,
37         //    please go to the master branch and run Init.C
38         //    in iotcloud/version2/src/C to initialize 
39         //    a table on the cloud side.
40         //    If you want to read the committed keys and values 
41         //    on the cloud, then you can use Read.java in
42         //    iotcloud/version2/src/java/simple_test.
43         // 2) If you want to use the Serial library, 
44         //    please install "screen" on your machine and run
45         //    it on the serial port right after flashing the
46         //    firmware onto the Particle board.
47         //    e.g. sudo screen /dev/ttyACM0
48
49         // TODO: Profiling!
50         //Serial.begin();
51         //Serial.print("Time begin setup: ");
52         //Serial.println(micros());
53
54         // Connect only to WiFi
55         WiFi.on();
56         WiFi.connect();
57         if (!waitFor(WiFi.ready, RETRY_TIME))
58                 System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME);
59         while(!WiFi.localIP()); // Wait for valid IP
60     // Add delay if needed (for the humidity sensor we need ~2s delay)
61     // Delays are in millis but micros() returns microsecond time
62         if (micros() < CONNECTION_DELAY)
63                 delay((CONNECTION_DELAY - micros())/1000);
64
65         // Get the MAC address
66         byte mac[6];
67         WiFi.macAddress(mac);
68         // Prepare machine ID
69         int64_t mac4 = (int64_t) mac[4];
70         int64_t mac5 = (int64_t) mac[5];
71         machineId = (mac4 * 256) +  mac5;
72         
73         IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
74         IoTString *password = new IoTString("reallysecret");
75     // TODO: Profiling
76         //Serial.print("Time begin rebuilding table: ");
77         //Serial.println(micros());     
78         
79         t1 = new Table(baseurl, password, machineId, -1);
80         t1->rebuild();
81
82         baseurl->releaseRef();
83         password->releaseRef();
84 }
85
86 void loop() {
87
88         // Machine ID
89         sprintf(keyBuffer, "ir%04x", machineId);
90         IoTString * iKey = new IoTString(keyBuffer);
91         // Do updates for the humidity
92         sprintf(dataBuffer, "motion-detected");
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("Time end loop: ");
103         //Serial.println(micros());
104         //while(true) { }
105         System.sleep(SLEEP_MODE_DEEP, 0);
106 }