pelion-example-common-DISCO_F413ZH

Revision:
25:d8a84856bd96
Parent:
23:64f3f756acf6
Child:
26:25ac72eedf55
--- a/main.cpp	Sun Mar 24 18:02:30 2019 +0000
+++ b/main.cpp	Wed Mar 27 18:17:34 2019 +0000
@@ -39,27 +39,38 @@
 // Default User button for GET example
 InterruptIn button(BUTTON1);
 // Default LED to use for PUT/POST example
-DigitalOut led(LED1);
+DigitalOut led(LED1, 0);
+
+// How often to fetch sensor data (in seconds)
+#define SENSORS_POLL_INTERVAL 3.0
+
+// Send all sensor data or just limited (useful for when running out of memory)
+#define SEND_ALL_SENSORS
+
+// Sensors related includes and initialization
 // Temperature reading from microcontroller
 AnalogIn adc_temp(ADC_TEMP);
 // Voltage reference reading from microcontroller
 AnalogIn adc_vref(ADC_VREF);
 
-#define SENSORS_POLL_INTERVAL 1.0
+// Declaring pointers for access to Pelion Device Management Client resources outside of main()
+MbedCloudClientResource *res_button;
+MbedCloudClientResource *res_led;
+MbedCloudClientResource *res_post;
 
-// Declaring pointers for access to Pelion Device Management Client resources outside of main()
-MbedCloudClientResource *button_res;
-MbedCloudClientResource *led_res;
-MbedCloudClientResource *post_res;
-
-MbedCloudClientResource *temp_res;
-MbedCloudClientResource *volt_res;
-
+// Additional resources for sensor readings
+#ifdef SEND_ALL_SENSORS
+MbedCloudClientResource *res_temperature;
+MbedCloudClientResource *res_voltage;
+#endif /* SEND_ALL_SENSORS */
 
 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
 // This is great because things such as network operations are illegal in ISR, so updating a resource in a button's fall() function is not allowed
 EventQueue eventQueue;
 
+// When the device is registered, this variable will be used to access various useful information, like device ID etc.
+static const ConnectorClientEndpointInfo* endpointInfo;
+
 /**
  * PUT handler - sets the value of the built-in LED
  * @param resource The resource that triggered the callback
@@ -90,8 +101,8 @@
  * This function will be triggered either by a physical button press or by a ticker every 5 seconds (see below)
  */
 void button_press() {
-    int v = button_res->get_value_int() + 1;
-    button_res->set_value(v);
+    int v = res_button->get_value_int() + 1;
+    res_button->set_value(v);
     printf("Button clicked %d times\n", v);
 }
 
@@ -109,8 +120,6 @@
  * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
  * When the device is registered, this variable will be used to access various useful information, like device ID etc.
  */
-
-static const ConnectorClientEndpointInfo* endpointInfo;
 void registered(const ConnectorClientEndpointInfo *endpoint) {
     printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
     endpointInfo = endpoint;
@@ -125,8 +134,8 @@
     float vref = adc_vref.read();
     printf("ADC temp:  %6.4f C,  vref: %6.4f %%\r\n", temp, vref);
     if (endpointInfo) {
-        temp_res->set_value(temp);
-        volt_res->set_value(vref);
+        res_temperature->set_value(temp);
+        res_voltage->set_value(vref);
     }
 }
 
@@ -138,16 +147,11 @@
     if (storage_status != 0) {
         printf("Storage mounting failed.\n");
     }
-
-#if USE_BUTTON == 1
     // If the User button is pressed ons start, then format storage.
     bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE);
     if (btn_pressed) {
         printf("User button is pushed on start...\n");
     }
-#else
-    bool btn_pressed = FALSE;
-#endif /* USE_BUTTON */
 
     if (storage_status || btn_pressed) {
         printf("Formatting the storage...\n");
@@ -181,31 +185,31 @@
     }
 
     // Creating resources, which can be written or read from the cloud
-    button_res = client.create_resource("3200/0/5501", "button_count");
-    button_res->set_value(0);
-    button_res->methods(M2MMethod::GET);
-    button_res->observable(true);
-    button_res->attach_notification_callback(button_callback);
+    res_button = client.create_resource("3200/0/5501", "Button Count");
+    res_button->set_value(0);
+    res_button->methods(M2MMethod::GET);
+    res_button->observable(true);
+    res_button->attach_notification_callback(button_callback);
 
-    led_res = client.create_resource("3201/0/5853", "led_state");
-    led_res->set_value(led.read());
-    led_res->methods(M2MMethod::GET | M2MMethod::PUT);
-    led_res->attach_put_callback(put_callback);
+    res_led = client.create_resource("3201/0/5853", "LED State");
+    res_led->set_value(led.read());
+    res_led->methods(M2MMethod::GET | M2MMethod::PUT);
+    res_led->attach_put_callback(put_callback);
 
-    post_res = client.create_resource("3300/0/5605", "execute_function");
-    post_res->methods(M2MMethod::POST);
-    post_res->attach_post_callback(post_callback);
+    res_post = client.create_resource("3300/0/5605", "Execute Function");
+    res_post->methods(M2MMethod::POST);
+    res_post->attach_post_callback(post_callback);
 
     // Sensor resources
-    temp_res = client.create_resource("3303/0/5700", "temperature");
-    temp_res->set_value(0);
-    temp_res->methods(M2MMethod::GET);
-    temp_res->observable(true);
+    res_temperature = client.create_resource("3303/0/5700", "Temperature (C)");
+    res_temperature->set_value(0);
+    res_temperature->methods(M2MMethod::GET);
+    res_temperature->observable(true);
 
-    volt_res = client.create_resource("3316/0/5700", "voltage");
-    volt_res->set_value(0);
-    volt_res->methods(M2MMethod::GET);
-    volt_res->observable(true);
+    res_voltage = client.create_resource("3316/0/5700", "Voltage");
+    res_voltage->set_value(0);
+    res_voltage->methods(M2MMethod::GET);
+    res_voltage->observable(true);
 
     printf("Initialized Pelion Device Management Client. Registering...\n");