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.
NXP Pico i.MX7D full I/O interface document
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:
<uses-permission android:name="android.permission.INTERNET" />
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
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);
}
}
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);
}
/*
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);
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.
To get the source code for this project, visit the following GitHub link: https://github.com/iot-blog/aliyun-iot-android-things-nxp
Alibaba Clouder - February 3, 2019
Alibaba Clouder - August 6, 2019
GXIC - February 20, 2020
Alibaba Clouder - August 7, 2019
卓凌 - September 8, 2020
Alibaba Clouder - April 20, 2018
Provides secure and reliable communication between devices and the IoT Platform which allows you to manage a large number of devices on a single IoT Platform.
Learn MoreA cloud solution for smart technology providers to quickly build stable, cost-efficient, and reliable ubiquitous platforms
Learn MoreAlibaba Cloud offers an accelerated global networking solution that makes distance learning just the same as in-class teaching.
Learn MoreMigrate your Internet Data Center’s (IDC) Internet gateway to the cloud securely through Alibaba Cloud’s high-quality Internet bandwidth and premium Mainland China route.
Learn MoreMore Posts by GXIC