Lora Personalized device for Everynet
Dependencies: LMiCPersonalizedforEverynet SX1276Lib X_NUCLEO_IKS01A1 cantcoap lwip mbed-rtos mbed
Fork of LoRaWAN-test-10secs by
Diff: main.cpp
- Revision:
- 10:155dc60fce79
- Parent:
- 9:84a69ca4d35a
- Child:
- 11:59c160a8926d
--- a/main.cpp Fri Jan 08 09:58:07 2016 +0000 +++ b/main.cpp Wed Jan 13 14:23:39 2016 +0000 @@ -20,10 +20,36 @@ #include "mbed.h" //#include "Node.h" #include "cantcoap.h" +#include "x_nucleo_iks01a1.h" +#include "rtos.h" #include "lmic.h" #include "debug.h" +/* Instantiate the expansion board */ +static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15); + +/* Retrieve the composing elements of the expansion board */ +static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope(); +static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer(); +static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; +static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; +static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; +static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; +static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; + +uint8_t id; +float value1, value2; +char buffer1[32], buffer2[32], buffer3[32]; +int32_t axes[3]; + +static bool temp_sent = false; +static bool hum_sent = false; +static bool press_sent = false; +static bool gyro_sent = false; +static bool motion_sent = false; +static bool magn_sent = false; + const std::string REGISTRATION_SEGMENT ="/rd"; const std::string ENDPOINT_SEGMENT = "?ep="; const std::string LIFETIME ="<="; @@ -102,6 +128,136 @@ //Node lwm2mNode("LR-test0");; unsigned int LoRaWAN_data_size = 0; +/* Helper function for printing floats & doubles */ +static char *printDouble(char* str, double v, int decimalDigits=2) +{ + int i = 1; + int intPart, fractPart; + int len; + char *ptr; + + /* prepare decimal digits multiplicator */ + for (;decimalDigits!=0; i*=10, decimalDigits--); + + /* calculate integer & fractinal 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; +} + +/* Helper function for printing integers */ +static char *printInt(char* str, int v) +{ + /* fill in integer part */ + sprintf(str, "%i", v); + + return str; +} + +/* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ +/* Node updates accelerometer every 60 seconds. Notification sending is done here. */ +static void exec_call_thread(void const *args) +{ + uint8_t id; + float value1, value2; + char buffer1[32], buffer2[32]; + int32_t axes[3]; + + debug_str("--- Starting new run ---\r\n"); + + humidity_sensor->ReadID(&id); + debug_str("HTS221 humidity & temperature = "); + debug_uint(id); + debug_str("\r\n"); + pressure_sensor->ReadID(&id); + debug_str("LPS25H pressure & temperature = "); + debug_uint(id); + debug_str("\r\n"); + magnetometer->ReadID(&id); + debug_str("LIS3MDL magnetometer = "); + debug_uint(id); + debug_str("\r\n"); + gyroscope->ReadID(&id); + debug_str("LSM6DS0 accelerometer & gyroscope = "); + debug_uint(id); + debug_str("\r\n"); + + wait(3); + + while(1) { + debug_str("\r\n"); + + temp_sensor1->GetTemperature(&value1); + humidity_sensor->GetHumidity(&value2); + debug_str("HTS221: [temp] "); + debug_str(printDouble(buffer1, value1)); + debug_str("°C, [hum] "); + debug_str(printDouble(buffer2, value2)); + debug_str("%\r\n"); + //pc.printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); + + temp_sensor2->GetFahrenheit(&value1); + pressure_sensor->GetPressure(&value2); + debug_str("LPS25H: [temp] "); + debug_str(printDouble(buffer1, value1)); + debug_str("°F, [press] "); + debug_str(printDouble(buffer2, value2)); + debug_str("mbar\r\n"); + //pc.printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); + + debug_str("---\r\n"); + + magnetometer->Get_M_Axes(axes); + debug_str("LIS3MDL [mag/mgauss]: "); + debug_uint(axes[0]); + debug_str(", "); + debug_uint(axes[1]); + debug_str(", "); + debug_uint(axes[2]); + debug_str("\r\n"); + //pc.printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); + + accelerometer->Get_X_Axes(axes); + debug_str("LSM6DS0 [acc/mg]: "); + debug_uint(axes[0]); + debug_str(", "); + debug_uint(axes[1]); + debug_str(", "); + debug_uint(axes[2]); + debug_str("\r\n"); + //pc.printf("LSM6DS0 [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); + + gyroscope->Get_G_Axes(axes); + debug_str("LSM6DS0 [gyro/mdps]: "); + debug_uint(axes[0]); + debug_str(", "); + debug_uint(axes[1]); + debug_str(", "); + debug_uint(axes[2]); + debug_str("\r\n"); + //pc.printf("LSM6DS0 [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); + + wait(1.5); + } +} + std::string to_string( int x ) { int length = snprintf( NULL, 0, "%d", x ); assert( length >= 0 ); @@ -111,6 +267,7 @@ delete[] buf; return str; } + unsigned char * get_Registration_Payload(long *payload_size){ string registration_Payload =""; @@ -373,6 +530,157 @@ LoRaWAN_data_size = strlen(frame); } +static void prepareTxSensorsFrame( void ) +{ + std::string frame = ""; + std::string tmp; + debug_str("\r\n"); + + temp_sensor1->GetTemperature(&value1); + humidity_sensor->GetHumidity(&value2); + debug_str("HTS221: [temp] "); + debug_str(printDouble(buffer1, value1)); + tmp = "0,"; + tmp += buffer1; + tmp += ","; + + if ((!temp_sent) && (frame.length() + tmp.length() < 50)) + { + temp_sent = true; + frame += tmp; + } + + debug_str("Celsius, [hum] "); + debug_str(printDouble(buffer2, value2)); + tmp = "1,"; + tmp += buffer2; + tmp += ","; + + if ((!hum_sent) && (frame.length() + tmp.length() < 50)) + { + hum_sent = true; + frame += tmp; + } + + debug_str("%\r\n"); + //pc.printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); + + temp_sensor2->GetFahrenheit(&value1); + pressure_sensor->GetPressure(&value2); + debug_str("LPS25H: [temp] "); + debug_str(printDouble(buffer1, value1)); + debug_str("Farenheit, [press] "); + debug_str(printDouble(buffer2, value2)); + tmp = "2,"; + tmp += buffer2; + tmp += ","; + + if ((!press_sent) && (frame.length() + tmp.length() < 50)) + { + press_sent = true; + frame += tmp; + } + + debug_str("mbar\r\n"); + //pc.printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); + + debug_str("---\r\n"); + + magnetometer->Get_M_Axes(axes); + debug_str("LIS3MDL [mag/mgauss]: "); + debug_str(printInt(buffer1, axes[0])); + debug_str(", "); + debug_str(printInt(buffer2, axes[1])); + debug_str(", "); + debug_str(printInt(buffer3, axes[2])); + debug_str("\r\n"); + tmp = "3,"; + tmp += buffer1; + tmp += ";"; + tmp += buffer2; + tmp += ";"; + tmp += buffer3; + tmp += ","; + + if ((!magn_sent) && (frame.length() + tmp.length() < 50)) + { + magn_sent = true; + frame += tmp; + } + + //pc.printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); + + accelerometer->Get_X_Axes(axes); + debug_str("LSM6DS0 [acc/mg]: "); + debug_str(printInt(buffer1, axes[0])); + debug_str(", "); + debug_str(printInt(buffer2, axes[1])); + debug_str(", "); + debug_str(printInt(buffer3, axes[2])); + tmp = "4,"; + tmp += buffer1; + tmp += ";"; + tmp += buffer2; + tmp += ";"; + tmp += buffer3; + tmp += ","; + + if ((!motion_sent) && (frame.length() + tmp.length() < 50)) + { + motion_sent = true; + frame += tmp; + } + + debug_str("\r\n"); + //pc.printf("LSM6DS0 [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); + + gyroscope->Get_G_Axes(axes); + debug_str("LSM6DS0 [gyro/mdps]: "); + debug_str(printInt(buffer1, axes[0])); + debug_str(", "); + debug_str(printInt(buffer2, axes[1])); + debug_str(", "); + debug_str(printInt(buffer3, axes[2])); + tmp = "5,"; + tmp += buffer1; + tmp += ";"; + tmp += buffer2; + tmp += ";"; + tmp += buffer3; + + if ((!gyro_sent) && (frame.length() + tmp.length() < 50)) + { + gyro_sent = false; + temp_sent = false; + press_sent = false; + motion_sent = false; + magn_sent = false; + + frame += tmp; + } + + debug_str("\r\n Frame: "); + debug_str(frame.c_str()); + debug_str("\r\n"); + //pc.printf("LSM6DS0 [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); + + strncpy((char*) LMIC.frame, frame.c_str(), strlen(frame.c_str())); + +#if ( LORAWAN_CONFIRMED_MSG_ON == 1 ) + LMIC.frame[strlen(frame.c_str())] = LMIC.seqnoDn >> 8; + LMIC.frame[strlen(frame.c_str())+1] = LMIC.seqnoDn; + LMIC.frame[strlen(frame.c_str())+2] = LMIC.rssi >> 8; + LMIC.frame[strlen(frame.c_str())+3] = LMIC.rssi; + LMIC.frame[strlen(frame.c_str())+4] = LMIC.snr; +#endif + debug_str("Frame to be sent: "); +// debug_buf(LMIC.frame, strlen(frame) + 5); + debug_buf(LMIC.frame, strlen(frame.c_str())); + +// LoRaWAN_data_size = strlen(frame) + 5; + LoRaWAN_data_size = strlen(frame.c_str()); +} + static void prepareTxFrame( void ) { LMIC.frame[0] = AppLedStateOn; @@ -438,7 +746,8 @@ { //prepareTxFrame( ); //prepareTxCoapFrame(); - prepareTxLoraFrame(); + prepareTxSensorsFrame(); + //prepareTxLoraFrame(); LMIC_setTxData2( LORAWAN_APP_PORT, LMIC.frame, LoRaWAN_data_size, LORAWAN_CONFIRMED_MSG_ON ); @@ -450,6 +759,25 @@ // Initialization job static void onInit( osjob_t* j ) { + debug_str("--- Starting new run ---\r\n"); + + humidity_sensor->ReadID(&id); + debug_str("HTS221 humidity & temperature = "); + debug_uint(id); + debug_str("\r\n"); + pressure_sensor->ReadID(&id); + debug_str("LPS25H pressure & temperature = "); + debug_uint(id); + debug_str("\r\n"); + magnetometer->ReadID(&id); + debug_str("LIS3MDL magnetometer = "); + debug_uint(id); + debug_str("\r\n"); + gyroscope->ReadID(&id); + debug_str("LSM6DS0 accelerometer & gyroscope = "); + debug_uint(id); + debug_str("\r\n"); + // reset MAC state LMIC_reset( ); LMIC_setAdrMode( LORAWAN_ADR_ON ); @@ -475,6 +803,9 @@ // setup initial job os_setCallback( &initjob, onInit ); // execute scheduled jobs and events + + //static Thread exec_thread(exec_call_thread); + os_runloop( ); // (not reached) } @@ -525,3 +856,5 @@ //onSendFrame( NULL ); } } + +