Simple Mbed Cloud client application using features of K64 including app-shield, Wi-Fi and SD Card

Dependencies:   simple-mbed-cloud-client LM75B MMA7660

Fork of mbed-cloud-connect-example-wifi by Toyomasa Watarai

Configuration

  • Put ESP-WROOM-02 shield
  • Put Mbed Application Shield
  • Edit the text in the main.cpp

#define WIFI_SSID "SSID"
#define WIFI_PSWD "PASSWORD"
  • Download and replace your developer certificate from Mbed Cloud portal from the menu [Mbed Cloud] - [Certificate]
Committer:
MACRUM
Date:
Mon Jul 02 08:06:49 2018 +0000
Revision:
18:f9c7e854f127
Parent:
15:64d7f2f1f341
Update library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MarceloSalazar 0:e13a8a944e25 1 // ----------------------------------------------------------------------------
MarceloSalazar 0:e13a8a944e25 2 // Copyright 2016-2017 ARM Ltd.
MarceloSalazar 0:e13a8a944e25 3 //
MarceloSalazar 0:e13a8a944e25 4 // SPDX-License-Identifier: Apache-2.0
MarceloSalazar 0:e13a8a944e25 5 //
MarceloSalazar 0:e13a8a944e25 6 // Licensed under the Apache License, Version 2.0 (the "License");
MarceloSalazar 0:e13a8a944e25 7 // you may not use this file except in compliance with the License.
MarceloSalazar 0:e13a8a944e25 8 // You may obtain a copy of the License at
MarceloSalazar 0:e13a8a944e25 9 //
MarceloSalazar 0:e13a8a944e25 10 // http://www.apache.org/licenses/LICENSE-2.0
MarceloSalazar 0:e13a8a944e25 11 //
MarceloSalazar 0:e13a8a944e25 12 // Unless required by applicable law or agreed to in writing, software
MarceloSalazar 0:e13a8a944e25 13 // distributed under the License is distributed on an "AS IS" BASIS,
MarceloSalazar 0:e13a8a944e25 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MarceloSalazar 0:e13a8a944e25 15 // See the License for the specific language governing permissions and
MarceloSalazar 0:e13a8a944e25 16 // limitations under the License.
MarceloSalazar 0:e13a8a944e25 17 // ----------------------------------------------------------------------------
MarceloSalazar 0:e13a8a944e25 18
MarceloSalazar 1:1ccf36276cd3 19 #include "mbed.h"
MarceloSalazar 1:1ccf36276cd3 20 #include "mbed-trace/mbed_trace.h"
MarceloSalazar 1:1ccf36276cd3 21 #include "mbed-trace-helper.h"
MarceloSalazar 0:e13a8a944e25 22 #include "simple-mbed-cloud-client.h"
MarceloSalazar 1:1ccf36276cd3 23 #include "key-config-manager/kcm_status.h"
MarceloSalazar 1:1ccf36276cd3 24 #include "key-config-manager/key_config_manager.h"
MarceloSalazar 1:1ccf36276cd3 25 #include "SDBlockDevice.h"
MarceloSalazar 1:1ccf36276cd3 26 #include "FATFileSystem.h"
MACRUM 11:4cd5fd7c7821 27 #include "ESP8266Interface.h"
MACRUM 13:428ae6d8349a 28 #include "MMA7660.h"
MACRUM 13:428ae6d8349a 29 #include "LM75B.h"
MACRUM 11:4cd5fd7c7821 30
MACRUM 11:4cd5fd7c7821 31 #define WIFI_SSID "SSID"
MACRUM 11:4cd5fd7c7821 32 #define WIFI_PSWD "PASSWORD"
MarceloSalazar 1:1ccf36276cd3 33
maclobdell 8:ace9e5de6491 34 /* The following app uses Mbed Cloud with SD Card storage, button, & led */
maclobdell 8:ace9e5de6491 35
MACRUM 15:64d7f2f1f341 36 #if !defined(TARGET_K64F)
MACRUM 15:64d7f2f1f341 37 #error "This example only works with FRDM-K64F platform."
MACRUM 15:64d7f2f1f341 38 #endif
MACRUM 15:64d7f2f1f341 39
MarceloSalazar 4:a107dae867fb 40 // Placeholder to hardware that trigger events (timer, button, etc)
maclobdell 8:ace9e5de6491 41 InterruptIn sw2(SW2);
maclobdell 8:ace9e5de6491 42 DigitalOut led2(LED2);
MACRUM 11:4cd5fd7c7821 43 ESP8266Interface net(D1, D0);
MACRUM 11:4cd5fd7c7821 44
bridadan 5:c18fab181ede 45 // Placeholder for storage
bridadan 5:c18fab181ede 46 SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);
bridadan 5:c18fab181ede 47 FATFileSystem fs("sd");
maclobdell 8:ace9e5de6491 48
MarceloSalazar 0:e13a8a944e25 49 // Pointers to the resources that will be created in main_application().
MarceloSalazar 1:1ccf36276cd3 50 static MbedCloudClientResource* pattern_ptr;
maclobdell 8:ace9e5de6491 51 static MbedCloudClientResource* button_ptr;
MarceloSalazar 0:e13a8a944e25 52
MarceloSalazar 0:e13a8a944e25 53 // Pointer to mbedClient, used for calling close function.
MarceloSalazar 0:e13a8a944e25 54 static SimpleMbedCloudClient *client;
MarceloSalazar 0:e13a8a944e25 55
MarceloSalazar 1:1ccf36276cd3 56 static bool button_pressed = false;
maclobdell 8:ace9e5de6491 57 static int button_count = 0;
maclobdell 8:ace9e5de6491 58
MarceloSalazar 1:1ccf36276cd3 59 void button_press() {
MarceloSalazar 1:1ccf36276cd3 60 button_pressed = true;
maclobdell 8:ace9e5de6491 61 ++button_count;
maclobdell 8:ace9e5de6491 62 button_ptr->set_value(button_count);
MarceloSalazar 1:1ccf36276cd3 63 }
MarceloSalazar 1:1ccf36276cd3 64
MarceloSalazar 1:1ccf36276cd3 65 void pattern_updated(const char *) {
bridadan 5:c18fab181ede 66 printf("PUT received, new value: %s\n", pattern_ptr->get_value().c_str());
MarceloSalazar 2:6ed27f413b30 67 // Placeholder for PUT action
MarceloSalazar 0:e13a8a944e25 68 }
MarceloSalazar 0:e13a8a944e25 69
MarceloSalazar 0:e13a8a944e25 70 void blink_callback(void *) {
bridadan 5:c18fab181ede 71 String pattern_str = pattern_ptr->get_value();
bridadan 5:c18fab181ede 72 const char *pattern = pattern_str.c_str();
MarceloSalazar 2:6ed27f413b30 73 printf("POST received. LED pattern = %s\n", pattern);
MarceloSalazar 2:6ed27f413b30 74 // Placeholder for POST action
MarceloSalazar 0:e13a8a944e25 75 // The pattern is something like 500:200:500, so parse that.
maclobdell 8:ace9e5de6491 76 // LED blinking is done while parsing.
maclobdell 8:ace9e5de6491 77
maclobdell 8:ace9e5de6491 78 while (*pattern != '\0') {
maclobdell 8:ace9e5de6491 79 //make a short blink on the led
maclobdell 8:ace9e5de6491 80 led2 = 0;
maclobdell 8:ace9e5de6491 81 wait_ms(20);
maclobdell 8:ace9e5de6491 82 led2 = 1;
maclobdell 8:ace9e5de6491 83 // Wait for requested time.
maclobdell 8:ace9e5de6491 84 wait_ms(atoi(pattern));
maclobdell 8:ace9e5de6491 85 // Search for next value.
maclobdell 8:ace9e5de6491 86 pattern = strchr(pattern, ':');
maclobdell 8:ace9e5de6491 87 if(!pattern) {
maclobdell 8:ace9e5de6491 88 //we're done, give one last blink to end the pattern
maclobdell 8:ace9e5de6491 89 led2 = 0;
maclobdell 8:ace9e5de6491 90 wait_ms(20);
maclobdell 8:ace9e5de6491 91 led2 = 1;
maclobdell 8:ace9e5de6491 92 break; // while
maclobdell 8:ace9e5de6491 93 }
maclobdell 8:ace9e5de6491 94 pattern++;
maclobdell 8:ace9e5de6491 95 }
MarceloSalazar 0:e13a8a944e25 96 }
MarceloSalazar 0:e13a8a944e25 97
MarceloSalazar 2:6ed27f413b30 98 void button_callback(const M2MBase& object, const NoticationDeliveryStatus status)
MarceloSalazar 0:e13a8a944e25 99 {
MarceloSalazar 2:6ed27f413b30 100 printf("Button notification. Callback: (%s)\n", object.uri_path());
MarceloSalazar 2:6ed27f413b30 101 // Placeholder for GET
MarceloSalazar 0:e13a8a944e25 102 }
MarceloSalazar 0:e13a8a944e25 103
MACRUM 13:428ae6d8349a 104 void accel_callback(const M2MBase& object, const NoticationDeliveryStatus status)
MACRUM 13:428ae6d8349a 105 {
MACRUM 13:428ae6d8349a 106 // Do nothing.
MACRUM 13:428ae6d8349a 107 }
MACRUM 13:428ae6d8349a 108
MACRUM 13:428ae6d8349a 109 void temp_callback(const M2MBase& object, const NoticationDeliveryStatus status)
MACRUM 13:428ae6d8349a 110 {
MACRUM 13:428ae6d8349a 111 // Do nothing.
MACRUM 13:428ae6d8349a 112 }
MACRUM 13:428ae6d8349a 113
MarceloSalazar 0:e13a8a944e25 114 int main(void)
MarceloSalazar 0:e13a8a944e25 115 {
MarceloSalazar 2:6ed27f413b30 116 // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364)
MarceloSalazar 2:6ed27f413b30 117 // Older versions: workaround to prevent possible deletion of credentials:
MarceloSalazar 0:e13a8a944e25 118 wait(2);
MarceloSalazar 0:e13a8a944e25 119
MarceloSalazar 1:1ccf36276cd3 120 // Misc OS setup
MarceloSalazar 1:1ccf36276cd3 121 srand(time(NULL));
MarceloSalazar 1:1ccf36276cd3 122
MarceloSalazar 1:1ccf36276cd3 123 printf("Start Simple Mbed Cloud Client\n");
MarceloSalazar 1:1ccf36276cd3 124
MarceloSalazar 1:1ccf36276cd3 125 // Initialize SD card
MarceloSalazar 1:1ccf36276cd3 126 int status = sd.init();
MarceloSalazar 1:1ccf36276cd3 127 if (status != BD_ERROR_OK) {
MarceloSalazar 1:1ccf36276cd3 128 printf("Failed to init SD card\r\n");
MarceloSalazar 1:1ccf36276cd3 129 return -1;
MarceloSalazar 1:1ccf36276cd3 130 }
MarceloSalazar 0:e13a8a944e25 131
MarceloSalazar 1:1ccf36276cd3 132 // Mount the file system (reformatting on failure)
MarceloSalazar 1:1ccf36276cd3 133 status = fs.mount(&sd);
MarceloSalazar 1:1ccf36276cd3 134 if (status) {
MarceloSalazar 1:1ccf36276cd3 135 printf("Failed to mount FAT file system, reformatting...\r\n");
MarceloSalazar 1:1ccf36276cd3 136 status = fs.reformat(&sd);
MarceloSalazar 1:1ccf36276cd3 137 if (status) {
MarceloSalazar 1:1ccf36276cd3 138 printf("Failed to reformat FAT file system\r\n");
MarceloSalazar 1:1ccf36276cd3 139 return -1;
MarceloSalazar 1:1ccf36276cd3 140 } else {
MarceloSalazar 1:1ccf36276cd3 141 printf("Reformat and mount complete\r\n");
MarceloSalazar 1:1ccf36276cd3 142 }
MarceloSalazar 1:1ccf36276cd3 143 }
MarceloSalazar 1:1ccf36276cd3 144
MACRUM 11:4cd5fd7c7821 145 printf("Connecting to the network using Wi-Fi...\n");
MarceloSalazar 1:1ccf36276cd3 146
MACRUM 11:4cd5fd7c7821 147 status = net.connect(WIFI_SSID, WIFI_PSWD, NSAPI_SECURITY_WPA_WPA2);
MACRUM 11:4cd5fd7c7821 148
MarceloSalazar 1:1ccf36276cd3 149 if (status) {
MarceloSalazar 1:1ccf36276cd3 150 printf("Connection to Network Failed %d!\n", status);
MarceloSalazar 1:1ccf36276cd3 151 return -1;
MarceloSalazar 1:1ccf36276cd3 152 } else {
MarceloSalazar 1:1ccf36276cd3 153 const char *ip_addr = net.get_ip_address();
MarceloSalazar 1:1ccf36276cd3 154 printf("Connected successfully\n");
MarceloSalazar 1:1ccf36276cd3 155 printf("IP address %s\n", ip_addr);
MarceloSalazar 1:1ccf36276cd3 156 }
MarceloSalazar 1:1ccf36276cd3 157
MarceloSalazar 1:1ccf36276cd3 158 SimpleMbedCloudClient mbedClient(&net);
MACRUM 15:64d7f2f1f341 159
MarceloSalazar 0:e13a8a944e25 160 // Save pointer to mbedClient so that other functions can access it.
MarceloSalazar 0:e13a8a944e25 161 client = &mbedClient;
MarceloSalazar 0:e13a8a944e25 162
MarceloSalazar 1:1ccf36276cd3 163 status = mbedClient.init();
MarceloSalazar 1:1ccf36276cd3 164 if (status) {
MarceloSalazar 1:1ccf36276cd3 165 return -1;
MarceloSalazar 1:1ccf36276cd3 166 }
MarceloSalazar 1:1ccf36276cd3 167
MarceloSalazar 0:e13a8a944e25 168 printf("Client initialized\r\n");
MarceloSalazar 0:e13a8a944e25 169
MarceloSalazar 1:1ccf36276cd3 170 // Mbed Cloud Client resource setup
MarceloSalazar 1:1ccf36276cd3 171 MbedCloudClientResource *button = mbedClient.create_resource("3200/0/5501", "button_resource");
MarceloSalazar 1:1ccf36276cd3 172 button->set_value("0");
MarceloSalazar 1:1ccf36276cd3 173 button->methods(M2MMethod::GET);
MarceloSalazar 1:1ccf36276cd3 174 button->observable(true);
bridadan 5:c18fab181ede 175 button->attach_notification_callback(button_callback);
maclobdell 8:ace9e5de6491 176 button_ptr = button;
maclobdell 8:ace9e5de6491 177
MarceloSalazar 1:1ccf36276cd3 178 MbedCloudClientResource *pattern = mbedClient.create_resource("3201/0/5853", "pattern_resource");
MarceloSalazar 1:1ccf36276cd3 179 pattern->set_value("500:500:500:500");
MarceloSalazar 1:1ccf36276cd3 180 pattern->methods(M2MMethod::GET | M2MMethod::PUT);
bridadan 5:c18fab181ede 181 pattern->attach_put_callback(pattern_updated);
MarceloSalazar 1:1ccf36276cd3 182 pattern_ptr = pattern;
MarceloSalazar 0:e13a8a944e25 183
MarceloSalazar 1:1ccf36276cd3 184 MbedCloudClientResource *blink = mbedClient.create_resource("3201/0/5850", "blink_resource");
MarceloSalazar 1:1ccf36276cd3 185 blink->methods(M2MMethod::POST);
bridadan 5:c18fab181ede 186 blink->attach_post_callback(blink_callback);
MarceloSalazar 0:e13a8a944e25 187
MACRUM 13:428ae6d8349a 188 /* Accelerometer */
MACRUM 13:428ae6d8349a 189 const int NUM_AXIS = 3;
MACRUM 13:428ae6d8349a 190 MbedCloudClientResource* accel[NUM_AXIS];
MACRUM 13:428ae6d8349a 191 accel[0] = mbedClient.create_resource("3313/0/5702", "accel_x");
MACRUM 13:428ae6d8349a 192 accel[1] = mbedClient.create_resource("3313/0/5703", "accel_y");
MACRUM 13:428ae6d8349a 193 accel[2] = mbedClient.create_resource("3313/0/5704", "accel_z");
MACRUM 13:428ae6d8349a 194 for (int i=0; i < NUM_AXIS; i++) {
MACRUM 13:428ae6d8349a 195 accel[i]->set_value(0);
MACRUM 13:428ae6d8349a 196 accel[i]->methods(M2MMethod::GET);
MACRUM 13:428ae6d8349a 197 accel[i]->attach_notification_callback(accel_callback);
MACRUM 13:428ae6d8349a 198 accel[i]->observable(true);
MACRUM 13:428ae6d8349a 199 }
MACRUM 13:428ae6d8349a 200 MbedCloudClientResource* acc_unit = mbedClient.create_resource("3313/0/5701", "unit");
MACRUM 13:428ae6d8349a 201 acc_unit->set_value("G");
MACRUM 13:428ae6d8349a 202
MACRUM 13:428ae6d8349a 203 /* Temperature */
MACRUM 13:428ae6d8349a 204 MbedCloudClientResource *temp = mbedClient.create_resource("3303/0/5700", "temperature");
MACRUM 13:428ae6d8349a 205 temp->set_value("0");
MACRUM 13:428ae6d8349a 206 temp->methods(M2MMethod::GET);
MACRUM 13:428ae6d8349a 207 temp->attach_notification_callback(temp_callback);
MACRUM 13:428ae6d8349a 208 temp->observable(true);
MACRUM 13:428ae6d8349a 209 MbedCloudClientResource *temp_unit = mbedClient.create_resource("3303/0/5701", "unit");
MACRUM 13:428ae6d8349a 210 temp_unit->set_value("Cel");
MACRUM 13:428ae6d8349a 211
MarceloSalazar 0:e13a8a944e25 212 mbedClient.register_and_connect();
MarceloSalazar 0:e13a8a944e25 213
MACRUM 13:428ae6d8349a 214 printf("Waiting for register and connect ");
bridadan 6:254a7e7fbef1 215 // Wait for client to finish registering
bridadan 6:254a7e7fbef1 216 while (!mbedClient.is_client_registered()) {
MACRUM 13:428ae6d8349a 217 printf(".");
MACRUM 13:428ae6d8349a 218 wait_ms(500);
bridadan 6:254a7e7fbef1 219 }
MACRUM 13:428ae6d8349a 220 printf("\n\n");
bridadan 6:254a7e7fbef1 221
MACRUM 15:64d7f2f1f341 222 sw2.mode(PullUp);
MACRUM 15:64d7f2f1f341 223 sw2.fall(button_press);
MACRUM 15:64d7f2f1f341 224 button_count = 0;
MACRUM 13:428ae6d8349a 225
MACRUM 13:428ae6d8349a 226 // For sensors
MACRUM 13:428ae6d8349a 227 LM75B lm75b(I2C_SDA, I2C_SCL); // temperature
MACRUM 13:428ae6d8349a 228 MMA7660 mma7660(I2C_SDA, I2C_SCL); // accel
MACRUM 13:428ae6d8349a 229
MACRUM 13:428ae6d8349a 230 const long measurementPeriod = 5; // sec
MACRUM 13:428ae6d8349a 231 Timer timer;
MACRUM 13:428ae6d8349a 232 timer.start();
MACRUM 13:428ae6d8349a 233
MarceloSalazar 0:e13a8a944e25 234 // Check if client is registering or registered, if true sleep and repeat.
MarceloSalazar 0:e13a8a944e25 235 while (mbedClient.is_register_called()) {
maclobdell 8:ace9e5de6491 236
MarceloSalazar 1:1ccf36276cd3 237 wait_ms(100);
MarceloSalazar 1:1ccf36276cd3 238
MarceloSalazar 1:1ccf36276cd3 239 if (button_pressed) {
MarceloSalazar 1:1ccf36276cd3 240 button_pressed = false;
maclobdell 8:ace9e5de6491 241 printf("button clicked %d times\r\n", button_count);
MarceloSalazar 0:e13a8a944e25 242 }
MACRUM 13:428ae6d8349a 243 if( timer.read() > measurementPeriod) {
MACRUM 13:428ae6d8349a 244 timer.reset();
MACRUM 13:428ae6d8349a 245 float temperature, acc[3];
MACRUM 13:428ae6d8349a 246 const unsigned int buf_size = 20;
MACRUM 13:428ae6d8349a 247 char buf[buf_size];
MACRUM 13:428ae6d8349a 248
MACRUM 13:428ae6d8349a 249 mma7660.readData(acc);
MACRUM 13:428ae6d8349a 250 for(int i=0; i < NUM_AXIS; i++) {
MACRUM 13:428ae6d8349a 251 snprintf(buf, buf_size, "%f", acc[i]);
MACRUM 13:428ae6d8349a 252 accel[i]->set_value(buf);
MACRUM 13:428ae6d8349a 253 }
MACRUM 13:428ae6d8349a 254 printf("acc: %f,%f,%f\n", acc[0], acc[1], acc[2]);
MACRUM 13:428ae6d8349a 255
MACRUM 13:428ae6d8349a 256 temperature = lm75b.read();
MACRUM 13:428ae6d8349a 257 snprintf(buf, buf_size, "%f", temperature);
MACRUM 13:428ae6d8349a 258 temp->set_value(buf);
MACRUM 13:428ae6d8349a 259 printf("temp: %s\n", buf);
MACRUM 13:428ae6d8349a 260 }
MACRUM 15:64d7f2f1f341 261 }
MACRUM 13:428ae6d8349a 262
MarceloSalazar 0:e13a8a944e25 263 // Client unregistered, exit program.
MarceloSalazar 0:e13a8a944e25 264 return 0;
MarceloSalazar 0:e13a8a944e25 265 }