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(),
// 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
#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);
//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);
//Serial.print("Time end loop: ");
//Serial.println(micros());
- System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);
-
//while(true) { }
+ System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);
}