BG96_K6xF_pelion-example-frdm_Temp

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