Mbed Cloud Example Project - LPC546xx (Completed Version)
Fork of mbed-cloud-example-lpc546xx by
Diff: main.cpp
- Revision:
- 4:5994ee6e8f63
- Parent:
- 0:1f42bb53bff5
- Child:
- 5:e521ea1f4c22
--- a/main.cpp Mon Sep 24 15:04:20 2018 +0000 +++ b/main.cpp Thu Oct 11 04:05:03 2018 +0000 @@ -28,9 +28,10 @@ // Placeholder to hardware that trigger events (timer, button, etc) Ticker timer; +InterruptIn sw2(SW2); +DigitalOut led(LED1,1); // Placeholder for storage -//SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4); SDBlockDevice sd(D11, D12, D13, D10); //MOSI, MISO, SCLK, CS FATFileSystem fs("sd"); @@ -38,35 +39,34 @@ // Pointers to the resources that will be created in main_application(). static MbedCloudClientResource* pattern_ptr; -// Pointer to mbedClient, used for calling close function. -static SimpleMbedCloudClient *client; - static bool button_pressed = false; - void button_press() { button_pressed = true; } -void pattern_updated(const char *) { - printf("PUT received, new value: %s\n", pattern_ptr->get_value().c_str()); - // Placeholder for PUT action +void led_toggle() { + led = !led; +} + +void blink_rate_updated(const char *) { + printf("PUT received, Storing LED Blink Rate: %s (ms)\r\n", pattern_ptr->get_value().c_str()); } -void blink_callback(void *) { +void blink_enable_callback(void *) { String pattern_str = pattern_ptr->get_value(); const char *pattern = pattern_str.c_str(); - printf("POST received. LED pattern = %s\n", pattern); - // Placeholder for POST action - // The pattern is something like 500:200:500, so parse that. + printf("POST received. Enabling LED Blink Rate = %s (ms)\n", pattern); + + float value = atoi(pattern_ptr->get_value().c_str())/1000.0; + timer.detach(); + timer.attach(&led_toggle,value); } void button_callback(const M2MBase& object, const NoticationDeliveryStatus status) { printf("Button notification. Callback: (%s)\n", object.uri_path()); - // Placeholder for GET } - int main(void) { // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364) @@ -101,8 +101,8 @@ } } + // Connect to Network (obtain IP address) printf("Connecting to the network using Ethernet...\n"); - status = net.connect(); if (status) { printf("Connection to Network Failed %d!\n", status); @@ -113,15 +113,12 @@ printf("IP address %s\n", ip_addr); } + // Mbed Cloud Client Initialization SimpleMbedCloudClient mbedClient(&net); - // Save pointer to mbedClient so that other functions can access it. - client = &mbedClient; - status = mbedClient.init(); if (status) { return -1; } - printf("Client initialized\r\n"); // Mbed Cloud Client resource setup @@ -131,16 +128,18 @@ button->observable(true); button->attach_notification_callback(button_callback); - MbedCloudClientResource *pattern = mbedClient.create_resource("3201/0/5853", "pattern_resource"); - pattern->set_value("500:500:500:500"); + MbedCloudClientResource *pattern = mbedClient.create_resource("3201/0/5853", "blink_rate_resource"); + pattern->set_value("500"); pattern->methods(M2MMethod::GET | M2MMethod::PUT); - pattern->attach_put_callback(pattern_updated); + pattern->observable(false); + pattern->attach_put_callback(blink_rate_updated); pattern_ptr = pattern; - MbedCloudClientResource *blink = mbedClient.create_resource("3201/0/5850", "blink_resource"); + MbedCloudClientResource *blink = mbedClient.create_resource("3201/0/5850", "blink_enable_resource"); blink->methods(M2MMethod::POST); - blink->attach_post_callback(blink_callback); + blink->attach_post_callback(blink_enable_callback); + // Mbed Cloud Register Client and Connect mbedClient.register_and_connect(); // Wait for client to finish registering @@ -148,9 +147,10 @@ wait_ms(100); } - // Placeholder for callback to update local resource when GET comes. - timer.attach(&button_press, 5.0); - + // Setup LED and Push BUtton + timer.attach(&led_toggle, 0.5); + sw2.fall(&button_press); + // Check if client is registering or registered, if true sleep and repeat. while (mbedClient.is_register_called()) { static int button_count = 0; @@ -158,7 +158,7 @@ if (button_pressed) { button_pressed = false; - printf("Simulated button clicked %d times\r\n", ++button_count); + printf("Button Pressed %d time(s).\r\n",(++button_count)); button->set_value(button_count); } }