Open the SDK download link in the browser
https://help.aliyun.com/document_detail/42648.html?spm=a2c4g.11186623.6.576.696c3f691yLvtj
select JAVA SDK
Find the JAVA SDK DEMO download
Unzip the DEMO source code, import this maven project with eclipse, you need to connect to the external network to
configure Ali's Internet of Things devices, open
https://iot.console.aliyun.com/lk/vpc/instance?spm=5176.cniot.0.0 with a browser .2e8111faI7UO4P
Click Device Management -> Products
Create a new product,
add two attributes and an event
Edit the physical model of the product
After editing, click the publish button to publish the product
Add a new device to the
device The main connection parameters of the new device are:
These three parameters can be found in the device properties:
Device certificate
Modify the device configuration under JavaLinkKitDemo: device_id.json, open ThingTemplate.java and run the main function:
Run according to java application, need to
connect to the Internet to test the IoT device successfully connect to Alibaba Cloud
The management console device status is online.
Edit the attribute report on line 106 of ThingTemplate.java, add two custom attribute report programs and an event report program, and call them periodically.
//属性上报
handlePropertySet("MicSwitch", new ValueWrapper.IntValueWrapper(1));
int myi=0;
int tmpMicSwitch=0;
while(myi<100000) {
myi++;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(myi%2==1) {
handlePropertySet("MicSwitch", new ValueWrapper.IntValueWrapper(1));
handlePropertySet("hue", new ValueWrapper.IntValueWrapper(myi+100));
}else {
handlePropertySet("MicSwitch", new ValueWrapper.IntValueWrapper(0));
//事件上报
Map<String,ValueWrapper> values = new HashMap<>();
//values.put("eventValue",new ValueWrapper.IntValueWrapper(0));
OutputParams outputParams = new OutputParams(values);
//事件上报
handleEventSet("Offline_alarm",outputParams);
}
}
Re-run ThingTemplate.java to view the data reported by the IoT device in the Alibaba device log and the properties and events of the object model:
View object model data
View event
View log