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