since a couple of days I'm struggling with a problem in reading temperature/humidity data from sensor (DHT11) using Android Things kit (i.MX7D). I've googled many examples and all of them were made using Arduino, Raspberry Pi or STM's (so C/C++), but none for i.MX7D and Java.
My problem is that I cannot read real values of temperature/humidity, because all I get from the sensor is only a boolean value indicating HIGH/LOW state. I haven't found any library for this sensor that would somehow help to convert it to real degrees/percent values.
Do you know if it's even possible to obtain these real values using the hardware that I have? If it is, could you please give me a hint or show some code how to do that, so that I can finally make some progress? I will much appreciate all kind of help.
Here is my piece of code:
import android.app.Activity;
import android.os.Bundle;
import com.google.android.things.pio.Gpio;
import com.google.android.things.pio.GpioCallback;
import com.google.android.things.pio.PeripheralManager;
public class MainActivity extends Activity {
private Gpio gpio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PeripheralManager manager = PeripheralManager.getInstance();
try {
gpio = manager.openGpio("GPIO2_IO05");
configureInput(gpio);
configureOutput(gpio);
} catch (IOException e) {
e.printStackTrace();
}
}
private GpioCallback gpioCallback = new GpioCallback() {
@Override
public boolean onGpioEdge(Gpio gpio) {
try {
if (gpio.getValue()) {
System.out.println("high");
} else {
System.out.println("low");
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
@Override
public void onGpioError(Gpio gpio, int error) {
System.out.println(gpio + ": Error event " + error);
}
};
public void configureInput(Gpio gpio) throws IOException {
gpio.setDirection(Gpio.DIRECTION_IN);
gpio.setActiveType(Gpio.ACTIVE_HIGH);
gpio.setEdgeTriggerType(Gpio.EDGE_BOTH);
gpio.registerGpioCallback(gpioCallback);
}
public void configureOutput(Gpio gpio) throws IOException {
gpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_HIGH);
gpio.setActiveType(Gpio.ACTIVE_LOW);
gpio.setValue(true);
}
}
It's not possible to read from that sensor from Android things. It uses a one wire protocol similar to I2C but the speed required for the GPIO port to be able to read it is too fast for Android things.
As suggested, you can read it using an Arduino and then connect the Arduino as I2C slave, of you can use a different temperature and humidity sensor, like the BME280 which communicate via I2C and it's still reasonably cheap