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 00024 // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi. 00025 NetworkInterface *net; 00026 00027 // Default block device available on the target board 00028 BlockDevice* bd = BlockDevice::get_default_instance(); 00029 SlicingBlockDevice sd(bd, 0, 2*1024*1024); 00030 00031 #if COMPONENT_SD || COMPONENT_NUSD 00032 // Use FATFileSystem for SD card type blockdevices 00033 FATFileSystem fs("fs"); 00034 #else 00035 // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions 00036 LittleFileSystem fs("fs"); 00037 #endif 00038 00039 // Default User button for GET example and for resetting the storage 00040 InterruptIn button(BUTTON1); 00041 // Default LED to use for PUT/POST example 00042 DigitalOut DoorActivate(PD_3, 0); 00043 00044 // How often to fetch sensor data (in seconds) 00045 #define SENSORS_POLL_INTERVAL 15.0 00046 int dflag = 1; 00047 // Sensors related includes and initialization 00048 #include "HTS221Sensor.h" 00049 #include "LPS22HBSensor.h" 00050 #include "VL53L0X.h" 00051 00052 static DevI2C devI2c(PB_11,PB_10); 00053 static HTS221Sensor sen_hum_temp(&devI2c); 00054 static LPS22HBSensor sen_press_temp(&devI2c); 00055 00056 static DigitalOut shutdown_pin(PC_6); 00057 static VL53L0X sen_distance(&devI2c, &shutdown_pin, PC_7); 00058 00059 // Declaring pointers for access to Pelion Client resources outside of main() 00060 MbedCloudClientResource *res_button; 00061 MbedCloudClientResource *DoorControl; 00062 00063 00064 // Additional resources for sensor readings 00065 00066 MbedCloudClientResource *res_humidity; 00067 MbedCloudClientResource *res_temperature; 00068 MbedCloudClientResource *res_pressure; 00069 MbedCloudClientResource *res_distance; 00070 00071 // define your personal password 00072 #define DoorPassword "MyPassword" 00073 00074 char DoorCode[32]; 00075 DigitalOut ConnectTrue(LED1); 00076 DigitalOut CodeError(LED2); 00077 int ErrorCount = 0; 00078 int ErrorLockoutTime = 0; 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 00094 if(ErrorLockoutTime != 0) { /* Ignore password attempts while locked */ 00095 printf("Password Lockout!\r\n"); 00096 return; 00097 } 00098 printf("*** PUT received, new value: %s \n", newValue.c_str()); 00099 strcpy(DoorCode, newValue.c_str()); 00100 printf("%s\r\n", DoorCode); 00101 if(!strcmp(DoorCode, DoorPassword )) { 00102 DoorActivate = 1; /* Activate relay for 1500ms */ 00103 wait_ms(1500); 00104 DoorActivate = 0; 00105 DoorCode[0] = NULL; 00106 // clear password error data 00107 ErrorCount = 0; 00108 ErrorLockoutTime = 0; 00109 } else { /* bad password attempt */ 00110 DoorCode[0] = NULL; 00111 ErrorCount++; 00112 if(ErrorCount >= 3) { /* If password submitted is wring lockout the remote control on the third error */ 00113 ErrorLockoutTime = (ErrorCount - 2) * 2; /* Double the lockout time after every failed password */ 00114 CodeError = 1; /* Indicate lockout on LED */ 00115 printf("PasswordLocked %d Minutes\r\n", ErrorLockoutTime); 00116 } 00117 } 00118 00119 00120 } 00121 00122 00123 00124 /** 00125 * Button function triggered by the physical button press. 00126 */ 00127 void button_press() { 00128 int v = res_button->get_value_int() + 1; 00129 res_button->set_value(v); 00130 printf("*** Button clicked %d times \n", v); 00131 } 00132 00133 /** 00134 * Notification callback handler 00135 * @param resource The resource that triggered the callback 00136 * @param status The delivery status of the notification 00137 */ 00138 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { 00139 printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status); 00140 } 00141 00142 /** 00143 * Registration callback handler 00144 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) 00145 */ 00146 void registered(const ConnectorClientEndpointInfo *endpoint) { 00147 printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); 00148 endpointInfo = endpoint; 00149 } 00150 00151 /** 00152 * Initialize sensors 00153 */ 00154 void sensors_init() { 00155 uint8_t id1, id2; 00156 00157 printf ("\nSensors configuration:\n"); 00158 // Initialize sensors 00159 sen_hum_temp.init(NULL); 00160 sen_press_temp.init(NULL); 00161 sen_distance.init_sensor(VL53L0X_DEFAULT_ADDRESS); 00162 00163 /// Call sensors enable routines 00164 sen_hum_temp.enable(); 00165 sen_press_temp.enable(); 00166 00167 00168 sen_hum_temp.read_id(&id1); 00169 sen_press_temp.read_id(&id2); 00170 00171 printf("HTS221 humidity & temperature = 0x%X\n", id1); 00172 printf("LPS22HB pressure & temperature = 0x%X\n", id2); 00173 00174 printf("\n"); ; 00175 } 00176 00177 /** 00178 * Update sensors and report their values. 00179 * This function is called periodically. 00180 */ 00181 void sensors_update() { 00182 float temp1_value, humid_value, pressure_value; 00183 uint32_t distance_value, distance_reading; 00184 00185 if(!dflag++) { /* Only update the sensors once a minute */ 00186 sen_hum_temp.get_humidity(&humid_value); 00187 sen_hum_temp.get_temperature(&temp1_value); 00188 sen_press_temp.get_pressure(&pressure_value); 00189 res_humidity->set_value(humid_value); 00190 res_temperature->set_value(temp1_value); 00191 res_pressure->set_value(pressure_value); 00192 if(ErrorLockoutTime != 0) { 00193 ErrorLockoutTime--; 00194 if(ErrorLockoutTime == 0) { 00195 printf("Unlocked\r\n"); 00196 CodeError = 0; 00197 } 00198 } 00199 } 00200 if(dflag > 3) dflag = 0; 00201 distance_reading = sen_distance.get_distance(&distance_value); 00202 00203 00204 if (distance_reading == VL53L0X_ERROR_NONE) { 00205 // printf("VL53L0X dist: %7ld mm\n", distance_value); 00206 res_distance->set_value((int)distance_value); 00207 } else { 00208 // printf("VL53L0X dist: -- \n"); 00209 distance_value = 1999; 00210 res_distance->set_value((int)distance_value); 00211 } 00212 } 00213 00214 int main(void) { 00215 printf("\nStarting Simple Pelion Device Management Client example\n"); 00216 ConnectTrue = 0; 00217 CodeError = 0; 00218 DoorActivate = 0; 00219 int storage_status = fs.mount(&sd); 00220 if (storage_status != 0) { 00221 printf("Storage mounting failed.\n"); 00222 } 00223 // If the User button is pressed ons start, then format storage. 00224 bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE); 00225 if (btn_pressed) { 00226 printf("User button is pushed on start...\n"); 00227 } 00228 00229 if (storage_status || btn_pressed) { 00230 printf("Formatting the storage...\n"); 00231 int storage_status = StorageHelper::format(&fs, &sd); 00232 if (storage_status != 0) { 00233 printf("ERROR: Failed to reformat the storage (%d).\n", storage_status); 00234 } 00235 } else { 00236 printf("You can hold the user button during boot to format the storage and change the device identity.\n"); 00237 } 00238 00239 sensors_init(); 00240 00241 // Connect to the internet (DHCP is expected to be on) 00242 printf("Connecting to the network using Wifi...\n"); 00243 net = NetworkInterface::get_default_instance(); 00244 00245 nsapi_error_t net_status = -1; 00246 for (int tries = 0; tries < 3; tries++) { 00247 net_status = net->connect(); 00248 if (net_status == NSAPI_ERROR_OK) { 00249 break; 00250 } else { 00251 printf("Unable to connect to network. Retrying...\n"); 00252 } 00253 } 00254 00255 if (net_status != NSAPI_ERROR_OK) { 00256 printf("ERROR: Connecting to the network failed (%d)!\n", net_status); 00257 return -1; 00258 } 00259 00260 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); 00261 00262 printf("Initializing Pelion Device Management Client...\n"); 00263 00264 // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM 00265 SimpleMbedCloudClient client(net, bd, &fs); 00266 int client_status = client.init(); 00267 if (client_status != 0) { 00268 printf("ERROR: Pelion Client initialization failed (%d)\n", client_status); 00269 return -1; 00270 } 00271 00272 // Creating resources, which can be written or read from the cloud 00273 res_button = client.create_resource("3200/0/5501", "Button Count"); 00274 res_button->set_value(0); 00275 res_button->methods(M2MMethod::GET); 00276 res_button->observable(true); 00277 //res_button->max_age(6000); 00278 res_button->attach_notification_callback(button_callback); 00279 00280 DoorControl = client.create_resource("3201/0/5853", "Door Control"); 00281 DoorControl->set_value(1); 00282 DoorControl->methods(M2MMethod::GET | M2MMethod::PUT); 00283 DoorControl->attach_put_callback(put_callback); 00284 00285 // Sensor resources 00286 res_temperature = client.create_resource("3303/0/5700", "Temperature HTS221 (C)"); 00287 res_temperature->set_value(0); 00288 res_temperature->methods(M2MMethod::GET); 00289 res_temperature->observable(true); 00290 00291 res_humidity = client.create_resource("3304/0/5700", "Humidity"); 00292 res_humidity->set_value(0); 00293 res_humidity->methods(M2MMethod::GET); 00294 res_humidity->observable(true); 00295 00296 res_pressure = client.create_resource("3323/0/5700", "Pressure"); 00297 res_pressure->set_value(0); 00298 res_pressure->methods(M2MMethod::GET); 00299 res_pressure->observable(true); 00300 00301 res_distance = client.create_resource("3330/0/5700", "Distance"); 00302 res_distance->set_value((float)999.9); 00303 res_distance->methods(M2MMethod::GET); 00304 res_distance->observable(true); 00305 00306 printf("Initialized Pelion Client. Registering...\n"); 00307 00308 // Callback that fires when registering is complete 00309 client.on_registered(®istered); 00310 00311 // Register with Pelion DM 00312 client.register_and_connect(); 00313 00314 int i = 6000; // wait up 600 seconds before attaching sensors and button events 00315 while (i-- > 0 && !client.is_client_registered()) { 00316 wait_ms(100); 00317 } 00318 ConnectTrue = 1; 00319 button.fall(eventQueue.event(&button_press)); 00320 res_button->set_value(0); 00321 sensors_update(); 00322 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations 00323 Ticker timer; 00324 timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL); 00325 00326 // You can easily run the eventQueue in a separate thread if required 00327 eventQueue.dispatch_forever(); 00328 } 00329 00330 #endif
Generated on Tue Jul 12 2022 16:16:48 by
1.7.2