This article describes how to connect Google Android Things hardware to IoT Platform. An indoor air test project is used in this example.

Hardware

  • Hardware list for the project

    The following table describes the hardware used in the indoor air test project.

    Hardware Picture Remarks

    NXP Pico i.MX7D

    Development board

    Android Things

    Based on Android things 1.0.

    For more information, see the Developer Guide for NXP Pico i.MX7D.

    Note You can also use Raspberry Pi instead. For more information, see Control Raspberry Pi servers from IoT Platform.

    DHT12

    Temperature and humidity sensor

    Android Things Supports the inter-integrated circuit (I2C) data communication method.

    ZE08-CH2O

    Formaldehyde detection sensor

    Android Things Supports the universal asynchronous receiver-transmitter (UART) data communication method.
  • Hardware connection diagram
    Android Things
    • Connect the SCL (clock line) and SDA (data line) pins of the temperature and humidity sensor (DHT12) to the I2C SCL and SDA pins of the development board.
    • Connect the TXD (transmit data) pin of the formaldehyde detection sensor (ZE08-CH2O) to the RXD (receive data) pin of the development board, and connect the RXD pin of ZE08-CH2O to the TXD pin of the development board.

Create a product and a device in the IoT Platform console

  1. Log on to the IoT Platform console.
  2. Create a product.
    1. In the left-side navigation pane, choose Devices > Products.
    2. On the Products page, click Create Product to create a product. For more information, see Create a product.
  3. Create a TSL model.
    1. On the page that appears after the product is created, click Create TSL.
    2. On the Product Details page, click the Define Feature tab. Click Edit Draft and then Add Self-defined Feature.
    3. Add the properties described in the following table.
      Property Identifier Data Type Valid value Description
      Temperature temperature float -50 to 100 The temperature data collected by the temperature and humidity sensor DHT12.
      Humidity humidity float 0 to 100 The humidity data collected by the temperature and humidity sensor DHT12.
      Formaldehyde concentration ch2o double 0 to 3 The formaldehyde concentration collected by the formaldehyde detection sensor ZE08-CH2O.
    4. Click Release online to publish the TSL model.
  4. Create a device.

    On the Devices page, click Add Device to add a device to the product. For more information, see Create a device.

Develop an Android Things device

  1. Use Android Studio to create an Android Things project and add the network permission.
    <uses-permission android:name="android.permission.INTERNET" />
  2. Add eclipse.paho.mqtt to the Gradle file.
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
  3. Configure the device to read data from the temperature and humidity sensor DHT12 by using 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. Configure the device to read data from the formaldehyde detection sensor ZE08-CH2O by using 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. Connect the device to IoT Platform and report 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 pubTopYourPc = "/sys/${YourProductKey}/${YourDeviceName}/thing/event/property/post";
    
    mqttClient.publish(pubTopic, message);

You can visit aliyun-iot-androidthings-nxp on GitHub to download the complete sample project.

View real-time data of the device

After the device is started, log on to the IoT Platform console. On the Device Details page, view the real-time property data of the device in the Status section.

Android Things