Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of example-Ethernet-mbed-Cloud-connect by
main.cpp
00001 // ---------------------------------------------------------------------------- 00002 // Copyright 2016-2018 ARM Ltd. 00003 // 00004 // SPDX-License-Identifier: Apache-2.0 00005 // 00006 // Licensed under the Apache License, Version 2.0 (the "License"); 00007 // you may not use this file except in compliance with the License. 00008 // You may obtain a copy of the License at 00009 // 00010 // http://www.apache.org/licenses/LICENSE-2.0 00011 // 00012 // Unless required by applicable law or agreed to in writing, software 00013 // distributed under the License is distributed on an "AS IS" BASIS, 00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 // See the License for the specific language governing permissions and 00016 // limitations under the License. 00017 // ---------------------------------------------------------------------------- 00018 00019 #include "mbed.h" 00020 #include "simple-mbed-cloud-client.h" 00021 #include "SDBlockDevice.h" 00022 #include "FATFileSystem.h" 00023 #include "EthernetInterface.h" 00024 00025 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) 00026 // 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 00027 EventQueue eventQueue; 00028 00029 // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available) 00030 SDBlockDevice bd(MBED_CONF_APP_SPI_MOSI, MBED_CONF_APP_SPI_MISO, MBED_CONF_APP_SPI_CLK, MBED_CONF_APP_SPI_CS); 00031 FATFileSystem fs("sd", &bd); 00032 00033 // Declaring pointers for access to Mbed Cloud Client resources outside of main() 00034 MbedCloudClientResource *button_res; 00035 MbedCloudClientResource *pattern_res; 00036 00037 // This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button 00038 void fake_button_press() { 00039 int v = button_res->get_value_int() + 1; 00040 00041 button_res->set_value(v); 00042 00043 printf("Simulated button clicked %d times\n", v); 00044 } 00045 00046 /** 00047 * PUT handler 00048 * @param resource The resource that triggered the callback 00049 * @param newValue Updated value for the resource 00050 */ 00051 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) { 00052 printf("PUT received, new value: %s\n", newValue.c_str()); 00053 } 00054 00055 /** 00056 * POST handler 00057 * @param resource The resource that triggered the callback 00058 * @param buffer If a body was passed to the POST function, this contains the data. 00059 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. 00060 * @param size Size of the body 00061 */ 00062 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { 00063 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str()); 00064 00065 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern 00066 00067 // Parse the pattern string, and toggle the LED in that pattern 00068 string s = std::string(pattern_res->get_value().c_str()); 00069 size_t i = 0; 00070 size_t pos = s.find(':'); 00071 while (pos != string::npos) { 00072 wait_ms(atoi(s.substr(i, pos - i).c_str())); 00073 augmentedLed = !augmentedLed; 00074 00075 i = ++pos; 00076 pos = s.find(':', pos); 00077 00078 if (pos == string::npos) { 00079 wait_ms(atoi(s.substr(i, s.length()).c_str())); 00080 augmentedLed = !augmentedLed; 00081 } 00082 } 00083 } 00084 00085 /** 00086 * Notification callback handler 00087 * @param resource The resource that triggered the callback 00088 * @param status The delivery status of the notification 00089 */ 00090 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { 00091 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); 00092 } 00093 00094 /** 00095 * Registration callback handler 00096 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) 00097 */ 00098 void registered(const ConnectorClientEndpointInfo *endpoint) { 00099 printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); 00100 } 00101 00102 int main(void) { 00103 printf("Starting Simple Mbed Cloud Client example\n"); 00104 printf("Connecting to the network using Ethernet...\n"); 00105 00106 // Connect to the internet (DHCP is expected to be on) 00107 EthernetInterface net; 00108 nsapi_error_t status = net.connect(); 00109 00110 if (status != 0) { 00111 printf("Connecting to the network failed %d!\n", status); 00112 return -1; 00113 } 00114 00115 printf("Connected to the network successfully. IP address: %s\n", net.get_ip_address()); 00116 00117 // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud 00118 SimpleMbedCloudClient client(&net, &bd, &fs); 00119 int client_status = client.init(); 00120 if (client_status != 0) { 00121 printf("Initializing Mbed Cloud Client failed (%d)\n", client_status); 00122 return -1; 00123 } 00124 00125 // Creating resources, which can be written or read from the cloud 00126 button_res = client.create_resource("3200/0/5501", "button_count"); 00127 button_res->set_value(0); 00128 button_res->methods(M2MMethod::GET); 00129 button_res->observable(true); 00130 button_res->attach_notification_callback(button_callback); 00131 00132 pattern_res = client.create_resource("3201/0/5853", "blink_pattern"); 00133 pattern_res->set_value("500:500:500:500:500:500:500:500"); 00134 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT); 00135 pattern_res->attach_put_callback(pattern_updated); 00136 00137 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action"); 00138 blink_res->methods(M2MMethod::POST); 00139 blink_res->attach_post_callback(blink_callback); 00140 00141 printf("Initialized Mbed Cloud Client. Registering...\n"); 00142 00143 // Callback that fires when registering is complete 00144 client.on_registered(®istered); 00145 00146 // Register with Mbed Cloud 00147 client.register_and_connect(); 00148 00149 // Placeholder for callback to update local resource when GET comes. 00150 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations 00151 Ticker timer; 00152 timer.attach(eventQueue.event(&fake_button_press), 5.0); 00153 00154 // You can easily run the eventQueue in a separate thread if required 00155 eventQueue.dispatch_forever(); 00156 }
Generated on Wed Jul 13 2022 01:52:44 by
1.7.2
