共计 1943 个字符,预计需要花费 5 分钟才能阅读完成。
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 接口文档
温湿度传感器 DHT12DHT12
反对 I2C,淘宝有售
甲醛传感器 ZE08-CH2O
ZE08 反对 UART,淘宝有售
设施接线
2. 阿里云 IoT 物联网套件
2.1 开明阿里云 IoT 物联网套件
IoT 物联网套件官网地址
2.2 创立高级版产品
产品属性定义
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 云端控制台查看实时数据
物联网平台产品介绍详情:https://www.aliyun.com/product/iot/iot_instc_public_cn
阿里云物联网平台客户交换群
正文完