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.
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 #include "mbed.h" 00021 #include "simple-mbed-cloud-client.h" 00022 #include "LittleFileSystem.h" 00023 #include<math.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 // Send all sensor data or just limited (useful for when running out of memory) 00049 #define SEND_ALL_SENSORS 00050 00051 // Sensors related includes and initialization 00052 #include "HTS221Sensor.h" //humidity and temperature 00053 00054 static DevI2C devI2c(PB_11,PB_10); 00055 static HTS221Sensor sen_hum_temp(&devI2c); 00056 //static LSM6DSLSensor sen_acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); 00057 00058 static DigitalOut shutdown_pin(PC_6); 00059 00060 // Temperature reading from microcontroller 00061 AnalogIn adc_temp(ADC_TEMP); 00062 // Voltage reference reading from microcontroller 00063 AnalogIn adc_vref(ADC_VREF); 00064 00065 // Declaring pointers for access to Pelion Client resources outside of main() 00066 MbedCloudClientResource *res_button; 00067 MbedCloudClientResource *res_led; 00068 00069 // Additional resources for sensor readings 00070 #ifdef SEND_ALL_SENSORS 00071 MbedCloudClientResource *res_temperature; 00072 00073 00074 #endif /* SEND_ALL_SENSORS */ 00075 00076 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) 00077 // 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 00078 EventQueue eventQueue; 00079 00080 // When the device is registered, this variable will be used to access various useful information, like device ID etc. 00081 static const ConnectorClientEndpointInfo* endpointInfo; 00082 00083 /** 00084 * PUT handler 00085 * @param resource The resource that triggered the callback 00086 * @param newValue Updated value for the resource 00087 */ 00088 void put_callback(MbedCloudClientResource *resource, m2m::String newValue) { 00089 printf("*** PUT received, new value: %s \n", newValue.c_str()); 00090 led = atoi(newValue.c_str()); 00091 } 00092 00093 /** 00094 * POST handler 00095 * @param resource The resource that triggered the callback 00096 * @param buffer If a body was passed to the POST function, this contains the data. 00097 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. 00098 * @param size Size of the body 00099 */ 00100 void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { 00101 printf("*** POST received (length %u). Payload: ", size); 00102 for (size_t ix = 0; ix < size; ix++) { 00103 printf("%02x ", buffer[ix]); 00104 } 00105 printf("\n"); 00106 } 00107 00108 /** 00109 * Button function triggered by the physical button press. 00110 */ 00111 void button_press() { 00112 int v = res_button->get_value_int() + 1; 00113 res_button->set_value(v); 00114 printf("*** Button clicked %d times \n", v); 00115 } 00116 // Called everytime a new character goes into 00117 // the RX buffer. Test that character for \n 00118 // Note, rxGetLastChar() gets the last char that 00119 // we received but it does NOT remove it from 00120 // the RX buffer. 00121 00122 /** 00123 * Notification callback handler 00124 * @param resource The resource that triggered the callback 00125 * @param status The delivery status of the notification 00126 */ 00127 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { 00128 printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status); 00129 } 00130 00131 /** 00132 * Registration callback handler 00133 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) 00134 */ 00135 void registered(const ConnectorClientEndpointInfo *endpoint) { 00136 printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); 00137 endpointInfo = endpoint; 00138 } 00139 00140 /** 00141 * Initialize sensors 00142 */ 00143 void sensors_init() { 00144 uint8_t id1, id2; 00145 printf ("\nSensors configuration:\n"); 00146 // Initialize sensors 00147 sen_hum_temp.init(NULL); 00148 00149 /// Call sensors enable routines 00150 sen_hum_temp.enable(); 00151 00152 sen_hum_temp.read_id(&id1); 00153 00154 printf("HTS221 humidity & temperature = 0x%X\n", id1); 00155 printf("\n"); ; 00156 } 00157 00158 /** 00159 * Update sensors and report their values. 00160 * This function is called periodically. 00161 */ 00162 void sensors_update() { 00163 float temp1_value = 0.0; 00164 //float temp3_value = 0.0; 00165 00166 sen_hum_temp.get_temperature(&temp1_value); 00167 00168 printf("%2.2f ^1",temp1_value); 00169 printf("\n"); 00170 00171 wait_us(4000);//Wait until the loop_timer reaches 4000us (250Hz) before starting the next loop 00172 00173 if (endpointInfo) { 00174 #ifdef SEND_ALL_SENSORS 00175 res_temperature->set_value(temp1_value); 00176 00177 #endif /* SEND_ALL_SENSORS */ 00178 } 00179 } 00180 00181 int main(void) 00182 { 00183 printf("\nStarting Simple Pelion Device Management Client example\n"); 00184 00185 int storage_status = fs.mount(&sd); 00186 if (storage_status != 0) { 00187 printf("Storage mounting failed.\n"); 00188 } 00189 // If the User button is pressed ons start, then format storage. 00190 bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE); 00191 if (btn_pressed) { 00192 printf("User button is pushed on start...\n"); 00193 } 00194 00195 if (storage_status || btn_pressed) { 00196 printf("Formatting the storage...\n"); 00197 int storage_status = StorageHelper::format(&fs, &sd); 00198 if (storage_status != 0) { 00199 printf("ERROR: Failed to reformat the storage (%d).\n", storage_status); 00200 } 00201 } else { 00202 printf("You can hold the user button during boot to format the storage and change the device identity.\n"); 00203 } 00204 00205 sensors_init(); 00206 00207 // Connect to the internet (DHCP is expected to be on) 00208 printf("Connecting to the network using Wifi...\n"); 00209 net = NetworkInterface::get_default_instance(); 00210 00211 nsapi_error_t net_status = -1; 00212 for (int tries = 0; tries < 3; tries++) { 00213 net_status = net->connect(); 00214 if (net_status == NSAPI_ERROR_OK) { 00215 break; 00216 } else { 00217 printf("Unable to connect to network. Retrying...\n"); 00218 } 00219 } 00220 00221 if (net_status != NSAPI_ERROR_OK) { 00222 printf("ERROR: Connecting to the network failed (%d)!\n", net_status); 00223 return -1; 00224 } 00225 00226 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); 00227 00228 printf("Initializing Pelion Device Management Client...\n"); 00229 00230 // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM 00231 SimpleMbedCloudClient client(net, bd, &fs); 00232 int client_status = client.init(); 00233 if (client_status != 0) { 00234 printf("ERROR: Pelion Client initialization failed (%d)\n", client_status); 00235 return -1; 00236 } 00237 00238 // Creating resources, which can be written or read from the cloud 00239 res_button = client.create_resource("3200/0/5501", "Button Count"); 00240 res_button->set_value(0); 00241 res_button->methods(M2MMethod::GET); 00242 res_button->observable(true); 00243 res_button->attach_notification_callback(button_callback); 00244 00245 res_led = client.create_resource("3201/0/5853", "LED State"); 00246 res_led->set_value(1); 00247 res_led->methods(M2MMethod::GET | M2MMethod::PUT); 00248 res_led->attach_put_callback(put_callback); 00249 00250 #ifdef SEND_ALL_SENSORS 00251 // Sensor resources 00252 res_temperature = client.create_resource("3303/0/5700", "Temperature HTS221 (C)"); 00253 res_temperature->set_value(0); 00254 res_temperature->methods(M2MMethod::GET); 00255 res_temperature->observable(true); 00256 00257 #endif /* SEND_ALL_SENSORS */ 00258 00259 printf("Initialized Pelion Client. Registering...\n"); 00260 00261 // Callback that fires when registering is complete 00262 client.on_registered(®istered); 00263 00264 // Register with Pelion DM 00265 client.register_and_connect(); 00266 00267 int i = 600; // wait up 60 seconds before attaching sensors and button events 00268 while (i-- > 0 && !client.is_client_registered()) { 00269 wait_ms(100); 00270 } 00271 00272 button.fall(eventQueue.event(&button_press)); 00273 00274 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations 00275 Ticker timer; 00276 timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL); 00277 00278 // You can easily run the eventQueue in a separate thread if required 00279 eventQueue.dispatch_forever(); 00280 } 00281 00282 #endif
Generated on Sat Jul 23 2022 06:49:00 by
1.7.2