Sending Temperature Values from Server to Client using sht85

Hi there!

I have recently managed to combine sht85 with light lightness server code. J-link is able to produce 2 values (Temperature and Humidity) whenever I key in the number 3 .

I want to be able to send these data to the client side. I have taken a look at similar posts regarding this topic but cannot seem to solve it.

As far as I know, the Client code is able to control the light lightness of the server (network is provisioned using NRF mesh app) and receive the server's present lightness level.

Hence, I want to be able to control the lightness of the server from client and be able to receive both Lightness level as well as Temperature 

Below are the codes that I have edited to allow only temperature to be sent over for now for testing purposes. (will eventually send over humidity)

light_lightness_client to allow temperature reading from server side

/* Light lightness client model interface: Process the received status message in this callback */
static void app_light_lightness_client_status_cb(const light_lightness_client_t *p_self,
                                                 const access_message_rx_meta_t *p_meta,
                                                 const light_lightness_status_params_t *p_in)
{
    if (p_in->remaining_time_ms > 0)
    {
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Lightness client: 0x%04x, Present Lightness: %d, Target Lightness: %d, Remaining Time: %d ms\n",
              p_meta->src.value, p_in->present_lightness, p_in->target_lightness, p_in->remaining_time_ms);
    }
    else
    {
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Lightness client: 0x%04x, Present lightness: %d, Temperature: %d\n",
              p_meta->src.value, p_in->present_lightness, p_in->temp_read); //just added temp_read to read temperature values from server
    }
}

light_lightness_server (temperature and humidity reading). In this file, temp_read is the actual value of temperature

 if(button_number == 3)
    {
              int32_t temperature, humidity;
              int32_t temp_read, hum_read;
                  
        

                  //Output of measurement is multiplied by a thousand, hence, there is a need to divide the values by 1000

              int16_t ret = sht3x_measure_blocking_read(SHT3X_I2C_ADDR_DFLT,
                                                          &temperature, &humidity);
        
               if (ret == STATUS_OK) 
               {
                temp_read = temperature / 1000;
                hum_read = humidity / 1000; 
                  
                  

               __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Temperature (in degree celsius): %d\n", temp_read);
               __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Humidity (in relative humidity): %d\n", hum_read);  
         
              } else {
                   __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "sht3x_measure_blocking_read() failed. Error: %d\n", ret);
              }

      }

app_light_lightness.c to publish temp_read

static void light_lightness_state_set_cb(const light_lightness_setup_server_t * p_self,
                                         const access_message_rx_meta_t * p_meta,
                                         const light_lightness_set_params_t * p_in,
                                         const model_transition_t * p_in_transition,
                                         light_lightness_status_params_t * p_out)
{
    app_light_lightness_setup_server_t * p_app;
    uint16_t present_lightness;
    uint32_t transition_time_ms;
    light_lightness_range_status_params_t range;

    p_app = PARENT_BY_FIELD_GET(app_light_lightness_setup_server_t,
                                light_lightness_setup_server,
                                p_self);
    NRF_MESH_ASSERT(p_app && p_in);
    app_transition_params_t * p_params = app_transition_requested_get(&p_app->state.transition);

    /* Notify anyone interested that we just got a command to set the lightness */
    if (p_app->app_add_notify.app_notify_set_cb != NULL)
    {
        p_app->app_add_notify.app_notify_set_cb(p_app->app_add_notify.p_app_notify_v, p_in->lightness);
    }

    p_app->app_light_lightness_get_cb(p_app, &present_lightness);

    /* If delta is too large, target value should get clipped to range limits */
    range_get(p_self->state.handle, &range);

    /* There is a special case for lightness value of `0`. If requested lightness is zero, and
     * present value is also zero, target should be zero */
    p_app->state.target_snapshot = p_in->lightness > range.range_max ? range.range_max :
                                   p_in->lightness < range.range_min && p_in->lightness != 0 ? range.range_min :
                                   p_in->lightness;
    p_app->state.init_present_snapshot = p_app->state.present_lightness;

    ERROR_CHECK(light_lightness_mc_actual_state_set(p_self->state.handle, p_app->state.target_snapshot));
    if (p_app->state.target_snapshot >= LIGHT_LIGHTNESS_LAST_MIN)
    {
        ERROR_CHECK(light_lightness_mc_last_state_set(p_self->state.handle, p_app->state.target_snapshot,
                                                      LIGHT_LIGHTNESS_MC_WRITE_DESTINATION_FLASH_ONLY));
    }

    if (!p_self->state.initialized || (present_lightness != p_in->lightness))
    {
        (void) transition_parameters_set(p_app,
                                         (int32_t)p_app->state.target_snapshot - (int32_t)present_lightness,
                                         p_in_transition,
                                         APP_TRANSITION_TYPE_SET);

        transition_time_ms = p_params->transition_time_ms;
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,
              "SET: target lightness: %d  delay: %d  tt: %d  req-delta: %d \n",
              p_app->state.target_snapshot,
              p_app->state.transition.delay_ms,
              p_params->transition_time_ms,
              p_params->required_delta);

        app_transition_trigger(&p_app->state.transition);
    }
    else
    {
        transition_time_ms = 0;
    }

    /* Prepare response */
    if (p_out != NULL)
    {
        p_out->present_lightness = p_app->state.present_lightness;
        p_out->target_lightness  = p_app->state.target_snapshot;
        p_out->remaining_time_ms = transition_time_ms;
        p_out->temp_read = p_app->state.temp_read; //just added 
    }
 }

