Simple Mbed Cloud Client application using features of K64 & K66
Connect to Mbed Cloud!
This example was customized a bit for FRDM-K66 and FRDM-K64F.
It depends on having an SD Card plugged in for storage of credentials. It could be changed later to use a SPI flash or other storage on a shield or wired in.
The app keeps track of how many times switch 2 (SW2) is pressed. The value can be retrieved via a GET request to Mbed Cloud.
Also, it will blink a pattern based on milisecond (ms) timing values that can be sent from Mbed Cloud. The pattern can be sent with a PUT request and the blinking sequence can be triggered by a POST request.
Diff: main.cpp
- Revision:
- 2:6ed27f413b30
- Parent:
- 1:1ccf36276cd3
- Child:
- 4:a107dae867fb
diff -r 1ccf36276cd3 -r 6ed27f413b30 main.cpp
--- a/main.cpp Wed Feb 14 10:43:56 2018 +0000
+++ b/main.cpp Wed Feb 14 13:20:30 2018 +0000
@@ -26,9 +26,6 @@
#include "FATFileSystem.h"
#include "EthernetInterface.h"
-#define LED_OFF 1
-
-DigitalOut led(LED_RED, LED_OFF);
InterruptIn button(BUTTON1);
// Pointers to the resources that will be created in main_application().
@@ -45,97 +42,41 @@
void pattern_updated(const char *) {
printf("PUT received, new value: %s\n", pattern_ptr->get_value());
+ // Placeholder for PUT action
}
void blink_callback(void *) {
const char *pattern = pattern_ptr->get_value();
- printf("LED pattern = %s\n", pattern);
+ printf("POST received. LED pattern = %s\n", pattern);
+ // Placeholder for POST action
// The pattern is something like 500:200:500, so parse that.
- // LED blinking is done while parsing.
- led = !led;
- while (*pattern != '\0') {
- // Wait for requested time.
- wait_ms(atoi(pattern));
- led = !led;
- // Search for next value.
- pattern = strchr(pattern, ':');
- if(!pattern) {
- break; // while
- }
- pattern++;
- }
-
- led = LED_OFF;
}
-void button_notification_status_callback(const M2MBase& object, const NoticationDeliveryStatus status)
+void button_callback(const M2MBase& object, const NoticationDeliveryStatus status)
{
- switch(status) {
- case NOTIFICATION_STATUS_BUILD_ERROR:
- printf("Notification callback: (%s) error when building CoAP message\n", object.uri_path());
- break;
- case NOTIFICATION_STATUS_RESEND_QUEUE_FULL:
- printf("Notification callback: (%s) CoAP resend queue full\n", object.uri_path());
- break;
- case NOTIFICATION_STATUS_SENT:
- printf("Notification callback: (%s) Notification sent to server\n", object.uri_path());
- break;
- case NOTIFICATION_STATUS_DELIVERED:
- printf("Notification callback: (%s) Notification delivered\n", object.uri_path());
- break;
- case NOTIFICATION_STATUS_SEND_FAILED:
- printf("Notification callback: (%s) Notification sending failed\n", object.uri_path());
- break;
- case NOTIFICATION_STATUS_SUBSCRIBED:
- printf("Notification callback: (%s) subscribed\n", object.uri_path());
- break;
- case NOTIFICATION_STATUS_UNSUBSCRIBED:
- printf("Notification callback: (%s) subscription removed\n", object.uri_path());
- break;
- default:
- break;
- }
+ printf("Button notification. Callback: (%s)\n", object.uri_path());
+ // Placeholder for GET
}
-// This function is called when a POST request is received for resource 5000/0/1.
-void unregister_cb(void *)
-{
- printf("Unregister resource executed\n");
- client->close();
-}
-
-// This function is called when a POST request is received for resource 5000/0/2.
-void factory_reset_cb(void *)
-{
- printf("Factory reset resource executed\n");
- client->close();
- kcm_status_e kcm_status = kcm_factory_reset();
- if (kcm_status != KCM_STATUS_SUCCESS) {
- printf("Failed to do factory reset - %d\n", kcm_status);
- } else {
- printf("Factory reset completed. Now restart the device\n");
- }
-}
int main(void)
{
- // IOTMORF-1712: DAPLINK starts the previous application during flashing a new binary
- // This is workaround to prevent possible deletion of credentials or storage corruption
- // while replacing the application binary.
+ // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364)
+ // Older versions: workaround to prevent possible deletion of credentials:
wait(2);
// Misc OS setup
srand(time(NULL));
+ // Placeholder for network
EthernetInterface net;
+
+ // Placeholder for storage
SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);
FATFileSystem fs("sd");
printf("Start Simple Mbed Cloud Client\n");
- // Initialize button interrupt
- button.fall(&button_press);
-
// Initialize SD card
int status = sd.init();
if (status != BD_ERROR_OK) {
@@ -148,7 +89,6 @@
if (status) {
printf("Failed to mount FAT file system, reformatting...\r\n");
status = fs.reformat(&sd);
-
if (status) {
printf("Failed to reformat FAT file system\r\n");
return -1;
@@ -169,6 +109,9 @@
printf("IP address %s\n", ip_addr);
}
+ // Placeholder for GET requests. Initialize button interrupt.
+ button.fall(&button_press);
+
SimpleMbedCloudClient mbedClient(&net);
// Save pointer to mbedClient so that other functions can access it.
client = &mbedClient;
@@ -185,7 +128,7 @@
button->set_value("0");
button->methods(M2MMethod::GET);
button->observable(true);
- button->attach_notification(M2MMethod::GET, (void*)button_notification_status_callback);
+ button->attach_notification(M2MMethod::GET, (void*)button_callback);
MbedCloudClientResource *pattern = mbedClient.create_resource("3201/0/5853", "pattern_resource");
pattern->set_value("500:500:500:500");
@@ -197,14 +140,6 @@
blink->methods(M2MMethod::POST);
blink->attach(M2MMethod::POST, (void*)blink_callback);
- MbedCloudClientResource *unregister = mbedClient.create_resource("5000/0/1", "unregister");
- unregister->methods(M2MMethod::POST);
- unregister->attach(M2MMethod::POST, (void*)unregister_cb);
-
- MbedCloudClientResource *factoryReset = mbedClient.create_resource("5000/0/2", "factory_reset");
- factoryReset->methods(M2MMethod::POST);
- factoryReset->attach(M2MMethod::POST, (void*)factory_reset_cb);
-
mbedClient.register_and_connect();
// Check if client is registering or registered, if true sleep and repeat.