Mike Fiore / Mbed OS unh-hackathon-example

Dependencies:   ST_INTERFACES X_NUCLEO_53L0A1 X_NUCLEO_COMMON X_NUCLEO_IKS01A1 Senet_Packet libmDot-mbed5

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "x_nucleo_iks01a1.h"
00003 #include "x_nucleo_53l0a1.h"
00004 #include "senet_packet.h"
00005 #include "mDot.h"
00006 #include "dot_util.h"
00007 
00008 //////////////////////////////////////////////////////////////////////////
00009 // * these options must match the the Senet credentials for your device //
00010 // * edit their values to match your configuration                      //
00011 /////////////////////////////////////////////////////////////////////////
00012 // Network Id for Senet public network
00013 static uint8_t app_eui[] = { 0x00, 0x25, 0x0C, 0x00, 0x00, 0x01, 0x00, 0x01 };
00014 // Register at or Sign in to http://portal.senetco.com/ and register your NodeId to receive your ApKey
00015 // replace this default key with yours
00016 static uint8_t app_key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
00017 
00018 mDot* dot = NULL;
00019 
00020 DevI2C i2c(D14, D15);
00021 
00022 /* Instantiate the expansion board */
00023 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(&i2c);
00024 // MAKE SURE YOU JUMPER PIN D7 TO PIN D8!
00025 static X_NUCLEO_53L0A1 *range_board = X_NUCLEO_53L0A1::Instance(&i2c, A2, D7, D2);
00026 
00027 /* Retrieve the composing elements of the expansion board */
00028 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
00029 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
00030 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
00031 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
00032 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
00033 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
00034 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
00035 
00036 /* Helper function for printing floats & doubles */
00037 static char *printDouble(char* str, double v, int decimalDigits=2) {
00038     int i = 1;
00039     int intPart, fractPart;
00040     int len;
00041     char *ptr;
00042 
00043     /* prepare decimal digits multiplicator */
00044     for (;decimalDigits!=0; i*=10, decimalDigits--);
00045 
00046     /* calculate integer & fractinal parts */
00047     intPart = (int)v;
00048     fractPart = (int)((v-(double)(int)v)*i);
00049 
00050     /* fill in integer part */
00051     sprintf(str, "%i.", intPart);
00052 
00053     /* prepare fill in of fractional part */
00054     len = strlen(str);
00055     ptr = &str[len];
00056 
00057     /* fill in leading fractional zeros */
00058     for (i/=10;i>1; i/=10, ptr++) {
00059       if(fractPart >= i) break;
00060       *ptr = '0';
00061     }
00062 
00063     /* fill in (rest of) fractional part */
00064     sprintf(ptr, "%i", fractPart);
00065 
00066     return str;
00067 }
00068 
00069 void start_mems_board() {
00070     uint8_t id;
00071 
00072     humidity_sensor->read_id(&id);
00073     printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
00074 
00075     pressure_sensor->read_id(&id);
00076     printf("LPS25H  pressure & temperature    = 0x%X\r\n", id);
00077 
00078     magnetometer->read_id(&id);
00079     printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
00080 
00081     gyroscope->read_id(&id);
00082     printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
00083 
00084     wait(3);
00085 }
00086 
00087 void start_range_board() {
00088     range_board->InitBoard();
00089 
00090     wait(1);
00091 }
00092 
00093 int main() {
00094     float value1, value2;
00095     char buffer1[32], buffer2[32];
00096     int32_t axes[3];
00097     int ret;
00098     int status;
00099     uint32_t distance;    
00100     std::vector<uint8_t> tx_data;
00101     uint8_t buffer[64];
00102     float value;
00103         
00104     SensorPacket packet(buffer, sizeof(buffer));
00105 
00106     printf("\r\n--- Application starting up ---\r\n");
00107 
00108     printf("\r\n--- Initializing mems board ---\r\n");
00109     start_mems_board();
00110 
00111     printf("\r\n--- Initializing range board ---\r\n");
00112     start_range_board();
00113 
00114     printf("\r\n--- Initializing LoRa stack ---\r\n");
00115     // get a handle to the stack
00116     dot = mDot::getInstance();
00117     // reset config to start from a known state
00118     dot->resetConfig();
00119     dot->resetNetworkSession();
00120     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00121     if (dot->getJoinMode() != mDot::OTA) {
00122         logInfo("changing network join mode to OTA");
00123         if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
00124             logError("failed to set network join mode to OTA");
00125         }
00126     }
00127     update_ota_config_id_key(app_eui, app_key, 0, true, 0);
00128     // save changes to configuration
00129     logInfo("saving configuration");
00130     if (!dot->saveConfig()) {
00131         logError("failed to save configuration");
00132     }
00133 
00134     // display configuration
00135     display_config();
00136 
00137     // join network
00138     while (true) {
00139         if (dot->joinNetwork() == mDot::MDOT_OK) {
00140             break;
00141         }
00142 
00143         wait(1);
00144     }
00145 
00146     while (true) {
00147         // display sensor data on debug port
00148         ret = temp_sensor1->get_temperature(&value1);
00149         if (ret) {
00150             printf("failed to get temp C\r\n");
00151         }
00152         ret = humidity_sensor->get_humidity(&value2);
00153         if (ret) {
00154             printf("failed to get humidity\r\n");
00155         }
00156         printf("HTS221: [temp] %7s°C,   [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00157 
00158         ret = temp_sensor2->get_fahrenheit(&value1);
00159         if (ret) {
00160             printf("failed to get temp F\r\n");
00161         }
00162         ret = pressure_sensor->get_pressure(&value2);
00163         if (ret) {
00164             printf("failed to get pressure F\r\n");
00165         }
00166         printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00167 
00168         ret = magnetometer->get_m_axes(axes);
00169         if (ret) {
00170             printf("failed to get magnetometer\r\n");
00171         }
00172         printf("LIS3MDL [mag/mgauss]:  %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);
00173         
00174         ret = accelerometer->get_x_axes(axes);
00175         if (ret) {
00176             printf("failed to get accelerometer\r\n");
00177         }
00178         printf("LSM6DS0 [acc/mg]:      %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);
00179 
00180         ret = gyroscope->get_g_axes(axes);
00181         if (ret) {
00182             printf("failed to get gyroscope\r\n");
00183         }
00184         printf("LSM6DS0 [gyro/mdps]:   %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);
00185 
00186         status = range_board->sensor_centre->GetDistance(&distance);
00187         if (status == VL53L0X_ERROR_NONE) {
00188             printf("Distance : %ld\n", distance);
00189         } else {
00190             printf("failed to get distance - possibly out of range!\r\n");
00191         }
00192 
00193         // add temperature to packet - slot 0, sensor type 2
00194         packet.addSensorValue(0, 2, (int16_t)value1);
00195 
00196         // add distance to packet - slot 1, sensor type 3
00197         packet.addSensorValue(1, 3, (int16_t)distance);
00198 
00199         // add pressure to packet - slot 2, sensor type 4
00200         packet.addSensorValue(2, 4, (int16_t)value2);
00201 
00202         //serialize and send the data
00203         packet.serialize();
00204         tx_data.assign(packet.payload(), packet.payload() + packet.length());
00205 
00206         if (dot->send(tx_data) != mDot::MDOT_OK) {
00207             printf("failed to send data!\r\n");
00208         }
00209 
00210         wait(1);
00211     }
00212 }