Programming nrf52832 d internal temperature sensor

Hi,

I am trying to program the internal temperature of the nrf52832 development kit, I have this code so far it builds and when I flash it on the board; I get a message that the temp driver cannot be found. Can someone please help me with this issue?


#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/i2c.h> /* STEP 3 - Include the header file of the I2C API */
#include <zephyr/sys/printk.h> /* STEP 4.1 - Include the header file of printk() */

/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS 1000

void main(void)
{
struct device *temp_dev;

int ret;
temp_dev = device_get_binding("TEMP_0");
if (!temp_dev) {
printk("Didn't find the temp driver!\n");
return;
}

while (1) {
struct sensor_value val;

ret = sensor_sample_fetch(temp_dev);

if (ret) {
printk("Failed to sample temp sensor!\n");
return;
}

ret = sensor_channel_get(temp_dev, SENSOR_CHAN_DIE_TEMP, &val);

if (ret) {
printk("failed to get temp sensor data!\n");
return;
}
printk("Temp %d.%d\n", val.val1, val.val2);

k_msleep(SLEEP_TIME_MS);
  }
}

Thanks in advance

  • Hi Rawan

    Which version of the nRF Connect SDK are you using? 

    Can you post your prj.conf and your overlay files? 

    Regards

    Runar

  • Hi,

    Thanks for getting back to me.

    I changed the main code (I can send it if needed) and this is what I have right now, the code builds and flashes and I get this message in the nrf terminal, I try opening the nrf connect for desktop ble it is not working, is there an alternative? 

    I downloaded the nrf connect for mobile as my peripheral and I can connect but where can I read the temperature values? 

    This is the prj.conf:
    CONFIG_STDOUT_CONSOLE=y
    CONFIG_PRINTK=y
    CONFIG_PWM=y
    CONFIG_LOG=y
    CONFIG_LOG_PRINTK=y
    CONFIG_LOG_MODE_IMMEDIATE=y
    CONFIG_PWM_LOG_LEVEL_DBG=y
    CONFIG_I2C=y
    CONFIG_CBPRINTF_FP_SUPPORT=y
    CONFIG_GPIO=y
    CONFIG_SERIAL=y
    CONFIG_SENSOR=y
    CONFIG_TEMP_NRF5=y
    CONFIG_BT=y
    CONFIG_BT_DEBUG_LOG=y
    CONFIG_BT_SMP=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_DIS=y
    CONFIG_BT_DIS_PNP=n
    CONFIG_BT_BAS=y
    CONFIG_BT_DEVICE_NAME="Zephyr Health Thermometer"
    CONFIG_BT_DEVICE_APPEARANCE=768
    CONFIG_BT_ATT_ENFORCE_FLOW=n
    CONFIG_CBPRINTF_FP_SUPPORT=y
    The overlay file: 
    &temp {
        compatible = "nordic,nrf-temp";
        status = "okay";
    };
  • Can I install the ble app separately? outside of the nrf connect for desktop app?

    Am using the nrf52832 the nrf connect sdk is v2.3.0

  • Hi, 

    can you try the following :

    uninstall the Bluetooth Low Energy from the OS 'Add and Remove Programs' and try to reinstall is and see if this resolves the issue?
    • make sure that the client rebooted the machine before reinstalling
    • If the above does not solve it can you try to manually install jLink 7.66a manually after 4.0.0 is installed

    Can you upload the code with using insert --> code? 

    If you place a breakpoint on the  line "ret = sensor_channel_get(temp_dev, SENSOR_CHAN_DIE_TEMP, &val);" do you get any values in the "ret" variable? 

    Regards

    Runar

  • Hi, 

    I tried a different computer the application works now thanks but the device does not connect it displays an error message that access to the COM is denied,

    I have a question related to connecting the bluetooth on the board, I have this main code written and I am able to connect ble via the nrf connect for mobile but am not sure where can I read the temperature values. 

    Main: 

    #include <stdio.h>
    #include <stddef.h>
    #include <string.h>
    #include <errno.h>

    #include <zephyr/types.h>
    #include <zephyr/kernel.h>
    #include <zephyr/drivers/sensor.h>
    #include <zephyr/sys/printk.h>
    #include <zephyr/sys/byteorder.h>

    #include <zephyr/bluetooth/bluetooth.h>
    #include <zephyr/bluetooth/hci.h>
    #include <zephyr/bluetooth/conn.h>
    #include <zephyr/bluetooth/uuid.h>
    #include <zephyr/bluetooth/gatt.h>
    #include <zephyr/bluetooth/services/bas.h>

    //#include "hts.h"
    #ifdef __cplusplus
    extern "C" {
    #endif

    void hts_init(void);
    void hts_indicate(void);

    #ifdef __cplusplus
    }
    #endif

    //THIS IS THE hts.c
    #ifdef CONFIG_TEMP_NRF5
    static const struct device *temp_dev = DEVICE_DT_GET_ANY(nordic_nrf_temp);
    #else
    static const struct device *temp_dev;
    #endif

    static uint8_t simulate_htm;
    static uint8_t indicating;
    static struct bt_gatt_indicate_params ind_params;

    static void htmc_ccc_cfg_changed(const struct bt_gatt_attr *attr,
                     uint16_t value)
    {
        simulate_htm = (value == BT_GATT_CCC_INDICATE) ? 1 : 0;
    }

    static void indicate_cb(struct bt_conn *conn,
                struct bt_gatt_indicate_params *params, uint8_t err)
    {
        printk("Indication %s\n", err != 0U ? "fail" : "success");
    }

    static void indicate_destroy(struct bt_gatt_indicate_params *params)
    {
        printk("Indication complete\n");
        indicating = 0U;
    }

    /* Health Thermometer Service Declaration */
    BT_GATT_SERVICE_DEFINE(hts_svc,
        BT_GATT_PRIMARY_SERVICE(BT_UUID_HTS),
        BT_GATT_CHARACTERISTIC(BT_UUID_HTS_MEASUREMENT, BT_GATT_CHRC_INDICATE,
                       BT_GATT_PERM_NONE, NULL, NULL, NULL),
        BT_GATT_CCC(htmc_ccc_cfg_changed,
                BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
        /* more optional Characteristics */
    );
    //IT ENDS HERE

    void hts_init(void)
    {
        if (temp_dev == NULL || !device_is_ready(temp_dev)) {
            printk("no temperature device; using simulated data\n");
            temp_dev = NULL;
        } else {
            printk("temp device is %p, name is %s\n", temp_dev,
                   temp_dev->name);
        }
    }

    static const struct bt_data ad[] = {
        BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
        BT_DATA_BYTES(BT_DATA_UUID16_ALL,
                  BT_UUID_16_ENCODE(BT_UUID_HTS_VAL),
                  BT_UUID_16_ENCODE(BT_UUID_DIS_VAL),
                  BT_UUID_16_ENCODE(BT_UUID_BAS_VAL)),
    };

    static void connected(struct bt_conn *conn, uint8_t err)
    {
        if (err) {
            printk("Connection failed (err 0x%02x)\n", err);
        } else {
            printk("Connected\n");
        }
    }

    static void disconnected(struct bt_conn *conn, uint8_t reason)
    {
        printk("Disconnected (reason 0x%02x)\n", reason);
    }

    static struct bt_conn_cb conn_callbacks = {
        .connected = connected,
        .disconnected = disconnected,
    };

    static void bt_ready(void)
    {
        int err;

        printk("Bluetooth initialized\n");

        hts_init();

        err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
        if (err) {
            printk("Advertising failed to start (err %d)\n", err);
            return;
        }

        printk("Advertising successfully started\n");
    }

    static void auth_cancel(struct bt_conn *conn)
    {
        char addr[BT_ADDR_LE_STR_LEN];

        bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

        printk("Pairing cancelled: %s\n", addr);
    }

    static struct bt_conn_auth_cb auth_cb_display = {
        .cancel = auth_cancel,
    };

    static void bas_notify(void)
    {
        uint8_t battery_level = bt_bas_get_battery_level();

        battery_level--;

        if (!battery_level) {
            battery_level = 100U;
        }

        bt_bas_set_battery_level(battery_level);
    }

    void hts_indicate(void)
    {
        /* Temperature measurements simulation */
        struct sensor_value temp_value;

        if (simulate_htm) {
            static uint8_t htm[5];
            static double temperature = 20U;
            uint32_t mantissa;
            uint8_t exponent;
            int r;

            if (indicating) {
                return;
            }

            if (!temp_dev) {
                temperature++;
                if (temperature == 30U) {
                    temperature = 20U;
                }

                goto gatt_indicate;
            }

            r = sensor_sample_fetch(temp_dev);
            if (r) {
                printk("sensor_sample_fetch failed return: %d\n", r);
            }

            r = sensor_channel_get(temp_dev, SENSOR_CHAN_DIE_TEMP,
                           &temp_value);
            if (r) {
                printk("sensor_channel_get failed return: %d\n", r);
            }

            temperature = sensor_value_to_double(&temp_value);

    gatt_indicate:
            printf("temperature is %gC\n", temperature);

            mantissa = (uint32_t)(temperature * 100);
            exponent = (uint8_t)-2;

            htm[0] = 0; /* temperature in celsius */
            sys_put_le24(mantissa, (uint8_t *)&htm[1]);
            htm[4] = exponent;

            ind_params.attr = &hts_svc.attrs[2];
            ind_params.func = indicate_cb;
            ind_params.destroy = indicate_destroy;
            ind_params.data = &htm;
            ind_params.len = sizeof(htm);

            if (bt_gatt_indicate(NULL, &ind_params) == 0) {
                indicating = 1U;
            }
        }
    }

    void main(void)
    {
        int err;

        err = bt_enable(NULL);
        if (err) {
            printk("Bluetooth init failed (err %d)\n", err);
            return;
        }

        bt_ready();

        bt_conn_cb_register(&conn_callbacks);
        bt_conn_auth_cb_register(&auth_cb_display);

        /* Implement indicate. At the moment there is no suitable way
         * of starting delayed work so we do it here
         */
        float ret;
        while (1) {
            k_sleep(K_SECONDS(1));

            /* Temperature measurements simulation */
            hts_indicate();

            /* Battery level simulation */
            bas_notify();
        }
    }
Related