I have also added temp_read in the necessary files such as in light_lightness_common.h

/** Parameters for the light_lightness Status message. */
typedef struct
{
    uint16_t present_lightness;            /**< The present value of the Lightness state */
    uint16_t target_lightness;             /**< The target value of the Lightness state (optional) */
    uint32_t remaining_time_ms;            /**< Remaining time value in milliseconds */
    int32_t  temp_read;                   // just added
} light_lightness_status_params_t;

as well as

typedef struct
{
    /** Present value of the lightness state */
    uint16_t present_lightness;
    /** Target value of the lightness state, as received from the model interface. */
    uint16_t target_lightness;
    /** Initial present lightness required for handling Set/Delta Set message. */
    uint16_t initial_present_lightness;

    int32_t temp_read;// just added 

    /** Present value when message was received */
    uint16_t init_present_snapshot;
    /** Requested target */
    uint16_t target_snapshot;

    /** To detect if TID is new while processing delta transition */
    bool new_tid;

    /* Elapsed time at last publication. */
    uint32_t published_ms;

    /** Structure for using transition module functionality */
    app_transition_t transition;
} app_light_lightness_state_t;

These are all the modifications I have made thus far. I am not too sure if I am doing this correctly as I am pretty new to this. If there is anything wrong or anything else to add, I would greatly appreciate guidance in this matter as I have not been able to get this to work.

Thanks in advance!

Parents
  • Hi,

    Are you receiving any data on client side when you try to send the temperature?

    Also which SDK version are you using?

    if (p_in->remaining_time_ms > 0)
        {
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Lightness client: 0x%04x, Present Lightness: %d, Target Lightness: %d, Remaining Time: %d ms\n",
                  p_meta->src.value, p_in->present_lightness, p_in->target_lightness, p_in->remaining_time_ms);
        }
        else
        {
            __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Lightness client: 0x%04x, Present lightness: %d, Temperature: %d\n",
                  p_meta->src.value, p_in->present_lightness, p_in->temp_read); //just added temp_read to read temperature values from server
        }

    Also this code snippet on client, won't this if statement prevent the client from ever reading the temp value if the if statement is true? Can you check if this is the case?

  • Can you check on the server side what temperature value that are being sent? Is it sent at all?

  • 00> <t:     402591>, main.c,  422, Button 3 pressed
    00> <t:     402593>, main.c,  461, 
    00>     --------------------------------------------------------------
    00>      RTT 1) Decrease LED state, until min value is reached.
    00>      RTT 2) Increase LED state, until max value is reached.
    00>      RTT 4) Clear all the states to reset the node.
    00>     --------------------------------------------------------------
    00> <t:     403132>, main.c,  492, Temperature: 26
    00> <t:     403134>, main.c,  493, Humidity: 58
    00> <t:     403136>, main.c,  511, 
    00>     --------------------------------------------------------------
    00>      RTT 1) Decrease LED state, until min value is reached.
    00>      RTT 2) Increase LED state, until max value is reached.
    00>      RTT 4) Clear all the states to reset the node.
    00>     --------------------------------------------------------------

    This is the log on the server side. Whenever I enter '3', an update of temperature and humidity will be shown. I am not sure if its being sent to client side.

  • I suspect that you are not sending the values correctly. Looking at the "if(button_number == 3)" in the code above it looks like you aren't publishing anything to the client when button 3 is pressed. It only reads out the temperature and humidity.

  • I just added these lines of code under if(button_number ==3)

    uint32_t status2 = temp_status_publish(&m_light_lightness_server_0);
                  if ( status2 != NRF_SUCCESS)
               {
                __LOG(LOG_SRC_APP, LOG_LEVEL_WARN, "Unable to publish status message, status: %d\n", status2);
               }

    In the J-link it does not print any error, hence, I am assuming it is publishing correctly.

Reply Children
Related