Temperature code into Light Lightness Server code

Hi 

I am using sht85 to detect temperature/humidity in my Light lightness server code

 In the original server code, by pressing 1 or 2 you get to either decrease or increase the lightness level respectively. I modified the code so that when number 3 is pressed, both temperature and humidity will be display in the J-Link

This is the code for button number 3

 if(button_number == 3)
    {
              int32_t temperature, humidity;
               int32_t temp_read, hum_read;
                  char temp_str[5];
                  char hum_str[5];
        
           int8_t ret = sht3x_measure_blocking_read(SHT3X_I2C_ADDR_DFLT,
                                                 &temperature, &humidity);
               if (ret == STATUS_OK) {
                temp_read = temperature / 1000;
                hum_read = humidity / 1000; 
                  
                itoa(temp_read, temp_str,10);
                 itoa(hum_read, hum_str, 10);
                  
         

              }
          __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Temperature: %d\n", temp_str);
         __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Humidity: %d\n", hum_str);
      }   

However, The display in J-Link is not what I had expected

The temperature and Humidity values are extremely high. Is there something I am doing wrong in the code? If so, please advise on what to change. Thank you!

Parents
  • HI Vidar,

    Thank you for your prompt reply ! I did the changes that you told me to do. However, the temperature values that appear in the J-Link appear to be just 0 even after pressing button 3 a few times.

    As such, instead of putting 'temperature' in the LOG, i tried it with 'temp_read' instead. Here is the code.

    if(button_number == 3)
        {
                  int32_t temperature, humidity;
                  int32_t temp_read, hum_read;
                      char temp_str[5];
                      char hum_str[5];
            
                  int8_t ret = sht3x_read(SHT3X_I2C_ADDR_DFLT,
                                               &temperature, &humidity);
    
                   if (ret == STATUS_OK) {
                     temp_read = temperature / 1000 ;
                    //hum_read = humidity / 1000;  I removed humidity for this test first
                      
                    // itoa(temp_read, temp_str,10);
                    // itoa(hum_read, hum_str, 10);
                     
                  }
                  __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Temperature: %d\n", temp_read); 
          }   

    Ultimately, I managed to get something in the output. However I realized that the output is always goes in one loop from 0-255. Every time I press button 3, the temperature seems to increase until 255 before going back to 0 and starting all over again as shown in the picture here

    Any possible solutions to this? Your help is much appreciated 

  • Hi,

    Please try again with the blocking read (sht3x_measure_blocking_read) and check if it returns with an error.

    e.g.

     if(button_number == 3)
        {
                  int32_t temperature, humidity;
                   int32_t temp_read, hum_read;
                      char temp_str[5];
                      char hum_str[5];
            
               int8_t ret = sht3x_measure_blocking_read(SHT3X_I2C_ADDR_DFLT,
                                                     &temperature, &humidity);
                   if (ret == STATUS_OK) {
                    temp_read = temperature / 1000;
                    hum_read = humidity / 1000; 
                      
                      
                    // itoa(temp_read, temp_str,10); // Reduntant. The __LOG macro will do the conversion
                    // itoa(hum_read, hum_str, 10);
                   __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Temperature: %d\n", temperature);
                   __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Humidity: %d\n", humidity);  
             
                  } else {
                       __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "sht3x_measure_blocking_read() failed. Error: %d\n", ret);
                  }
    
          }  

  • Hi Vidar,

    I got an error of -1. If I am not wrong ret is supposed to be 0 right? Since STATUS_OK is defined as 0

  • Hi,

    -1 corresponds to 'STATUS_ERR_BAD_DATA ' according to the sht3x.h header: https://github.com/Sensirion/embedded-sht/blob/fcc8a523210cc1241a2750899ff6b0f68f3ed212/sht3x/sht3x.h#L53. So the next step should probably to debug the function to see where the error is returned.

Reply Children
Related