Index: rtos_arduino/trunk/arduino_lib/libraries/Milkcocoa_Arduino_SDK/Milkcocoa.cpp
===================================================================
--- rtos_arduino/trunk/arduino_lib/libraries/Milkcocoa_Arduino_SDK/Milkcocoa.cpp	(revision 144)
+++ rtos_arduino/trunk/arduino_lib/libraries/Milkcocoa_Arduino_SDK/Milkcocoa.cpp	(revision 180)
@@ -35,5 +35,5 @@
 DataElement::DataElement(char *json_string) {
   paJsonObj = aJson.parse(json_string);
-  params = aJson.getObjectItem(paJsonObj, "params");    
+  params = aJson.getObjectItem(paJsonObj, "params");
 }
 
@@ -56,21 +56,24 @@
 }
 
-char *DataElement::getString(const char *key) {
+bool DataElement::getString(const char *key, char **pdata) {
   aJsonObject* obj = aJson.getObjectItem(params, key);
-  return obj->valuestring;
-}
-
-int DataElement::getInt(const char *key) {
+  if((obj == NULL) || (obj->type != aJson_String)) {return false;}
+  *pdata = obj->valuestring;    
+  return true;
+}
+
+bool DataElement::getInt(const char *key, int *pdata) {
+  aJsonObject* obj = aJson.getObjectItem(params, key);    
+  if((obj == NULL) || (obj->type != aJson_Int)) {return false;}
+  *pdata = obj->valueint;
+  return true;
+}
+
+bool DataElement::getFloat(const char *key, float *pdata) {
   aJsonObject* obj = aJson.getObjectItem(params, key);
-  if(obj == NULL)
-    Serial.println("obj is NULL");
-  return obj->valueint;
-}
-
-float DataElement::getFloat(const char *key) {
-  aJsonObject* obj = aJson.getObjectItem(params, key);
-  return obj->valuefloat;
-}
-
+  if((obj == NULL) || (obj->type != aJson_Float)) {return false;}
+  *pdata = obj->valuefloat;
+  return true;
+}
 
 char *DataElement::toCharArray() {
@@ -106,49 +109,76 @@
 }
 
-void Milkcocoa::connect() {
-  int ret;
+/*
+ *  tmout
+ *   1-   : Connect try time.
+ *   0    : retry Forever
+ *  Return code
+ *   0     : Success
+ *   other : Error code (see Adafruit_MQTT::connectErrorString()) 
+ */
+int8_t Milkcocoa::connect(int32_t tmout) {
+  int8_t ret;
+  int32_t retry = 0;
 
   if (mqtt->connected()) {
-    return;
-  }
-
-  Serial.print("Connecting to MQTT... ");
+    return 0;
+  }
+
+  MILKCOCOA_DEBUG_PRINTLN("Connecting to MQTT... ");
 
   while ((ret = mqtt->connect()) != 0) { // connect will return 0 for connected
-       Serial.println(mqtt->connectErrorString(ret));
-       Serial.println(ret);
-       Serial.println("Retrying MQTT connection in 5 seconds...");
-       mqtt->disconnect();
-       delay(5000);  // wait 5 seconds
-  }
-  Serial.println("MQTT Connected!");
-}
-
-bool Milkcocoa::push(const char *path, DataElement *pdataelement) {
+      mqtt->disconnect();
+      // Timeout
+      if ((tmout !=  0) && (++retry == tmout)) {
+          return ret;
+      }
+      delay(MILKCOCOA_CONNECT_DELAY_MS);
+  }
+  MILKCOCOA_DEBUG_PRINTLN("MQTT Connected!");
+
+  return 0;  
+}
+
+int8_t Milkcocoa::push(const char *path, DataElement *pdataelement, uint16_t timeout) {
   char topic[100];
-  bool ret;
+  int8_t ret;
   char *send_array;
+
+  if((ret = connect(timeout)) != 0){return ret;}
+    
   sprintf(topic, "%s/%s/push", app_id, path);
   Adafruit_MQTT_Publish pushPublisher = Adafruit_MQTT_Publish(mqtt, topic);
   send_array = pdataelement->toCharArray();
-  ret = pushPublisher.publish(send_array);
+  ret = (pushPublisher.publish(send_array))? 0 : MILKCOCOA_PUSH_ERRORNO;
   free(send_array);
   return ret;
 }
 
-bool Milkcocoa::send(const char *path, DataElement *pdataelement) {
+int8_t Milkcocoa::send(const char *path, DataElement *pdataelement, uint16_t timeout) {
   char topic[100];
-  bool ret;
-  char *send_array;    
+  int8_t ret;
+  char *send_array;
+
+  if((ret = connect(timeout)) != 0){return ret;}
+    
   sprintf(topic, "%s/%s/send", app_id, path);
   Adafruit_MQTT_Publish pushPublisher = Adafruit_MQTT_Publish(mqtt, topic);
   send_array = pdataelement->toCharArray();
-  ret = pushPublisher.publish(send_array);
+  ret = (pushPublisher.publish(send_array))? 0 : MILKCOCOA_PUSH_ERRORNO;
   free(send_array);    
   return ret;
 }
 
-void Milkcocoa::loop() {
-  connect();
+/*
+ *  Return code
+ *   0     : Success
+ *   other : Error code
+ *           -1,1-6 : see Adafruit_MQTT::connectErrorString()
+ */
+int8_t Milkcocoa::loop(uint16_t timeout) {
+  int8_t ret;
+
+  if((ret = connect(timeout)) != 0){return ret;}
+   
   Adafruit_MQTT_Subscribe *subscription;
   while ((subscription = mqtt->readSubscription(1000))) {
@@ -161,4 +191,6 @@
     }
   }
+
+  return 0;  
 }
 
