BG96_K6xF_pelion-example-frdm_Temp

Dependencies:   FXAS21002 FXOS8700Q

Overview

This document is based on https://os.mbed.com/teams/NXP/code/pelion-example-frdm/ and the code forked Daniel_Lee's(https://os.mbed.com/users/Daniel_Lee/code/BG96_K6xF_pelion-example-frdm/)BG96_K6xF_pelion-example-frdm repository and added some features. Need a WIZnet IoT Shield BG96 board and development board.

This example is known to work great on the following platforms:

/media/uploads/stkim92/pel01.png

Requirement

  1. FRDM-K64F or FRDM-K66F
  2. WIZnet IoT Shield BG96 board
  3. USIM card

Example functionality

This example showcases the following device functionality:

Read onboard FXOS8700Q accelerometer, magnetometer and temperature(on shield). And report the values as Pelion LWM2M resources (see image below). (FRDM-K66F only) Read onboard FXAS21002 gyroscope and report the values as Pelion LWM2M resources. On user button click, increment Pelion LWM2M button resource. Allow the user to change the state of the board LED from Pelion LWM2M led_state resource and PUT request.

1. Import into Compiler

/media/uploads/stkim92/pel1.png

2. Apply Update Certificate

/media/uploads/stkim92/pel03.png

3. Compile and Program

/media/uploads/stkim92/pel04.png

4. If successfully connect to cellular networks(SKTelecom) then you can get below message

Device's Result

include the mbed library with this snippet

You can hold the user button during boot to format the storage and change the device identity.

M2Mnet(BG96) Power ON



Sensors configuration:

FXOS8700Q accelerometer = 0xC7

FXOS8700Q magnetometer  = 0xC7



Connecting to the network using the default network interface...

Connected to the network successfully. IP address: 2001:2D8:65

Initializing Pelion Device Management Client...

Initialized Pelion Device Management Client. Registering...

Press the user button to increment the LwM2M resource value...

Celsius temp : 26.10 C                                                             

FXOS8700Q mag:    0.217 x,   0.420 y,   0.288 z [gauss]     

Pelion Cloud Result (1)

/media/uploads/stkim92/pel4.png

Pelion Cloud Result (2)

/media/uploads/stkim92/pel5.png

Revision:
14:bd420de18a97
Parent:
13:ec1c3a64ee39
--- a/main.cpp	Wed Jul 24 16:20:11 2019 +0000
+++ b/main.cpp	Tue Aug 06 14:38:56 2019 +0000
@@ -43,7 +43,7 @@
 
 // How often to fetch sensor data (in seconds)
 #define SENSORS_POLL_INTERVAL 3.0
-
+#define MBED_CONF_IOTSHIELD_SENSOR_TEMP             A1
 // Send all sensor data or just limited (useful for when running out of memory)
 //#define SEND_ALL_SENSORS
 
@@ -51,6 +51,8 @@
 #include "FXOS8700Q.h"
 #include "FXAS21002.h"
 
+AnalogIn   tempVal(MBED_CONF_IOTSHIELD_SENSOR_TEMP); //19.08.06 added by tom
+
 #ifdef TARGET_K66F
 I2C sens_i2c(PTD9, PTD8);
 #else
@@ -67,9 +69,11 @@
 MbedCloudClientResource *res_button;
 MbedCloudClientResource *res_led;
 MbedCloudClientResource *res_post;
+MbedCloudClientResource *res_temperature; //19.08.06 added by tom
 
 // Additional resources for sensor readings
 #ifdef SEND_ALL_SENSORS
+MbedCloudClientResource *res_temperature; //19.08.06 added by tom
 MbedCloudClientResource *res_magnometer_x;
 MbedCloudClientResource *res_magnometer_y;
 MbedCloudClientResource *res_magnometer_z;
@@ -83,6 +87,10 @@
 #endif /* TARGET_K66F */
 #endif /* SEND_ALL_SENSORS */
 
+float temp =0;
+float voltage = 0;    // 전압 계산 값 넣을 변수
+float celsius = 0;    // 섭씨 계산 값 넣을 변수
+
 void BG96_Modem_PowerON(void)
 {
     DigitalOut BG96_RESET(D7);
@@ -181,11 +189,22 @@
     printf("\n"); ;
 }
 
+float getTemperature_C(float _voltage)
+{
+    // LM35는 섭씨 1도당 10mV의 전위차를 갖는다.
+    // 센서핀의 전압이 0.28V라면 280mV이므로 온도는 28도씨.
+    // 100을 곱해서 섭씨 온도로 나타냄
+    celsius = voltage * 100.0;
+    return celsius;
+}
+
+
 /**
  * Update sensors and report their values.
  * This function is called periodically.
  */
 void sensors_update() {
+
     motion_data_counts_t acc_raw, mag_raw;
 
     sens_acc.getAxis(acc_raw);
@@ -193,6 +212,15 @@
 
     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;
     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;
+    
+    temp = tempVal.read_u16()/100;
+    voltage = temp * 4.95 / 1024;
+    celsius = getTemperature_C(voltage);
+    printf("Celsius temp : %.2f C", celsius);
+    if (endpointInfo) {
+        res_temperature->set_value(celsius);
+    }
+    
 #ifdef TARGET_K66F
     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;
 #endif /* TARGET_K66F */
@@ -209,6 +237,7 @@
 
     if (endpointInfo) {
 #ifdef SEND_ALL_SENSORS
+        res_temperature->set_value(celsius);
         res_accelerometer_x->set_value(acc_x);
         res_accelerometer_y->set_value(acc_y);
         res_accelerometer_z->set_value(acc_z);
@@ -224,6 +253,9 @@
     }
 }
 
+
+
+
 int main(void) {
     printf("\nStarting Simple Pelion Device Management Client example\n");
 
@@ -278,6 +310,12 @@
     }
 
     // Creating resources, which can be written or read from the cloud
+    
+    res_temperature = client.create_resource("3303/0/5700", "Temperature (C)");
+    res_temperature->set_value(0);
+    res_temperature->methods(M2MMethod::GET);
+    res_temperature->observable(true);
+    
     res_button = client.create_resource("3200/0/5501", "button_count");
     res_button->set_value(0);
     res_button->methods(M2MMethod::GET);
@@ -362,4 +400,5 @@
     eventQueue.dispatch_forever();
 }
 
+
 #endif /* MBED_TEST_MODE */