aliyun-iot-androidthings-nxp
谷歌 Android things 物联网硬件接入阿里云 IoT 物联网套件云端实战项目代码 https://github.com/iot-blog/aliyun-iot-android-things-nxp
1.硬件设备
android things 开发板 NXP Pico i.MX7D
NXP Pico i.MX7D 完整I/O接口文档
温湿度传感器 DHT12
DHT12 支持 I2C,淘宝有售
甲醛传感器 ZE08-CH2O
ZE08 支持 UART,淘宝有售
设备接线
aliyun_iot_androidthings_dht12_ze08
2.阿里云 IoT 物联网套件
2.1 开通阿里云 IoT 物联网套件
IoT物联网套件官网地址
2.2 创建高级版产品
产品属性定义
iot_product_property
2.3 设备端开发
a) 使用 Android Studio 创建 Android things 工程,添加网络权限
<uses-permission android:name="android.permission.INTERNET" />
复制代码
b) gradle 引入 eclipse.paho.mqtt
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
复制代码
c) DHT12Thermometer 通过 I2C 读取 DHT12 数据
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);
}
}
复制代码
d) Ze08CH2O 通过 UART 获取 ZE08 数据
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);
}
复制代码
e) 创建阿里云 IoT 连接,上报数据
/*
payload格式
{
"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);
复制代码
2.4 设备启动后,在阿里云 IoT 云端控制台查看实时数据
iot_device_status
物联网平台产品介绍详情:https://www.aliyun.com/product/iot/iot_instc_public_cn
阿里云物联网平台客户交流群
评论