This IOT research work is the working part of the program Change the Wi-Fi ssid and password and program STM32l475 board to see the result on the terminal (C# GUI)

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Committer:
saileshtimilsena
Date:
Tue Feb 23 20:03:58 2021 +0000
Revision:
37:b7acef59086c
Parent:
35:871447da961d
Updated Version for the Research Work...; Change the Wi-Fi ssid and password and program STM32L475 board and see the result in GUI with graph

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adustm 1:e86b1cffc402 1 // ----------------------------------------------------------------------------
adustm 4:cf7342047b4d 2 // Copyright 2016-2018 ARM Ltd.
adustm 1:e86b1cffc402 3 //
adustm 1:e86b1cffc402 4 // SPDX-License-Identifier: Apache-2.0
adustm 1:e86b1cffc402 5 //
adustm 1:e86b1cffc402 6 // Licensed under the Apache License, Version 2.0 (the "License");
adustm 1:e86b1cffc402 7 // you may not use this file except in compliance with the License.
adustm 1:e86b1cffc402 8 // You may obtain a copy of the License at
adustm 1:e86b1cffc402 9 //
adustm 1:e86b1cffc402 10 // http://www.apache.org/licenses/LICENSE-2.0
adustm 1:e86b1cffc402 11 //
adustm 1:e86b1cffc402 12 // Unless required by applicable law or agreed to in writing, software
adustm 1:e86b1cffc402 13 // distributed under the License is distributed on an "AS IS" BASIS,
adustm 1:e86b1cffc402 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
adustm 1:e86b1cffc402 15 // See the License for the specific language governing permissions and
adustm 1:e86b1cffc402 16 // limitations under the License.
adustm 1:e86b1cffc402 17 // ----------------------------------------------------------------------------
MarceloSalazar 9:265744785d33 18 #ifndef MBED_TEST_MODE
screamer 28:0e774865873d 19
adustm 1:e86b1cffc402 20 #include "mbed.h"
adustm 1:e86b1cffc402 21 #include "simple-mbed-cloud-client.h"
screamer 10:b27c962b3c3f 22 #include "LittleFileSystem.h"
saileshtimilsena 37:b7acef59086c 23 #include<math.h>
saileshtimilsena 35:871447da961d 24
screamer 33:cfd9430e7d1e 25 // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi.
screamer 33:cfd9430e7d1e 26 NetworkInterface *net;
screamer 33:cfd9430e7d1e 27
screamer 33:cfd9430e7d1e 28 // Default block device available on the target board
screamer 33:cfd9430e7d1e 29 BlockDevice* bd = BlockDevice::get_default_instance();
screamer 33:cfd9430e7d1e 30 SlicingBlockDevice sd(bd, 0, 2*1024*1024);
screamer 33:cfd9430e7d1e 31
screamer 33:cfd9430e7d1e 32 #if COMPONENT_SD || COMPONENT_NUSD
screamer 33:cfd9430e7d1e 33 // Use FATFileSystem for SD card type blockdevices
screamer 33:cfd9430e7d1e 34 FATFileSystem fs("fs");
screamer 33:cfd9430e7d1e 35 #else
screamer 33:cfd9430e7d1e 36 // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions
screamer 33:cfd9430e7d1e 37 LittleFileSystem fs("fs");
screamer 33:cfd9430e7d1e 38 #endif
screamer 33:cfd9430e7d1e 39
screamer 33:cfd9430e7d1e 40 // Default User button for GET example and for resetting the storage
screamer 33:cfd9430e7d1e 41 InterruptIn button(BUTTON1);
screamer 33:cfd9430e7d1e 42 // Default LED to use for PUT/POST example
screamer 33:cfd9430e7d1e 43 DigitalOut led(LED1, 1);
screamer 33:cfd9430e7d1e 44
screamer 33:cfd9430e7d1e 45 // How often to fetch sensor data (in seconds)
screamer 33:cfd9430e7d1e 46 #define SENSORS_POLL_INTERVAL 3.0
screamer 33:cfd9430e7d1e 47
screamer 33:cfd9430e7d1e 48 // Send all sensor data or just limited (useful for when running out of memory)
screamer 33:cfd9430e7d1e 49 #define SEND_ALL_SENSORS
screamer 33:cfd9430e7d1e 50
screamer 33:cfd9430e7d1e 51 // Sensors related includes and initialization
saileshtimilsena 35:871447da961d 52 #include "HTS221Sensor.h" //humidity and temperature
saileshtimilsena 35:871447da961d 53 #include "LPS22HBSensor.h" //Barometer
screamer 10:b27c962b3c3f 54
screamer 10:b27c962b3c3f 55 static DevI2C devI2c(PB_11,PB_10);
screamer 12:1f1a50e973db 56 static HTS221Sensor sen_hum_temp(&devI2c);
screamer 12:1f1a50e973db 57 static LPS22HBSensor sen_press_temp(&devI2c);
saileshtimilsena 37:b7acef59086c 58 //static LSM6DSLSensor sen_acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11);
saileshtimilsena 37:b7acef59086c 59
screamer 28:0e774865873d 60 static DigitalOut shutdown_pin(PC_6);
saileshtimilsena 35:871447da961d 61
screamer 33:cfd9430e7d1e 62 // Temperature reading from microcontroller
screamer 33:cfd9430e7d1e 63 AnalogIn adc_temp(ADC_TEMP);
screamer 33:cfd9430e7d1e 64 // Voltage reference reading from microcontroller
screamer 33:cfd9430e7d1e 65 AnalogIn adc_vref(ADC_VREF);
screamer 11:8df4529f060d 66
MarceloSalazar 9:265744785d33 67 // Declaring pointers for access to Pelion Client resources outside of main()
screamer 12:1f1a50e973db 68 MbedCloudClientResource *res_button;
screamer 12:1f1a50e973db 69 MbedCloudClientResource *res_led;
adustm 1:e86b1cffc402 70
screamer 33:cfd9430e7d1e 71 // Additional resources for sensor readings
screamer 28:0e774865873d 72 #ifdef SEND_ALL_SENSORS
screamer 12:1f1a50e973db 73 MbedCloudClientResource *res_humidity;
screamer 12:1f1a50e973db 74 MbedCloudClientResource *res_pressure;
saileshtimilsena 37:b7acef59086c 75 MbedCloudClientResource *res_temperature;
saileshtimilsena 37:b7acef59086c 76
saileshtimilsena 37:b7acef59086c 77
screamer 13:42b49a0caade 78 #endif /* SEND_ALL_SENSORS */
adustm 1:e86b1cffc402 79
screamer 33:cfd9430e7d1e 80 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
screamer 33:cfd9430e7d1e 81 // 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
screamer 33:cfd9430e7d1e 82 EventQueue eventQueue;
screamer 33:cfd9430e7d1e 83
screamer 10:b27c962b3c3f 84 // When the device is registered, this variable will be used to access various useful information, like device ID etc.
screamer 10:b27c962b3c3f 85 static const ConnectorClientEndpointInfo* endpointInfo;
adustm 1:e86b1cffc402 86
screamer 10:b27c962b3c3f 87 /**
adustm 4:cf7342047b4d 88 * PUT handler
adustm 4:cf7342047b4d 89 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 90 * @param newValue Updated value for the resource
adustm 4:cf7342047b4d 91 */
screamer 32:2871fbeb627d 92 void put_callback(MbedCloudClientResource *resource, m2m::String newValue) {
screamer 29:6ff737b67e7d 93 printf("*** PUT received, new value: %s \n", newValue.c_str());
screamer 11:8df4529f060d 94 led = atoi(newValue.c_str());
adustm 1:e86b1cffc402 95 }
adustm 1:e86b1cffc402 96
adustm 4:cf7342047b4d 97 /**
adustm 4:cf7342047b4d 98 * POST handler
adustm 4:cf7342047b4d 99 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 100 * @param buffer If a body was passed to the POST function, this contains the data.
adustm 4:cf7342047b4d 101 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
adustm 4:cf7342047b4d 102 * @param size Size of the body
adustm 4:cf7342047b4d 103 */
screamer 32:2871fbeb627d 104 void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
screamer 32:2871fbeb627d 105 printf("*** POST received (length %u). Payload: ", size);
screamer 32:2871fbeb627d 106 for (size_t ix = 0; ix < size; ix++) {
screamer 32:2871fbeb627d 107 printf("%02x ", buffer[ix]);
screamer 32:2871fbeb627d 108 }
screamer 32:2871fbeb627d 109 printf("\n");
screamer 11:8df4529f060d 110 }
adustm 1:e86b1cffc402 111
screamer 11:8df4529f060d 112 /**
screamer 13:42b49a0caade 113 * Button function triggered by the physical button press.
screamer 11:8df4529f060d 114 */
screamer 11:8df4529f060d 115 void button_press() {
screamer 12:1f1a50e973db 116 int v = res_button->get_value_int() + 1;
screamer 12:1f1a50e973db 117 res_button->set_value(v);
screamer 29:6ff737b67e7d 118 printf("*** Button clicked %d times \n", v);
adustm 1:e86b1cffc402 119 }
saileshtimilsena 35:871447da961d 120 // Called everytime a new character goes into
saileshtimilsena 35:871447da961d 121 // the RX buffer. Test that character for \n
saileshtimilsena 35:871447da961d 122 // Note, rxGetLastChar() gets the last char that
saileshtimilsena 35:871447da961d 123 // we received but it does NOT remove it from
saileshtimilsena 35:871447da961d 124 // the RX buffer.
saileshtimilsena 37:b7acef59086c 125
adustm 4:cf7342047b4d 126 /**
adustm 4:cf7342047b4d 127 * Notification callback handler
adustm 4:cf7342047b4d 128 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 129 * @param status The delivery status of the notification
adustm 4:cf7342047b4d 130 */
adustm 4:cf7342047b4d 131 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
screamer 29:6ff737b67e7d 132 printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status);
adustm 4:cf7342047b4d 133 }
adustm 1:e86b1cffc402 134
adustm 4:cf7342047b4d 135 /**
adustm 4:cf7342047b4d 136 * Registration callback handler
adustm 4:cf7342047b4d 137 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
adustm 4:cf7342047b4d 138 */
adustm 4:cf7342047b4d 139 void registered(const ConnectorClientEndpointInfo *endpoint) {
screamer 17:fc98adcf835a 140 printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
screamer 10:b27c962b3c3f 141 endpointInfo = endpoint;
adustm 4:cf7342047b4d 142 }
adustm 1:e86b1cffc402 143
screamer 10:b27c962b3c3f 144 /**
screamer 10:b27c962b3c3f 145 * Initialize sensors
screamer 10:b27c962b3c3f 146 */
screamer 10:b27c962b3c3f 147 void sensors_init() {
saileshtimilsena 35:871447da961d 148 uint8_t id1, id2;
screamer 29:6ff737b67e7d 149 printf ("\nSensors configuration:\n");
screamer 10:b27c962b3c3f 150 // Initialize sensors
screamer 12:1f1a50e973db 151 sen_hum_temp.init(NULL);
screamer 12:1f1a50e973db 152 sen_press_temp.init(NULL);
screamer 10:b27c962b3c3f 153
screamer 10:b27c962b3c3f 154 /// Call sensors enable routines
screamer 12:1f1a50e973db 155 sen_hum_temp.enable();
screamer 12:1f1a50e973db 156 sen_press_temp.enable();
saileshtimilsena 37:b7acef59086c 157
screamer 29:6ff737b67e7d 158 sen_hum_temp.read_id(&id1);
screamer 29:6ff737b67e7d 159 sen_press_temp.read_id(&id2);
screamer 10:b27c962b3c3f 160
screamer 29:6ff737b67e7d 161 printf("HTS221 humidity & temperature = 0x%X\n", id1);
screamer 29:6ff737b67e7d 162 printf("LPS22HB pressure & temperature = 0x%X\n", id2);
screamer 17:fc98adcf835a 163 printf("\n"); ;
screamer 10:b27c962b3c3f 164 }
screamer 10:b27c962b3c3f 165
screamer 10:b27c962b3c3f 166 /**
screamer 10:b27c962b3c3f 167 * Update sensors and report their values.
screamer 10:b27c962b3c3f 168 * This function is called periodically.
screamer 10:b27c962b3c3f 169 */
screamer 10:b27c962b3c3f 170 void sensors_update() {
saileshtimilsena 37:b7acef59086c 171 float humid_value, pressure_value = 0.0;
saileshtimilsena 37:b7acef59086c 172 float temp1_value = 0.0;
saileshtimilsena 37:b7acef59086c 173 //float temp3_value = 0.0;
saileshtimilsena 35:871447da961d 174
saileshtimilsena 37:b7acef59086c 175 sen_hum_temp.get_humidity(&humid_value);
saileshtimilsena 37:b7acef59086c 176 sen_hum_temp.get_temperature(&temp1_value);
saileshtimilsena 37:b7acef59086c 177 sen_press_temp.get_pressure(&pressure_value);
saileshtimilsena 37:b7acef59086c 178 //temp3_value = adc_temp.read()*100;
saileshtimilsena 37:b7acef59086c 179 //sen_acc_gyro.get_g_axes(g_axes);
saileshtimilsena 37:b7acef59086c 180
saileshtimilsena 37:b7acef59086c 181 printf("%2.2f^%2.1f^%3.2f",temp1_value,humid_value,pressure_value);
saileshtimilsena 37:b7acef59086c 182 printf("\n");
saileshtimilsena 35:871447da961d 183
saileshtimilsena 37:b7acef59086c 184 wait_us(4000);//Wait until the loop_timer reaches 4000us (250Hz) before starting the next loop
saileshtimilsena 37:b7acef59086c 185
screamer 13:42b49a0caade 186 if (endpointInfo) {
screamer 33:cfd9430e7d1e 187 #ifdef SEND_ALL_SENSORS
screamer 32:2871fbeb627d 188 res_humidity->set_value(humid_value);
saileshtimilsena 37:b7acef59086c 189 res_temperature->set_value(temp1_value);
screamer 32:2871fbeb627d 190 res_pressure->set_value(pressure_value);
saileshtimilsena 37:b7acef59086c 191 //res_adc_temp->set_value(temp3_value);
saileshtimilsena 37:b7acef59086c 192
screamer 13:42b49a0caade 193 #endif /* SEND_ALL_SENSORS */
screamer 28:0e774865873d 194 }
screamer 10:b27c962b3c3f 195 }
screamer 10:b27c962b3c3f 196
saileshtimilsena 35:871447da961d 197 int main(void)
saileshtimilsena 35:871447da961d 198 {
screamer 17:fc98adcf835a 199 printf("\nStarting Simple Pelion Device Management Client example\n");
adustm 4:cf7342047b4d 200
screamer 29:6ff737b67e7d 201 int storage_status = fs.mount(&sd);
screamer 29:6ff737b67e7d 202 if (storage_status != 0) {
screamer 29:6ff737b67e7d 203 printf("Storage mounting failed.\n");
screamer 29:6ff737b67e7d 204 }
screamer 30:15743b79c6cb 205 // If the User button is pressed ons start, then format storage.
screamer 29:6ff737b67e7d 206 bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE);
screamer 29:6ff737b67e7d 207 if (btn_pressed) {
screamer 29:6ff737b67e7d 208 printf("User button is pushed on start...\n");
screamer 29:6ff737b67e7d 209 }
screamer 30:15743b79c6cb 210
screamer 29:6ff737b67e7d 211 if (storage_status || btn_pressed) {
screamer 29:6ff737b67e7d 212 printf("Formatting the storage...\n");
screamer 30:15743b79c6cb 213 int storage_status = StorageHelper::format(&fs, &sd);
screamer 10:b27c962b3c3f 214 if (storage_status != 0) {
screamer 13:42b49a0caade 215 printf("ERROR: Failed to reformat the storage (%d).\n", storage_status);
screamer 10:b27c962b3c3f 216 }
screamer 28:0e774865873d 217 } else {
screamer 28:0e774865873d 218 printf("You can hold the user button during boot to format the storage and change the device identity.\n");
screamer 10:b27c962b3c3f 219 }
screamer 10:b27c962b3c3f 220
screamer 10:b27c962b3c3f 221 sensors_init();
screamer 10:b27c962b3c3f 222
adustm 4:cf7342047b4d 223 // Connect to the internet (DHCP is expected to be on)
screamer 13:42b49a0caade 224 printf("Connecting to the network using Wifi...\n");
MarceloSalazar 9:265744785d33 225 net = NetworkInterface::get_default_instance();
adustm 4:cf7342047b4d 226
screamer 10:b27c962b3c3f 227 nsapi_error_t net_status = -1;
screamer 10:b27c962b3c3f 228 for (int tries = 0; tries < 3; tries++) {
screamer 10:b27c962b3c3f 229 net_status = net->connect();
screamer 10:b27c962b3c3f 230 if (net_status == NSAPI_ERROR_OK) {
screamer 10:b27c962b3c3f 231 break;
screamer 10:b27c962b3c3f 232 } else {
screamer 13:42b49a0caade 233 printf("Unable to connect to network. Retrying...\n");
screamer 10:b27c962b3c3f 234 }
screamer 10:b27c962b3c3f 235 }
MarceloSalazar 9:265744785d33 236
screamer 10:b27c962b3c3f 237 if (net_status != NSAPI_ERROR_OK) {
screamer 13:42b49a0caade 238 printf("ERROR: Connecting to the network failed (%d)!\n", net_status);
adustm 1:e86b1cffc402 239 return -1;
adustm 1:e86b1cffc402 240 }
adustm 1:e86b1cffc402 241
MarceloSalazar 9:265744785d33 242 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
adustm 1:e86b1cffc402 243
screamer 17:fc98adcf835a 244 printf("Initializing Pelion Device Management Client...\n");
screamer 17:fc98adcf835a 245
MarceloSalazar 9:265744785d33 246 // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM
MarceloSalazar 9:265744785d33 247 SimpleMbedCloudClient client(net, bd, &fs);
adustm 4:cf7342047b4d 248 int client_status = client.init();
adustm 4:cf7342047b4d 249 if (client_status != 0) {
screamer 13:42b49a0caade 250 printf("ERROR: Pelion Client initialization failed (%d)\n", client_status);
adustm 1:e86b1cffc402 251 return -1;
adustm 1:e86b1cffc402 252 }
adustm 1:e86b1cffc402 253
adustm 4:cf7342047b4d 254 // Creating resources, which can be written or read from the cloud
screamer 32:2871fbeb627d 255 res_button = client.create_resource("3200/0/5501", "Button Count");
screamer 12:1f1a50e973db 256 res_button->set_value(0);
screamer 12:1f1a50e973db 257 res_button->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 258 res_button->observable(true);
screamer 12:1f1a50e973db 259 res_button->attach_notification_callback(button_callback);
adustm 1:e86b1cffc402 260
screamer 33:cfd9430e7d1e 261 res_led = client.create_resource("3201/0/5853", "LED State");
screamer 33:cfd9430e7d1e 262 res_led->set_value(1);
screamer 33:cfd9430e7d1e 263 res_led->methods(M2MMethod::GET | M2MMethod::PUT);
screamer 33:cfd9430e7d1e 264 res_led->attach_put_callback(put_callback);
screamer 33:cfd9430e7d1e 265
screamer 33:cfd9430e7d1e 266 #ifdef SEND_ALL_SENSORS
screamer 10:b27c962b3c3f 267 // Sensor resources
saileshtimilsena 37:b7acef59086c 268 res_temperature = client.create_resource("3303/0/5700", "Temperature HTS221 (C)");
screamer 12:1f1a50e973db 269 res_temperature->set_value(0);
screamer 12:1f1a50e973db 270 res_temperature->methods(M2MMethod::GET);
saileshtimilsena 37:b7acef59086c 271 res_temperature->observable(true);
screamer 10:b27c962b3c3f 272
screamer 32:2871fbeb627d 273 res_humidity = client.create_resource("3304/0/5700", "Humidity");
screamer 12:1f1a50e973db 274 res_humidity->set_value(0);
screamer 12:1f1a50e973db 275 res_humidity->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 276 res_humidity->observable(true);
screamer 10:b27c962b3c3f 277
screamer 32:2871fbeb627d 278 res_pressure = client.create_resource("3323/0/5700", "Pressure");
screamer 28:0e774865873d 279 res_pressure->set_value(0);
screamer 28:0e774865873d 280 res_pressure->methods(M2MMethod::GET);
screamer 28:0e774865873d 281 res_pressure->observable(true);
saileshtimilsena 37:b7acef59086c 282
screamer 13:42b49a0caade 283 #endif /* SEND_ALL_SENSORS */
screamer 11:8df4529f060d 284
MarceloSalazar 9:265744785d33 285 printf("Initialized Pelion Client. Registering...\n");
adustm 1:e86b1cffc402 286
adustm 4:cf7342047b4d 287 // Callback that fires when registering is complete
adustm 4:cf7342047b4d 288 client.on_registered(&registered);
adustm 1:e86b1cffc402 289
MarceloSalazar 9:265744785d33 290 // Register with Pelion DM
adustm 4:cf7342047b4d 291 client.register_and_connect();
adustm 1:e86b1cffc402 292
screamer 17:fc98adcf835a 293 int i = 600; // wait up 60 seconds before attaching sensors and button events
screamer 12:1f1a50e973db 294 while (i-- > 0 && !client.is_client_registered()) {
screamer 12:1f1a50e973db 295 wait_ms(100);
screamer 12:1f1a50e973db 296 }
saileshtimilsena 37:b7acef59086c 297
screamer 11:8df4529f060d 298 button.fall(eventQueue.event(&button_press));
screamer 10:b27c962b3c3f 299
screamer 15:a0430d40a918 300 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
adustm 4:cf7342047b4d 301 Ticker timer;
screamer 11:8df4529f060d 302 timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL);
adustm 1:e86b1cffc402 303
adustm 4:cf7342047b4d 304 // You can easily run the eventQueue in a separate thread if required
adustm 4:cf7342047b4d 305 eventQueue.dispatch_forever();
adustm 1:e86b1cffc402 306 }
screamer 28:0e774865873d 307
MarceloSalazar 9:265744785d33 308 #endif