Mbed Cloud example program for workshop in W27 2018.

Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

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