Toyomasa Watarai / Mbed OS mbed-cloud-example_WiFi_ESP32

Fork of mbed-cloud-example_GR-LYCHEE by Renesas

Committer:
MACRUM
Date:
Wed Aug 08 04:16:17 2018 +0000
Revision:
11:924e1a1bb03e
Parent:
9:9fade4bb2774
Child:
12:b1a3a0ccd96e
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MarceloSalazar 0:e13a8a944e25 1 // ----------------------------------------------------------------------------
MarceloSalazar 9:9fade4bb2774 2 // Copyright 2016-2018 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 0:e13a8a944e25 20 #include "simple-mbed-cloud-client.h"
MarceloSalazar 1:1ccf36276cd3 21 #include "SDBlockDevice.h"
MarceloSalazar 1:1ccf36276cd3 22 #include "FATFileSystem.h"
MACRUM 11:924e1a1bb03e 23 #include "ESP32Interface.h"
MACRUM 11:924e1a1bb03e 24
MACRUM 11:924e1a1bb03e 25 #define SSID "SSID"
MACRUM 11:924e1a1bb03e 26 #define PASSWORD "PASSWORD"
MarceloSalazar 1:1ccf36276cd3 27
MarceloSalazar 9:9fade4bb2774 28 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
MarceloSalazar 9:9fade4bb2774 29 // 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
MarceloSalazar 9:9fade4bb2774 30 EventQueue eventQueue;
MarceloSalazar 0:e13a8a944e25 31
MarceloSalazar 9:9fade4bb2774 32 // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available)
MACRUM 11:924e1a1bb03e 33 SDBlockDevice sd(P5_6, P5_7, P5_4, P5_5);
MarceloSalazar 9:9fade4bb2774 34 FATFileSystem fs("sd", &sd);
MACRUM 11:924e1a1bb03e 35 ESP32Interface net(MBED_CONF_APP_WIFI_EN, MBED_CONF_APP_WIFI_IO0, MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
MACRUM 11:924e1a1bb03e 36
MACRUM 11:924e1a1bb03e 37 InterruptIn btn(USER_BUTTON0);
bridadan 5:c18fab181ede 38
MarceloSalazar 9:9fade4bb2774 39 // Declaring pointers for access to Mbed Cloud Client resources outside of main()
MarceloSalazar 9:9fade4bb2774 40 MbedCloudClientResource *button_res;
MarceloSalazar 9:9fade4bb2774 41 MbedCloudClientResource *pattern_res;
MarceloSalazar 0:e13a8a944e25 42
MACRUM 11:924e1a1bb03e 43 static int button_count = 0;
MarceloSalazar 0:e13a8a944e25 44
MACRUM 11:924e1a1bb03e 45 void button_press()
MACRUM 11:924e1a1bb03e 46 {
MACRUM 11:924e1a1bb03e 47 ++button_count;
MACRUM 11:924e1a1bb03e 48 button_res->set_value(button_count);
MarceloSalazar 1:1ccf36276cd3 49
MACRUM 11:924e1a1bb03e 50 printf("User button clicked %d times\n", button_count);
MarceloSalazar 1:1ccf36276cd3 51 }
MarceloSalazar 1:1ccf36276cd3 52
MarceloSalazar 9:9fade4bb2774 53 /**
MarceloSalazar 9:9fade4bb2774 54 * PUT handler
MarceloSalazar 9:9fade4bb2774 55 * @param resource The resource that triggered the callback
MarceloSalazar 9:9fade4bb2774 56 * @param newValue Updated value for the resource
MarceloSalazar 9:9fade4bb2774 57 */
MACRUM 11:924e1a1bb03e 58 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue)
MACRUM 11:924e1a1bb03e 59 {
MarceloSalazar 9:9fade4bb2774 60 printf("PUT received, new value: %s\n", newValue.c_str());
MarceloSalazar 0:e13a8a944e25 61 }
MarceloSalazar 0:e13a8a944e25 62
MarceloSalazar 9:9fade4bb2774 63 /**
MarceloSalazar 9:9fade4bb2774 64 * POST handler
MarceloSalazar 9:9fade4bb2774 65 * @param resource The resource that triggered the callback
MarceloSalazar 9:9fade4bb2774 66 * @param buffer If a body was passed to the POST function, this contains the data.
MarceloSalazar 9:9fade4bb2774 67 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
MarceloSalazar 9:9fade4bb2774 68 * @param size Size of the body
MarceloSalazar 9:9fade4bb2774 69 */
MACRUM 11:924e1a1bb03e 70 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size)
MACRUM 11:924e1a1bb03e 71 {
MarceloSalazar 9:9fade4bb2774 72 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
MarceloSalazar 9:9fade4bb2774 73
MarceloSalazar 9:9fade4bb2774 74 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
MarceloSalazar 0:e13a8a944e25 75
MarceloSalazar 9:9fade4bb2774 76 // Parse the pattern string, and toggle the LED in that pattern
MarceloSalazar 9:9fade4bb2774 77 string s = std::string(pattern_res->get_value().c_str());
MarceloSalazar 9:9fade4bb2774 78 size_t i = 0;
MarceloSalazar 9:9fade4bb2774 79 size_t pos = s.find(':');
MarceloSalazar 9:9fade4bb2774 80 while (pos != string::npos) {
MarceloSalazar 9:9fade4bb2774 81 wait_ms(atoi(s.substr(i, pos - i).c_str()));
MarceloSalazar 9:9fade4bb2774 82 augmentedLed = !augmentedLed;
MarceloSalazar 9:9fade4bb2774 83
MarceloSalazar 9:9fade4bb2774 84 i = ++pos;
MarceloSalazar 9:9fade4bb2774 85 pos = s.find(':', pos);
MarceloSalazar 9:9fade4bb2774 86
MarceloSalazar 9:9fade4bb2774 87 if (pos == string::npos) {
MarceloSalazar 9:9fade4bb2774 88 wait_ms(atoi(s.substr(i, s.length()).c_str()));
MarceloSalazar 9:9fade4bb2774 89 augmentedLed = !augmentedLed;
MarceloSalazar 9:9fade4bb2774 90 }
MarceloSalazar 9:9fade4bb2774 91 }
MarceloSalazar 0:e13a8a944e25 92 }
MarceloSalazar 0:e13a8a944e25 93
MarceloSalazar 9:9fade4bb2774 94 /**
MarceloSalazar 9:9fade4bb2774 95 * Notification callback handler
MarceloSalazar 9:9fade4bb2774 96 * @param resource The resource that triggered the callback
MarceloSalazar 9:9fade4bb2774 97 * @param status The delivery status of the notification
MarceloSalazar 9:9fade4bb2774 98 */
MACRUM 11:924e1a1bb03e 99 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status)
MACRUM 11:924e1a1bb03e 100 {
MarceloSalazar 9:9fade4bb2774 101 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
MarceloSalazar 9:9fade4bb2774 102 }
MarceloSalazar 1:1ccf36276cd3 103
MarceloSalazar 9:9fade4bb2774 104 /**
MarceloSalazar 9:9fade4bb2774 105 * Registration callback handler
MarceloSalazar 9:9fade4bb2774 106 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
MarceloSalazar 9:9fade4bb2774 107 */
MACRUM 11:924e1a1bb03e 108 void registered(const ConnectorClientEndpointInfo *endpoint)
MACRUM 11:924e1a1bb03e 109 {
MarceloSalazar 9:9fade4bb2774 110 printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
MarceloSalazar 9:9fade4bb2774 111 }
MarceloSalazar 1:1ccf36276cd3 112
MACRUM 11:924e1a1bb03e 113 int main(void)
MACRUM 11:924e1a1bb03e 114 {
MarceloSalazar 9:9fade4bb2774 115 printf("Starting Simple Mbed Cloud Client example\n");
MACRUM 11:924e1a1bb03e 116 printf("Connecting to the network using Wi-Fi...\n");
MarceloSalazar 9:9fade4bb2774 117
MarceloSalazar 9:9fade4bb2774 118 // Connect to the internet (DHCP is expected to be on)
MACRUM 11:924e1a1bb03e 119 nsapi_error_t status = net.connect(SSID, PASSWORD, NSAPI_SECURITY_WPA_WPA2);
MarceloSalazar 9:9fade4bb2774 120
MarceloSalazar 9:9fade4bb2774 121 if (status != 0) {
MarceloSalazar 9:9fade4bb2774 122 printf("Connecting to the network failed %d!\n", status);
MarceloSalazar 1:1ccf36276cd3 123 return -1;
MarceloSalazar 1:1ccf36276cd3 124 }
MarceloSalazar 0:e13a8a944e25 125
MarceloSalazar 9:9fade4bb2774 126 printf("Connected to the network successfully. IP address: %s\n", net.get_ip_address());
MarceloSalazar 1:1ccf36276cd3 127
MarceloSalazar 9:9fade4bb2774 128 // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud
MarceloSalazar 9:9fade4bb2774 129 SimpleMbedCloudClient client(&net, &sd, &fs);
MarceloSalazar 9:9fade4bb2774 130 int client_status = client.init();
MarceloSalazar 9:9fade4bb2774 131 if (client_status != 0) {
MarceloSalazar 9:9fade4bb2774 132 printf("Initializing Mbed Cloud Client failed (%d)\n", client_status);
MarceloSalazar 1:1ccf36276cd3 133 return -1;
MarceloSalazar 1:1ccf36276cd3 134 }
MarceloSalazar 1:1ccf36276cd3 135
MarceloSalazar 9:9fade4bb2774 136 // Creating resources, which can be written or read from the cloud
MarceloSalazar 9:9fade4bb2774 137 button_res = client.create_resource("3200/0/5501", "button_count");
MarceloSalazar 9:9fade4bb2774 138 button_res->set_value(0);
MarceloSalazar 9:9fade4bb2774 139 button_res->methods(M2MMethod::GET);
MarceloSalazar 9:9fade4bb2774 140 button_res->observable(true);
MarceloSalazar 9:9fade4bb2774 141 button_res->attach_notification_callback(button_callback);
MarceloSalazar 0:e13a8a944e25 142
MarceloSalazar 9:9fade4bb2774 143 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
MarceloSalazar 9:9fade4bb2774 144 pattern_res->set_value("500:500:500:500:500:500:500:500");
MarceloSalazar 9:9fade4bb2774 145 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
MarceloSalazar 9:9fade4bb2774 146 pattern_res->attach_put_callback(pattern_updated);
MarceloSalazar 0:e13a8a944e25 147
MarceloSalazar 9:9fade4bb2774 148 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
MarceloSalazar 9:9fade4bb2774 149 blink_res->methods(M2MMethod::POST);
MarceloSalazar 9:9fade4bb2774 150 blink_res->attach_post_callback(blink_callback);
MarceloSalazar 9:9fade4bb2774 151
MarceloSalazar 9:9fade4bb2774 152 printf("Initialized Mbed Cloud Client. Registering...\n");
MarceloSalazar 0:e13a8a944e25 153
MACRUM 11:924e1a1bb03e 154
MarceloSalazar 9:9fade4bb2774 155 // Callback that fires when registering is complete
MarceloSalazar 9:9fade4bb2774 156 client.on_registered(&registered);
MarceloSalazar 0:e13a8a944e25 157
MarceloSalazar 9:9fade4bb2774 158 // Register with Mbed Cloud
MarceloSalazar 9:9fade4bb2774 159 client.register_and_connect();
bridadan 6:254a7e7fbef1 160
MACRUM 11:924e1a1bb03e 161 // Setup the button
MACRUM 11:924e1a1bb03e 162 btn.mode(PullUp);
MACRUM 11:924e1a1bb03e 163
MACRUM 11:924e1a1bb03e 164 // The button fall handler is placed in the event queue so it will run in
MACRUM 11:924e1a1bb03e 165 // thread context instead of ISR context, which allows safely updating the cloud resource
MACRUM 11:924e1a1bb03e 166 btn.fall(eventQueue.event(&button_press));
MACRUM 11:924e1a1bb03e 167 button_count = 0;
MarceloSalazar 1:1ccf36276cd3 168
MarceloSalazar 9:9fade4bb2774 169 // You can easily run the eventQueue in a separate thread if required
MarceloSalazar 9:9fade4bb2774 170 eventQueue.dispatch_forever();
MarceloSalazar 0:e13a8a944e25 171 }