DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Integrating Android Things With Alibaba Cloud IoT

Integrating Android Things With Alibaba Cloud IoT

Let's build a functional formaldehyde and temperature sensor!

Leona Zhang user avatar by
Leona Zhang
·
Nov. 20, 18 · Tutorial
Like (1)
Save
Tweet
Share
7.48K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will show you how you can connect Android Things to your Internet of Things (IoT) network easily with Alibaba Cloud IoT Platform. We will build a functional formaldehyde and temperature sensor for this project.

Hardware Devices

Project Device List

Project Device List android iot cloud

NXP i.MX7D Pins

NXP Pico i.MX7D full I/O interface document:

https://developer.android.com/things/hardware/imx7d#io-pinout

Device Wiring Diagram

Android Things Device Wiring Diagram

Alibaba Cloud IoT Configuration

After setting up the hardware, it's time to configure the software on Alibaba Cloud IoT Platform.

First, navigate to the IoT console and activate Alibaba Cloud IoT. Create an advanced product and add product attribute definition:

Image title

Alibaba Cloud IoT Configuration

Android Things Device Development

  1. Create an Android Things project using Android Studio, and grant network permissions
    <uses-permission android:name="android.permission.INTERNET" />

  2. Add the "eclipse.paho.mqtt" repository to gradle
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'

  3. Read DHT12 data through I2C
    private void readDataFromI2C() {
    
            try {
    
                byte[] data = new byte[5];
                i2cDevice.readRegBuffer(0x00, data, data.length);
    
                // check data
                if ((data[0] + data[1] + data[2] + data[3]) % 256 != data[4]) {
                    humidity = temperature = 0;
                    return;
                }
                // humidity data
                humidity = Double.valueOf(String.valueOf(data[0]) + "." + String.valueOf(data[1]));
                Log.d(TAG, "humidity: " + humidity);
                // temperature data
                if (data[3] < 128) {
                    temperature = Double.valueOf(String.valueOf(data[2]) + "." + String.valueOf(data[3]));
                } else {
                    temperature = Double.valueOf("-" + String.valueOf(data[2]) + "." + String.valueOf(data[3] - 128));
                }
    
                Log.d(TAG, "temperature: " + temperature);
    
            } catch (IOException e) {
                Log.e(TAG, "readDataFromI2C error " + e.getMessage(), e);
            }
    
        }

  4. Obtain Ze08CH2O data through UART
    try {
                    // data buffer
                    byte[] buffer = new byte[9];
    
                    while (uartDevice.read(buffer, buffer.length) > 0) {
    
                        if (checkSum(buffer)) {
                            ppbCh2o = buffer[4] * 256 + buffer[5];
                            ch2o = ppbCh2o / 66.64 * 0.08;
                        } else {
                            ch2o = ppbCh2o = 0;
                        }
                        Log.d(TAG, "ch2o: " + ch2o);
                    }
    
                } catch (IOException e) {
                    Log.e(TAG, "Ze08CH2O read data error " + e.getMessage(), e);
                }

  5. Create an Alibaba Cloud IoT connection and report the data
    /*
    payload format
    {
      "id": 123243,
      "params": {
        "temperature": 25.6,
        "humidity": 60.3,
        "ch2o": 0.048
      },
      "method": "thing.event.property.post"
    }
    */
    MqttMessage message = new MqttMessage(payload.getBytes("utf-8"));
    message.setQos(1);
    
    String pubTopic = "/sys/" + productKey + "/" + deviceName + "/thing/event/property/post";
    
    mqttClient.publish(pubTopic, message);


Real-Time Data on Cloud Console

After the device is started, you can view the real-time data of the device on the Alibaba Cloud IoT console, Device Management -> Running Status.

Image title

Source Code

To get the source code for this project, visit the following GitHub page.

IoT Alibaba Cloud Android Things Cloud Android (robot)

Published at DZone with permission of Leona Zhang. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Brief Overview of the Spring Cloud Framework
  • ChatGPT Prompts for Agile Practitioners
  • Load Balancing Pattern
  • Top 5 Node.js REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: