Workshop example
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
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 #ifndef MBED_TEST_MODE 00019 00020 00021 #include "mbed.h" 00022 #include "simple-mbed-cloud-client.h" 00023 #include "LittleFileSystem.h" 00024 00025 // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi. 00026 NetworkInterface *net; 00027 00028 // Default block device available on the target board 00029 BlockDevice* bd = BlockDevice::get_default_instance(); 00030 SlicingBlockDevice sd(bd, 0, 2*1024*1024); 00031 00032 #if COMPONENT_SD || COMPONENT_NUSD 00033 // Use FATFileSystem for SD card type blockdevices 00034 FATFileSystem fs("fs"); 00035 #else 00036 // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions 00037 LittleFileSystem fs("fs"); 00038 #endif 00039 00040 // Default User button for GET example and for resetting the storage 00041 InterruptIn button(BUTTON1); 00042 // Default LED to use for PUT/POST example 00043 DigitalOut led(LED1, 1); 00044 00045 // How often to fetch sensor data (in seconds) 00046 #define SENSORS_POLL_INTERVAL 3.0 00047 00048 00049 00050 // Sensors related includes and initialization 00051 00052 00053 // Temperature reading from microcontroller 00054 AnalogIn adc_temp(ADC_TEMP); 00055 // Voltage reference reading from microcontroller 00056 AnalogIn adc_vref(ADC_VREF); 00057 00058 // Declaring pointers for access to Pelion Client resources outside of main() 00059 MbedCloudClientResource *res_button; 00060 MbedCloudClientResource *res_led; 00061 00062 00063 MbedCloudClientResource *res_adc_temp; 00064 MbedCloudClientResource *res_adc_voltage; 00065 00066 00067 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) 00068 // 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 00069 EventQueue eventQueue; 00070 00071 // When the device is registered, this variable will be used to access various useful information, like device ID etc. 00072 static const ConnectorClientEndpointInfo* endpointInfo; 00073 00074 /** 00075 * PUT handler 00076 * @param resource The resource that triggered the callback 00077 * @param newValue Updated value for the resource 00078 */ 00079 void put_callback(MbedCloudClientResource *resource, m2m::String newValue) { 00080 printf("*** PUT received, new value: %s \n", newValue.c_str()); 00081 led = atoi(newValue.c_str()); 00082 } 00083 00084 /** 00085 * POST handler 00086 * @param resource The resource that triggered the callback 00087 * @param buffer If a body was passed to the POST function, this contains the data. 00088 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. 00089 * @param size Size of the body 00090 */ 00091 void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { 00092 printf("*** POST received (length %u). Payload: ", size); 00093 for (size_t ix = 0; ix < size; ix++) { 00094 printf("%02x ", buffer[ix]); 00095 } 00096 printf("\n"); 00097 } 00098 00099 /** 00100 * Button function triggered by the physical button press. 00101 */ 00102 void button_press() { 00103 int v = res_button->get_value_int() + 1; 00104 res_button->set_value(v); 00105 printf("*** Button clicked %d times \n", v); 00106 } 00107 00108 /** 00109 * Notification callback handler 00110 * @param resource The resource that triggered the callback 00111 * @param status The delivery status of the notification 00112 */ 00113 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { 00114 printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status); 00115 } 00116 00117 /** 00118 * Registration callback handler 00119 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) 00120 */ 00121 void registered(const ConnectorClientEndpointInfo *endpoint) { 00122 printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); 00123 endpointInfo = endpoint; 00124 } 00125 00126 /** 00127 * Initialize sensors 00128 */ 00129 void sensors_init() { 00130 00131 printf ("\nSensors configuration:\n"); 00132 00133 printf("\n"); ; 00134 } 00135 00136 /** 00137 * Update sensors and report their values. 00138 * This function is called periodically. 00139 */ 00140 void sensors_update() { 00141 float temp3_value, volt_value = 0.0; 00142 00143 temp3_value = adc_temp.read()*100; 00144 volt_value = adc_vref.read(); 00145 00146 00147 00148 if (endpointInfo) { 00149 00150 res_adc_temp->set_value(temp3_value); 00151 res_adc_voltage->set_value(volt_value); 00152 00153 } 00154 } 00155 00156 int main(void) { 00157 printf("\nStarting Simple Pelion Device Management Client example\n"); 00158 00159 int storage_status = fs.mount(&sd); 00160 if (storage_status != 0) { 00161 printf("Storage mounting failed.\n"); 00162 } 00163 // If the User button is pressed ons start, then format storage. 00164 bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE); 00165 if (btn_pressed) { 00166 printf("User button is pushed on start...\n"); 00167 } 00168 00169 if (storage_status || btn_pressed) { 00170 printf("Formatting the storage...\n"); 00171 int storage_status = StorageHelper::format(&fs, &sd); 00172 if (storage_status != 0) { 00173 printf("ERROR: Failed to reformat the storage (%d).\n", storage_status); 00174 } 00175 } else { 00176 printf("You can hold the user button during boot to format the storage and change the device identity.\n"); 00177 } 00178 00179 sensors_init(); 00180 00181 // Connect to the internet (DHCP is expected to be on) 00182 printf("Connecting to the network using Wifi...\n"); 00183 net = NetworkInterface::get_default_instance(); 00184 00185 nsapi_error_t net_status = -1; 00186 for (int tries = 0; tries < 3; tries++) { 00187 net_status = net->connect(); 00188 if (net_status == NSAPI_ERROR_OK) { 00189 break; 00190 } else { 00191 printf("Unable to connect to network. Retrying...\n"); 00192 } 00193 } 00194 00195 if (net_status != NSAPI_ERROR_OK) { 00196 printf("ERROR: Connecting to the network failed (%d)!\n", net_status); 00197 return -1; 00198 } 00199 00200 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); 00201 00202 printf("Initializing Pelion Device Management Client...\n"); 00203 00204 // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM 00205 SimpleMbedCloudClient client(net, bd, &fs); 00206 int client_status = client.init(); 00207 if (client_status != 0) { 00208 printf("ERROR: Pelion Client initialization failed (%d)\n", client_status); 00209 return -1; 00210 } 00211 00212 // Creating resources, which can be written or read from the cloud 00213 res_button = client.create_resource("3200/0/5501", "Button Count"); 00214 res_button->set_value(0); 00215 res_button->methods(M2MMethod::GET); 00216 res_button->observable(true); 00217 res_button->attach_notification_callback(button_callback); 00218 00219 res_led = client.create_resource("3201/0/5853", "LED State"); 00220 res_led->set_value(1); 00221 res_led->methods(M2MMethod::GET | M2MMethod::PUT); 00222 res_led->attach_put_callback(put_callback); 00223 00224 res_adc_temp = client.create_resource("3303/2/5700", "Temperature ADC (C)"); 00225 res_adc_temp->set_value(0); 00226 res_adc_temp->methods(M2MMethod::GET); 00227 res_adc_temp->observable(true); 00228 00229 res_adc_voltage = client.create_resource("3316/0/5700", "Voltage"); 00230 res_adc_voltage->set_value(0); 00231 res_adc_voltage->methods(M2MMethod::GET); 00232 res_adc_voltage->observable(true); 00233 00234 printf("Initialized Pelion Client. Registering...\n"); 00235 00236 // Callback that fires when registering is complete 00237 client.on_registered(®istered); 00238 00239 // Register with Pelion DM 00240 client.register_and_connect(); 00241 00242 int i = 600; // wait up 60 seconds before attaching sensors and button events 00243 while (i-- > 0 && !client.is_client_registered()) { 00244 wait_ms(100); 00245 } 00246 00247 button.fall(eventQueue.event(&button_press)); 00248 00249 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations 00250 Ticker timer; 00251 timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL); 00252 00253 // You can easily run the eventQueue in a separate thread if required 00254 eventQueue.dispatch_forever(); 00255 } 00256 00257 #endif
Generated on Tue Jul 12 2022 22:34:14 by 1.7.2