Shortening keys and data; changing slot number from 4 to 2 to fit in 8 sensors (16...
[iotcloud.git] / version2 / src / C / Sensor-Arduino.ino
index 86e610154efdc9bbfcb870b795d9a2232f0f140c..2521bae76095fff3bfa3d0f5ed98258811ae9efa 100644 (file)
@@ -14,7 +14,7 @@ SYSTEM_THREAD(ENABLED)
 #define ERRPIN                         D7              // Error pin
 #define DHTTYPE                        DHT22   // DHT 22  (AM2302)
 // IoTCloud
-#define SLEEP_TIME                     300             // Sleep time in seconds
+#define SLEEP_TIME                     120             // Sleep time in seconds
 #define RETRY_SLEEP_TIME       15              // Sleep time in seconds
 #define RETRY_TIME                     10000   // stop trying after 10 seconds
 #define CONNECTION_DELAY       2100000 // Need to delay after connecting WiFi to wait for sensor
@@ -74,8 +74,8 @@ void setup() {
        if (!waitFor(WiFi.ready, RETRY_TIME))
                System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME);
        // Check if we already got an IP from the router
-       while(WiFi.localIP().toString().equals("0.0.0.0")) { }
-       // Add delay if needed (for the humidity sensor we need ~2s delay)
+       while(!WiFi.localIP());
+    // Add delay if needed (for the humidity sensor we need ~2s delay)
        if (micros() < CONNECTION_DELAY)
                // Delays are in millis but micros() returns microsecond time
                delay((CONNECTION_DELAY - micros())/1000);
@@ -97,6 +97,7 @@ void setup() {
        int64_t mac4 = (int64_t) mac[4];
        int64_t mac5 = (int64_t) mac[5];
        machineId = (mac4 * 256) +  mac5;
+       //machineId = (int64_t) mac[5];   // Shorter version of machine ID
        
        // IoTCloud library
        timer = TimingSingleton_getInstance();
@@ -104,6 +105,7 @@ void setup() {
        transStatusList = new MyVector<TransactionStatus *>();
        IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/");
        IoTString *password = new IoTString("reallysecret");
+    // TODO: Profiling
        //Serial.print("Time begin rebuilding table: ");
        //Serial.println(micros());     
        
@@ -112,7 +114,7 @@ void setup() {
 
        baseurl->releaseRef();
        password->releaseRef();
-       
+    // TODO: Profiling 
        //Serial.print("Time end setup: ");
        //Serial.println(micros());
 }
@@ -120,6 +122,7 @@ void setup() {
 void loop() {
        // Wait until sensor is ready
        //delay(2000);
+    // TODO: Profiling
        //Serial.print("Time begin loop: ");
        //Serial.println(micros());
        // Read humidity
@@ -131,10 +134,10 @@ void loop() {
                return;
        // Humidity
        // Key
-       sprintf(keyBuffer, "humid%d", machineId);
+       sprintf(keyBuffer, "h%04x", machineId);
        IoTString * iKeyHumid = new IoTString(keyBuffer);
        // Do updates for the temperature
-       sprintf(dataBuffer, "%f", humid);
+       sprintf(dataBuffer, "%0.2f", humid);
        IoTString * iValueHumid = new IoTString(dataBuffer);
 
        // Check and create a new key if it isn't created yet
@@ -145,10 +148,10 @@ void loop() {
 
        // Temperature
        // Key
-       sprintf(keyBuffer, "tempF%d", machineId);
+       sprintf(keyBuffer, "t%04x", machineId);
        IoTString * iKeyTempF = new IoTString(keyBuffer);
        // Do updates for the temperature
-       sprintf(dataBuffer, "%f", tempF);
+       sprintf(dataBuffer, "%0.2f", tempF);
        IoTString * iValueTempF = new IoTString(dataBuffer);
 
        // Check and create a new key if it isn't created yet
@@ -175,7 +178,7 @@ void loop() {
        // Turn off sensor
        digitalWrite(PWRPIN, LOW);
 
-       // TODO: Profiling!
+       // TODO: Profiling
        //Serial.print("Time end loop: ");
        //Serial.println(micros());
        //while(true) { }