#include "pbkdf2-sha256.h"
AESKey::AESKey(Array<char> *password, Array<char> *salt, int iterationCount, int keyLength) {
- key = new Array<char>(keyLength / 8);
- PKCS5_PBKDF2_HMAC((unsigned char *) password->internalArray(), password->length(),
- (unsigned char *) salt->internalArray(), salt->length(),
- iterationCount, keyLength / 8, (unsigned char *) key->internalArray());
+
+ // Check if we get the same salt and password from EEPROM---create new ones otherwise
+ 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> * storedPwd = new Array<char>(pwdBuffer, PWD_LEN);
+ 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
+ key = new Array<char>(keyLength / 8);
+ PKCS5_PBKDF2_HMAC((unsigned char *) password->internalArray(), password->length(),
+ (unsigned char *) salt->internalArray(), salt->length(),
+ iterationCount, keyLength / 8, (unsigned char *) key->internalArray());
+ // Store new salt and password into EEPROM---write all at once adjacently
+ char buffer[SALT_LEN + PWD_LEN + KEY_LEN];
+ memcpy(buffer + SALT_OFFSET, (unsigned char *) salt->internalArray(), SALT_LEN);
+ memcpy(buffer + PWD_OFFSET, (unsigned char *) password->internalArray(), PWD_LEN);
+ memcpy(buffer + KEY_OFFSET, (unsigned char *) key->internalArray(), KEY_LEN);
+ EEPROM.put(ADDR_SALT, buffer);
+ }
aes_key_setup((BYTE *)key->internalArray(), key_schedule, keyLength);
}
#include "TransactionStatus.h"
#include "DHT.h"
+SYSTEM_MODE(SEMI_AUTOMATIC)
+SYSTEM_THREAD(ENABLED)
+
// System defines
// Arduino
-#define DHTPIN 2 // Data pin
-#define PWRPIN D5 // Power pin
-#define ERRPIN D7 // Error pin
-#define DHTTYPE DHT22 // DHT 22 (AM2302)
+#define DHTPIN 2 // Data pin
+#define PWRPIN D5 // Power pin
+#define ERRPIN D7 // Error pin
+#define DHTTYPE DHT22 // DHT 22 (AM2302)
// IoTCloud
-#define SLEEP_TIME 10 // Sleep time in seconds
+#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
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
Table *t1;
int64_t machineId;
-
void setup() {
// TODO:
// 1) Before running PHOTON's with this firmware,
// please go to the master branch and run Init.C
- // to initialize a table on the cloud side.
+ // in iotcloud/version2/src/C to initialize
+ // a table on the cloud side.
// If you want to read the committed keys and values
- // on the cloud, then you can use Read.java.
+ // on the cloud, then you can use Read.java in
+ // iotcloud/version2/src/java/simple_test.
// 2) If you want to use the Serial library,
// please install "screen" on your machine and run
// it on the serial port right after flashing the
// We use one I/O pin to power the sensor so
// that we can make it go to sleep when we go into
// deep sleep.
+
+ // TODO: Profiling!
+ //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
digitalWrite(PWRPIN, HIGH);
-
+ // Arduino DHT
+ dht.begin();
// Prepare device key from MAC (just last 2 of 6 digits)
byte mac[6];
WiFi.macAddress(mac);
transStatusList = new MyVector<TransactionStatus *>();
IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
IoTString *password = new IoTString("reallysecret");
+
+ //Serial.print("Time begin rebuilding table: ");
+ //Serial.println(micros());
+
t1 = new Table(baseurl, password, machineId, -1);
t1->rebuild();
baseurl->releaseRef();
password->releaseRef();
- // Arduino DHT
- dht.begin();
+ //Serial.print("Time end setup: ");
+ //Serial.println(micros());
}
void loop() {
-
// Wait until sensor is ready
- delay(2000);
+ //delay(2000);
+ //Serial.print("Time begin loop: ");
+ //Serial.println(micros());
// Read humidity
float humid = dht.readHumidity();
// Read temperature as Fahrenheit
// Turn off sensor
digitalWrite(PWRPIN, LOW);
+
+ //Serial.print("Time end loop: ");
+ //Serial.println(micros());
System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);
+
+ //while(true) { }
}