I am using a soft I²C implementation to read a set of Sensirion SHT21 sensors. I am trying to figure out a way of having the sensors answer to see if they are actually connected to the device. I am using the Arduino which means all my code is C/C++
The libraries I am using are here.
The code used to read the sensors is the following:
#include <Ports.h>
#include <PortsSHT21.h>
//Define soft I²C channels for three sensors
SHT21 hsensor2 (2); // pins A1 and D5 - Sensor 2
//define variables for temp data
float h, t;
void setup() {}
void loop()
{
// Get data from sensor soft I²C
hsensor2.measure(SHT21::HUMI);
hsensor2.measure(SHT21::TEMP);
hsensor2.calculate(h, t);
float hum2 = (h);
float temp2 = (t);
}
The large code block is the code for the measure() function. Notice that it returns 0 at one point without performing a connReset(). This should be a way to detect a valid device such as …
or
or
Your code should be clearing h and t to 0 before making the read so that you can test for valid values. Like this…
If not then you could make (copy) your own version of the
measure()function that tests for valid return value inmeas[type]. You would need to setmeas[type]to a known invalid value before the read (such as0).You probably know that if you add a method to a library cpp then you also need to add a corresponding prototype to the .h otherwise the arduino will fail to compile your code.
.cpp
.h