Warming-up sensor, turn on WiFi/Particle, read from sensor, send to cloud and sleep...
authorrtrimana <rtrimana@uci.edu>
Mon, 23 Apr 2018 17:24:37 +0000 (10:24 -0700)
committerrtrimana <rtrimana@uci.edu>
Mon, 23 Apr 2018 17:24:37 +0000 (10:24 -0700)
version2/src/C/Crypto.cpp
version2/src/C/Crypto.h
version2/src/C/Sensor-Arduino.ino

index b1a3cb43857d79e3584cd62dc6e600292b785a4a..b8e38967271210f12ed54362e714f0c21ff8bcdc 100755 (executable)
@@ -3,17 +3,19 @@
 
 AESKey::AESKey(Array<char> *password, Array<char> *salt, int iterationCount, int keyLength) {
 
-       // Check if we get the same salt and password from EEPROM---create new ones otherwise
+       // Read the stored password and salt from EEPROM
        char saltBuffer[SALT_LEN], pwdBuffer[PWD_LEN], keyBuffer[KEY_LEN];
        EEPROM.get(ADDR_SALT, saltBuffer);
-       Array<char> * storedSalt = new Array<char>(saltBuffer, SALT_LEN);
        EEPROM.get(ADDR_PWD, pwdBuffer);
+       Array<char> * storedSalt = new Array<char>(saltBuffer, SALT_LEN);
        Array<char> * storedPwd = new Array<char>(pwdBuffer, PWD_LEN);
-       if (storedSalt->equals(salt) && storedPwd->equals(password)) { // Take key from EEPROM if match
+       // Compare the input password and salt against the stored ones
+       if (storedSalt->equals(salt) && storedPwd->equals(password)) {
+               // Take key from EEPROM if match
                EEPROM.get(ADDR_KEY, keyBuffer);
                key = new Array<char>(keyBuffer, KEY_LEN);
-               delay(2000);    // Delay for sensor
-       } else {        // Generate a new key if not match
+       } else {
+               // Generate a new key if not match
                key = new Array<char>(keyLength / 8);
                PKCS5_PBKDF2_HMAC((unsigned char *) password->internalArray(), password->length(),
                                                                                        (unsigned char *) salt->internalArray(), salt->length(),
index 15931bd369bc85a766a57fcbc0494dc78781a72f..29a218657cb5cdf596a9647dc1a9dbe64bd75905 100755 (executable)
@@ -9,6 +9,7 @@
 // 1) SALT
 // 2) PASSWORD
 // 3) LEN
+// We assume that these 3 items are put adjacently in the EEPROM
 #define SALT_LEN                       8               // 8 bytes
 #define PWD_LEN                                12              // 12 bytes
 #define KEY_LEN                                16              // 16 bytes
index 5f3f10c7c5e8de1740196d0be62c3a13b09c26d4..241d6524a5e4589afdde4f8e34fea8f71f02dd7b 100644 (file)
@@ -15,8 +15,8 @@ SYSTEM_THREAD(ENABLED)
 #define DHTTYPE                        DHT22   // DHT 22  (AM2302)
 // IoTCloud
 #define SLEEP_TIME                     30              // Sleep time in seconds
-#define RETRY_SLEEP_TIME                     // Sleep time in seconds
-#define RETRY_TIME                     100000  // stop trying after 5 seconds
+#define RETRY_SLEEP_TIME       15              // Sleep time in seconds
+#define RETRY_TIME                     10000   // stop trying after 10 seconds
 
 // Initialize DHT sensor for normal 16mhz Arduino
 DHT dht(DHTPIN, DHTTYPE);
@@ -54,17 +54,18 @@ void setup() {
        //Serial.begin();
        //Serial.print("Time begin setup: ");
        //Serial.println(micros());
-       Particle.connect();
-       // Wait for maximum 10 seconds - sleep if WiFi is not connected
-       // Wake up and try again after 5 seconds
-       if (!waitFor(Particle.connected, RETRY_TIME))
-               System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME);
-               
-       pinMode(PWRPIN, OUTPUT);
        // Turn on sensor
+       pinMode(PWRPIN, OUTPUT);
        digitalWrite(PWRPIN, HIGH);
        // Arduino DHT
        dht.begin();
+       // Connect to WiFi
+       Particle.connect();
+       // Wait for a maximum amount of time - sleep if WiFi is not connected
+       // Wake up and try again afterwards
+       if (!waitFor(Particle.connected, RETRY_TIME))
+               System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME);
+               
        // Prepare device key from MAC (just last 2 of 6 digits)
        byte mac[6];
        WiFi.macAddress(mac);
@@ -160,8 +161,7 @@ void loop() {
 
        //Serial.print("Time end loop: ");
        //Serial.println(micros());
-       System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);
-
        //while(true) { }
+       System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);
 }