Simple Mbed Cloud client application using features of K64F including Wi-Fi and SD Card
Fork of mbed-cloud-connect-example-ethernet by
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]
main.cpp@19:999d13f83602, 2018-07-03 (annotated)
- Committer:
- MACRUM
- Date:
- Tue Jul 03 04:32:09 2018 +0000
- Revision:
- 19:999d13f83602
- Parent:
- 18:139960a75894
Add sensor access code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maclobdell | 11:ae1f6fe932dc | 1 | // ---------------------------------------------------------------------------- |
maclobdell | 11:ae1f6fe932dc | 2 | // Copyright 2016-2018 ARM Ltd. |
maclobdell | 11:ae1f6fe932dc | 3 | // |
maclobdell | 11:ae1f6fe932dc | 4 | // SPDX-License-Identifier: Apache-2.0 |
maclobdell | 11:ae1f6fe932dc | 5 | // |
maclobdell | 11:ae1f6fe932dc | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
maclobdell | 11:ae1f6fe932dc | 7 | // you may not use this file except in compliance with the License. |
maclobdell | 11:ae1f6fe932dc | 8 | // You may obtain a copy of the License at |
maclobdell | 11:ae1f6fe932dc | 9 | // |
maclobdell | 11:ae1f6fe932dc | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
maclobdell | 11:ae1f6fe932dc | 11 | // |
maclobdell | 11:ae1f6fe932dc | 12 | // Unless required by applicable law or agreed to in writing, software |
maclobdell | 11:ae1f6fe932dc | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
maclobdell | 11:ae1f6fe932dc | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
maclobdell | 11:ae1f6fe932dc | 15 | // See the License for the specific language governing permissions and |
maclobdell | 11:ae1f6fe932dc | 16 | // limitations under the License. |
maclobdell | 11:ae1f6fe932dc | 17 | // ---------------------------------------------------------------------------- |
maclobdell | 11:ae1f6fe932dc | 18 | |
maclobdell | 11:ae1f6fe932dc | 19 | #include "mbed.h" |
maclobdell | 11:ae1f6fe932dc | 20 | #include "simple-mbed-cloud-client.h" |
maclobdell | 11:ae1f6fe932dc | 21 | #include "SDBlockDevice.h" |
maclobdell | 11:ae1f6fe932dc | 22 | #include "FATFileSystem.h" |
MACRUM | 18:139960a75894 | 23 | #include "ESP8266Interface.h" |
MACRUM | 19:999d13f83602 | 24 | #include "MMA7660.h" |
MACRUM | 19:999d13f83602 | 25 | #include "LM75B.h" |
MACRUM | 18:139960a75894 | 26 | |
MACRUM | 18:139960a75894 | 27 | #define WIFI_SSID "SSID" |
MACRUM | 18:139960a75894 | 28 | #define WIFI_PSWD "PASSWORD" |
maclobdell | 11:ae1f6fe932dc | 29 | |
maclobdell | 11:ae1f6fe932dc | 30 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
maclobdell | 11:ae1f6fe932dc | 31 | // 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 |
maclobdell | 11:ae1f6fe932dc | 32 | EventQueue eventQueue; |
maclobdell | 11:ae1f6fe932dc | 33 | Thread thread1; |
maclobdell | 11:ae1f6fe932dc | 34 | |
maclobdell | 11:ae1f6fe932dc | 35 | // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available) |
MACRUM | 19:999d13f83602 | 36 | /* K64 & K66 */ |
maclobdell | 11:ae1f6fe932dc | 37 | InterruptIn sw2(SW2); |
maclobdell | 11:ae1f6fe932dc | 38 | DigitalOut led2(LED2); |
MACRUM | 19:999d13f83602 | 39 | |
maclobdell | 11:ae1f6fe932dc | 40 | SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4); |
maclobdell | 11:ae1f6fe932dc | 41 | FATFileSystem fs("sd", &sd); |
MACRUM | 18:139960a75894 | 42 | ESP8266Interface net(D1, D0); |
MACRUM | 19:999d13f83602 | 43 | LM75B lm75b(I2C_SDA, I2C_SCL); // temperature |
MACRUM | 19:999d13f83602 | 44 | MMA7660 mma7660(I2C_SDA, I2C_SCL); // accel |
MACRUM | 18:139960a75894 | 45 | |
MACRUM | 19:999d13f83602 | 46 | const int NUM_AXIS = 3; |
maclobdell | 11:ae1f6fe932dc | 47 | |
maclobdell | 11:ae1f6fe932dc | 48 | // Declaring pointers for access to Mbed Cloud Client resources outside of main() |
maclobdell | 11:ae1f6fe932dc | 49 | MbedCloudClientResource *button_res; |
maclobdell | 11:ae1f6fe932dc | 50 | MbedCloudClientResource *pattern_res; |
MACRUM | 19:999d13f83602 | 51 | MbedCloudClientResource *temp_res; |
MACRUM | 19:999d13f83602 | 52 | MbedCloudClientResource *temp_unit_res; |
MACRUM | 19:999d13f83602 | 53 | MbedCloudClientResource *accel_res[NUM_AXIS]; |
MACRUM | 19:999d13f83602 | 54 | MbedCloudClientResource *acc_unit_res; |
maclobdell | 11:ae1f6fe932dc | 55 | |
maclobdell | 11:ae1f6fe932dc | 56 | static bool button_pressed = false; |
maclobdell | 11:ae1f6fe932dc | 57 | static int button_count = 0; |
MACRUM | 19:999d13f83602 | 58 | |
MACRUM | 19:999d13f83602 | 59 | void button_press() |
MACRUM | 19:999d13f83602 | 60 | { |
maclobdell | 11:ae1f6fe932dc | 61 | button_pressed = true; |
maclobdell | 11:ae1f6fe932dc | 62 | ++button_count; |
maclobdell | 11:ae1f6fe932dc | 63 | button_res->set_value(button_count); |
maclobdell | 11:ae1f6fe932dc | 64 | } |
maclobdell | 11:ae1f6fe932dc | 65 | |
maclobdell | 11:ae1f6fe932dc | 66 | /** |
maclobdell | 11:ae1f6fe932dc | 67 | * PUT handler |
maclobdell | 11:ae1f6fe932dc | 68 | * @param resource The resource that triggered the callback |
maclobdell | 11:ae1f6fe932dc | 69 | * @param newValue Updated value for the resource |
maclobdell | 11:ae1f6fe932dc | 70 | */ |
MACRUM | 19:999d13f83602 | 71 | void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) |
MACRUM | 19:999d13f83602 | 72 | { |
maclobdell | 11:ae1f6fe932dc | 73 | printf("PUT received, new value: %s\n", newValue.c_str()); |
maclobdell | 11:ae1f6fe932dc | 74 | } |
maclobdell | 11:ae1f6fe932dc | 75 | |
maclobdell | 11:ae1f6fe932dc | 76 | /** |
maclobdell | 11:ae1f6fe932dc | 77 | * POST handler |
maclobdell | 11:ae1f6fe932dc | 78 | * @param resource The resource that triggered the callback |
maclobdell | 11:ae1f6fe932dc | 79 | * @param buffer If a body was passed to the POST function, this contains the data. |
maclobdell | 11:ae1f6fe932dc | 80 | * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. |
maclobdell | 11:ae1f6fe932dc | 81 | * @param size Size of the body |
maclobdell | 11:ae1f6fe932dc | 82 | */ |
MACRUM | 19:999d13f83602 | 83 | void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) |
MACRUM | 19:999d13f83602 | 84 | { |
maclobdell | 11:ae1f6fe932dc | 85 | printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str()); |
maclobdell | 11:ae1f6fe932dc | 86 | |
maclobdell | 11:ae1f6fe932dc | 87 | static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern |
maclobdell | 11:ae1f6fe932dc | 88 | |
maclobdell | 11:ae1f6fe932dc | 89 | // Parse the pattern string, and toggle the LED in that pattern |
maclobdell | 11:ae1f6fe932dc | 90 | string s = std::string(pattern_res->get_value().c_str()); |
maclobdell | 11:ae1f6fe932dc | 91 | size_t i = 0; |
maclobdell | 11:ae1f6fe932dc | 92 | size_t pos = s.find(':'); |
maclobdell | 11:ae1f6fe932dc | 93 | while (pos != string::npos) { |
maclobdell | 11:ae1f6fe932dc | 94 | wait_ms(atoi(s.substr(i, pos - i).c_str())); |
maclobdell | 11:ae1f6fe932dc | 95 | augmentedLed = !augmentedLed; |
maclobdell | 11:ae1f6fe932dc | 96 | |
maclobdell | 11:ae1f6fe932dc | 97 | i = ++pos; |
maclobdell | 11:ae1f6fe932dc | 98 | pos = s.find(':', pos); |
maclobdell | 11:ae1f6fe932dc | 99 | |
maclobdell | 11:ae1f6fe932dc | 100 | if (pos == string::npos) { |
maclobdell | 11:ae1f6fe932dc | 101 | wait_ms(atoi(s.substr(i, s.length()).c_str())); |
maclobdell | 11:ae1f6fe932dc | 102 | augmentedLed = !augmentedLed; |
maclobdell | 11:ae1f6fe932dc | 103 | } |
maclobdell | 11:ae1f6fe932dc | 104 | } |
maclobdell | 11:ae1f6fe932dc | 105 | } |
maclobdell | 11:ae1f6fe932dc | 106 | |
maclobdell | 11:ae1f6fe932dc | 107 | /** |
maclobdell | 11:ae1f6fe932dc | 108 | * Notification callback handler |
maclobdell | 11:ae1f6fe932dc | 109 | * @param resource The resource that triggered the callback |
maclobdell | 11:ae1f6fe932dc | 110 | * @param status The delivery status of the notification |
maclobdell | 11:ae1f6fe932dc | 111 | */ |
MACRUM | 19:999d13f83602 | 112 | void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) |
MACRUM | 19:999d13f83602 | 113 | { |
maclobdell | 11:ae1f6fe932dc | 114 | printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); |
maclobdell | 11:ae1f6fe932dc | 115 | } |
maclobdell | 11:ae1f6fe932dc | 116 | |
MACRUM | 19:999d13f83602 | 117 | void temp_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) |
MACRUM | 19:999d13f83602 | 118 | { |
MACRUM | 19:999d13f83602 | 119 | printf("Temperature notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); |
MACRUM | 19:999d13f83602 | 120 | } |
MACRUM | 19:999d13f83602 | 121 | |
MACRUM | 19:999d13f83602 | 122 | void accel_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) |
MACRUM | 19:999d13f83602 | 123 | { |
MACRUM | 19:999d13f83602 | 124 | printf("Accelerometer notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); |
MACRUM | 19:999d13f83602 | 125 | } |
MACRUM | 19:999d13f83602 | 126 | |
MACRUM | 19:999d13f83602 | 127 | void measure_sensors() |
MACRUM | 19:999d13f83602 | 128 | { |
MACRUM | 19:999d13f83602 | 129 | float temperature, acc[3]; |
MACRUM | 19:999d13f83602 | 130 | const unsigned int buf_size = 20; |
MACRUM | 19:999d13f83602 | 131 | char buf[buf_size]; |
MACRUM | 19:999d13f83602 | 132 | |
MACRUM | 19:999d13f83602 | 133 | mma7660.readData(acc); |
MACRUM | 19:999d13f83602 | 134 | for(int i=0; i < NUM_AXIS; i++) { |
MACRUM | 19:999d13f83602 | 135 | snprintf(buf, buf_size, "%f", acc[i]); |
MACRUM | 19:999d13f83602 | 136 | accel_res[i]->set_value(buf); |
MACRUM | 19:999d13f83602 | 137 | } |
MACRUM | 19:999d13f83602 | 138 | printf("acc: %f,%f,%f\n", acc[0], acc[1], acc[2]); |
MACRUM | 19:999d13f83602 | 139 | |
MACRUM | 19:999d13f83602 | 140 | temperature = lm75b.read(); |
MACRUM | 19:999d13f83602 | 141 | snprintf(buf, buf_size, "%f", temperature); |
MACRUM | 19:999d13f83602 | 142 | temp_res->set_value(buf); |
MACRUM | 19:999d13f83602 | 143 | printf("temp: %s\n", buf); |
MACRUM | 19:999d13f83602 | 144 | } |
MACRUM | 19:999d13f83602 | 145 | |
maclobdell | 11:ae1f6fe932dc | 146 | /** |
maclobdell | 11:ae1f6fe932dc | 147 | * Registration callback handler |
maclobdell | 11:ae1f6fe932dc | 148 | * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) |
maclobdell | 11:ae1f6fe932dc | 149 | */ |
MACRUM | 19:999d13f83602 | 150 | void registered(const ConnectorClientEndpointInfo *endpoint) |
MACRUM | 19:999d13f83602 | 151 | { |
maclobdell | 11:ae1f6fe932dc | 152 | printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); |
maclobdell | 11:ae1f6fe932dc | 153 | } |
maclobdell | 11:ae1f6fe932dc | 154 | |
MACRUM | 19:999d13f83602 | 155 | int main(void) |
MACRUM | 19:999d13f83602 | 156 | { |
maclobdell | 11:ae1f6fe932dc | 157 | printf("Starting Simple Mbed Cloud Client example\n"); |
MACRUM | 18:139960a75894 | 158 | printf("Connecting to the network using Wi-Fi...\n"); |
maclobdell | 11:ae1f6fe932dc | 159 | |
maclobdell | 11:ae1f6fe932dc | 160 | // Connect to the internet (DHCP is expected to be on) |
MACRUM | 18:139960a75894 | 161 | nsapi_error_t status = net.connect(WIFI_SSID, WIFI_PSWD, NSAPI_SECURITY_WPA_WPA2); |
maclobdell | 11:ae1f6fe932dc | 162 | |
maclobdell | 11:ae1f6fe932dc | 163 | if (status != 0) { |
maclobdell | 11:ae1f6fe932dc | 164 | printf("Connecting to the network failed %d!\n", status); |
maclobdell | 11:ae1f6fe932dc | 165 | return -1; |
maclobdell | 11:ae1f6fe932dc | 166 | } |
maclobdell | 11:ae1f6fe932dc | 167 | |
maclobdell | 11:ae1f6fe932dc | 168 | printf("Connected to the network successfully. IP address: %s\n", net.get_ip_address()); |
maclobdell | 11:ae1f6fe932dc | 169 | |
maclobdell | 11:ae1f6fe932dc | 170 | // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud |
maclobdell | 11:ae1f6fe932dc | 171 | SimpleMbedCloudClient client(&net, &sd, &fs); |
maclobdell | 11:ae1f6fe932dc | 172 | int client_status = client.init(); |
maclobdell | 11:ae1f6fe932dc | 173 | if (client_status != 0) { |
maclobdell | 11:ae1f6fe932dc | 174 | printf("Initializing Mbed Cloud Client failed (%d)\n", client_status); |
maclobdell | 11:ae1f6fe932dc | 175 | return -1; |
maclobdell | 11:ae1f6fe932dc | 176 | } |
maclobdell | 11:ae1f6fe932dc | 177 | |
maclobdell | 11:ae1f6fe932dc | 178 | // Creating resources, which can be written or read from the cloud |
maclobdell | 11:ae1f6fe932dc | 179 | button_res = client.create_resource("3200/0/5501", "button_count"); |
maclobdell | 11:ae1f6fe932dc | 180 | button_res->set_value(0); |
maclobdell | 11:ae1f6fe932dc | 181 | button_res->methods(M2MMethod::GET); |
maclobdell | 11:ae1f6fe932dc | 182 | button_res->observable(true); |
maclobdell | 11:ae1f6fe932dc | 183 | button_res->attach_notification_callback(button_callback); |
maclobdell | 11:ae1f6fe932dc | 184 | |
maclobdell | 11:ae1f6fe932dc | 185 | pattern_res = client.create_resource("3201/0/5853", "blink_pattern"); |
maclobdell | 11:ae1f6fe932dc | 186 | pattern_res->set_value("500:500:500:500:500:500:500:500"); |
maclobdell | 11:ae1f6fe932dc | 187 | pattern_res->methods(M2MMethod::GET | M2MMethod::PUT); |
maclobdell | 11:ae1f6fe932dc | 188 | pattern_res->attach_put_callback(pattern_updated); |
maclobdell | 11:ae1f6fe932dc | 189 | |
maclobdell | 11:ae1f6fe932dc | 190 | MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action"); |
maclobdell | 11:ae1f6fe932dc | 191 | blink_res->methods(M2MMethod::POST); |
maclobdell | 11:ae1f6fe932dc | 192 | blink_res->attach_post_callback(blink_callback); |
MACRUM | 19:999d13f83602 | 193 | |
MACRUM | 19:999d13f83602 | 194 | temp_res = client.create_resource("3303/0/5700", "temperature"); |
MACRUM | 19:999d13f83602 | 195 | temp_res->set_value("0"); |
MACRUM | 19:999d13f83602 | 196 | temp_res->methods(M2MMethod::GET); |
MACRUM | 19:999d13f83602 | 197 | temp_res->attach_notification_callback(temp_callback); |
MACRUM | 19:999d13f83602 | 198 | temp_res->observable(true); |
maclobdell | 11:ae1f6fe932dc | 199 | |
MACRUM | 19:999d13f83602 | 200 | temp_unit_res = client.create_resource("3303/0/5701", "unit"); |
MACRUM | 19:999d13f83602 | 201 | temp_unit_res->set_value("Cel"); |
MACRUM | 19:999d13f83602 | 202 | |
MACRUM | 19:999d13f83602 | 203 | accel_res[0] = client.create_resource("3313/0/5702", "accel_x"); |
MACRUM | 19:999d13f83602 | 204 | accel_res[1] = client.create_resource("3313/0/5703", "accel_y"); |
MACRUM | 19:999d13f83602 | 205 | accel_res[2] = client.create_resource("3313/0/5704", "accel_z"); |
MACRUM | 19:999d13f83602 | 206 | |
MACRUM | 19:999d13f83602 | 207 | for (int i=0; i < NUM_AXIS; i++) { |
MACRUM | 19:999d13f83602 | 208 | accel_res[i]->set_value(0); |
MACRUM | 19:999d13f83602 | 209 | accel_res[i]->methods(M2MMethod::GET); |
MACRUM | 19:999d13f83602 | 210 | accel_res[i]->attach_notification_callback(accel_callback); |
MACRUM | 19:999d13f83602 | 211 | accel_res[i]->observable(true); |
MACRUM | 19:999d13f83602 | 212 | } |
MACRUM | 19:999d13f83602 | 213 | |
MACRUM | 19:999d13f83602 | 214 | acc_unit_res = client.create_resource("3313/0/5701", "unit"); |
MACRUM | 19:999d13f83602 | 215 | acc_unit_res->set_value("G"); |
MACRUM | 19:999d13f83602 | 216 | |
maclobdell | 11:ae1f6fe932dc | 217 | printf("Initialized Mbed Cloud Client. Registering...\n"); |
maclobdell | 11:ae1f6fe932dc | 218 | |
maclobdell | 11:ae1f6fe932dc | 219 | // Callback that fires when registering is complete |
maclobdell | 11:ae1f6fe932dc | 220 | client.on_registered(®istered); |
maclobdell | 11:ae1f6fe932dc | 221 | |
maclobdell | 11:ae1f6fe932dc | 222 | // Register with Mbed Cloud |
maclobdell | 11:ae1f6fe932dc | 223 | client.register_and_connect(); |
maclobdell | 11:ae1f6fe932dc | 224 | |
MACRUM | 19:999d13f83602 | 225 | // Setup the button |
MACRUM | 19:999d13f83602 | 226 | sw2.mode(PullUp); |
MACRUM | 19:999d13f83602 | 227 | |
maclobdell | 11:ae1f6fe932dc | 228 | // The button fall handler is placed in the event queue so it will run in |
MACRUM | 19:999d13f83602 | 229 | // thread context instead of ISR context, which allows safely updating the cloud resource |
MACRUM | 19:999d13f83602 | 230 | sw2.fall(eventQueue.event(&button_press)); |
MACRUM | 19:999d13f83602 | 231 | button_count = 0; |
MACRUM | 19:999d13f83602 | 232 | |
MACRUM | 19:999d13f83602 | 233 | // Placeholder for callback to update local resource when GET comes. |
MACRUM | 19:999d13f83602 | 234 | // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
MACRUM | 19:999d13f83602 | 235 | Ticker timer; |
MACRUM | 19:999d13f83602 | 236 | timer.attach(eventQueue.event(&measure_sensors), 5.0); |
maclobdell | 11:ae1f6fe932dc | 237 | |
maclobdell | 11:ae1f6fe932dc | 238 | // Start the event queue in a separate thread so the main thread continues |
maclobdell | 11:ae1f6fe932dc | 239 | thread1.start(callback(&eventQueue, &EventQueue::dispatch_forever)); |
maclobdell | 11:ae1f6fe932dc | 240 | |
MACRUM | 19:999d13f83602 | 241 | while(1) { |
maclobdell | 11:ae1f6fe932dc | 242 | wait_ms(100); |
maclobdell | 11:ae1f6fe932dc | 243 | |
maclobdell | 11:ae1f6fe932dc | 244 | if (button_pressed) { |
maclobdell | 11:ae1f6fe932dc | 245 | button_pressed = false; |
MACRUM | 19:999d13f83602 | 246 | printf("button clicked %d times\r\n", button_count); |
maclobdell | 11:ae1f6fe932dc | 247 | } |
MACRUM | 19:999d13f83602 | 248 | |
maclobdell | 11:ae1f6fe932dc | 249 | } |
maclobdell | 11:ae1f6fe932dc | 250 | } |