Integrating Android Things with Alibaba Cloud IoT

Hardware Devices

Project Device List

NXP i.MX7D Pins

Device Wiring Diagram

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" />
  1. Add the “eclipse.paho.mqtt” repository to gradle
  • implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
  1. 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); } }
  1. 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); }
  1. 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

Source Code

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Redis vs Memcached 比較

Deploying Spring Boot App to AWS using CircleCI — Complete guide

Spring Boot Fullstack Blockchain Application With Hyperledger Fabric running on Kubernetes (Part 5)

Learning Ktor — Quick and Dirty Scratch Notes

Programming code displayed on a screen.