Reading temperature via DHT11 using Android Things

I use a raspberry pi3 sensor and DHT11 to monitor the temperature. I have the following contact positions

VCC for output: 2 Ground for output: 6 Output in GPIO: BCM22, i.e. No 15

The code I 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); } } 

The above code gives me "Nothing working" as the output.

Any suggestion where I could be wrong?

+7
android android-things sensor raspberry-pi3
source share
1 answer

You cannot read data from DHT11 using Raspberry Pi 3 with Android Things, since the DHT11 response pulse duration is from 26--28 us to 70 us, but the maximum RP3 frequency with AT GPIO is about 3 kHz, which means a pulse duration of about 300 μs . Take a look at this question.

0
source share

All Articles