7
votes

I am using Raspberry pi3 and DHT11 sensor for temperature monitoring project. I have following pin positions

VCC to pin no : 2 Ground to pin no : 6 Output to GPIO : BCM22 i.e pin no 15

Code that I have used:

public class WeatherStationActivity extends Activity {

    private Handler mHandler = new Handler();
    private TextView mTxtStatus;

    private PeripheralManagerService service = new PeripheralManagerService();

    private Gpio tempGpio;

    private int i = 0;
    int[] dht11_dat = {0, 0, 0, 0, 0};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("Weather station", "Started Weather Station");
        setContentView(R.layout.activity_main);
        mTxtStatus = (TextView) findViewById(R.id.txtStatus);
        try {
            tempGpio = service.openGpio("BCM22");
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (i == 10) {
                        handler.removeCallbacks(this);
                    } else {
                        getTemp();
                        handler.postDelayed(this, 5000);
                    }
                    i++;
                }
            }, 5000);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    private void getTemp() {
        boolean laststate = false;
        try {
            laststate = tempGpio.getValue();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int j = 0;
        final int MAXTIMINGS = 85;

        dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;


        try {

            tempGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
//            tempGpio.setActiveType(Gpio.ACTIVE_LOW);
            tempGpio.setValue(false);
//            Thread.sleep(18);
            TimeUnit.MILLISECONDS.sleep(18);
//            tempGpio.setActiveType(Gpio.ACTIVE_HIGH);
//            tempGpio.setActiveType(Gpio.ACTIVE_HIGH);
            tempGpio.setValue(true);
            TimeUnit.MICROSECONDS.sleep(40);
            tempGpio.setDirection(Gpio.DIRECTION_IN);


           /* tempGpio.setActiveType(Gpio.ACTIVE_HIGH);
            tempGpio.setValue(true);*/
//            tempGpio.setValue(true);
            StringBuilder value = new StringBuilder();

            for (int i = 0; i < MAXTIMINGS; i++) {
                int counter = 0;
                while (tempGpio.getValue() == laststate) {
                    counter++;
                    TimeUnit.MICROSECONDS.sleep(1);
                    if (counter == 255) {
                        break;
                    }
                }
                laststate = tempGpio.getValue();
                mTxtStatus.append("\nLast State of Sensor " + laststate);
                if (counter == 255) {
                    break;
                }
                //* ignore first 3 transitions *//*
                if ((i >= 4) && (i % 2 == 0)) {
                    //* shove each bit into the storage bytes *//*
                    dht11_dat[j / 8] <<= 1;
                    if (counter > 16) {
                        dht11_dat[j / 8] |= 1;
                    }
                    j++;
                }
            }
            // check we read 40 bits (8bit x 5 ) + verify checksum in the last
            // byte
            if ((j >= 40) && checkParity()) {
                value.append(dht11_dat[2]).append(".").append(dht11_dat[3]);
                Log.i("Logger", "temperature value readed: " + value.toString());
                mTxtStatus.append("\nTemp " + value.toString());
            } else {
                mTxtStatus.append("\nNothing is working ");
                Log.i("Logger", "Nothing is working ");
            }

        } catch (IOException e) {

            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private boolean checkParity() {
        return dht11_dat[4] == (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3] & 0xFF);
    }
}

Above code is giving me "Nothing is working" as output.

Any suggestion where I might be doing wrong?

1
can you pls format it properly?Willi Mentzel
@WilliMentzel done!. I pulled that code from my sample app. can you please take a look and point me to the problem here?Manoj
I cannot help you with this specific problem, but I helped you to present your question in a manner that people are more likely to answer/upvote it :)Willi Mentzel

1 Answers

0
votes

You can't read data from DHT11 using Raspberry Pi 3 with Android Things because duration of DHT11 response pulses is from 26-28 us to 70 us, but max frequency of RP3 with AT GPIO is around 3kHz, which means around 300 us pulse duration. Take a look at answers to this question.