Azure Sphere MT3620 dev kit - temperature reading

Adam 106 Reputation points
2020-10-21T10:48:11.373+00:00

I have some code to try and read from the Temperature I2C, which is obtaining values but these do not make sense, nor are they changing over time.
The data also fails the CRC check, further suggesting what is coming back isn't valid.
Does anyone have any working examples of being able to read from the temperature sensor?

Our code:

#define SHT31_ADDRESS (0x44 << 1)
i2cFd = I2CMaster_Open(MT3620_RDB_HEADER2_ISU0_I2C);

void SendTempTelemetry(void)
{
char telemetryBufferTemp[TELEMETRY_BUFFER_SIZE];
char telemetryBufferHum[TELEMETRY_BUFFER_SIZE];
uint8_t readData[6];

I2CMaster_Read(i2cFd, SHT31_ADDRESS, readData, sizeof(readData));

 if (readData[2] != CalcCRC8(&readData[0], 2))
       Log_Debug("ERROR: temperature CRC failed.\n");
    if (readData[5] != CalcCRC8(&readData[3], 2))
        Log_Debug("ERROR: humidity CRC failed.\n");

    uint16_t ST;
    ST = readData[0];
    ST = (uint16_t)(ST << 8);
    ST = (uint16_t)(ST | readData[1]);

    uint16_t SRH;
    SRH = readData[3];
    SRH = (uint16_t)(SRH << 8);
    SRH = (uint16_t)(SRH | readData[4]);

    float Temperature = NAN;
    float Humidity = NAN;
    Temperature = (float)ST * 175 / 0xffff - 45;
    Humidity = (float)SRH * 100 / 0xffff;

    int len0 = snprintf(telemetryBufferTemp, TELEMETRY_BUFFER_SIZE, "{\"Temperature\":%3.2f}", Temperature);
    if (len0 < 0 || len0 >= TELEMETRY_BUFFER_SIZE) {
        Log_Debug("ERROR: Cannot write temp telemetry to buffer.\n");
        return;
    }

    int len1 = snprintf(telemetryBufferHum, TELEMETRY_BUFFER_SIZE, "{\"Humidity\":%3.2f}", Humidity); 
    if (len1 < 0 || len1 >= TELEMETRY_BUFFER_SIZE) {
        Log_Debug("ERROR: Cannot write hum telemetry to buffer.\n");
        return;
    }

    SendTelemetry(telemetryBufferTemp);
    SendTelemetry(telemetryBufferHum);

}

In general temperature is always returning 124, and humidity 74 without any variation over time.

Azure Sphere
Azure Sphere
An Azure internet of things security solution including hardware, operating system, and cloud components.
168 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,202 questions
{count} votes

Accepted answer
  1. Adam 106 Reputation points
    2020-10-22T12:14:46.627+00:00

    Hi both.
    I've managed to get the results by using the grove.h libraries, which read from the UART ISU0 port instead of I2C - same as what is on the hacster.io pages above.
    However unsure why the I2C direct function itself shouldn't work, as I understand that didn't exist when those instructions were created.

    Regards,
    Adam

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.