hhh

Dependencies:   HC_SR04_Ultrasonic_Library X_NUCLEO_IKS01A2 mbed LoRaWAN-lib SX1272Lib

Fork of LoRaWANdemoUnina by Salvatore Gulfo

Revision:
8:346c55cb6033
Parent:
7:5077515c163b
Child:
9:85c85c65299a
--- a/app/main.cpp	Mon Apr 24 13:29:34 2017 +0000
+++ b/app/main.cpp	Tue Oct 03 13:11:33 2017 +0000
@@ -19,6 +19,16 @@
 #include "LoRaMac.h"
 #include "Commissioning.h"
 #include "SerialDisplay.h"
+#include "XNucleoIKS01A2.h"
+#include "string.h"
+
+/* Instantiate the expansion board */
+static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
+
+/* Retrieve the composing elements of the expansion board */
+static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
+//static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
+//static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
 
 /*!
  * Defines the application data transmission duty cycle. 5s, value in [ms].
@@ -34,7 +44,7 @@
 /*!
  * Default datarate
  */
-#define LORAWAN_DEFAULT_DATARATE                    DR_0
+#define LORAWAN_DEFAULT_DATARATE                    DR_0                      //SF 12
 
 /*!
  * LoRaWAN confirmed messages
@@ -57,7 +67,7 @@
  *
  * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
  */
-#define LORAWAN_DUTYCYCLE_ON                        false
+#define LORAWAN_DUTYCYCLE_ON                        true
 
 #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP          1
 
@@ -78,7 +88,7 @@
 /*!
  * LoRaWAN application port
  */
-#define LORAWAN_APP_PORT                            15
+#define LORAWAN_APP_PORT                            13
 
 /*!
  * User application data buffer size
@@ -165,6 +175,43 @@
  */
 static bool NextTx = true;
 
+/* Helper function for printing floats & doubles */
+static char *print_double(char* str, double v, int decimalDigits=2)
+{
+  int i = 1;
+  int intPart;
+  int fractPart;
+  int len;
+  char *ptr;
+
+  /* prepare decimal digits multiplicator */
+  for (;decimalDigits!=0; i*=10, decimalDigits--);
+
+  /* calculate integer & fractional parts */
+  intPart = (int)v;
+  fractPart = (int)((v-(double)(int)v)*i);
+
+  /* fill in integer part */
+  sprintf(str, "%i.", intPart);
+
+  /* prepare fill in of fractional part */
+  len = strlen(str);
+  ptr = &str[len];
+
+  /* fill in leading fractional zeros */
+  for (i/=10;i>1; i/=10, ptr++) {
+    if (fractPart >= i) {
+      break;
+    }
+    *ptr = '0';
+  }
+
+  /* fill in (rest of) fractional part */
+  sprintf(ptr, "%i", fractPart);
+
+  return str;
+}
+
 /*!
  * Device states
  */
@@ -214,7 +261,8 @@
     uint8_t Port;
     uint8_t *Buffer;
     uint8_t BufferSize;
-}LoRaMacUplinkStatus;
+    
+    }LoRaMacUplinkStatus;
 volatile bool UplinkStatusUpdated = false;
 
 /*!
@@ -284,10 +332,75 @@
 /*!
  * \brief   Prepares the payload of the frame
  */
