From dd6f9bea39d1412ed3792bb3affc477f96c5f01a Mon Sep 17 00:00:00 2001 From: rtrimana Date: Mon, 23 Apr 2018 10:24:37 -0700 Subject: [PATCH] Warming-up sensor, turn on WiFi/Particle, read from sensor, send to cloud and sleep again (~3.8s). --- version2/src/C/Crypto.cpp | 12 +++++++----- version2/src/C/Crypto.h | 1 + version2/src/C/Sensor-Arduino.ino | 22 +++++++++++----------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/version2/src/C/Crypto.cpp b/version2/src/C/Crypto.cpp index b1a3cb4..b8e3896 100755 --- a/version2/src/C/Crypto.cpp +++ b/version2/src/C/Crypto.cpp @@ -3,17 +3,19 @@ AESKey::AESKey(Array *password, Array *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 * storedSalt = new Array(saltBuffer, SALT_LEN); EEPROM.get(ADDR_PWD, pwdBuffer); + Array * storedSalt = new Array(saltBuffer, SALT_LEN); Array * storedPwd = new Array(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(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(keyLength / 8); PKCS5_PBKDF2_HMAC((unsigned char *) password->internalArray(), password->length(), (unsigned char *) salt->internalArray(), salt->length(), diff --git a/version2/src/C/Crypto.h b/version2/src/C/Crypto.h index 15931bd..29a2186 100755 --- a/version2/src/C/Crypto.h +++ b/version2/src/C/Crypto.h @@ -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 diff --git a/version2/src/C/Sensor-Arduino.ino b/version2/src/C/Sensor-Arduino.ino index 5f3f10c..241d652 100644 --- a/version2/src/C/Sensor-Arduino.ino +++ b/version2/src/C/Sensor-Arduino.ino @@ -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 5 // 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); } -- 2.34.1