1. Hardware devices












1.1 List of project equipment












The device name

The picture

note

Buy address

NXP Pico I. MX7D development board


Android Things 1.0

Google developers Conference giveaways can be replaced with raspberry PI

DHT12Temperature and humidity sensor


I2C data communication mode

taobao

ZE08-CH2OFormaldehyde detection sensor


UART data communication mode

taobao






1.2 INTRODUCTION to the stitching of NXP I. MX7D development board









Complete I/O interface documentation













1.3 Schematic diagram of device Wiring



















2. Aliyun IoT Kit












2.1 Open aliyun IoT kit









IoT Suite official website address






2.2 Creating advanced Products















The property name

identifier

The data type

describe

The temperature

temperature

float

DHT12 sensor acquisition

humidity

humidity

float

DHT12 sensor acquisition

Formaldehyde concentration

ch2o

double

ZE08 sensor collection













2.3 Android Things device development












Copy the code

  1. <uses-permission android:name=”android.permission.INTERNET” />









Copy the code

  1. Implementation ‘org. Eclipse. Paho: org. Eclipse paho. Client. Mqttv3:1.2.0’



Copy the code

  1. private void readDataFromI2C() {
  2.         try {
  3.             byte[] data = new byte[5];
  4.             i2cDevice.readRegBuffer(0x00, data, data.length);
  5.             // check data
  6. if ((data[0] + data[1] + data[2] + data[3]) % 256 ! = data[4]) {
  7.                 humidity = temperature = 0;
  8.                 return;
  9.             }
  10.             // humidity data
  11.             humidity = Double.valueOf(String.valueOf(data[0]) + “.” + String.valueOf(data[1]));
  12.             Log.d(TAG, “humidity: ” + humidity);
  13.             // temperature data
  14.             if (data[3] < 128) {
  15.                 temperature = Double.valueOf(String.valueOf(data[2]) + “.” + String.valueOf(data[3]));
  16.             } else {
  17.                 temperature = Double.valueOf(“-” + String.valueOf(data[2]) + “.” + String.valueOf(data[3] – 128));
  18.             }
  19.             Log.d(TAG, “temperature: ” + temperature);
  20.         } catch (IOException e) {
  21.             Log.e(TAG, “readDataFromI2C error ” + e.getMessage(), e);
  22.         }
  23.     }



Copy the code

  1. try {
  2.                 // data buffer
  3.                 byte[] buffer = new byte[9];
  4.                 while (uartDevice.read(buffer, buffer.length) > 0) {
  5.                     if (checkSum(buffer)) {
  6.                         ppbCh2o = buffer[4] * 256 + buffer[5];
  7. Ch2o = ppbCh2o / 66.64 * 0.08;
  8.                     } else {
  9.                         ch2o = ppbCh2o = 0;
  10.                     }
  11.                     Log.d(TAG, “ch2o: ” + ch2o);
  12.                 }
  13.             } catch (IOException e) {
  14.                 Log.e(TAG, “Ze08CH2O read data error ” + e.getMessage(), e);
  15.             }



Copy the code

  1. / *
  2. The payload format
  3. {
  4.   “id”: 123243,
  5.   “params”: {
  6. “Temperature” : 25.6,
  7. “Humidity” : 60.3,
  8. “Ch2o” : 0.048
  9.   },
  10.   “method”: “thing.event.property.post”
  11. }
  12. * /
  13. MqttMessage message = new MqttMessage(payload.getBytes(“utf-8”));
  14. message.setQos(1);
  15. String pubTopic = “/sys/” + productKey + “/” + deviceName + “/thing/event/property/post”;
  16. mqttClient.publish(pubTopic, message);









2.4 Real-time data on the cloud console









Device Management -> Running Status










3. The source code









Github.com/iot-blog/al…