+    float value1, value2;
+    char buffer1[32];
+  //  int16_t Myaxes_int[3];
+  //  float Myaxes[3];
+  //  float sensitivity;
+ //   int32_t axes[3];
+ //   char Cont[64];
+   uint32_t Cont[64];
+ //  InterruptIn BlueButton(USER_BUTTON);  
 static void PrepareTxFrame( uint8_t port )
 {
+    
     switch( port )
     {
+        
+    case 13:
+    
+        hum_temp->get_temperature(&value1);
+        hum_temp->get_humidity(&value2);
+        strcpy((char*)Cont, (char*) print_double(buffer1,value2) );
+//       strcat((char*)Cont, " " );
+ //       strcat((char*)Cont, print_double(buffer1,value2) );
+ //       strcat((char*)Cont, " ");
+    //    strcat((char*)Cont,  print_double(buffer1,value2));
+   //     strcat((char*)Cont, " ");
+        
+ /*          acc_gyro->get_x_axes(axes);   
+           strcpy(Cont,(char*) print_double(buffer1,axes[0]));
+           strcat(Cont, " ");
+           strcat(Cont,(char*)print_double(buffer1,axes[1]));
+           strcat(Cont, " ");
+           strcat(Cont,(char*)print_double(buffer1,axes[2]));
+           strcat(Cont, " ");
+           strcat(Cont, (char*)print_double(buffer1,value1));
+           strcat(Cont, " ");
+           strcat(Cont, (char*)print_double(buffer1,value2));
+           AppDataSize = sprintf ((char*)AppData, Cont);     
+           
+         acc_gyro->get_x_axes_raw(Myaxes_int);
+         acc_gyro->get_x_sensitivity(&sensitivity);
+         Myaxes[0]=((float)(Myaxes_int[0]))*sensitivity;
+         Myaxes[1]=((float)(Myaxes_int[1]))*sensitivity;
+         Myaxes[2]=((float)(Myaxes_int[2]))*sensitivity;
+         strcpy(Cont, (char*) print_double(buffer1, value1));
+         strcat(Cont, " ");
+         strcat(Cont, (char*) print_double(buffer1, value2));
+         strcat(Cont, " ");         
+         strcat(Cont, (char*) print_double(buffer1, Myaxes[0]));
+         strcat(Cont, " ");
+         strcat(Cont, (char*) print_double(buffer1, Myaxes[1]));
+         strcat(Cont, " ");
+         strcat(Cont, (char*) print_double(buffer1, Myaxes[2]));
+         strcat(Cont, " ");
+         strcat(Cont, (char*) print_double(buffer1, value1));
+         strcat(Cont, " ");
+         strcat(Cont, (char*) print_double(buffer1, value2)); */
+         
+       // while(1) {
+        AppDataSize = sprintf ((char*)AppData,(char*)Cont);    
+  /*       if (BlueButton)
+         { AppDataSize = sprintf((char*)AppData, "0");   
+          break;  }
+          else
+       {  AppDataSize = sprintf((char*)AppData, "1");   
+         break;        }  
+ //   AppDataSize = sprintf((char*)AppData, "69"); 
+         //} */
+    break;
+    
     case 15:
         {
             AppData[0] = AppLedStateOn;
@@ -300,7 +413,7 @@
                 AppData[5] = LoRaMacDownlinkStatus.Snr;
             }
         }
-        break;
+        break;     
     case 224:
         if( ComplianceTest.LinkCheck == true )
         {
@@ -336,7 +449,8 @@
  *
  * \retval  [0: frame could be send, 1: error]
  */
-static bool SendFrame( void )
+static bool SendFrame(void)
+
 {
     McpsReq_t mcpsReq;
     LoRaMacTxInfo_t txInfo;
@@ -764,6 +878,12 @@
     LoRaMacPrimitives_t LoRaMacPrimitives;
     LoRaMacCallback_t LoRaMacCallbacks;
     MibRequestConfirm_t mibReq;
+    
+    //enable sensors
+  //  accelerometer -> enable();
+    hum_temp -> enable();
+   // acc_gyro->enable_x();
+   // acc_gyro->enable_g();
 
     BoardInit( );
     SerialDisplayInit( );
@@ -771,6 +891,9 @@
     SerialDisplayUpdateEui( 5, DevEui );
     SerialDisplayUpdateEui( 6, AppEui );
     SerialDisplayUpdateKey( 7, AppKey );
+    
+    
+    
 
 #if( OVER_THE_AIR_ACTIVATION == 0 )
     SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );