K6xF board with BG96 of WIZnet for CatM1 board.

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 "FATFileSystem.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 = NetworkInterface::get_default_instance();
00027 
00028 // Default block device available on the target board
00029 BlockDevice *bd = BlockDevice::get_default_instance();
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 led(LED1, 0);
00043 
00044 // How often to fetch sensor data (in seconds)
00045 #define SENSORS_POLL_INTERVAL 3.0
00046 
00047 // Send all sensor data or just limited (useful for when running out of memory)
00048 //#define SEND_ALL_SENSORS
00049 
00050 // Sensors related includes and initialization
00051 #include "FXOS8700Q.h"
00052 #include "FXAS21002.h"
00053 
00054 #ifdef TARGET_K66F
00055 I2C sens_i2c(PTD9, PTD8);
00056 #else
00057 I2C sens_i2c(PTE25, PTE24);
00058 #endif /* TARGET_K66F */
00059 
00060 FXOS8700QAccelerometer sens_acc(sens_i2c, FXOS8700CQ_SLAVE_ADDR1);    // Configured for the FRDM-K64F with onboard sensors
00061 FXOS8700QMagnetometer sens_mag(sens_i2c, FXOS8700CQ_SLAVE_ADDR1);
00062 #ifdef TARGET_K66F
00063 FXAS21002 sens_gyro(PTD9, PTD8, 0x20);
00064 #endif /* TARGET_K66F */
00065 
00066 // Declaring pointers for access to Pelion Device Management Client resources outside of main()
00067 MbedCloudClientResource *res_button;
00068 MbedCloudClientResource *res_led;
00069 MbedCloudClientResource *res_post;
00070 
00071 // Additional resources for sensor readings
00072 #ifdef SEND_ALL_SENSORS
00073 MbedCloudClientResource *res_magnometer_x;
00074 MbedCloudClientResource *res_magnometer_y;
00075 MbedCloudClientResource *res_magnometer_z;
00076 MbedCloudClientResource *res_accelerometer_x;
00077 MbedCloudClientResource *res_accelerometer_y;
00078 MbedCloudClientResource *res_accelerometer_z;
00079 #ifdef TARGET_K66F
00080 MbedCloudClientResource *res_gyroscope_x;
00081 MbedCloudClientResource *res_gyroscope_y;
00082 MbedCloudClientResource *res_gyroscope_z;
00083 #endif /* TARGET_K66F */
00084 #endif /* SEND_ALL_SENSORS */
00085 
00086 void BG96_Modem_PowerON(void)
00087 {
00088     DigitalOut BG96_RESET(D7);
00089     DigitalOut BG96_PWRKEY(D9);
00090  
00091     BG96_RESET = 1;
00092     BG96_PWRKEY = 1;
00093     wait_ms(200);
00094  
00095     BG96_RESET = 0;
00096     BG96_PWRKEY = 0;
00097     wait_ms(300);
00098  
00099     BG96_RESET = 1;
00100     wait_ms(5000);
00101 }
00102 
00103 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
00104 // 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
00105 EventQueue eventQueue;
00106 
00107 // When the device is registered, this variable will be used to access various useful information, like device ID etc.
00108 static const ConnectorClientEndpointInfo* endpointInfo;
00109 
00110 /**
00111  * PUT handler - sets the value of the built-in LED
00112  * @param resource The resource that triggered the callback
00113  * @param newValue Updated value for the resource
00114  */
00115 void put_callback(MbedCloudClientResource *resource, m2m::String newValue) {
00116     printf("*** PUT received, new value: %s                             \n", newValue.c_str());
00117     led = atoi(newValue.c_str());
00118 }
00119 
00120 /**
00121  * POST handler - prints the content of the payload
00122  * @param resource The resource that triggered the callback
00123  * @param buffer If a body was passed to the POST function, this contains the data.
00124  *               Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
00125  * @param size Size of the body
00126  */
00127 void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
00128     printf("*** POST received (length %u). Payload: ", size);
00129     for (size_t ix = 0; ix < size; ix++) {
00130         printf("%02x ", buffer[ix]);
00131     }
00132     printf("\n");
00133 }
00134 
00135 /**
00136  * Button handler
00137  * This function will be triggered either by a physical button press or by a ticker every 5 seconds (see below)
00138  */
00139 void button_press() {
00140     int v = res_button->get_value_int() + 1;
00141     res_button->set_value(v);
00142     printf("*** Button clicked %d times                                 \n", v);
00143 }
00144 
00145 /**
00146  * Notification callback handler
00147  * @param resource The resource that triggered the callback
00148  * @param status The delivery status of the notification
00149  */
00150 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
00151     printf("*** Button notification, status %s (%d)                     \n", MbedCloudClientResource::delivery_status_to_string(status), status);
00152 }
00153 
00154 /**
00155  * Registration callback handler
00156  * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
00157  */
00158 void registered(const ConnectorClientEndpointInfo *endpoint) {
00159     printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
00160     endpointInfo = endpoint;
00161 }
00162 
00163 /**
00164  * Initialize sensors
00165  */
00166 void sensors_init() {
00167     printf ("\nSensors configuration:\n");
00168 
00169     sens_acc.enable();
00170     sens_mag.enable();
00171 #ifdef TARGET_K66F
00172     sens_gyro.activate(true);
00173 #endif /* TARGET_K66F */
00174 
00175     printf("FXOS8700Q accelerometer = 0x%X\n", sens_acc.whoAmI());
00176     printf("FXOS8700Q magnetometer  = 0x%X\n", sens_mag.whoAmI());
00177 #ifdef TARGET_K66F
00178     printf("FXAS21002 gyroscope     = 0x%X\n", sens_gyro.getStatus());
00179 #endif /* TARGET_K66F */
00180 
00181     printf("\n"); ;
00182 }
00183 
00184 /**
00185  * Update sensors and report their values.
00186  * This function is called periodically.
00187  */
00188 void sensors_update() {
00189     motion_data_counts_t acc_raw, mag_raw;
00190 
00191     sens_acc.getAxis(acc_raw);
00192     sens_mag.getAxis(mag_raw);
00193 
00194     float mag_x = (double)mag_raw.x / 1000.0, mag_y = (double)mag_raw.y / 1000.0, mag_z = (double)mag_raw.z / 1000.0;
00195     float acc_x = (double)acc_raw.x / 1000.0, acc_y = (double)acc_raw.y / 1000.0, acc_z = (double)acc_raw.z / 1000.0;
00196 #ifdef TARGET_K66F
00197     float gyro_x = (double)sens_gyro.getX() / 1000.0, gyro_y = (double)sens_gyro.getY() / 1000.0, gyro_z = (double)sens_gyro.getZ() / 1000.0;
00198 #endif /* TARGET_K66F */
00199 
00200     printf("                                                             \n");
00201     printf("FXOS8700Q mag:  %7.3f x, %7.3f y, %7.3f z [gauss]        \n", mag_x, mag_y, mag_z);
00202     printf("FXOS8700Q acc:  %7.3f x, %7.3f y, %7.3f z [g]            \n", acc_x, acc_y, acc_z);
00203 #ifdef TARGET_K66F
00204     printf("FXAS21002 gryo: %7.3f x, %7.3f y, %7.3f z [dps]          \n", gyro_x, gyro_y, gyro_z);
00205     printf("\r\033[4A");
00206 #else
00207     printf("\r\033[3A");
00208 #endif /* TARGET_K66F */
00209 
00210     if (endpointInfo) {
00211 #ifdef SEND_ALL_SENSORS
00212         res_accelerometer_x->set_value(acc_x);
00213         res_accelerometer_y->set_value(acc_y);
00214         res_accelerometer_z->set_value(acc_z);
00215         res_magnometer_x->set_value(mag_x);
00216         res_magnometer_y->set_value(mag_y);
00217         res_magnometer_z->set_value(mag_z);
00218 #ifdef TARGET_K66F
00219         res_gyroscope_x->set_value(gyro_x);
00220         res_gyroscope_y->set_value(gyro_y);
00221         res_gyroscope_z->set_value(gyro_z);
00222 #endif /* TARGET_K66F */
00223 #endif /* SEND_ALL_SENSORS */
00224     }
00225 }
00226 
00227 int main(void) {
00228     printf("\nStarting Simple Pelion Device Management Client example\n");
00229 
00230     int storage_status = fs.mount(bd);
00231     if (storage_status != 0) {
00232         printf("Storage mounting failed.\n");
00233     }
00234 #if USE_BUTTON == 1
00235     // If the User button is pressed ons start, then format storage.
00236     bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE);
00237     if (btn_pressed) {
00238         printf("User button is pushed on start...\n");
00239     }
00240 #else
00241     bool btn_pressed = FALSE;
00242 #endif /* USE_BUTTON */
00243 
00244     if (storage_status || btn_pressed) {
00245         printf("Formatting the storage...\n");
00246         int storage_status = StorageHelper::format(&fs, bd);
00247         if (storage_status != 0) {
00248             printf("ERROR: Failed to reformat the storage (%d).\n", storage_status);
00249         }
00250     } else {
00251         printf("You can hold the user button during boot to format the storage and change the device identity.\n");
00252     }
00253 
00254     BG96_Modem_PowerON();
00255     printf("M2Mnet(BG96) Power ON\n");
00256     
00257     sensors_init();
00258 
00259     // Connect to the Internet (DHCP is expected to be on)
00260     printf("Connecting to the network using the default network interface...\n");
00261     net = NetworkInterface::get_default_instance();
00262 
00263     nsapi_error_t net_status = NSAPI_ERROR_NO_CONNECTION;
00264     while ((net_status = net->connect()) != NSAPI_ERROR_OK) {
00265         printf("Unable to connect to network (%d). Retrying...\n", net_status);
00266     }
00267 
00268     printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
00269 
00270     printf("Initializing Pelion Device Management Client...\n");
00271 
00272     // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management
00273     SimpleMbedCloudClient client(net, bd, &fs);
00274     int client_status = client.init();
00275     if (client_status != 0) {
00276         printf("Pelion Client initialization failed (%d)\n", client_status);
00277         return -1;
00278     }
00279 
00280     // Creating resources, which can be written or read from the cloud
00281     res_button = client.create_resource("3200/0/5501", "button_count");
00282     res_button->set_value(0);
00283     res_button->methods(M2MMethod::GET);
00284     res_button->observable(true);
00285     res_button->attach_notification_callback(button_callback);
00286 
00287     res_led = client.create_resource("3201/0/5853", "led_state");
00288     res_led->set_value(led.read());
00289     res_led->methods(M2MMethod::GET | M2MMethod::PUT);
00290     res_led->attach_put_callback(put_callback);
00291 
00292     res_post = client.create_resource("3300/0/5605", "execute_function");
00293     res_post->methods(M2MMethod::POST);
00294     res_post->attach_post_callback(post_callback);
00295 
00296 #ifdef SEND_ALL_SENSORS
00297     res_accelerometer_x = client.create_resource("3313/0/5702", "Accelerometer X");
00298     res_accelerometer_x->set_value(0);
00299     res_accelerometer_x->methods(M2MMethod::GET);
00300     res_accelerometer_x->observable(true);
00301 
00302     res_accelerometer_y = client.create_resource("3313/0/5703", "Accelerometer Y");
00303     res_accelerometer_y->set_value(0);
00304     res_accelerometer_y->methods(M2MMethod::GET);
00305     res_accelerometer_y->observable(true);
00306 
00307     res_accelerometer_z = client.create_resource("3313/0/5704", "Accelerometer Z");
00308     res_accelerometer_z->set_value(0);
00309     res_accelerometer_z->methods(M2MMethod::GET);
00310     res_accelerometer_z->observable(true);
00311 
00312     res_magnometer_x = client.create_resource("3314/0/5702", "Magnometer X");
00313     res_magnometer_x->set_value(0);
00314     res_magnometer_x->methods(M2MMethod::GET);
00315     res_magnometer_x->observable(true);
00316 
00317     res_magnometer_y = client.create_resource("3314/0/5703", "Magnometer Y");
00318     res_magnometer_y->set_value(0);
00319     res_magnometer_y->methods(M2MMethod::GET);
00320     res_magnometer_y->observable(true);
00321 
00322     res_magnometer_z = client.create_resource("3314/0/5704", "Magnometer Z");
00323     res_magnometer_z->set_value(0);
00324     res_magnometer_z->methods(M2MMethod::GET);
00325     res_magnometer_z->observable(true);
00326 
00327 #ifdef TARGET_K66F
00328     res_gyroscope_x = client.create_resource("3334/0/5702", "Gyroscope X");
00329     res_gyroscope_x->set_value(0);
00330     res_gyroscope_x->methods(M2MMethod::GET);
00331     res_gyroscope_x->observable(true);
00332 
00333     res_gyroscope_y = client.create_resource("3334/0/5703", "Gyroscope Y");
00334     res_gyroscope_y->set_value(0);
00335     res_gyroscope_y->methods(M2MMethod::GET);
00336     res_gyroscope_y->observable(true);
00337 
00338     res_gyroscope_z = client.create_resource("3334/0/5704", "Gyroscope Z");
00339     res_gyroscope_z->set_value(0);
00340     res_gyroscope_z->methods(M2MMethod::GET);
00341     res_gyroscope_z->observable(true);
00342 #endif /* TARGET_K66F */
00343 #endif /* SEND_ALL_SENSORS */
00344 
00345     printf("Initialized Pelion Device Management Client. Registering...\n");
00346 
00347     // Callback that fires when registering is complete
00348     client.on_registered(&registered);
00349 
00350     // Register with Pelion DM
00351     client.register_and_connect();
00352 
00353     // The button fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
00354     button.fall(eventQueue.event(&button_press));
00355     printf("Press the user button to increment the LwM2M resource value...\n");
00356 
00357     // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
00358     Ticker timer;
00359     timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL);
00360 
00361     // You can easily run the eventQueue in a separate thread if required
00362     eventQueue.dispatch_forever();
00363 }
00364 
00365 #endif /* MBED_TEST_MODE */