Index: rtos_arduino/trunk/examples/Milkcocoa_basic/rca_app.cpp
===================================================================
--- rtos_arduino/trunk/examples/Milkcocoa_basic/rca_app.cpp	(revision 174)
+++ rtos_arduino/trunk/examples/Milkcocoa_basic/rca_app.cpp	(revision 182)
@@ -4,10 +4,12 @@
 #include "Client_ESP8266.h"
 
-#define WLAN_SSID       ""
-#define WLAN_PASSWORD   ""
+#define SSID       ""
+#define PASSWORD   ""
 
 #define MILKCOCOA_APP_ID      ""
-#define MILKCOCOA_DATASTORE   "esp8266"
-#define MILKCOCOA_DATASTORE_CHAT   "chat"
+#define MILKCOCOA_DATASTORE   ""
+
+#define MILKCOCOA_PUSH
+#define MILKCOCOA_ON
 
 #define MILKCOCOA_SERVERPORT  1883
@@ -21,18 +23,33 @@
 
 extern void onpush(DataElement *elem);
-extern void onpush_chat(DataElement *elem);
-    
+
 void setup()
 {
+    int ret;
+    
     Serial.begin(115200);
-    Serial.println(F("Milkcocoa SDK demo"));
+    Serial.print("Milkcocoa SDK demo");
 
     // Connect to WiFi access point.
     Serial.println(); Serial.println();
     Serial.print("Connecting to ");
-    Serial.println(WLAN_SSID);
-    
-    wifi.begin(Serial5, 115200);
-    
+    Serial.println(SSID);
+
+    ret = wifi.begin(Serial5, 115200);
+
+    if(ret == 1) {
+        Serial.print("Cannot communicate with ESP8266.");
+        while(1);        
+    } else if(ret == 2) {
+        Serial.println("FW Version mismatch.");
+        Serial.print("FW Version:");
+        Serial.println(wifi.getVersion().c_str());
+        Serial.print("Supported FW Version:");
+        Serial.println(ESP8266_SUPPORT_VERSION);
+        while(1);
+    } else {
+        Serial.print("begin ok\r\n");
+    }
+
     Serial.print("FW Version:");
     Serial.println(wifi.getVersion().c_str());
@@ -44,5 +61,5 @@
     }
     
-    if (wifi.joinAP(WLAN_SSID, WLAN_PASSWORD)) {
+    if (wifi.joinAP(SSID, PASSWORD)) {
         Serial.print("Join AP success\r\n");
         Serial.print("IP: ");
@@ -51,5 +68,11 @@
         Serial.print("Join AP failure\r\n");
     }
-        
+
+    if (wifi.stopServer()) {
+        Serial.print("Stop server ok\r\n");
+    } else {
+        Serial.print("Stop server err\r\n");
+    }        
+    
     if (wifi.disableMUX()) {
         Serial.print("single ok\r\n");
@@ -58,4 +81,5 @@
     }
     
+#ifdef MILKCOCOA_ON
     if (milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush)){
         Serial.println("milkcocoa on sucesss");   
@@ -65,54 +89,84 @@
     }
 
-    if (milkcocoa.on(MILKCOCOA_DATASTORE_CHAT, "push", onpush_chat)){
-        Serial.println("milkcocoa on sucesss");   
-    }
-    else {
-        Serial.println("milkcocoa on failure");   
-    }    
-    
     pinMode(13, OUTPUT);
-    
+    digitalWrite(13, LOW);   
+#endif /* MILKCOCOA_ON  */
+
     Serial.println("setup end\r\n");
 }
 
-int loop_cnt = 0;
-int cnt;
+#ifdef MILKCOCOA_PUSH
+bool req_led_on = false;
+#endif /* MILKCOCOA_PUSH */
+
 void loop() {
-    milkcocoa.loop();
-    char buffer[100];
+    int8_t ret;
+    int8_t push_ret;
+    
+#ifdef MILKCOCOA_ON
+    while((ret = milkcocoa.loop(1)) != 0) {
+        Serial.println("Milkcocoa.loop() connection error.");
+        Serial.println(milkcocoa.connectErrorString(ret));
+        Serial.println(ret);
+        Serial.println("Retrying MQTT connection in 5 seconds...");
+        delay(5000);
+    }
+    Serial.print(".");
+#endif /* MILKCOCOA_ON */
+    
+#ifdef MILKCOCOA_PUSH
+    DataElement elem = DataElement();
+    
+    if(req_led_on) {
+        Serial.println("Push data to Milkcocoa : Key:LED, Value:ON");
+        elem.setValue("LED", "ON");
+    }else {
+        Serial.println("Push data to Milkcocoa : Key:LED, Value:OFF");        
+        elem.setValue("LED", "OFF");
+    }
+    
+    do {
+        push_ret = milkcocoa.push(MILKCOCOA_DATASTORE, &elem);
+        if (push_ret != 0) {
+            Serial.println("Milkcocoa.push() error.");
+            Serial.println(milkcocoa.pushErrorString(push_ret));
+            Serial.println(push_ret);
+            Serial.println("Retrying MQTT push in 5 seconds...");
+            delay(5000);
+        }        
+    }while(push_ret != 0);
+    
+    Serial.println("Push success.");
+    req_led_on = (req_led_on)? false : true;
+    delay(2000);    
+#endif /* MILKCOCOA_PUSH */
+    
+    if (Serial.available() > 0) {
+        Serial.read();
+        Serial.print("Pause  : Input any char to continue.\n\r");
+        while(!(Serial.available() > 0));
+        Serial.print("Resume.\n\r");
+        Serial.read();
+    }    
+}
 
-    sprintf(buffer, "RCA is Alive %d", loop_cnt++);      
-    DataElement elem = DataElement();
-    elem.setValue("message", buffer);
-    milkcocoa.push(MILKCOCOA_DATASTORE_CHAT, &elem);
-    
-//    DataElement elem = DataElement();
-//    elem.setValue("v", cnt++);
-//    milkcocoa.push(MILKCOCOA_DATASTORE, elem);
-
-    Serial.print(".");
-    delay(1000);
-};
-
+#ifdef MILKCOCOA_ON
 void onpush(DataElement *pelem) {
-    int v;
-    Serial.print("onpush : ");
-    v = pelem->getInt("v");
-    Serial.println(v);
-    if (v == 1) {
+    char *data;
+    if(!pelem->getString("LED", &data)) {
+        Serial.print("onpush : key LED is not found.");
+        return;
+    };
+    Serial.print("onpush : {LED, ");
+    Serial.write(data);
+    Serial.println("}.");
+    if (strcmp(data, "ON") == 0) {
         Serial.println("LED : ON!");
         digitalWrite(13, HIGH);
     }
-    else {
+    else if(strcmp(data, "OFF") == 0) {
         Serial.println("LED : OFF!");
         digitalWrite(13, LOW);
     }
-};
-
-void onpush_chat(DataElement *pelem) {
-    char *c;
-    Serial.print("onpush_chat : ");
-    c = pelem->getString("message");
-    Serial.println(c);
-};
+}
+#endif /* MILKCOCOA_ON */
