Obtaining temparature using Rainbow Hat's Temperature Sensor and show it on Segment Display using Nxp Pico i.MX7D

Android Things

In our previous article we saw blinking of Rainbow Hat's Led you can find the tutorial from here Blinking 3 Rainbow Hat Led In Same Time Using Android Things Nxp Pico. In this tutorial, we are going to create a weather station using the temperature sensor of Rainbow Hat. 

To create a project please follow the tutorial given above.

Hardware Requirements: 

1. Nxp Pico Starter Kit

2. Rainbow Hat

Programming Example: 

We have to add this dependencies in build.gradle(Module: app).

implementation 'com.google.android.things.contrib:driver-rainbowhat:+'

here '+' means the latest available dependency.

next, to use the latest Rainbow Hat we need minimum sdk version 27 in build.gradle(Module: app) file.

minSdkVersion 27

next, we have to specify the app permission in AndroidManifest.xml file.

<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />

next, we have to import the driver in our MainActivity.java file

import com.google.android.things.contrib.driver.rainbowhat.RainbowHat;

Now, we will see the code.

This is our MainActivity.java

package com.sodiarc.rainbowhat2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.things.contrib.driver.bmx280.Bmx280;
import com.google.android.things.contrib.driver.ht16k33.AlphanumericDisplay;
import com.google.android.things.contrib.driver.ht16k33.Ht16k33;
import com.google.android.things.contrib.driver.rainbowhat.RainbowHat;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            AlphanumericDisplay segment = RainbowHat.openDisplay();
            segment.setBrightness(Ht16k33.HT16K33_BRIGHTNESS_MAX);
            segment.setEnabled(true);
            Bmx280 sensor = RainbowHat.openSensor();
            sensor.setTemperatureOversampling(Bmx280.OVERSAMPLING_1X);
            segment.display(sensor.readTemperature());
            sensor.close();
            segment.close();
        } catch (Exception e) {
            Log.d("Exception ", "DEVICE IS UNABLE TO PERFORM THE OPERATION");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

So, that's all for this article. Please comment below if you have any doubt. Thank You.

Android Things Article Author

Author

Hi folks, I am co-founder and CEO at Sodiarc Technologies Pvt. Ltd. I am working on android for more than 5 years. I have skills, which includes Android, iOS, PHP and lot more. If you have any project related queries we can discuss it briefly, mail me at info@sodiarc.com

Android Things Article Author

Author

Hi folks, I am CTO at Sodiarc Technologies Pvt. Ltd. I am programming enthusiast. I have skills, which includes Android, iOS, PHP and lot more. If you have any project related queries we can discuss it briefly, mail me at info@sodiarc.com

Android Things Article Author

Author

Hi folks, I am a content writer and a blogger. Writing is my passion. A B.Tech graduate in Information Technology, who is still paving the way for achieving the goals of her life.


Website Security Test