Toyomasa Watarai / Mbed OS mbed-cloud-example_WiFi_ESP32

Fork of mbed-cloud-example_GR-LYCHEE by Renesas

Committer:
MACRUM
Date:
Tue Sep 18 07:21:42 2018 +0000
Revision:
14:34eab7c4e26d
Parent:
13:feeaccc4717f
Child:
15:a0fb0c1c32a0
Wi-Fi settings are moved to the mbed_app.json.

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