Program test for Coragem

Dependencies:   SX1272 SPI_MX25R

Files at this revision

API Documentation at this revision

Comitter:
marcoantonioara
Date:
Wed Nov 13 16:42:06 2019 +0000
Parent:
3:cbe3f441353e
Commit message:
Test version firmware Coragem using all sensors

Changed in this revision

BME280.lib Show diff for this revision Revisions of this file
SPI_MX25R.lib Show annotated file Show diff for this revision Revisions of this file
SX1272.lib Show annotated file Show diff for this revision Revisions of this file
Si1133.cpp Show annotated file Show diff for this revision Revisions of this file
Si1133.h Show annotated file Show diff for this revision Revisions of this file
bme280.txt Show annotated file Show diff for this revision Revisions of this file
bmx160.txt Show annotated file Show diff for this revision Revisions of this file
gps.txt Show annotated file Show diff for this revision Revisions of this file
lora.txt Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
mbed_coragem.lib Show annotated file Show diff for this revision Revisions of this file
mbed_coragem_new.lib Show diff for this revision Revisions of this file
memory.txt Show annotated file Show diff for this revision Revisions of this file
--- a/BME280.lib	Tue Sep 03 21:25:05 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/users/MACRUM/code/BME280/#ddcaa259e65b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SPI_MX25R.lib	Wed Nov 13 16:42:06 2019 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/alec1/code/SPI_MX25R/#f72110475fec
--- a/SX1272.lib	Tue Sep 03 21:25:05 2019 +0000
+++ b/SX1272.lib	Wed Nov 13 16:42:06 2019 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/marcoantonioara/code/SX1272/#f77a79f4239a
+https://os.mbed.com/users/marcoantonioara/code/SX1272/#d5e647e6def9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Si1133.cpp	Wed Nov 13 16:42:06 2019 +0000
@@ -0,0 +1,926 @@
+/***************************************************************************//**
+ * @file Si1133.cpp
+ *******************************************************************************
+ * @section License
+ * <b>(C) Copyright 2017 Silicon Labs, http://www.silabs.com</b>
+ *******************************************************************************
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+#include "Si1133.h"
+
+#define SI1133_I2C_ADDRESS (0xAA) /** Hardcoded address for Si1133 sensor */
+
+/** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
+
+#define X_ORDER_MASK            0x0070
+#define Y_ORDER_MASK            0x0007
+#define SIGN_MASK               0x0080
+#define GET_X_ORDER(m)          ( ((m) & X_ORDER_MASK) >> 4)
+#define GET_Y_ORDER(m)          ( ((m) & Y_ORDER_MASK) )
+#define GET_SIGN(m)             ( ((m) & SIGN_MASK) >> 7)
+
+#define UV_INPUT_FRACTION       15
+#define UV_OUTPUT_FRACTION      12
+#define UV_NUMCOEFF             2
+
+#define ADC_THRESHOLD           16000
+#define INPUT_FRACTION_HIGH     7
+#define INPUT_FRACTION_LOW      15
+#define LUX_OUTPUT_FRACTION     12
+#define NUMCOEFF_LOW            9
+#define NUMCOEFF_HIGH           4
+
+/** @endcond */
+
+/** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
+/***************************************************************************//**
+ * @brief
+ *    Coefficients for lux calculation
+ ******************************************************************************/
+const Si1133::LuxCoeff_t Si1133::lk = {
+    {   {     0, 209 },           /**< coeff_high[0]   */
+        {  1665, 93  },           /**< coeff_high[1]   */
+        {  2064, 65  },           /**< coeff_high[2]   */
+        { -2671, 234 } },         /**< coeff_high[3]   */
+    {   {     0, 0   },           /**< coeff_low[0]    */
+        {  1921, 29053 },         /**< coeff_low[1]    */
+        { -1022, 36363 },         /**< coeff_low[2]    */
+        {  2320, 20789 },         /**< coeff_low[3]    */
+        {  -367, 57909 },         /**< coeff_low[4]    */
+        { -1774, 38240 },         /**< coeff_low[5]    */
+        {  -608, 46775 },         /**< coeff_low[6]    */
+        { -1503, 51831 },         /**< coeff_low[7]    */
+        { -1886, 58928 } }        /**< coeff_low[8]    */
+};
+
+/***************************************************************************//**
+ * @brief
+ *    Coefficients for UV index calculation
+ ******************************************************************************/
+const Si1133::Coeff_t Si1133::uk[2] = {
+    { 1281, 30902 },            /**< coeff[0]        */
+    { -638, 46301 }             /**< coeff[1]        */
+};
+
+/**************************************************************************//**
+* @name Error Codes
+* @{
+******************************************************************************/
+#define SI1133_OK                            0x0000   /**< No errors                  */
+#define SI1133_ERROR_I2C_TRANSACTION_FAILED  0x0001   /**< I2C transaction failed     */
+#define SI1133_ERROR_SLEEP_FAILED            0x0002   /**< Entering sleep mode failed */
+/**@}*/
+
+/** @endcond */
+
+Si1133::Si1133(PinName sda, PinName scl, int hz) : m_I2C(sda, scl)
+{
+    //Set the I2C bus frequency
+    m_I2C.frequency(hz);
+}
+
+Si1133::~Si1133(void)
+{
+    deinit();
+}
+
+bool Si1133::open()
+{
+    //Probe for the Si1133 using a Zero Length Transfer
+    if (m_I2C.write(SI1133_I2C_ADDRESS, NULL, 0)) {
+        //Return success
+        return false;
+    }
+
+    // initialize sensor
+    if (SI1133_OK == init()) {
+        return true;
+    }
+    return false;
+}
+
+/** Measure the current light level (in lux) on the Si1133
+ *
+ * @returns The current temperature measurement in Lux.
+ */
+float Si1133::get_light_level()
+{
+    float lux, uvi;
+    measure_lux_uv(&lux, &uvi);
+    return lux;
+}
+
+/** Measure the current UV Index on the Si1133
+ *
+ * @returns The current UV index.
+ */
+float Si1133::get_uv_index()
+{
+    float lux, uvi;
+    measure_lux_uv(&lux, &uvi);
+    return uvi;
+}
+
+bool Si1133::get_light_and_uv(float *light_level, float *uv_index)
+{
+    if(measure_lux_uv(light_level, uv_index)) {
+        return false;
+    }
+    return true;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Reads register from the Si1133 sensor
+ *
+ * @param[in] reg
+ *    The register address to read from in the sensor.
+ *
+ * @param[out] data
+ *    The data read from the sensor
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::read_register(enum Si1133::Register reg, uint8_t *data)
+{
+    char buf[1];
+    buf[0] = (char) reg;
+
+    if (m_I2C.write(SI1133_I2C_ADDRESS, buf, 1, true)) {
+        //Return failure
+        return SI1133_ERROR_I2C_TRANSACTION_FAILED;
+    }
+
+    if (m_I2C.read(SI1133_I2C_ADDRESS, buf, 1)) {
+        //Return failure
+        return SI1133_ERROR_I2C_TRANSACTION_FAILED;
+    }
+
+    *data = buf[0];
+
+    return SI1133_OK;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Writes register in the Si1133 sensor
+ *
+ * @param[in] reg
+ *    The register address to write to in the sensor
+ *
+ * @param[in] data
+ *    The data to write to the sensor
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::write_register(enum Si1133::Register reg, uint8_t data)
+{
+    char buf[2];
+    buf[0] = (char) reg;
+    buf[1] = (char) data;
+
+    if (m_I2C.write(SI1133_I2C_ADDRESS, buf, 2)) {
+        //Return failure
+        return SI1133_ERROR_I2C_TRANSACTION_FAILED;
+    }
+
+    return SI1133_OK;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Writes a block of data to the Si1133 sensor.
+ *
+ * @param[in] reg
+ *    The first register to begin writing to
+ *
+ * @param[in] length
+ *    The number of bytes to write to the sensor
+ *
+ * @param[in] data
+ *    The data to write to the sensor
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::write_register_block(enum Si1133::Register reg, uint8_t length, uint8_t *data)
+{
+    char buf[3];
+    buf[0] = (char)reg;
+
+    if (length > 2) {
+        return SI1133_ERROR_I2C_TRANSACTION_FAILED;
+    }
+
+    memcpy(&buf[1], data, length);
+
+    if (m_I2C.write(SI1133_I2C_ADDRESS, buf, length + 1)) {
+        //Return failure
+        return SI1133_ERROR_I2C_TRANSACTION_FAILED;
+    }
+
+    return SI1133_OK;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Reads a block of data from the Si1133 sensor.
+ *
+ * @param[in] reg
+ *    The first register to begin reading from
+ *
+ * @param[in] length
+ *    The number of bytes to write to the sensor
+ *
+ * @param[out] data
+ *    The data read from the sensor
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::read_register_block(enum Si1133::Register reg, uint8_t length, uint8_t *data)
+{
+    char reg_c = (char)reg;
+    if (m_I2C.write(SI1133_I2C_ADDRESS, &reg_c, 1, true)) {
+        //Return failure
+        return SI1133_ERROR_I2C_TRANSACTION_FAILED;
+    }
+
+    if (m_I2C.read(SI1133_I2C_ADDRESS, (char*) data, length)) {
+        //Return failure
+        return SI1133_ERROR_I2C_TRANSACTION_FAILED;
+    }
+
+    return SI1133_OK;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Reads the interrupt status register of the device
+ *
+ * @param[out] irqStatus
+ *    The contentof the IRQ status register
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::get_irq_status(uint8_t *irq_status)
+{
+    return read_register(REG_IRQ_STATUS, irq_status);
+}
+
+uint32_t Si1133::wake(void)
+{
+    uint8_t response;
+    read_register(REG_RESPONSE0, &response);
+    response = (response & 31) | 128;
+    write_register(REG_RESPONSE0, response);
+    read_register(REG_RESPONSE0, &response);
+    if (response & 224 == 128)
+        return 0;
+    else return 1;
+}
+
+
+/***************************************************************************//**
+ * @brief
+ *    Waits until the Si1133 is sleeping before proceeding
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::wait_until_sleep(void)
+{
+  uint32_t ret;
+  uint8_t response;
+  size_t count = 0;
+
+  /* This loops until the Si1133 is known to be in its sleep state  */
+  /* or if an i2c error occurs                                      */
+  while ( count < 5 ) {
+    ret = read_register(REG_RESPONSE0, &response);
+    if ( (response & (uint8_t)RSP0_CHIPSTAT_MASK) == (uint8_t)RSP0_SLEEP ) {
+      return SI1133_OK;
+    }
+
+    if ( ret != SI1133_OK ) {
+      return SI1133_ERROR_SLEEP_FAILED;
+    }
+
+    count++;
+  }
+
+  return SI1133_ERROR_SLEEP_FAILED;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Resets the Si1133
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::reset(void)
+{
+    uint32_t retval;
+
+    /* Do not access the Si1133 earlier than 25 ms from power-up */
+    wait_ms(30);
+
+    /* Perform the Reset Command */
+    retval = write_register(REG_COMMAND, (uint8_t)CMD_RESET);
+
+    /* Delay for 10 ms. This delay is needed to allow the Si1133   */
+    /* to perform internal reset sequence.                         */
+    wait_ms(10);
+
+    return retval;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Helper function to send a command to the Si1133
+ *
+ * @param[in] command
+ *    The command to send to the sensor
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::send_cmd(enum Si1133::Command command)
+{
+    uint8_t response;
+    uint8_t response_stored;
+    uint8_t count = 0;
+    uint32_t ret;
+
+    /* Get the response register contents */
+    ret = read_register(REG_RESPONSE0, &response_stored);
+    if ( ret != SI1133_OK ) {
+        return ret;
+    }
+
+    response_stored = response_stored & (uint8_t)RSP0_COUNTER_MASK;
+
+    /* Double-check the response register is consistent */
+    while ( count < 5 ) {
+        ret = wait_until_sleep();
+        if ( ret != SI1133_OK ) {
+            return ret;
+        }
+        /* Skip if the command is RESET COMMAND COUNTER */
+        if ( command == (uint8_t)CMD_RESET_CMD_CTR ) {
+            break;
+        }
+
+        ret = read_register(REG_RESPONSE0, &response);
+
+        if ( (response & (uint8_t)RSP0_COUNTER_MASK) == response_stored ) {
+            break;
+        } else {
+            if ( ret != SI1133_OK ) {
+                return ret;
+            } else {
+                response_stored = response & (uint8_t)RSP0_COUNTER_MASK;
+            }
+        }
+
+        count++;
+    }
+
+    /* Send the command */
+    ret = write_register(REG_COMMAND, command);
+    if ( ret != SI1133_OK ) {
+        return ret;
+    }
+
+    count = 0;
+    /* Expect a change in the response register */
+    while ( count < 5 ) {
+        /* Skip if the command is RESET COMMAND COUNTER */
+        if ( command == (uint8_t)CMD_RESET_CMD_CTR ) {
+            break;
+        }
+
+        ret = read_register(REG_RESPONSE0, &response);
+        if ( (response & (uint8_t)RSP0_COUNTER_MASK) != response_stored ) {
+            break;
+        } else {
+            if ( ret != SI1133_OK ) {
+                return ret;
+            }
+        }
+
+        count++;
+    }
+
+    return SI1133_OK;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Sends a RESET COMMAND COUNTER command to the Si1133
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::reset_cmd_counter(void)
+{
+  return send_cmd(CMD_RESET_CMD_CTR);
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Sends a FORCE command to the Si1133
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::force_measurement(void)
+{
+  return send_cmd(CMD_FORCE_CH);
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Sends a START command to the Si1133
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::start_measurement(void)
+{
+  return send_cmd(CMD_START);
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Sends a PAUSE command to the Si1133
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::pause_measurement(void)
+{
+  return send_cmd(CMD_PAUSE_CH);
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Writes a byte to an Si1133 Parameter
+ *
+ * @param[in] address
+ *    The parameter address
+ *
+ * @param[in] value
+ *    The byte value to be written to the Si1133 parameter
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ *
+ * @note
+ *    This function ensures that the Si1133 is idle and ready to
+ *    receive a command before writing the parameter. Furthermore,
+ *    command completion is checked. If setting parameter is not done
+ *    properly, no measurements will occur. This is the most common
+ *    error. It is highly recommended that host code make use of this
+ *    function.
+ ******************************************************************************/
+uint32_t Si1133::set_parameter (enum Si1133::Parameter address, uint8_t value)
+{
+    uint32_t retval;
+    uint8_t buffer[2];
+    uint8_t response_stored;
+    uint8_t response;
+    size_t count;
+
+    retval = wait_until_sleep();
+    if ( retval != SI1133_OK ) {
+        return retval;
+    }
+
+    read_register(REG_RESPONSE0, &response_stored);
+    response_stored &= (uint8_t)RSP0_COUNTER_MASK;
+
+    buffer[0] = value;
+    buffer[1] = 0x80 + ((uint8_t)address & 0x3F);
+
+    retval = write_register_block(REG_HOSTIN0, 2, (uint8_t*) buffer);
+    if ( retval != SI1133_OK ) {
+        return retval;
+    }
+
+    /* Wait for command to finish */
+    count = 0;
+    /* Expect a change in the response register */
+    while ( count < 5 ) {
+        retval = read_register(REG_RESPONSE0, &response);
+        if ( (response & (uint8_t)RSP0_COUNTER_MASK) != response_stored ) {
+            break;
+        } else {
+            if ( retval != SI1133_OK ) {
+                return retval;
+            }
+        }
+
+        count++;
+    }
+
+    if (count >= 5) {
+        return SI1133_ERROR_I2C_TRANSACTION_FAILED;
+    }
+
+    return SI1133_OK;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Reads a parameter from the Si1133
+ *
+ * @param[in] address
+ *    The address of the parameter.
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::read_parameter (enum Si1133::Parameter address)
+{
+    uint8_t retval;
+    uint8_t cmd;
+
+    cmd = 0x40 + ((uint8_t)address & 0x3F);
+
+    retval = send_cmd((enum Si1133::Command)cmd);
+    if ( retval != SI1133_OK ) {
+        return retval;
+    }
+
+    read_register(REG_RESPONSE1, &retval);
+
+    return retval;
+}
+
+/**************************************************************************//**
+ * @brief
+ *    Initializes the Si1133 chip
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ *****************************************************************************/
+uint32_t Si1133::init (void)
+{
+    uint32_t retval;
+
+    /* Allow some time for the part to power up */
+    wait_ms(5);
+
+    retval = reset();
+
+    wait_ms(10);
+
+    retval += set_parameter(PARAM_CH_LIST, 0x0f);
+    retval += set_parameter(PARAM_ADCCONFIG0, 0x78);
+    retval += set_parameter(PARAM_ADCSENS0, 0x71);
+    retval += set_parameter(PARAM_ADCPOST0, 0x40);
+    retval += set_parameter(PARAM_ADCCONFIG1, 0x4d);
+    retval += set_parameter(PARAM_ADCSENS1, 0xe1);
+    retval += set_parameter(PARAM_ADCPOST1, 0x40);
+    retval += set_parameter(PARAM_ADCCONFIG2, 0x41);
+    retval += set_parameter(PARAM_ADCSENS2, 0xe1);
+    retval += set_parameter(PARAM_ADCPOST2, 0x50);
+    retval += set_parameter(PARAM_ADCCONFIG3, 0x4d);
+    retval += set_parameter(PARAM_ADCSENS3, 0x87);
+    retval += set_parameter(PARAM_ADCPOST3, 0x40);
+
+    retval += write_register(REG_IRQ_ENABLE, 0x0f);
+
+    return retval;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Stops the measurements on all channel and waits until the chip
+ *    goes to sleep state.
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::deinit (void)
+{
+    uint32_t retval;
+
+    retval = set_parameter(PARAM_CH_LIST, 0x3f);
+    retval += pause_measurement();
+    retval += wait_until_sleep();
+
+    return retval;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Read samples from the Si1133 chip
+ *
+ * @param[out] samples
+ *    Retrieves interrupt status and measurement data for channel 0..3 and
+ *    converts the data to int32_t format
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::measure (Si1133::Samples_t *samples)
+{
+    uint8_t buffer[13];
+    uint32_t retval;
+
+    retval = read_register_block(REG_IRQ_STATUS, 13, buffer);
+
+    samples->irq_status = buffer[0];
+
+    samples->ch0 = buffer[1] << 16;
+    samples->ch0 |= buffer[2] << 8;
+    samples->ch0 |= buffer[3];
+    if ( samples->ch0 & 0x800000 ) {
+        samples->ch0 |= 0xFF000000;
+    }
+
+    samples->ch1 = buffer[4] << 16;
+    samples->ch1 |= buffer[5] << 8;
+    samples->ch1 |= buffer[6];
+    if ( samples->ch1 & 0x800000 ) {
+        samples->ch1 |= 0xFF000000;
+    }
+
+    samples->ch2 = buffer[7] << 16;
+    samples->ch2 |= buffer[8] << 8;
+    samples->ch2 |= buffer[9];
+    if ( samples->ch2 & 0x800000 ) {
+        samples->ch2 |= 0xFF000000;
+    }
+
+    samples->ch3 = buffer[10] << 16;
+    samples->ch3 |= buffer[11] << 8;
+    samples->ch3 |= buffer[12];
+    if ( samples->ch3 & 0x800000 ) {
+        samples->ch3 |= 0xFF000000;
+    }
+
+    return retval;
+}
+
+int32_t Si1133::calculate_polynomial_helper (int32_t input, int8_t fraction, uint16_t mag, int8_t shift)
+{
+    int32_t value;
+
+    if ( shift < 0 ) {
+        value = ( (input << fraction) / mag) >> -shift;
+    } else {
+        value = ( (input << fraction) / mag) << shift;
+    }
+
+    return value;
+}
+
+int32_t Si1133::calculate_polynomial (int32_t x, int32_t y, uint8_t input_fraction, uint8_t output_fraction, uint8_t num_coeff, const Si1133::Coeff_t *kp)
+{
+    uint8_t info, x_order, y_order, counter;
+    int8_t sign, shift;
+    uint16_t mag;
+    int32_t output = 0, x1, x2, y1, y2;
+
+    for ( counter = 0; counter < num_coeff; counter++ ) {
+        info = kp->info;
+        x_order = GET_X_ORDER(info);
+        y_order = GET_Y_ORDER(info);
+
+        shift = ( (uint16_t) kp->info & 0xff00) >> 8;
+        shift ^= 0x00ff;
+        shift += 1;
+        shift = -shift;
+
+        mag = kp->mag;
+
+        if ( GET_SIGN(info) ) {
+            sign = -1;
+        } else {
+            sign = 1;
+        }
+
+        if ( (x_order == 0) && (y_order == 0) ) {
+            output += sign * mag << output_fraction;
+        } else {
+            if ( x_order > 0 ) {
+                x1 = calculate_polynomial_helper(x, input_fraction, mag, shift);
+                if ( x_order > 1 ) {
+                    x2 = calculate_polynomial_helper(x, input_fraction, mag, shift);
+                } else {
+                    x2 = 1;
+                }
+            } else {
+                x1 = 1;
+                x2 = 1;
+            }
+
+            if ( y_order > 0 ) {
+                y1 = calculate_polynomial_helper(y, input_fraction, mag, shift);
+                if ( y_order > 1 ) {
+                    y2 = calculate_polynomial_helper(y, input_fraction, mag, shift);
+                } else {
+                    y2 = 1;
+                }
+            } else {
+                y1 = 1;
+                y2 = 1;
+            }
+
+            output += sign * x1 * x2 * y1 * y2;
+        }
+
+        kp++;
+    }
+
+    if ( output < 0 ) {
+        output = -output;
+    }
+
+    return output;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Compute UV index
+ *
+ * @param[in] uv
+ *    UV sensor raw data
+ *
+ * @param[in] uk
+ *    UV calculation coefficients
+ *
+ * @return
+ *    UV index scaled by UV_OUPTUT_FRACTION
+ ******************************************************************************/
+int32_t Si1133::get_uv (int32_t uv)
+{
+    int32_t uvi;
+
+    uvi = calculate_polynomial(0, uv, UV_INPUT_FRACTION, UV_OUTPUT_FRACTION, UV_NUMCOEFF, uk);
+
+    return uvi;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Compute lux value
+ *
+ * @param[in] vis_high
+ *    Visible light sensor raw data
+ *
+ * @param[in] vis_low
+ *    Visible light sensor raw data
+ *
+ * @param[in] ir
+ *    Infrared sensor raw data
+ *
+ * @param[in] lk
+ *    Lux calculation coefficients
+ *
+ * @return
+ *    Lux value scaled by LUX_OUPTUT_FRACTION
+ ******************************************************************************/
+int32_t Si1133::get_lux (int32_t vis_high, int32_t vis_low, int32_t ir)
+{
+    int32_t lux;
+
+    if ( (vis_high > ADC_THRESHOLD) || (ir > ADC_THRESHOLD) ) {
+        lux = calculate_polynomial(vis_high,
+                                   ir,
+                                   INPUT_FRACTION_HIGH,
+                                   LUX_OUTPUT_FRACTION,
+                                   NUMCOEFF_HIGH,
+                                   &(lk.coeff_high[0]) );
+    } else {
+        lux = calculate_polynomial(vis_low,
+                                   ir,
+                                   INPUT_FRACTION_LOW,
+                                   LUX_OUTPUT_FRACTION,
+                                   NUMCOEFF_LOW,
+                                   &(lk.coeff_low[0]) );
+    }
+
+    return lux;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Measure lux and UV index using the Si1133 sensor
+ *
+ * @param[out] lux
+ *    The measured ambient light illuminace in lux
+ *
+ * @param[out] uvi
+ *    UV index
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::measure_lux_uv (float *lux, float *uvi)
+{
+    Si1133::Samples_t samples;
+    uint32_t retval;
+    uint8_t response;
+
+    /* Force measurement */
+    retval = force_measurement();
+
+    /* Go to sleep while the sensor does the conversion */
+    wait_ms(200);
+
+    /* Check if the measurement finished, if not then wait */
+    retval += read_register(REG_IRQ_STATUS, &response);
+    while ( response != 0x0F ) {
+        wait_ms(5);
+        retval += read_register(REG_IRQ_STATUS, &response);
+    }
+
+    /* Get the results */
+    measure(&samples);
+
+    /* Convert the readings to lux */
+    *lux = (float) get_lux(samples.ch1, samples.ch3, samples.ch2);
+    *lux = *lux / (1 << LUX_OUTPUT_FRACTION);
+
+    /* Convert the readings to UV index */
+    *uvi = (float) get_uv(samples.ch0);
+    *uvi = *uvi / (1 << UV_OUTPUT_FRACTION);
+
+    return retval;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Reads Hardware ID from the SI1133 sensor
+ *
+ * @param[out] hardwareID
+ *    The Hardware ID of the chip (should be 0x33)
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::get_hardware_id (uint8_t *hardware_id)
+{
+    uint32_t retval;
+
+    retval = read_register(REG_PART_ID, hardware_id);
+
+    return retval;
+}
+
+/***************************************************************************//**
+ * @brief
+ *    Retrieve the sample values from the chip and convert them
+ *    to lux and UV index values
+ *
+ * @param[out] lux
+ *    The measured ambient light illuminace in lux
+ *
+ * @param[out] uvi
+ *    UV index
+ *
+ * @return
+ *    Returns zero on OK, non-zero otherwise
+ ******************************************************************************/
+uint32_t Si1133::get_measurement (float *lux, float *uvi)
+{
+    Si1133::Samples_t samples;
+    uint32_t retval;
+
+    /* Get the results */
+    retval = measure(&samples);
+
+    /* Convert the readings to lux */
+    *lux = (float) get_lux(samples.ch1, samples.ch3, samples.ch2);
+    *lux = *lux / (1 << LUX_OUTPUT_FRACTION);
+
+    /* Convert the readings to UV index */
+    *uvi = (float) get_uv(samples.ch0);
+    *uvi = *uvi / (1 << UV_OUTPUT_FRACTION);
+
+    return retval;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Si1133.h	Wed Nov 13 16:42:06 2019 +0000
@@ -0,0 +1,300 @@
+/***************************************************************************//**
+ * @file Si1133.h
+ *******************************************************************************
+ * @section License
+ * <b>(C) Copyright 2017 Silicon Labs, http://www.silabs.com</b>
+ *******************************************************************************
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************************/
+
+#ifndef SI1133_H
+#define SI1133_H
+
+#include "mbed.h"
+
+/** Si1133 class.
+ *  Used for taking Light level and UV index measurements.
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "Si1133.h"
+ *
+ * //Create an Si1133 object
+ * Si1133 sensor(PC4, PC5);
+ *
+ * int main()
+ * {
+ *     //Try to open the Si1133
+ *     if (sensor.open()) {
+ *         printf("Device detected!\n");
+ *
+ *         while (1) {
+ *             //Print the current light level
+ *             printf("Lux = %.3f\n", (float)sensor.get_light_level());
+ *             //Print the current UV index
+ *             printf("UV index = %.3f\n", (float)sensor.get_uv_index());
+ *
+ *             //Sleep for 0.5 seconds
+ *             wait(0.5);
+ *         }
+ *     } else {
+ *         error("Device not detected!\n");
+ *     }
+ * }
+ * @endcode
+ */
+class Si1133
+{
+public:
+
+    /** Create an Si1133 object connected to the specified I2C pins with the specified I2C slave address
+     *
+     * @param sda The I2C data pin.
+     * @param scl The I2C clock pin.
+     * @param hz The I2C bus frequency (defaults to 400kHz).
+     */
+    Si1133(PinName sda, PinName scl, int hz = 400000);
+
+    /**
+    * Si1133 destructor
+    */
+    ~Si1133(void);
+
+    /** Probe for the Si1133 and try to initialize the sensor
+     *
+     * @returns
+     *   'true' if the device exists on the bus,
+     *   'false' if the device doesn't exist on the bus.
+     */
+    bool open();
+
+    /** Measure the current light level (in lux) on the Si1133
+     *
+     * @returns The current temperature measurement in Lux.
+     */
+    float get_light_level();
+
+    /** Measure the current UV Index on the Si1133
+     *
+     * @returns The current UV index measurement.
+     */
+    float get_uv_index();
+
+    /** Do a combined measurement and return both the light level and UV index
+     *
+     * @param[out] light_level Measured light level in Lux
+     * @param[out] uv_index Measured UV index
+     *
+     * @returns true if measurement was successful
+     */
+    bool get_light_and_uv(float *light_level, float *uv_index);
+    
+    uint32_t wait_until_sleep(void);
+    uint32_t wake(void);
+
+#ifdef MBED_OPERATORS
+    /** A shorthand for get_light_level()
+     *
+     * @returns The current temperature measurement in Lux.
+     */
+    operator float();
+#endif
+
+private:
+    /**
+    * @name I2C Registers
+    * @{
+    */
+    enum Register {
+        REG_PART_ID     = 0x00,     /**< Part ID                                                            */
+        REG_HW_ID       = 0x01,     /**< Hardware ID                                                        */
+        REG_REV_ID      = 0x02,     /**< Hardware revision                                                  */
+        REG_HOSTIN0     = 0x0A,     /**< Data for parameter table on PARAM_SET write to COMMAND register    */
+        REG_COMMAND     = 0x0B,     /**< Initiated action in Sensor when specific codes written here        */
+        REG_IRQ_ENABLE  = 0x0F,     /**< Interrupt enable                                                   */
+        REG_RESPONSE1   = 0x10,     /**< Contains the readback value from a query or a set command          */
+        REG_RESPONSE0   = 0x11,     /**< Chip state and error status                                        */
+        REG_IRQ_STATUS  = 0x12,     /**< Interrupt status                                                   */
+        REG_HOSTOUT0    = 0x13,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT1    = 0x14,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT2    = 0x15,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT3    = 0x16,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT4    = 0x17,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT5    = 0x18,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT6    = 0x19,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT7    = 0x1A,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT8    = 0x1B,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT9    = 0x1C,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT10   = 0x1D,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT11   = 0x1E,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT12   = 0x1F,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT13   = 0x20,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT14   = 0x21,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT15   = 0x22,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT16   = 0x23,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT17   = 0x24,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT18   = 0x25,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT19   = 0x26,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT20   = 0x27,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT21   = 0x28,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT22   = 0x29,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT23   = 0x2A,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT24   = 0x2B,     /**< Captured Sensor Data                                               */
+        REG_HOSTOUT25   = 0x2C,     /**< Captured Sensor Data                                               */
+    };
+    /**@}*/
+
+    /**
+    * @name Parameters
+    * @{
+    */
+    enum Parameter {
+        PARAM_I2C_ADDR      = 0x00, /**< I2C address                                                        */
+        PARAM_CH_LIST       = 0x01, /**< Channel list                                                       */
+        PARAM_ADCCONFIG0    = 0x02, /**< ADC config for Channel 0                                           */
+        PARAM_ADCSENS0      = 0x03, /**< ADC sensitivity setting for Channel 0                              */
+        PARAM_ADCPOST0      = 0x04, /**< ADC resolution, shift and threshold settings for Channel 0         */
+        PARAM_MEASCONFIG0   = 0x05, /**< ADC measurement counter selection for Channel 0                    */
+        PARAM_ADCCONFIG1    = 0x06, /**< ADC config for Channel 1                                           */
+        PARAM_ADCSENS1      = 0x07, /**< ADC sensitivity setting for Channel 1                              */
+        PARAM_ADCPOST1      = 0x08, /**< ADC resolution, shift and threshold settings for Channel 1         */
+        PARAM_MEASCONFIG1   = 0x09, /**< ADC measurement counter selection for Channel 1                    */
+        PARAM_ADCCONFIG2    = 0x0A, /**< ADC config for Channel 2                                           */
+        PARAM_ADCSENS2      = 0x0B, /**< ADC sensitivity setting for Channel 2                              */
+        PARAM_ADCPOST2      = 0x0C, /**< ADC resolution, shift and threshold settings for Channel 2         */
+        PARAM_MEASCONFIG2   = 0x0D, /**< ADC measurement counter selection for Channel 2                    */
+        PARAM_ADCCONFIG3    = 0x0E, /**< ADC config for Channel 3                                           */
+        PARAM_ADCSENS3      = 0x0F, /**< ADC sensitivity setting for Channel 3                              */
+        PARAM_ADCPOST3      = 0x10, /**< ADC resolution, shift and threshold settings for Channel 3         */
+        PARAM_MEASCONFIG3   = 0x11, /**< ADC measurement counter selection for Channel 3                    */
+        PARAM_ADCCONFIG4    = 0x12, /**< ADC config for Channel 4                                           */
+        PARAM_ADCSENS4      = 0x13, /**< ADC sensitivity setting for Channel 4                              */
+        PARAM_ADCPOST4      = 0x14, /**< ADC resolution, shift and threshold settings for Channel 4         */
+        PARAM_MEASCONFIG4   = 0x15, /**< ADC measurement counter selection for Channel 4                    */
+        PARAM_ADCCONFIG5    = 0x16, /**< ADC config for Channel 5                                           */
+        PARAM_ADCSENS5      = 0x17, /**< ADC sensitivity setting for Channel 5                              */
+        PARAM_ADCPOST5      = 0x18, /**< ADC resolution, shift and threshold settings for Channel 5         */
+        PARAM_MEASCONFIG5   = 0x19, /**< ADC measurement counter selection for Channel 5                    */
+        PARAM_MEASRATE_H    = 0x1A, /**< Main measurement rate counter MSB                                  */
+        PARAM_MEASRATE_L    = 0x1B, /**< Main measurement rate counter LSB                                  */
+        PARAM_MEASCOUNT0    = 0x1C, /**< Measurement rate extension counter 0                               */
+        PARAM_MEASCOUNT1    = 0x1D, /**< Measurement rate extension counter 1                               */
+        PARAM_MEASCOUNT2    = 0x1E, /**< Measurement rate extension counter 2                               */
+        PARAM_THRESHOLD0_H  = 0x25, /**< Threshold level 0 MSB                                              */
+        PARAM_THRESHOLD0_L  = 0x26, /**< Threshold level 0 LSB                                              */
+        PARAM_THRESHOLD1_H  = 0x27, /**< Threshold level 1 MSB                                              */
+        PARAM_THRESHOLD1_L  = 0x28, /**< Threshold level 1 LSB                                              */
+        PARAM_THRESHOLD2_H  = 0x29, /**< Threshold level 2 MSB                                              */
+        PARAM_THRESHOLD2_L  = 0x2A, /**< Threshold level 2 LSB                                              */
+        PARAM_BURST         = 0x2B, /**< Burst enable and burst count                                       */
+    };
+    /**@}*/
+
+    /**
+    * @name Commands
+    * @{
+    */
+    enum Command {
+        CMD_RESET_CMD_CTR   = 0x00, /**< Resets the command counter                                         */
+        CMD_RESET           = 0x01, /**< Forces a Reset                                                     */
+        CMD_NEW_ADDR        = 0x02, /**< Stores the new I2C address                                         */
+        CMD_FORCE_CH        = 0x11, /**< Initiates a set of measurements specified in CHAN_LIST parameter   */
+        CMD_PAUSE_CH        = 0x12, /**< Pauses autonomous measurements                                     */
+        CMD_START           = 0x13, /**< Starts autonomous measurements                                     */
+        CMD_PARAM_SET       = 0x80, /**< Sets a parameter                                                   */
+        CMD_PARAM_QUERY     = 0x40, /**< Reads a parameter                                                  */
+    };
+    /**@}*/
+
+    /**
+    * @name Responses
+    * @{
+    */
+    enum Response {
+        RSP0_CHIPSTAT_MASK  = 0xE0, /**< Chip state mask in Response0 register                              */
+        RSP0_COUNTER_MASK   = 0x1F, /**< Command counter and error indicator mask in Response0 register     */
+        RSP0_SLEEP          = 0x20, /**< Sleep state indicator bit mask in Response0 register               */
+    };
+    /**@}*/
+
+    /**
+     * @brief
+     *    Structure to store the data measured by the Si1133
+     */
+    typedef struct {
+        uint8_t     irq_status;     /**< Interrupt status of the device    */
+        int32_t     ch0;            /**< Channel 0 measurement data        */
+        int32_t     ch1;            /**< Channel 1 measurement data        */
+        int32_t     ch2;            /**< Channel 2 measurement data        */
+        int32_t     ch3;            /**< Channel 3 measurement data        */
+    } Samples_t;
+
+    /**
+     * @brief
+     *    Structure to store the calculation coefficients
+     */
+    typedef struct {
+        int16_t     info;           /**< Info                              */
+        uint16_t    mag;            /**< Magnitude                         */
+    } Coeff_t;
+
+    /**
+     * @brief
+     *    Structure to store the coefficients used for Lux calculation
+     */
+    typedef struct {
+        Coeff_t   coeff_high[4];   /**< High amplitude coeffs */
+        Coeff_t   coeff_low[9];    /**< Low amplitude coeffs  */
+    } LuxCoeff_t;
+
+    /* Forward-declare constant coefficient table */
+    static const LuxCoeff_t lk;
+    static const Coeff_t uk[];
+
+    /* Private functions */
+    uint32_t read_register(enum Register reg, uint8_t *data);
+    uint32_t write_register(enum Register reg, uint8_t data);
+    uint32_t write_register_block(enum Register reg, uint8_t length, uint8_t *data);
+    uint32_t read_register_block(enum Register reg, uint8_t length, uint8_t *data);
+    uint32_t get_irq_status(uint8_t *irq_status);
+    
+    uint32_t reset(void);
+    uint32_t reset_cmd_counter (void);
+    uint32_t send_cmd(enum Command command);
+    uint32_t force_measurement (void);
+    uint32_t pause_measurement (void);
+    uint32_t start_measurement (void);
+    uint32_t set_parameter (enum Parameter address, uint8_t value);
+    uint32_t read_parameter (enum Parameter address);
+    uint32_t init (void);
+    uint32_t deinit (void);
+    uint32_t measure (Samples_t *samples);
+    int32_t  get_uv (int32_t uv);
+    int32_t  get_lux (int32_t vis_high, int32_t vis_low, int32_t ir);
+    uint32_t measure_lux_uv (float *lux, float *uvi);
+    uint32_t get_measurement (float *lux, float *uvi);
+    uint32_t get_hardware_id (uint8_t *hardware_id);
+
+    int32_t calculate_polynomial_helper (int32_t input, int8_t fraction, uint16_t mag, int8_t  shift);
+    int32_t calculate_polynomial (int32_t x, int32_t y, uint8_t input_fraction, uint8_t output_fraction, uint8_t num_coeff, const Coeff_t *kp);
+
+    /* Member variables */
+    I2C m_I2C;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bme280.txt	Wed Nov 13 16:42:06 2019 +0000
@@ -0,0 +1,198 @@
+//#include "mbed.h"
+
+DigitalOut BME_ADDR(P1_1);
+I2C bme(p13, p15);
+const int BME280_ADDR = (0x76 << 1);
+
+///////////////////////////////////////
+// Globals variables
+///////////////////////////////////////
+
+
+//______________BME_280________________
+char cmd_bme[18];
+uint32_t hum_raw;
+float humf;
+uint32_t temp_raw;
+float tempf;
+uint32_t press_raw;
+float pressf;
+//char cmd[4];
+uint16_t dig_T1,dig_P1,dig_H1, dig_H3;
+int16_t dig_T2, dig_T3,dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9,dig_H2, dig_H4, dig_H5, dig_H6;
+int32_t t_fine;
+
+
+void bme_init() { //bme initialization
+
+
+    bme.frequency(1000000);
+
+    cmd_bme[0] = 0xf2; // ctrl_hum
+    cmd_bme[1] = 0x01;
+    bme.write(BME280_ADDR, cmd_bme, 2);
+
+    cmd_bme[0] = 0xf4; // ctrl_meas
+    cmd_bme[1] = 0x27;
+    bme.write(BME280_ADDR, cmd_bme, 2);
+
+    cmd_bme[0] = 0xf5; // config
+    cmd_bme[1] = 0xa0;
+    bme.write(BME280_ADDR, cmd_bme, 2);
+
+    cmd_bme[0] = 0x88; // read dig_T regs
+    bme.write(BME280_ADDR, cmd_bme, 1);
+    bme.read(BME280_ADDR, cmd_bme, 6);
+
+    dig_T1 = (cmd_bme[1] << 8) | cmd_bme[0];
+    dig_T2 = (cmd_bme[3] << 8) | cmd_bme[2];
+    dig_T3 = (cmd_bme[5] << 8) | cmd_bme[4];
+
+//    DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
+
+    cmd_bme[0] = 0x8E; // read dig_P regs
+    bme.write(BME280_ADDR, cmd_bme, 1);
+    bme.read(BME280_ADDR, cmd_bme, 18);
+
+    dig_P1 = (cmd_bme[ 1] << 8) | cmd_bme[ 0];
+    dig_P2 = (cmd_bme[ 3] << 8) | cmd_bme[ 2];
+    dig_P3 = (cmd_bme[ 5] << 8) | cmd_bme[ 4];
+    dig_P4 = (cmd_bme[ 7] << 8) | cmd_bme[ 6];
+    dig_P5 = (cmd_bme[ 9] << 8) | cmd_bme[ 8];
+    dig_P6 = (cmd_bme[11] << 8) | cmd_bme[10];
+    dig_P7 = (cmd_bme[13] << 8) | cmd_bme[12];
+    dig_P8 = (cmd_bme[15] << 8) | cmd_bme[14];
+    dig_P9 = (cmd_bme[17] << 8) | cmd_bme[16];
+
+//    DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9);
+
+    cmd_bme[0] = 0xA1; // read dig_H regs
+    bme.write(BME280_ADDR, cmd_bme, 1);
+    bme.read(BME280_ADDR, cmd_bme, 9);
+
+    dig_H1 = cmd_bme[0];
+    dig_H2 = (cmd_bme[2] << 8) | cmd_bme[1];
+    dig_H3 = cmd_bme[3];
+    dig_H4 = (cmd_bme[4] << 4) | (cmd_bme[5] & 0x0f);
+    dig_H5 = (cmd_bme[7] << 4) | ((cmd_bme[6]>>4) & 0x0f);
+    dig_H6 = cmd_bme[8];
+
+//    DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6);
+}
+void bme_sleep (){
+    
+    cmd_bme[0] = 0xf4; // ctrl_meas
+    cmd_bme[1] = 0x24;
+    bme.write(BME280_ADDR, cmd_bme, 2);
+    
+    }
+    
+void bme_wake(){
+    
+    cmd_bme[0] = 0xf4; // ctrl_meas
+    cmd_bme[1] = 0x27;
+    bme.write(BME280_ADDR, cmd_bme, 2);
+    
+    }
+
+float getTemperature(){
+
+    
+    cmd_bme[0] = 0xfa; // temp_msb
+    bme.write(BME280_ADDR, cmd_bme, 1);
+    bme.read(BME280_ADDR, &cmd_bme[1], 1);
+    
+    cmd_bme[0] = 0xfb; // temp_lsb
+    bme.write(BME280_ADDR, cmd_bme, 1);
+    bme.read(BME280_ADDR, &cmd_bme[2], 1);
+    
+    cmd_bme[0] = 0xfc; // temp_xlsb
+    bme.write(BME280_ADDR, cmd_bme, 1);
+    bme.read(BME280_ADDR, &cmd_bme[3], 1);
+    
+    temp_raw = (cmd_bme[1] << 12) | (cmd_bme[2] << 4) | (cmd_bme[3] >> 4);
+    
+    int32_t temp;
+    
+    temp =
+        (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
+        ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
+    
+    t_fine = temp;
+    temp = (temp * 5 + 128) >> 8;
+    tempf = (float)temp;
+    
+    return (tempf/100.0f);
+}
+
+float getPressure(){
+
+    cmd_bme[0] = 0xf7; // press_msb
+    bme.write(BME280_ADDR, cmd_bme, 1);
+    bme.read(BME280_ADDR, &cmd_bme[1], 1);
+
+    cmd_bme[0] = 0xf8; // press_lsb
+    bme.write(BME280_ADDR, cmd_bme, 1);
+    bme.read(BME280_ADDR, &cmd_bme[2], 1);
+
+    cmd_bme[0] = 0xf9; // press_xlsb
+    bme.write(BME280_ADDR, cmd_bme, 1);
+    bme.read(BME280_ADDR, &cmd_bme[3], 1);
+
+    press_raw = (cmd_bme[1] << 12) | (cmd_bme[2] << 4) | (cmd_bme[3] >> 4);
+
+    int32_t var1, var2;
+    uint32_t press;
+
+    var1 = (t_fine >> 1) - 64000;
+    var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
+    var2 = var2 + ((var1 * dig_P5) << 1);
+    var2 = (var2 >> 2) + (dig_P4 << 16);
+    var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
+    var1 = ((32768 + var1) * dig_P1) >> 15;
+    if (var1 == 0) {
+        return 0;
+    }
+    press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
+    if(press < 0x80000000) {
+        press = (press << 1) / var1;
+    }
+    else {
+        press = (press / var1) * 2;
+    }
+    var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
+    var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
+    press = (press + ((var1 + var2 + dig_P7) >> 4));
+
+    pressf = (float)press;
+    return (pressf/100.0f);
+}
+
+float getHumidity(){
+
+
+    cmd_bme[0] = 0xfd; // hum_msb
+    bme.write(BME280_ADDR, cmd_bme, 1);
+    bme.read(BME280_ADDR, &cmd_bme[1], 1);
+
+    cmd_bme[0] = 0xfd; // hum_lsb
+    bme.write(BME280_ADDR, cmd_bme, 1);
+    bme.read(BME280_ADDR, &cmd_bme[2], 1);
+
+    hum_raw = (cmd_bme[1] << 8) | cmd_bme[2];
+
+    int32_t v_x1;
+
+    v_x1 = t_fine - 76800;
+    v_x1 =  (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
+               ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
+                                            (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
+                                            (int32_t)dig_H2 + 8192) >> 14));
+    v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
+    v_x1 = (v_x1 < 0 ? 0 : v_x1);
+    v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
+
+    humf = (float)(v_x1 >> 12);
+
+    return (humf/1024.0f);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bmx160.txt	Wed Nov 13 16:42:06 2019 +0000
@@ -0,0 +1,138 @@
+
+
+/* defines the axis for acc */
+#define ACC_NOOF_AXIS       3
+#define GYR_NOOF_AXIS       2
+ 
+/* bmi160 slave address */
+//DigitalOut BMX_ADDR(P1_10);
+#define BMI160_ADDR         ((0x69)<<1)
+ 
+#define RAD_DEG           57.29577951
+ 
+//Serial pc(USBTX, USBRX); // tx, rx
+ 
+I2C bmx(p13, p15);
+
+int16_t acc_sample_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555};
+int16_t gyr_sample_buffer[GYR_NOOF_AXIS] = {0x5555, 0x5555};
+ 
+double acc_result_buffer[ACC_NOOF_AXIS] = {0x5555, 0x5555, 0x5555};
+double gyr_result_buffer[GYR_NOOF_AXIS] = {0x5555, 0x5555};
+ 
+double accel_ang_x, accel_ang_y;
+double tiltx, tilty;
+double tiltx_prev, tilty_prev;
+ 
+char i2c_reg_buffer[2] = {0};
+
+void bmx_config(){
+    
+    bmx.frequency(20000);
+    
+    /*Reset BMX160*/
+    i2c_reg_buffer[0] = 0x7E;
+    i2c_reg_buffer[1] = 0xB6;    
+    bmx.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    wait_ms(200);
+    printf("BMX160 Resetado\n\r");
+    
+    /*Habilita o Acelerometro*/
+    i2c_reg_buffer[0] = 0x7E;
+    i2c_reg_buffer[1] = 0x11; //PMU Normal   
+    bmx.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    printf("Acc Habilitado\n\r");
+    
+    /*Habilita o Giroscopio*/
+    i2c_reg_buffer[0] = 0x7E;
+    i2c_reg_buffer[1] = 0x15;  //PMU Normal 
+    bmx.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    printf("Gyr Habilitado\n\r");
+    
+    /*Config o Data Rate ACC em 1600Hz*/
+    i2c_reg_buffer[0] = 0x40;
+    i2c_reg_buffer[1] = 0x2C;    
+    bmx.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    printf("Data Rate ACC Selecionado a 1600Hz\n\r");
+    
+    /*Config o Data Rate GYR em 1600Hz*/
+    i2c_reg_buffer[0] = 0x42;
+    i2c_reg_buffer[1] = 0x2C;    
+    bmx.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    printf("Data Rate GYR Selecionado a 1600Hz\n\r");
+    
+    /*Config o Range GYR em 250º/s*/
+    i2c_reg_buffer[0] = 0x43;
+    i2c_reg_buffer[1] = 0x03;    
+    bmx.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    printf("Range GYR Selecionado a 250deg/s\n\r");
+    
+}
+void bmx_read(){
+
+        i2c_reg_buffer[0] = 0x12;
+        bmx.write(BMI160_ADDR, i2c_reg_buffer, 1, true);
+        bmx.read(BMI160_ADDR, (char *)&acc_sample_buffer, sizeof(acc_sample_buffer), false);
+        
+        /*Le os Registradores do Giroscopio*/
+        i2c_reg_buffer[0] = 0x0C;
+        bmx.write(BMI160_ADDR, i2c_reg_buffer, 1, true);
+        bmx.read(BMI160_ADDR, (char *)&gyr_sample_buffer, sizeof(gyr_sample_buffer), false);
+        
+        /*Ajusta dados brutos Acelerometro em unidades de g */
+        acc_result_buffer[0] = (acc_sample_buffer[0]/16384.0);
+        acc_result_buffer[1] = (acc_sample_buffer[1]/16384.0);
+        acc_result_buffer[2] = (acc_sample_buffer[2]/16384.0);
+        
+        /*Ajusta dados Brutos do Giroscopio em unidades de deg/s */
+        gyr_result_buffer[0] = (gyr_sample_buffer[0]/131.2);
+        gyr_result_buffer[1] = (gyr_sample_buffer[1]/131.2);
+                
+        /*Calcula os Angulos de Inclinacao com valor do Acelerometro*/
+        accel_ang_x=atan(acc_result_buffer[0]/sqrt(pow(acc_result_buffer[1],2) + pow(acc_result_buffer[2],2)))*RAD_DEG;
+        accel_ang_y=atan(acc_result_buffer[1]/sqrt(pow(acc_result_buffer[0],2) + pow(acc_result_buffer[2],2)))*RAD_DEG;
+        
+        /*Calcula os Angulos de Rotacao com valor do Giroscopio e aplica filtro complementar realizando a fusao*/
+        tiltx = (0.98*(tiltx_prev+(gyr_result_buffer[0]*0.001)))+(0.02*(accel_ang_x));
+        tilty = (0.98*(tilty_prev+(gyr_result_buffer[1]*0.001)))+(0.02*(accel_ang_y));
+        
+        tiltx_prev = tiltx;
+        tilty_prev = tilty;                                 
+        
+        /*Imprime os dados ACC pre-formatados*/
+        printf("giro = %.3f,%.3f\n\r",tiltx, tilty);
+        
+}
+
+void bmx_sleep(){
+    
+    i2c_reg_buffer[0] = 0x7E;
+    i2c_reg_buffer[1] = 0x19;    
+    bmx.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    
+    wait_ms (1);
+    
+    i2c_reg_buffer[0] = 0x4C;
+    i2c_reg_buffer[1] = 0x80; 
+    bmx.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    
+    i2c_reg_buffer[0] = 0x4F;
+    i2c_reg_buffer[1] = 0x00;
+    bmx.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    
+    i2c_reg_buffer[0] = 0x4E;
+    i2c_reg_buffer[1] = 0x4B;
+    bmx.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    
+    i2c_reg_buffer[0] = 0x7;
+    i2c_reg_buffer[1] = 0x18;
+    bmx.write(BMI160_ADDR, i2c_reg_buffer, sizeof(i2c_reg_buffer), false);
+    
+    
+    }
+    
+void bmx_wake(){
+    
+    bmx_config();
+        
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gps.txt	Wed Nov 13 16:42:06 2019 +0000
@@ -0,0 +1,659 @@
+
+#define  SYNC1       0xB5
+#define  SYNC2       0x62
+
+//GPS DEclaration
+SPI spi_2(P0_5, P0_7, P0_11); // mosi, miso, sclk
+DigitalOut cs(P0_27);
+DigitalOut gps_reset(P1_2);
+
+
+
+
+
+
+typedef struct
+{
+    uint8_t cls;
+    uint8_t id;
+    uint16_t len;          //Length of the payload. Does not include cls, id, or checksum bytes
+    uint8_t *payload;
+    uint8_t checksumA; //Given to us from module. Checked against the rolling calculated A/B checksums.
+    uint8_t checksumB;
+} gps_ubxPacket;
+
+typedef struct
+{
+    uint32_t iTOW=0;
+    uint16_t year=0;
+    uint8_t month=0;
+    uint8_t day=0;
+    uint8_t hour=0;
+    uint8_t min=0;
+    uint8_t sec=0;
+    int8_t valid=0;
+    uint32_t tAcc=0;
+    int32_t nano=0;
+    uint8_t fixtype=0;
+    int8_t flags=0;
+    int8_t flags2=0;
+    uint8_t numSV=0;
+    int32_t lon=0;
+    int32_t lat=0;
+    int32_t height=0;
+    int32_t hMSL=0;
+    uint32_t hAcc=0;
+    uint32_t vAcc=0;
+    int32_t velN=0;
+    int32_t velE=0;
+    int32_t velD=0;
+    int32_t gSpeed=0;
+    int32_t headMot=0;
+    uint32_t sAcc=0;
+    uint32_t headAcc=0;
+    uint16_t pDOP=0;
+    int32_t headVeh;
+    int16_t magDec;
+    uint16_t magAcc;
+
+    
+} gps_navPVT;
+
+
+void gps_leBootMsg(){
+    #define MAXIMUM_PACKET_SIZE 60
+    int packet_size;
+    uint8_t packet[MAXIMUM_PACKET_SIZE];
+    uint8_t value;
+    int cont;
+    value = spi_2.write(0x00);
+    while (value  != '$' ){ //wait start boot msg
+        value = spi_2.write(0x00);
+        wait_ms(5);
+        cont++;
+        if (cont > 100)
+            return;
+    }
+    packet[0] = value;
+        value = spi_2.write(0x00);
+        for (packet_size = 1 ; packet_size <= MAXIMUM_PACKET_SIZE ;packet_size++) {
+            if (value  != '\n' ){
+                packet [packet_size]= value;
+                value = spi_2.write(0x00);
+            }
+            else {
+              lora_send_packet (packet , (uint8_t) packet_size+1); // manda atraves do lora a mensagem de boot do gps
+              return;
+            }             
+        }
+
+}
+
+void gps_le_envia_linha(){
+    
+    uint8_t packet[150];
+    uint8_t value;
+
+    for (int i=0;i < 149; i++){
+        if (value == '\n'){
+            lora_send_packet (packet , (uint8_t) i);
+            return;
+        }
+        else packet [i] =value;
+    }
+    lora_send_packet (packet , (uint8_t) 99);
+    
+}
+                               
+
+gps_ubxPacket gps_calcula_check(gps_ubxPacket Packet) {
+ 
+    uint8_t Buffer[Packet.len + 4];
+    uint8_t CK_A=0;
+    uint8_t CK_B=0;
+
+    Buffer[0]= Packet.cls;
+    Buffer[1]= Packet.id;
+    Buffer[2]= Packet.len & 0xFF;
+    Buffer[3]= (Packet.len >> 8)& 0xFF;;
+
+    //send_packet (Buffer, (uint8_t) 4 );
+        
+    for (uint16_t i = 0; i < Packet.len; i++) {
+        Buffer [i+4] = Packet.payload[i] ;
+    }
+    
+    //send_packet (Buffer, (uint8_t) Packet.len + 4 );
+
+    for(int i=0 ; i < Packet.len + 4 ; i++) {
+        CK_A = CK_A + Buffer[i];
+        CK_B = CK_B + CK_A;
+        CK_A = CK_A & 0xFF;
+        CK_B = CK_B & 0xFF;
+        //uint8_t packet_check[5] ={Buffer[i],(uint8_t) (i+1),CK_A,CK_B,'#'};
+//        send_packet (packet_check , (uint8_t) 5);
+    }
+    
+    
+//    uint8_t packet_check[2] ={CK_A, CK_B};
+//    send_packet (packet_check , (uint8_t) 2);
+
+    Packet.checksumA = CK_A;
+    Packet.checksumB = CK_B;
+    
+    return Packet;
+    
+}
+
+void send_gps_packet(gps_ubxPacket packet){
+    
+    spi_2.write(SYNC1);
+    spi_2.write(SYNC2);
+    spi_2.write(packet.cls);
+    spi_2.write(packet.id);
+    spi_2.write(packet.len & 0xFF);
+    spi_2.write((packet.len >> 8)& 0xFF);
+    
+    for (uint16_t i = 0; i < packet.len; i++) {
+        spi_2.write(packet.payload[i]);
+    }
+    spi_2.write(packet.checksumA);
+    spi_2.write(packet.checksumB);
+    
+    //=============imprime resposta
+    wait_ms(50);
+    gps_le_envia_linha();
+    gps_le_envia_linha();
+
+}
+
+gps_navPVT le_nav_pvt () {
+    
+    gps_navPVT Pac;
+    
+    char state = 0;
+    int cont =0;
+    
+    while (1){ //começou mensagem
+        if (cont > 250) return Pac;
+        if (state == 0 ){
+            if (spi_2.write(0x00) == 0xb5)
+                state =1;
+            else {
+                cont++;
+                wait_ms(40);
+            }
+                    
+        }
+        else if (state == 1){ // read 0xb5  
+            if (spi_2.write(0x00) == 0x62){
+                state =2;
+                //wait_ms(30);
+            }
+            else state =0;
+        }
+        else if (state == 2) {// read 0xb5 0x62
+            if (spi_2.write(0x00) == 0x01){
+                state =3;
+                //wait_ms(30);
+            }
+            else state =0;
+        }
+        else if (state == 3) {// read 0xb5 0x62 0x01
+            if (spi_2.write(0x00) == 0x07){
+                state =4;
+                //wait_ms(30);
+            }
+            else state =0;
+        }
+        else if (state == 4) {// read 0xb5 0x62 0x01 0x07
+            if (spi_2.write(0x00) == 0x5c){
+                state =5;
+                //wait_ms(30);
+            }
+            else state =0;
+        }
+        else if (state == 5) {// read 0xb5 0x62 0x01 0x07 0x5c
+            if (spi_2.write(0x00) == 0x00){
+                state =6;
+                //wait_ms(25);
+            }
+            else state =0;
+        }
+        else if (state == 6){ // read 0xb5 0x62 0x01 0x07 0x92 0x00
+            uint8_t value1,value2,value3,value4;
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.iTOW =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            Pac.year = (value2 << 8) + value1;
+            
+            Pac.month = 0xff & spi_2.write(0x00);
+            Pac.day = 0xff & spi_2.write(0x00);
+            Pac.hour = 0xff & spi_2.write(0x00);
+            Pac.min = 0xff & spi_2.write(0x00);
+            Pac.sec = 0xff & spi_2.write(0x00);
+            Pac.valid = 0xff & spi_2.write(0x00);
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.tAcc =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.nano =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            Pac.fixtype = 0xff & spi_2.write(0x00);
+            Pac.flags = 0xff & spi_2.write(0x00);
+            Pac.flags2 = 0xff & spi_2.write(0x00);
+            Pac.numSV = 0xff & spi_2.write(0x00);
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.lon =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.lat =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.height =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.hMSL  =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.hAcc  =  (value4 << 24) + (value3 <<16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.vAcc  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.velN  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.velE  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.velD  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.gSpeed  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.headMot  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.sAcc  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.headAcc =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);                
+            Pac.pDOP  = (value2 << 8) + value1;
+            
+            for (int i=0; i < 6; i++)spi_2.write(0x00); 
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.headAcc  =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);
+            value3 = 0xff & spi_2.write(0x00);
+            value4 = 0xff & spi_2.write(0x00);                   
+            Pac.headAcc =  (value4 << 24) + (value3 << 16) + (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);                
+            Pac.magDec = (value2 << 8) + value1;
+            
+            value1 = 0xff & spi_2.write(0x00);
+            value2 = 0xff & spi_2.write(0x00);                
+            Pac.magAcc = (value2 << 8) + value1;
+                
+                
+            return Pac;
+        }
+    }
+        wait_ms(100);  
+}
+
+void send_nav_pvt (){
+    uint8_t packet_nav_pvt[] = {    0xB5,   0x62,   0x01,   0x07,   0x00,   0x00,   0x08,   0x19};
+    
+    //=============envia pacote nav pvt
+    for ( int i=0; i< sizeof(packet_nav_pvt) ; i++){
+        spi_2.write(packet_nav_pvt[i]);
+        wait_ms(5);
+    }
+    
+    gps_navPVT Data = le_nav_pvt();
+    uint8_t packet [100];
+
+    
+    packet [0]= (Data.lon >> 24)& 0xff;
+    packet [1]= (Data.lon >> 16) & 0xff ;
+    packet [2]= (Data.lon >> 8) & 0xff;
+    packet [3]= Data.lon  & 0xff;
+    packet [4]= (Data.lat >> 24)& 0xff;
+    packet [5]= (Data.lat >> 16) & 0xff ;
+    packet [6]= (Data.lat >> 8) & 0xff;
+    packet [7]= Data.lat  & 0xff;
+    packet [8]= (Data.hMSL >> 24)& 0xff;
+    packet [9]= (Data.hMSL >> 16) & 0xff ;
+    packet [10]= (Data.hMSL >> 8) & 0xff;
+    packet [11]= Data.hMSL  & 0xff;
+    
+    if (Data.lon !=0 || Data.lat !=0  ){
+        lora_send_packet (packet , (uint8_t) 12);
+    }
+}
+
+void send_gps_data(uint8_t *packet, uint8_t size){
+    uint8_t packet_rec[size]; 
+    for ( int i=0; i< size  ; i++){
+        spi_2.write(packet[i]);
+        //wait_ms(5);
+    }
+    //send_packet (packet_rec ,size);
+}
+
+/*
+void wait_packet (uint8_t *header) {
+    
+    char state = 0;
+    int cont =0;
+    
+    while (1){ //começou mensagem
+        if (cont > 250) {
+            //led2=!led2;
+            return;
+        }
+        if (state == 0 ){
+            if (spi_2.write(0x00) == header[0])
+                state =1;
+            else {
+                cont++;
+                wait_ms(10);
+            }
+        
+            
+        }
+        else if (state == 1){ // read 0xb5  
+            if (spi_2.write(0x00) == header[1]){
+                state =2;
+            }
+            else state =0;
+        }
+        else if (state == 2) {// read 0xb5 0x62
+            if (spi_2.write(0x00) == header[2]){
+                state =3;
+            }
+            else state =0;
+        }
+        else if (state == 3) {// read 0xb5 0x62 0x06
+            if (spi_2.write(0x00) == header[3]){
+                state =4;
+            }
+            else state =0;
+        }
+        else if (state == 4) {// read 0xb5 0x62 0x06 0x013
+        
+            uint8_t packet[100];
+            uint8_t value = spi_2.write(0x00);
+            uint8_t packet_size;
+             
+            for (packet_size = 0 ; packet_size < 100 ;packet_size++) {
+                if (value  != '\n' ){
+                    packet [packet_size]= value;
+                    value = spi_2.write(0x00);
+                }
+                else {
+                  lora_send_packet (packet , (uint8_t) packet_size+1);
+                  //packet_size = MAXIMUM_PACKET_SIZE +1;
+                  return;
+                }             
+            }
+        
+        }
+    }
+}*/
+
+void wait_packet_byte (uint8_t *header, uint8_t byte) {
+    
+    char state = 0;
+    int cont =0;
+    
+    while (1){ //começou mensagem
+        if (cont > 250) {
+            //led2=!led2;
+            return;
+        }
+        if (state == 0 ){
+            if (spi_2.write(0x00) == header[0])
+                state =1;
+            else {
+                cont++;
+                wait_ms(10);
+            }
+        
+            
+        }
+        else if (state == 1){ // read 0xb5  
+            if (spi_2.write(0x00) == header[1]){
+                state =2;
+            }
+            else state =0;
+        }
+        else if (state == 2) {// read 0xb5 0x62
+            if (spi_2.write(0x00) == header[2]){
+                state =3;
+            }
+            else state =0;
+        }
+        else if (state == 3) {// read 0xb5 0x62 0x06
+            if (spi_2.write(0x00) == header[3]){
+                state =4;
+            }
+            else state =0;
+        }
+        else if (state == 4) {// read 0xb5 0x62 0x06 0x013
+            
+            if (byte == 0){//mostrar todo o pacote
+                uint8_t packet[100];
+                uint8_t value = spi_2.write(0x00);
+                uint8_t packet_size;
+                 
+                for (packet_size = 0 ; packet_size < 100 ;packet_size++) {
+                    if (value  != '\n' ){
+                        packet [packet_size]= value;
+                        value = spi_2.write(0x00);
+                    }
+                    else {
+                      lora_send_packet (packet , (uint8_t) packet_size+1);
+                      //packet_size = MAXIMUM_PACKET_SIZE +1;
+                      return;
+                    }             
+                }//fim for
+            }//fim if byte
+            
+            else {//mostrar apenas um byte
+            
+             
+                for (int i = 0 ; i < byte-1 ;i++)
+                    spi_2.write(0x00);
+                    
+                uint8_t packet[]={(uint8_t)99,(uint8_t)spi_2.write(0x00)};            
+                lora_send_packet (packet , (uint8_t) 2);
+                   
+                    
+                for (int i = 0 ; i < 100 ;i++)
+                    if (spi_2.write(0x00) == '\n') 
+                        return;
+    
+                return;
+                
+            }//fim else byte
+         
+         }
+        
+      }
+    
+}
+
+
+void gps_wait_same_packet () {
+    
+    char state = 0;
+    int cont =0;
+    
+    while (1){ //começou mensagem
+        if (cont > 250) {
+            //led2=!led2;
+            return;
+        }
+        if (state == 0 ){
+            if (spi_2.write(0x00) == 0xb5)
+                state =1;
+            else {
+                cont++;
+                wait_ms(10);
+            }
+        
+            
+        }
+        else if (state == 1){ // read 0xb5  
+            if (spi_2.write(0x00) == 0x62){
+                state =2;
+            }
+            else state =0;
+        }
+        else if (state == 2){
+                led1=1;
+                uint8_t packet[200];
+                uint8_t value = spi_2.write(0x00);
+                uint8_t packet_size;
+                 
+                for (packet_size = 0 ; packet_size < 200 ;packet_size++) {
+                    if (value  != '\n' ){
+                        packet [packet_size]= value;
+                        value = spi_2.write(0x00);
+                    }
+                    else {
+                      lora_send_packet (packet , (uint8_t) packet_size+1);
+                      //packet_size = MAXIMUM_PACKET_SIZE +1;
+                      return;
+                    }
+                             
+                }//fim for   
+                lora_send_packet (packet , 200);       
+        }
+    }
+}
+
+void gps_config_gnss (){
+    
+    uint8_t packet_cfg_gnss[] = {
+      0xB5, 0x62, // Header
+      0x06, 0x3E, // Class ID = CFG, Msg ID = UBX-CFG-GNSS
+      0x3C, 0x00, // Payload Length = 60 bytes
+      0x00, 0x00, 0x20, 0x07, // msgVer=0, numTrkChHw=32, numTrkChUse=32, numConfigBlocks=7
+      0x00, 0x08, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, // gnssId=0 (GPS), resTrkCh=8, maxTrkCh=12, ENABLE
+      0x01, 0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0x00, // gnssId=1 (SBAS), resTrkCh=1, maxTrkCh=2, ENABLE
+      0x02, 0x04, 0x0A, 0x00, 0x01, 0x00, 0x01, 0x00, // gnssId=2 (Galileo), resTrkCh=4, maxTrkCh=10, ENABLE 
+      0x03, 0x04, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, // gnssId=3 (BeiDou), resTrkCh=4, maxTrkCh=8, DISABLE 
+      0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, // gnssId=4 (IMES), resTrkCh=0, maxTrkCh=8, DISABLE
+      0x05, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, // gnssId=5 (QZSS), resTrkCh=0, maxTrkCh=3, DISABLE
+      0x06, 0x04, 0x08, 0x00, 0x01, 0x00, 0x01, 0x00, // gnssId=6 (GLONASS), resTrkCh=4, maxTrkCh=8, ENABLE
+      0x00, 0x00 //checksums A &amp; B
+    };
+    uint8_t pos_ck = sizeof(packet_cfg_gnss)-2;
+    
+    uint8_t ubxi;
+    //calcula checksum
+    for (ubxi=2; ubxi<pos_ck ; ubxi++) {
+        packet_cfg_gnss[pos_ck] = packet_cfg_gnss[pos_ck] + packet_cfg_gnss[ubxi];
+        packet_cfg_gnss[pos_ck+1] = packet_cfg_gnss[pos_ck+1] + packet_cfg_gnss[pos_ck];
+    }
+    
+}
+
+
+void gps_print_local (){
+    uint8_t packet_nav_pvt[] = {    0xB5,   0x62,   0x01,   0x07,   0x00,   0x00,   0x08,   0x19};
+    
+    //=============envia pacote nav pvt
+    for ( int i=0; i< sizeof(packet_nav_pvt) ; i++){
+        spi_2.write(packet_nav_pvt[i]);
+        wait_ms(20);
+    }
+    
+    gps_navPVT Data = le_nav_pvt();
+    uint8_t packet [100];
+    //uint8_t packet []= {(Data.lon >> 24) & 0xff ,(Data.lon >> 16) & 0xff ,(Data.lon >> 8) & 0xff,Data.lon  & 0xff,(Data.lat >> 24)& 0xff , (Data.lat >> 16) & 0xff ,(Data.lat >> 8) & 0xff , Data.lat  & 0xff ,(Data.hMSL >> 24)& 0xff ,(Data.hMSL >> 16) & 0xff ,(Data.hMSL >> 8) & 0xff, Data.hMSL  & 0xff};    
+    
+    printf ("gps lat=%d lon=%d \n",Data.lat ,Data.lon );
+}
+
+void gps_config (){
+    //spi gps configuration
+    spi_2.format(8,0);
+    spi_2.frequency(1000000); //1MHz
+    wait(0.1);
+    
+    //gps reset
+    gps_reset = 0;    
+    wait_ms(50);
+    gps_reset = 1;    
+    wait(1.5);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lora.txt	Wed Nov 13 16:42:06 2019 +0000
@@ -0,0 +1,246 @@
+#include "SX1272.h"
+
+InterruptIn dio0(P0_12);
+
+//#define BAND868
+#define MAX_DBM 14
+#define DATA 0
+#define PING 1
+#define PONG 2
+
+
+#define ADDRESS 10
+
+const uint32_t DEFAULT_CHANNEL=CH_12_900;//915Mhz CH_12_900 =>0xE4C000;
+
+///////////////////////////////////////////////////////////////////
+// CHANGE HERE THE LORA MODE, NODE ADDRESS
+#define LORAMODE  1
+#define node_addr 6
+//////////////////////////////////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////
+// CHANGE HERE THE THINGSPEAK FIELD BETWEEN 1 AND 4
+#define field_index 1
+///////////////////////////////////////////////////////////////////
+#define DEFAULT_DEST_ADDR 6
+
+
+///////////////////////////////////////
+// Globals variables
+///////////////////////////////////////
+
+//___________SX1272____________________
+int e;
+
+//___________mensagem_____________________
+uint8_t message[255];
+uint8_t buffer[255];
+uint8_t message_pong[]="Pong";
+uint8_t message_ping[]="Ping";
+uint8_t r_size;
+uint8_t lora_mode_ant;
+char float_temp[10];
+char float_press[10];
+char float_hum[10];
+int cont =0;
+int loraMode=LORAMODE;
+
+
+////////////////////////////////////////
+// SETUP SX1272 initialisation
+////////////////////////////////////////
+void lora_setup()
+{
+
+
+    //printf("------Coragem LoRa temperature sensor-------------\n");
+    //sx1272.ON();  // Power ON the module
+
+    int error_config_sx1272=0;
+
+    // Set transmission mode and print the result
+    e = sx1272.setMode(loraMode);
+    //printf("Mode: %d\n",loraMode);
+    if (e) error_config_sx1272=1;
+    //printf("Setting Mode: state %d\n",e);
+
+    // enable carrier sense
+    sx1272._enableCarrierSense=true;
+
+    // for LOW POWER
+    sx1272._RSSIonSend=false;
+
+
+    // Select frequency channel
+    e = sx1272.setChannel(DEFAULT_CHANNEL);
+    if (e) error_config_sx1272=1;
+    //printf("Setting Channel: state %d\n",e);
+
+    // Select amplifier line; PABOOST or RFO
+//    #ifdef PABOOST
+//        printf("pabboost\n");
+//        sx1272._needPABOOST=true;
+//    // previous way for setting output power
+//    // powerLevel='x';
+//    #else
+//    // previous way for setting output power
+//    // powerLevel='M';
+//    #endif
+
+    // previous way for setting output power
+    // e = sx1272.setPower(powerLevel);
+
+    e = sx1272.setPowerDBM((uint8_t)MAX_DBM);
+    if (e) error_config_sx1272=1;
+    printf("Setting Power: state %d\n",e);
+
+    // Set the node address and print the result
+    e = sx1272.setNodeAddress(node_addr);
+    if (e) error_config_sx1272=1;
+    printf("Setting node addr: state %d\n",e);
+
+    // Print a success message
+    if (!error_config_sx1272) printf("SX1272 successfully configured\n");
+    else printf("ERROR CONFIGURATION SX1272\n");
+
+    wait_ms(400);
+}
+void lora_send_packet (uint8_t *payload, uint8_t length8)   // envia pacote
+{
+
+    //write on FIFO
+    sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags
+    sx1272.writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);  // Stdby LoRa mode to write in FIFO
+    sx1272.writeRegister(REG_PAYLOAD_LENGTH_LORA, length8);
+    sx1272.writeRegister(REG_FIFO_TX_BASE_ADDR,0x00);
+    sx1272.writeRegister(REG_FIFO_ADDR_PTR,0x00);
+
+    for(unsigned int i = 0; i <= length8; i++) {
+        sx1272.writeRegister(REG_FIFO, payload[i]);  // Writing the payload in FIFO
+    }
+
+    //________________Send Data__________________
+    sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags
+    sx1272.writeRegister(REG_OP_MODE, LORA_TX_MODE);  // LORA mode - Tx
+
+    unsigned long exitTime = millis()+1400;//2 segundos para sair do for
+    unsigned long Time= millis();
+    char value = sx1272.readRegister(REG_IRQ_FLAGS);
+
+    while ((bitRead(value, 3) == 0) && (Time < exitTime)) {
+        value=sx1272.readRegister(REG_IRQ_FLAGS);
+        Time= millis();
+        wait_ms(50);
+    }
+
+    wait_ms(50);
+
+}
+
+void lora_send_data(int mode)  //data predefined
+{
+    led1=1;
+    sx1272.writeRegister(REG_OP_MODE,129);//standby
+    
+    
+
+    if (mode == DATA ) {
+        #ifdef  BME280  
+        sprintf(float_temp,"%2.2f",getTemperature());
+        sprintf(float_press,"%04.2f",getPressure());
+        sprintf(float_hum,"%2.2f",getHumidity());
+
+        //============= internet of turtles =============
+        sprintf(float_breathing_time,"%04.2f", last_breathing_time);
+        sprintf(float_diving_time,"%04.2f", last_diving_time);
+        r_size=sprintf((char*)message,"\\!#Dt%04.2f_Bt%04.2f_%s°C_%shPa_%s%%",last_diving_time,last_breathing_time,float_temp,float_press,float_hum);
+        // ==============================================
+
+        //size=sprintf((char*)message,"\\!#%s°C_%shPa_%s%%",float_temp,float_press,float_hum);
+
+        lora_send_packet(message,r_size);
+//        sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR,/* (uint8_t*)*/message, r_size);
+        printf("packet send :\n%s\nrsize=%d\n",message,r_size);
+        #endif
+
+    } else if (mode == PING ) {
+
+
+        //sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_ping, sizeof(message_ping));
+        lora_send_packet(message_ping,sizeof(message_ping));
+        printf("packet send :\n%s\n",message_ping);
+
+
+    } else if (mode == PONG )  {
+
+        //sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_pong, sizeof(message_pong));
+        lora_send_packet(message_pong,sizeof(message_pong));
+        printf("packet send :\n%s\n",message_pong);
+    }
+
+    wait_ms(300);
+
+    sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags
+    sx1272.writeRegister(REG_OP_MODE,133); //leitura continua
+    cont++;
+    printf("number=%d\n",cont);
+
+    led1=0;
+}
+
+
+void lora_print_packet()
+{
+
+    led2=1;
+    sx1272.writeRegister(REG_OP_MODE,129);//standby
+
+    uint8_t pac_size;
+
+    sx1272.writeRegister(REG_FIFO_ADDR_PTR,sx1272.readRegister(REG_FIFO_RX_CURRENT_ADDR));//set fifo pointer to read packet
+    pac_size = sx1272.readRegister(REG_RX_NB_BYTES);//read size of packet
+    for(int i =0 ; i<pac_size ; i++) { //print packet
+        buffer[i]=sx1272.readRegister(REG_FIFO);
+        if (buffer[i]== '\n')  printf(" \\n");
+        if (buffer[i]== 0x0B)  printf("tab");
+        printf("%c",buffer[i]);//print packet
+    }
+    printf("\n");
+
+
+    for(int i =0 ; i<pac_size ; i++) { 
+        if ((buffer[i] =='P') && (buffer[i+1] =='i') && (buffer[i+2] =='n') && (buffer[i+3] =='g') ) { //look for Ping in packet
+            sx1272.writeRegister(0x12,255);//clear flags
+            lora_send_data(PONG);
+            i=pac_size;
+        }
+        if ((buffer[i] =='R') && (buffer[i+1] =='e') && (buffer[i+2] =='s') && (buffer[i+3] =='e') && (buffer[i+4] =='t')  ) {// look reset
+            uint32_t *ptr;
+            ptr = (uint32_t *)1073743132;// endereço gpregret 0x4000051C
+            *ptr = 0xB1;//BOOTLOADER_DFU_START
+            NVIC_SystemReset();
+        }
+       
+    }
+
+
+    wait_ms(30);
+    sx1272.writeRegister(REG_IRQ_FLAGS,255);//clear flags
+    sx1272.writeRegister(REG_OP_MODE,133); //leitura continua
+    led2=0;
+
+}
+void lora_sleep(){
+    lora_mode_ant = sx1272.readRegister(REG_OP_MODE);
+    sx1272.writeRegister(REG_OP_MODE,LORA_SLEEP_MODE); //leitura continua
+    
+    }
+    
+int lora_wake(){
+    if (lora_mode_ant!= 0){
+        sx1272.writeRegister(REG_OP_MODE,lora_mode_ant); //leitura continua
+        return 0; 
+    }
+    else return -1;
+}
\ No newline at end of file
--- a/main.cpp	Tue Sep 03 21:25:05 2019 +0000
+++ b/main.cpp	Wed Nov 13 16:42:06 2019 +0000
@@ -1,558 +1,204 @@
-/*
- *  temperature sensor on analog 8 to test the LoRa gateway
- *
- *  Copyright (C) 2016 Congduc Pham, University of Pau, France
- *
- *  This program is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
-
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with the program.  If not, see <http://www.gnu.org/licenses/>.
- *
- *****************************************************************************
- * last update: Sep. 29th, 2017 by C. Pham
- * last update: oct 30th , 2017 by C.Dupaty
- * ADAPTATION FOR NUCLEO STM32, TESTED ON NUCLEO-L073RZ WITH 
- * SX1272MB2xAS SHIELD
- * DRAGINO SHIELD V95 WITH GPS  http://wiki.dragino.com/index.php?title=Lora/GPS_Shield 
- *      For DRAGINO move LORA_CLK LORA_DI LORA_DO straps to the right (arduino 11 12 13)
- * ALL CONFIGURATIONS FOR ARDUINO HAVE BEEN REMOVED
- * WORK ONLY IN EUROPE
- * please visit http://cpham.perso.univ-pau.fr/LORA/LoRaDevices.html
- * for original version for ARDUINO 
-*/
-  
-#include "mbed.h"
-#include "SX1272.h"
-
-
-#define BAND868
-#define MAX_DBM 14
-#define DATA 0
-#define PING 1
-#define PONG 2
-
-#define ADDRESS 10
-
-const uint32_t DEFAULT_CHANNEL=CH_12_900;//915Mhz CH_12_900 =>0xE4C000
-
-//#define WITH_EEPROM            //  TO DO ON STM32
-#define WITH_APPKEY
-#define NEW_DATA_FIELD
-//#define WITH_ACK
-
-///////////////////////////////////////////////////////////////////
-// CHANGE HERE THE LORA MODE, NODE ADDRESS 
-#define LORAMODE  1
-#define node_addr 6
-//////////////////////////////////////////////////////////////////
-
-///////////////////////////////////////////////////////////////////
-// CHANGE HERE THE THINGSPEAK FIELD BETWEEN 1 AND 4
-#define field_index 1
-///////////////////////////////////////////////////////////////////
-
-///////////////////////////////////////////////////////////////////
-// CHANGE HERE THE TIME IN SECONDS BETWEEN 2 READING & TRANSMISSION
-//#define minTime 120 // 2 minutes
-//#define maxTime 600 // 10 minutes
-#define minTime 5 // seconds
-#define maxTime 25 // seconds
-///////////////////////////////////////////////////////////////////
-
-#ifdef WITH_APPKEY
-///////////////////////////////////////////////////////////////////
-// CHANGE HERE THE APPKEY, BUT IF GW CHECKS FOR APPKEY, MUST BE
-// IN THE APPKEY LIST MAINTAINED BY GW.
-uint8_t my_appKey[4]={5, 6, 7, 8};
-///////////////////////////////////////////////////////////////////
-#endif
-
-#ifdef WITH_EEPROM
-#include <EEPROM.h>
-#endif
-
-#define DEFAULT_DEST_ADDR 6
-
-#ifdef WITH_ACK
-#define NB_RETRIES 2
-#endif
-
-#ifdef WITH_EEPROM
-struct sx1272config {
-
-    uint8_t flag1;
-    uint8_t flag2;
-    uint8_t seq;
-    // can add other fields such as LoRa mode,...
-};
-
-sx1272config my_sx1272config;
-#endif
-
-///////////////////////////////////////
-// Globals variables
-///////////////////////////////////////
-
-//______________BME_280________________
-char cmd_bme[18];
-uint32_t hum_raw;
-float humf;
-uint32_t temp_raw;
-float tempf;
-uint32_t press_raw;
-float pressf;
-//char cmd[4];
-
-//___________SX1272____________________
-int e;
-
-//___________mensagem_____________________
-uint8_t message[100];
-uint8_t buffer[100];
-uint8_t message_pong[]="Pong";
-uint8_t message_ping[]="Ping";
-uint8_t r_size;
-char float_temp[10];
-char float_press[10];
-char float_hum[10];
-int cont =0;
-
-
-DigitalOut led1(P1_13);
-DigitalOut led2(P1_14);
-InterruptIn dio0(P0_12);
-DigitalIn button1(P1_11);
-DigitalIn button2(P1_12);
-DigitalIn button3(P0_30);
-
-EventQueue queue;
-
-int loraMode=LORAMODE;
-
-//_________BME 280___
-
-I2C sensor(p13, p15);
-const int BME280_ADDR = (0x77 << 1);
-
-uint16_t dig_T1,dig_P1,dig_H1, dig_H3;
-int16_t dig_T2, dig_T3,dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9,dig_H2, dig_H4, dig_H5, dig_H6;
-int32_t t_fine;
-
- //============= internet of turtles =============
-Timer time_breathing;
-Timer time_diving;
-bool beathing = false;
-float last_breathing_time = 0.0;
-float last_diving_time = 0.0;
-
-char float_breathing_time[10];
-char float_diving_time[10];
-
-Timer seconds;
-
-//================================================
-void bme_init() { //bme initialization
-
-
-    sensor.frequency(1000000);
-
-    cmd_bme[0] = 0xf2; // ctrl_hum
-    cmd_bme[1] = 0x01;
-    sensor.write(BME280_ADDR, cmd_bme, 2);
-
-    cmd_bme[0] = 0xf4; // ctrl_meas
-    cmd_bme[1] = 0x27;
-    sensor.write(BME280_ADDR, cmd_bme, 2);
-
-    cmd_bme[0] = 0xf5; // config
-    cmd_bme[1] = 0xa0;
-    sensor.write(BME280_ADDR, cmd_bme, 2);
-
-    cmd_bme[0] = 0x88; // read dig_T regs
-    sensor.write(BME280_ADDR, cmd_bme, 1);
-    sensor.read(BME280_ADDR, cmd_bme, 6);
-
-    dig_T1 = (cmd_bme[1] << 8) | cmd_bme[0];
-    dig_T2 = (cmd_bme[3] << 8) | cmd_bme[2];
-    dig_T3 = (cmd_bme[5] << 8) | cmd_bme[4];
-
-//    DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
-
-    cmd_bme[0] = 0x8E; // read dig_P regs
-    sensor.write(BME280_ADDR, cmd_bme, 1);
-    sensor.read(BME280_ADDR, cmd_bme, 18);
-
-    dig_P1 = (cmd_bme[ 1] << 8) | cmd_bme[ 0];
-    dig_P2 = (cmd_bme[ 3] << 8) | cmd_bme[ 2];
-    dig_P3 = (cmd_bme[ 5] << 8) | cmd_bme[ 4];
-    dig_P4 = (cmd_bme[ 7] << 8) | cmd_bme[ 6];
-    dig_P5 = (cmd_bme[ 9] << 8) | cmd_bme[ 8];
-    dig_P6 = (cmd_bme[11] << 8) | cmd_bme[10];
-    dig_P7 = (cmd_bme[13] << 8) | cmd_bme[12];
-    dig_P8 = (cmd_bme[15] << 8) | cmd_bme[14];
-    dig_P9 = (cmd_bme[17] << 8) | cmd_bme[16];
-
-//    DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9);
-
-    cmd_bme[0] = 0xA1; // read dig_H regs
-    sensor.write(BME280_ADDR, cmd_bme, 1);
-    sensor.read(BME280_ADDR, cmd_bme, 9);
-
-    dig_H1 = cmd_bme[0];
-    dig_H2 = (cmd_bme[2] << 8) | cmd_bme[1];
-    dig_H3 = cmd_bme[3];
-    dig_H4 = (cmd_bme[4] << 4) | (cmd_bme[5] & 0x0f);
-    dig_H5 = (cmd_bme[7] << 4) | ((cmd_bme[6]>>4) & 0x0f);
-    dig_H6 = cmd_bme[8];
-
-//    DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6);
-}
-
-float getTemperature(){
-
-    
-    cmd_bme[0] = 0xfa; // temp_msb
-    sensor.write(BME280_ADDR, cmd_bme, 1);
-    sensor.read(BME280_ADDR, &cmd_bme[1], 1);
-    
-    cmd_bme[0] = 0xfb; // temp_lsb
-    sensor.write(BME280_ADDR, cmd_bme, 1);
-    sensor.read(BME280_ADDR, &cmd_bme[2], 1);
-    
-    cmd_bme[0] = 0xfc; // temp_xlsb
-    sensor.write(BME280_ADDR, cmd_bme, 1);
-    sensor.read(BME280_ADDR, &cmd_bme[3], 1);
-    
-    temp_raw = (cmd_bme[1] << 12) | (cmd_bme[2] << 4) | (cmd_bme[3] >> 4);
-    
-    int32_t temp;
-    
-    temp =
-        (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
-        ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
-    
-    t_fine = temp;
-    temp = (temp * 5 + 128) >> 8;
-    tempf = (float)temp;
-    
-    return (tempf/100.0f);
-}
-
-float getPressure(){
-
-    cmd_bme[0] = 0xf7; // press_msb
-    sensor.write(BME280_ADDR, cmd_bme, 1);
-    sensor.read(BME280_ADDR, &cmd_bme[1], 1);
-
-    cmd_bme[0] = 0xf8; // press_lsb
-    sensor.write(BME280_ADDR, cmd_bme, 1);
-    sensor.read(BME280_ADDR, &cmd_bme[2], 1);
-
-    cmd_bme[0] = 0xf9; // press_xlsb
-    sensor.write(BME280_ADDR, cmd_bme, 1);
-    sensor.read(BME280_ADDR, &cmd_bme[3], 1);
-
-    press_raw = (cmd_bme[1] << 12) | (cmd_bme[2] << 4) | (cmd_bme[3] >> 4);
-
-    int32_t var1, var2;
-    uint32_t press;
-
-    var1 = (t_fine >> 1) - 64000;
-    var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
-    var2 = var2 + ((var1 * dig_P5) << 1);
-    var2 = (var2 >> 2) + (dig_P4 << 16);
-    var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
-    var1 = ((32768 + var1) * dig_P1) >> 15;
-    if (var1 == 0) {
-        return 0;
-    }
-    press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
-    if(press < 0x80000000) {
-        press = (press << 1) / var1;
-    }
-    else {
-        press = (press / var1) * 2;
-    }
-    var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
-    var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
-    press = (press + ((var1 + var2 + dig_P7) >> 4));
-
-    pressf = (float)press;
-    return (pressf/100.0f);
-}
-
-float getHumidity(){
-
-
-    cmd_bme[0] = 0xfd; // hum_msb
-    sensor.write(BME280_ADDR, cmd_bme, 1);
-    sensor.read(BME280_ADDR, &cmd_bme[1], 1);
-
-    cmd_bme[0] = 0xfd; // hum_lsb
-    sensor.write(BME280_ADDR, cmd_bme, 1);
-    sensor.read(BME280_ADDR, &cmd_bme[2], 1);
-
-    hum_raw = (cmd_bme[1] << 8) | cmd_bme[2];
-
-    int32_t v_x1;
-
-    v_x1 = t_fine - 76800;
-    v_x1 =  (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
-               ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
-                                            (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
-                                            (int32_t)dig_H2 + 8192) >> 14));
-    v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
-    v_x1 = (v_x1 < 0 ? 0 : v_x1);
-    v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
-
-    humf = (float)(v_x1 >> 12);
-
-    return (humf/1024.0f);
-}
-
-/////////////////////////////////////////
-// SETUP SX1272 initialisation
-////////////////////////////////////////
-void setup(){
-      
-    
-      printf("------Coragem LoRa temperature sensor-------------\n");
-      //sx1272.ON();  // Power ON the module
-
-    int error_config_sx1272=0;
-    
-    // Set transmission mode and print the result
-    e = sx1272.setMode(loraMode);
-    printf("Mode: %d\n",loraMode);
-    if (e) error_config_sx1272=1;
-    printf("Setting Mode: state %d\n",e);
-    
-    // enable carrier sense
-    sx1272._enableCarrierSense=true;
-    
-    // for LOW POWER
-    sx1272._RSSIonSend=false;
-    
-    
-    // Select frequency channel
-    e = sx1272.setChannel(DEFAULT_CHANNEL);
-    if (e) error_config_sx1272=1;
-    printf("Setting Channel: state %d\n",e);
-    
-    // Select amplifier line; PABOOST or RFO
-//    #ifdef PABOOST
-//        printf("pabboost\n");
-//        sx1272._needPABOOST=true;
-//    // previous way for setting output power
-//    // powerLevel='x';
-//    #else
-//    // previous way for setting output power
-//    // powerLevel='M';  
-//    #endif
-    
-    // previous way for setting output power
-    // e = sx1272.setPower(powerLevel); 
-    
-    e = sx1272.setPowerDBM((uint8_t)MAX_DBM);
-    if (e) error_config_sx1272=1;
-    printf("Setting Power: state %d\n",e);
-    
-    // Set the node address and print the result
-    e = sx1272.setNodeAddress(node_addr);
-    if (e) error_config_sx1272=1;
-    printf("Setting node addr: state %d\n",e);
-    
-    // Print a success message
-    if (!error_config_sx1272) printf("SX1272 successfully configured\n");
-    else printf("ERROR CONFIGURATION SX1272\n");
-    
-    wait_ms(400);
-}
-
-
-void send_packet(int mode){
-    led1=1;
-    sx1272.writeRegister(0x01,129);//standby    
-    sx1272.setPacketType(PKT_TYPE_DATA);
-    
-    printf ("sendpacket1\n");
-    if (mode == DATA ) {
-       // uint8_t message[100];    
-        sprintf(float_temp,"%2.2f",getTemperature());
-        sprintf(float_press,"%04.2f",getPressure());
-        sprintf(float_hum,"%2.2f",getHumidity());
-        
-        printf ("sendpacket2\n");
-        //============= internet of turtles =============
-        sprintf(float_breathing_time,"%2.2f", last_breathing_time);
-        sprintf(float_diving_time,"%2.2f", last_diving_time);
-        r_size=sprintf((char*)message,"\\!#%s°C_%shPa_%s_Dt%s_Bt%s%%",float_temp,float_press,float_hum,float_diving_time,float_breathing_time);
-        // ==============================================
-        
-            printf ("sendpacket3\n");
-        //size=sprintf((char*)message,"\\!#%s°C_%shPa_%s%%",float_temp,float_press,float_hum);
-        sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message, r_size);
-        printf ("sendpacket4\n");
-        printf("packet send :\n%s\n",message);
-       
-    }
-    else if (mode == PING ){   
-    
-       // uint8_t message[]="Ping";
-        //strcpy( message, "Ping");
-
-        sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_ping, sizeof(message_ping));
-        printf("packet send :\n%s\n",message_ping);
-        
-        //sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message, sizeof(message));
-//        sx1272.writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);  // Stdby LoRa mode to write in FIFO
-        
-//        sx1272.writeRegister(0x0E,0x00);
-//        sx1272.writeRegister(0x0D,0x00);
-        
-//        sx1272.writeRegister(REG_PAYLOAD_LENGTH_LORA, sizeof(message));
-//        for(unsigned int i = 0; i < sizeof(message); i++)
-//        {
-//            sx1272.writeRegister(REG_FIFO, message[i]);  // Writing the payload in FIFO
-//        }
-//        sx1272.writeRegister(REG_OP_MODE, LORA_TX_MODE);  // LORA mode - Tx
-        
-//        int exitTime = millis()+2000;//2 segundos para sair do for
-        
-//        char value = sx1272.readRegister(REG_IRQ_FLAGS);
-//        while (( value && 8 == 0) && (millis() < exitTime))
-//        {
-//            value = sx1272.readRegister(REG_IRQ_FLAGS);
-//        }
-             
-
- //       printf("packet send :\n%s\n",message);
- //       wait_ms(500); 
- //       sx1272.writeRegister(0x12,255);//clean flags
- //       sx1272.writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);  // Stdby LoRa mode to write in FIFO
-        
-    }    
-    else if (mode == PONG )  {   
-       // uint8_t message_pong[]="Pong";
-        //strcpy( message, "Pong");
-
-        sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_pong, sizeof(message_pong));
-        printf("packet send :\n%s\n",message_pong);
-        
-    }
-    
-    cont++;
-    led1=0;    
-    sx1272.writeRegister(0x01,133); //leitura continua
-    printf("number=%d\n",cont);
-}
-
-
-void print_packet(){
-
-   led2=1;
-   sx1272.writeRegister(0x01,129);//standby    
-
-    uint8_t pac_size;
-
-    sx1272.writeRegister(0x0D,sx1272.readRegister(0x10));//set fifo pointer to read packet
-    pac_size = sx1272.readRegister(0x13);//read size of packet
-    for(int i =0 ; i<pac_size ; i++){ //print packet
-        buffer[i]=sx1272.readRegister(REG_FIFO);
-        printf("%c",buffer[i]);//print packet
-    }
-    printf("\n");
-
-    for(int i =0 ; i<pac_size ; i++){ //look for Ping in packet
-        if ((buffer[i] =='P') && (buffer[i+1] =='i') ){
-                wait(1);
-                sx1272.writeRegister(0x12,255);//clean flags
-                send_packet(PONG);
-                i=pac_size;
-        }
-    } 
-    
-    for(int i =0 ; i<pac_size ; i++){ //look for Ping in packet
-        if ((buffer[i] =='R') && (buffer[i+1] =='e') && (buffer[i+2] =='s') && (buffer[i+3] =='e')  ){
-             NVIC_SystemReset();
-        }
-    }
-    
-    wait_ms(300);
-    sx1272.writeRegister(0x12,255);//clean flags
-    
-    led2=0; 
-
-}
-
-
-int main(void) {     
-    led1=1;//on leds
-    led2=1;
-
-    Thread eventThread;
-    eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
- 
-    dio0.rise(queue.event(&print_packet)); //configura rotina de interrupçao para quando receber pacote
-    seconds.start();
-
-    bme_init();//configura sensor bme280
-    setup(); //configura sx1272
-    
-    //TESTE_COMUNICAÇAO LABRADOR
-    //sx1272.writeRegister(0x0B,0x3B);
-    //sx1272.writeRegister(0x18,0x10);
-    //sx1272.writeRegister(0x33,0x26);
-    //sx1272.writeRegister(0x1D,0x08);
-    //sx1272.writeRegister(0x1E,0xC4); 
-    
-    
-    sx1272.writeRegister(REG_OP_MODE,133); //leitura continua
-    sx1272.writeRegister(0x11,187);//configure interrupt mask to interrupt only when a packet receive and packet envied 
-    
-    led1=0;//off leds
-    led2=0;
-    
-//    for (int i=2 ; i<255;i++){
-//        int value = sx1272.readRegister(i);
-//        if (value !=0 )
-//            printf("reg 0x%02x= 0x%02x\n",i,value);
-//    }
-    
-    //long int time_start = seconds.read();
-    //printf ("timestart=%d  leitura = %d\n",time_start,seconds.read());
-    while(1){
-
-         if (button3 != beathing) {
-            beathing = !beathing;
-            if(button3){
-                send_packet(DATA);
-                
-                time_breathing.reset();
-                time_breathing.start();
-                
-                time_diving.stop();
-                last_diving_time = time_diving.read();
-            }else{
-                time_diving.reset();
-                time_diving.start();
-                
-                time_breathing.stop();
-                last_breathing_time = time_breathing.read();
-
-            }
-                
-        }
-        
-        wait_ms(50);
-    }
-
-}
+#include "mbed.h"
+
+///////////////////////////////////////
+// Defines
+///////////////////////////////////////
+//#define BMX160
+//#define BME280
+//#define SI1133
+#define LORA_SX1272
+//#define MEM_MX25R
+#define GPS_ZOE
+
+
+///////////////////////////////////////
+// Globals variables
+///////////////////////////////////////
+
+DigitalOut led1(P1_13);
+DigitalOut led2(P1_14);
+DigitalIn button1(P1_11);
+DigitalIn button2(P1_12);
+DigitalIn button3(P0_30);
+EventQueue queue;
+
+
+
+ //============= internet of turtles =============
+Timer time_breathing;
+Timer time_diving;
+bool beathing = false;
+float last_breathing_time = 0.0;
+float last_diving_time = 0.0;
+
+char float_breathing_time[10];
+char float_diving_time[10];
+
+//Timer seconds;
+#ifdef  BMX160
+    #include "bmx160.txt"
+#endif
+    
+#ifdef  BME280  
+    #include "bme280.txt"
+#endif
+
+#ifdef  LORA_SX1272
+    #include "lora.txt"
+#endif
+
+#ifdef  GPS_ZOE
+    #include "gps.txt"
+#endif
+
+#ifdef  MEM_MX25R
+    #include "memory.txt"
+    SPI_MX25R mem(P0_17, P0_20, P0_22, P0_24);
+#endif
+
+#ifdef  SI1133
+    #include "Si1133.h"
+    Si1133 si1133(P0_13, P0_15);
+#endif
+
+
+void coragem_sleep (){
+    
+    #ifdef  BMX160
+        bmx_sleep ();
+    #endif
+    
+    #ifdef  BME280  
+        bme_sleep ();
+    #endif
+    
+    #ifdef  LORA_SX1272
+        lora_sleep();
+    #endif
+    
+    #ifdef  SI1133     
+        si1133.wait_until_sleep();//ligth sensor sleep
+    #endif
+    
+    #ifdef  MEM_MX25R
+        mem.deepPowerdown();//memory sleep
+    #endif
+
+
+}
+
+void coragem_wake(){
+    
+    #ifdef  BMX160
+        bmx_wake();
+    #endif
+    
+    #ifdef  BME280  
+        bme_wake();
+    #endif
+    
+    
+    #ifdef  LORA_SX1272
+        lora_wake();
+    #endif
+    
+    
+    
+    #ifdef  SI1133   
+        si1133.wake();//ligth sensor wake up
+    #endif
+    
+    //memory wake
+    #ifdef  MEM_MX25R
+        mem.m_cs = CS_LOW ;
+        wait_ms(0.1);
+        mem.m_cs = CS_HIGH ;
+    #endif
+
+}
+
+int main(void) {
+    
+///////////////////////////////////////
+// Configuraion
+///////////////////////////////////////
+    printf("------Coragem all sensor-------------\n---------Configuration initied----------");
+    //led1=1;//on leds
+    led2=1;
+    
+    #ifdef  BMX160//bmx160 configuration
+        bmx_config();
+    #endif
+    
+    #ifdef  BME280 //sensor bme280 
+        BME_ADDR = 0;
+        bme_init();
+    #endif
+    
+
+    #ifdef  LORA_SX1272
+        //configura sx1272 
+        Thread eventThread;
+        eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); 
+        dio0.rise(queue.event(&lora_print_packet)); //configure interrupt rotine ro recieve packet
+        lora_setup();
+        sx1272.writeRegister(REG_OP_MODE,133); //leitura continua
+    #endif
+
+       
+    //gps configuration
+    #ifdef  GPS_ZOE
+        gps_config();
+    #endif
+    
+    
+    
+    
+    //wait_ms(2100);
+    
+    //led1=0;//off leds
+    led2=0;
+    printf("------Configuration finished------------\n");
+    
+    
+///////////////////////////////////////
+// Main Loop
+///////////////////////////////////////
+    uint8_t packet[]={0,1,2,3};
+    while (1) { 
+    
+        //TEST all sensors
+        #ifdef  BMX160
+            bmx_read();
+        #endif
+            
+        #ifdef  BME280  
+            printf("Temp %2.2f degC, Press %04.2f hPa, Hum %2.2f %%\n", getTemperature(), getPressure(), getHumidity());
+        #endif
+
+        #ifdef  LORA_SX1272
+            lora_send_packet (packet,sizeof(packet));
+        #endif
+        
+        #ifdef  SI1133  
+            if (si1133.open()) printf("Lux = %.3f UV index = %.3f\n", (float)si1133.get_light_level(), (float)si1133.get_uv_index());
+        #endif
+        
+        #ifdef  MEM_MX25R
+            memory_test();
+        #endif
+        
+        #ifdef  GPS_ZOE
+            //gps_print_local();
+            send_nav_pvt();
+        #endif
+        
+        
+        wait(1);
+
+        
+    }
+    
+}
+
+
--- a/mbed_app.json	Tue Sep 03 21:25:05 2019 +0000
+++ b/mbed_app.json	Wed Nov 13 16:42:06 2019 +0000
@@ -21,10 +21,7 @@
             "target.extra_labels_remove": ["SOFTDEVICE_COMMON", "SOFTDEVICE_S140_FULL", "NORDIC_SOFTDEVICE"],           
             "target.lf_clock_src": "NRF_LF_SRC_RC",
             "target.lf_clock_rc_calib_timer_interval": 16,
-            "target.lf_clock_rc_calib_mode_config": 2,
-            "target.mbed_app_start": "0x26000",
-            "target.mbed_app_size": "0xd1000",
-            "target.bootloader_img": null
+            "target.lf_clock_rc_calib_mode_config": 2
 
 
             
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_coragem.lib	Wed Nov 13 16:42:06 2019 +0000
@@ -0,0 +1,1 @@
+https://github.com/leiachewbacca/mbed_coragem/#b8dad0652c9cd2321126a590280a18a964e1eed0
--- a/mbed_coragem_new.lib	Tue Sep 03 21:25:05 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://github.com/leiachewbacca/mbed_coragem/#b8dad0652c9cd2321126a590280a18a964e1eed0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/memory.txt	Wed Nov 13 16:42:06 2019 +0000
@@ -0,0 +1,221 @@
+#include <ctype.h>
+#include "SPI_MX25R.h"
+
+
+ 
+// SPI_MX25R(PinName mosi, PinName miso, PinName sclk, PinName cs) ;
+SPI_MX25R spi_mem(P0_17, P0_20, P0_22, P0_24) ;
+
+
+void rdRange(unsigned long rd_start_address, unsigned long rd_end_address)
+{
+    unsigned int i, j ;
+    unsigned char data[0x10] ;
+    unsigned long current_address = rd_start_address ;
+    printf("\n\r") ;
+    printf("rd_start_address = 0x%08X\n\r", rd_start_address) ;  
+    printf("rd_end_address   = 0x%08X\n\r", rd_end_address) ;  
+    printf("\n\r") ;
+    printf("         ") ;             // print header +0 to +F
+    for (i = 0 ; i < 0x10 ; i++ ) {
+        printf("+%X ",i) ;
+    }
+    printf("\n\r") ;
+    printf("         -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --") ;             
+    printf("\n\r") ;
+        for (i = rd_start_address ; current_address < rd_end_address ; i++ ) {
+        printf("%06X : ", current_address ) ;   // print start address :
+        for (j = 0 ; j <= 15 ; j++ ) {
+            data[j] = spi_mem.read8(current_address ) ;
+            printf("%02X ", data[j]) ;
+            current_address = current_address + 0x01 ;
+        }
+        printf(": ") ;
+        for (i = 0 ; i < 0x10 ; i++ ) {
+            if (isprint(data[i])) {
+                printf("%c", data[i]) ;
+            } else {
+                printf(".") ;
+            }
+        }
+        printf(" :\n\r") ;
+    }
+    printf("\n\r") ;
+}  
+ 
+void rdSFDPRange(unsigned long rd_start_address, unsigned long rd_end_address)
+{
+    unsigned int i, j ;
+    unsigned char data[0x10] ;
+    unsigned long current_address = rd_start_address ;
+    printf("\n\r") ;
+    printf("rd_start_address = 0x%08X\n\r", rd_start_address) ;  
+    printf("rd_end_address   = 0x%08X\n\r", rd_end_address) ;  
+    printf("\n\r") ;
+    printf("         ") ;             // print header +0 to +F
+    for (i = 0 ; i < 0x10 ; i++ ) {
+        printf("+%X ",i) ;
+    }
+    printf("\n\r") ;
+    printf("         -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --") ;             
+    printf("\n\r") ;
+        for (i = rd_start_address ; current_address < rd_end_address ; i++ ) {
+        printf("%06X : ", current_address ) ;   // print start address :
+        for (j = 0 ; j <= 15 ; j++ ) {
+            data[j] = spi_mem.readSFDP(current_address ) ;
+            printf("%02X ", data[j]) ;
+            current_address = current_address + 0x01 ;
+        }
+        printf(": ") ;
+        for (i = 0 ; i < 0x10 ; i++ ) {
+            if (isprint(data[i])) {
+                printf("%c", data[i]) ;
+            } else {
+                printf(".") ;
+            }
+        }
+        printf(" :\n\r") ;
+    }
+    printf("\n\r") ;
+} 
+ 
+void rdFREADRange(unsigned long rd_start_address, unsigned long rd_end_address)
+{
+    unsigned int i, j ;
+    unsigned char data[0x10] ;
+    unsigned long current_address = rd_start_address ;
+    printf("\n\r") ;
+    printf("rd_start_address = 0x%08X\n\r", rd_start_address) ;  
+    printf("rd_end_address   = 0x%08X\n\r", rd_end_address) ;  
+    printf("\n\r") ;
+    printf("         ") ;             // print header +0 to +F
+    for (i = 0 ; i < 0x10 ; i++ ) {
+        printf("+%X ",i) ;
+    }
+    printf("\n\r") ;
+    printf("         -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --") ;             
+    printf("\n\r") ;
+        for (i = rd_start_address ; current_address < rd_end_address ; i++ ) {
+        printf("%06X : ", current_address ) ;   // print start address :
+        for (j = 0 ; j <= 15 ; j++ ) {
+            data[j] = spi_mem.readFREAD(current_address ) ;
+            printf("%02X ", data[j]) ;
+            current_address = current_address + 0x01 ;
+        }
+        printf(": ") ;
+        for (i = 0 ; i < 0x10 ; i++ ) {
+            if (isprint(data[i])) {
+                printf("%c", data[i]) ;
+            } else {
+                printf(".") ;
+            }
+        }
+        printf(" :\n\r") ;
+    }
+    printf("\n\r") ;
+} 
+ 
+void commandMenu(void)  // TeraTerm Display (Enable Terminal Local Echo On).
+{
+    printf("== Commands =========== (Comments) ===============================================\n\r") ;
+    printf("\n\r") ;
+    printf("  wren                  (Write Enable,  set WEL=1, 06h)\n\r") ;
+    printf("  wrdi                  (Write Disable, set WEL=0, 04h)\n\r") ;
+    printf("  rdid                  (Read Manu & JEDEC Dev ID, 9Fh)\n\r") ;
+    printf("  rems                  (Read Elect. Manu & Dev ID,90h)\n\r") ;
+    printf("  res                   (Read Elect. Signiture,    ABh)\n\r") ;
+    printf("  rdsr                  (Read Status Register,     05h)\n\r") ;
+    printf("  rdcr                  (Read Config Register,     15h)\n\r") ;
+    printf("  rdscr                 (Read Security Register,   2Bh)\n\r") ;
+    printf("  enso                  (Enter Secure OTP Area,    B1h)\n\r") ;
+    printf("  exso                  (Exit Secure OTP Area,     C1h)\n\r") ;
+    printf("  reset                 (ResetEnable + SoftwareReset = 66h + 99h)\n\r") ;
+    printf("  hp                    (Enter HP mode = WREN=06h + WRSR= 01h 000002h)\n\r") ;
+    printf("  lp                    (Enter LP mode = WREN=06h + WRSR= 01h 000000h)\n\r") ;
+    printf("  wrsr  data_3B         (Write SR 01h +3B data,ex: wrsr 000002h, set wren 1st)\n\r") ;
+    printf("  wrscr data_1B         (Write Security Reg 2Fh +1B data, caution LDSO bit OTP!)\n\r") ;
+    printf("  se    address         (4KB Sector Erase,     ex: be 0x00    <- note: sets WEL)\n\r") ;
+    printf("  be    address         (64KB Block Erase,     ex: be 0x00    <- note: sets WEL)\n\r") ;
+    printf("  32Kbe address         (32KB Block Erase,     ex: 32kbe 0x00 <- note: sets WEL)\n\r") ;
+    printf("  ce                    (Chip Erase = C7h,     ex: ce         <- note: sets WEL)\n\r") ;
+    printf("  pp    address         (256B Page Program,    ex: pp 0x00    <- note: sets WEL)\n\r") ;
+    printf("  read  address         (read 1 byte of data,  ex: read 0x0000)\n\r") ;
+    printf("  rd1   startadr endadr (read range of data,   ex: rd1  0x00 0x10)\n\r") ;
+    printf("  fread startadr endadr (read range of data,   ex: sfdp 0x00 0x10)\n\r") ;
+    printf("  sfdp  startadr endadr (read range of SFDP,   ex: sfdp 0x00 0x10)\n\r") ;
+    printf("==================================================================================\n\r") ;
+    printf("\n\r") ;
+}
+/*
+void page_program (int address, int numData){
+    
+    
+int i ;
+    
+    //scanf("%X", &address) ;
+    printf("writing %d bytes to 0x%X\n\r",numData, address) ;
+    spi_mem.writeEnable() ; // send WREN 1st
+    for (i = 0 ; i < numData ; i++ ) {
+        data[i] = i & 0xFF ;
+    }
+    spi_mem.programPage(address, data, numData) ;
+    data[0] = 0 ;
+    while((data[0] & 0x01) != 0x01 ) {
+        printf(".") ;
+        data[0] = spi_mem.readStatus() ;
+        wait(0.01) ;
+    }
+    printf("\n\r") ;
+    printf("                               PP = 02h done\n\r") ;
+    
+       
+}*/
+
+
+void memory_test() {
+    
+    
+    char cmd[32] ;
+    //int i ;
+    int numData = 256 ;
+    unsigned long address = 0 ;
+    unsigned long rd_start_address = 0 ;
+    unsigned long rd_end_address = 0 ;
+    uint8_t data[256] ;
+
+    int i ;
+
+    spi_mem.writeEnable() ; // send WREN 1st
+    spi_mem.sectorErase(address) ;
+    data[0] = 0x01 ;           // poll if WIP bit SR<0>=1
+    for (i=0 ; i<25 ; i++){
+        
+    while((data[0] & 0x01) != 0 ) {
+        printf(".\n") ;
+        data[0] = spi_mem.readStatus() ;
+        wait(0.1) ;
+    }                  // end poll
+    printf("SE = 20h done\n\r") ;
+
+    
+    
+    
+    
+    
+    address =0;
+    printf("writing %d bytes to 0x%X\n\r",numData, address) ;
+    spi_mem.writeEnable() ; // send WREN 1st
+    for (i = 0 ; i < numData ; i++ ) {
+        data[i] = i+1 & 0xFF ;
+    }
+    spi_mem.programPage(address, data, numData) ;
+    data[0] = 0 ;
+    i=0;
+    while((data[0] & 0x01) != 0x01 ) {
+        printf(",\n") ;
+        data[0] = spi_mem.readStatus() ;
+        wait(0.01) ;
+        if (i > 3000) break;
+            else i++;   
+    }
+}
\ No newline at end of file