This is a simple mbed client example demonstrating, registration of a device with mbed Device Connector and reading and writing values as well as deregistering on different Network Interfaces including Ethernet, WiFi, 6LoWPAN ND and Thread respectively.

Dependencies:   lwip

Fork of mbed-os-example-client by mbed-os-examples

Revision:
65:ea64e559b7d3
Parent:
63:c73f78fd7982
Child:
68:6c535975a6e3
--- a/main.cpp	Wed Feb 15 09:30:13 2017 +0000
+++ b/main.cpp	Mon Feb 20 15:10:50 2017 +0000
@@ -223,20 +223,24 @@
      * from mbed Device Connector, then up the value with one.
      */
     void handle_button_click() {
-        M2MObjectInstance* inst = btn_object->object_instance();
-        M2MResource* res = inst->resource("5501");
+        if (mbed_client.register_successful()) {
+            M2MObjectInstance* inst = btn_object->object_instance();
+            M2MResource* res = inst->resource("5501");
 
-        // up counter
-        counter++;
-#ifdef TARGET_K64F
-        printf("handle_button_click, new value of counter is %d\n", counter);
-#else
-        printf("simulate button_click, new value of counter is %d\n", counter);
-#endif
-        // serialize the value of counter as a string, and tell connector
-        char buffer[20];
-        int size = sprintf(buffer,"%d",counter);
-        res->set_value((uint8_t*)buffer, size);
+            // up counter
+            counter++;
+    #ifdef TARGET_K64F
+            printf("handle_button_click, new value of counter is %d\n", counter);
+    #else
+            printf("simulate button_click, new value of counter is %d\n", counter);
+    #endif
+            // serialize the value of counter as a string, and tell connector
+            char buffer[20];
+            int size = sprintf(buffer,"%d",counter);
+            res->set_value((uint8_t*)buffer, size);
+        } else {
+            printf("simulate button_click, device not registered\n");
+        }
     }
 
 private:
@@ -407,7 +411,7 @@
             break;
         }
         if(clicked) {
-           clicked = false;
+            clicked = false;
             button_resource.handle_button_click();
         }
     }