赞
踩
研究了物联网,那么接下来你需要做的就是如何去远程控制,实现开发板的远程通讯交互,那么BAT最近也都陆续更新了自己的物联网平台系统,借助BAT的资源,我们能很快的建立起一个物联网开发环境,接下来我们手把手看看阿里云物联网平台的使用吧
1.登录到阿里云,在左上角的产品菜单中找到“产品”
2.从“产品”菜单中找到“物联网套件”
3.点击“管理控制台”,进入物联网控制台进行相关配置
4.首先创建一个产品
5.填写相关信息,在这里我们选择基础版,因为高级版虽然增加了很多功能,但是云端开发SDK截止目前为止尚未更新,无法使用云端SDK远程控制开发板
6.创建完成后,选择设备,点击“查看”,进入选择“前往管理”添加设备信息
7.点击“添加设备”
8.填写设备名称
9.记录备份相关秘钥信息,点击一键复制,保存到记事本,方便开发使用
10.回到产品信息页,进入“消息通讯”,点击“定义Topic类”添加
11.填写相关信息
12.接下来我们开发things程序,首先在项目的build.gradle文件中添加阿里云仓库地址
- maven {
- url "http://maven.aliyun.com/nexus/content/repositories/releases/"
- }
13.在app模块的build.gradle文件中添加相关组件
compile 'com.aliyun.alink.linksdk:public-channel-core:0.2.1'
14.集成
- /**
- * Created by cc_want on 2018/4/26.
- */
- public class AliyunIoT {
- private String TAG ="AlibabaIOT";
-
- public static final String productKey = "<填写>"; //这个是设备模版productkey
- public static final String deviceName = "<填写>";//这个是设备名称
- public static final String deviceSecret = "<填写>";//这个是设备秘钥
-
- public static final String ACTION_SYS_SERVICE_CMD = String.format(Locale.CHINESE,"/%s/%s/%s",productKey,deviceName,"cmd");
-
- private IConnectionStateListener mConnectionStateListener;
- private IOnPushListener mIOnPushListener;
- private IOnSubscribeListener mSysServiceListener = new IOnSubscribeListener() {
- @Override
- public void onSuccess(String s) {
-
- }
-
- @Override
- public void onFailed(String s, AError aError) {
-
- }
-
- @Override
- public boolean needUISafety() {
- return true;
- }
- };
- AliyunIoT(){
-
- }
- private static AliyunIoT mIntance;
- public static AliyunIoT getIntance() {
- if (mIntance == null) {
- mIntance = new AliyunIoT();
- }
- return mIntance;
- }
- public void init(Context context, IConnectionStateListener listener) {
- mConnectionStateListener = listener;
- ALog.setLevel(ALog.LEVEL_DEBUG);
- MqttConfigure.mqttHost = "ssl://" + productKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com:1883";
-
- MqttInitParams initParams = new MqttInitParams(productKey, deviceName, deviceSecret);
- PersistentNet.getInstance().init(context, initParams);
-
- PersistentEventDispatcher.getInstance().registerOnTunnelStateListener(mConnectionStateListener, true);// 注册监听
- }
- public void register() {
- PersistentEventDispatcher.getInstance().registerOnPushListener(mIOnPushListener, true);
- PersistentNet.getInstance().subscribe(ACTION_SYS_SERVICE_CMD, mSysServiceListener);
- }
- public void setIOnPushListener(IOnPushListener listener) {
- this.mIOnPushListener = listener;
- }
- public void close() {
- PersistentEventDispatcher.getInstance().unregisterOnTunnelStateListener(mConnectionStateListener);//取消监听
- PersistentEventDispatcher.getInstance().unregisterOnPushListener(mIOnPushListener);
- PersistentNet.getInstance().unSubscribe(ACTION_SYS_SERVICE_CMD, mSysServiceListener);
- }
- }
在MainActivity中初始化
- AliyunIoT.getIntance().init(this, this);
- AliyunIoT.getIntance().setIOnPushListener(this);
实现IOnPushListener接口回调方法,接收处理阿里云物联网平台的消息
- /**
- * 下推消息回调接口
- *
- * @param topic
- * @param data 下推的消息内容
- */
- @Override
- public void onCommand(String topic, String data) {
- Log.e(TAG, "接收: topic=" + topic + " data=" + data);
- JSONObject json = JSON.parseObject(data);
- }
- /**
- * 根据method过滤消息
- *
- * @param topic : 本次下推消息的命令名称
- * @return : 若返回true,则onCommand被调用,返回false,则onCommand不会被调用
- */
- @Override
- public boolean shouldHandle(String topic) {
- Log.e(TAG, "接收 shouldHandle : topic=" + topic);
- return true;
- }
15.服务端集成
在开发板物联网平台使用中,我们常需要远程控制,所以我们需要有一个可以远程控制的程序,这里阿里云物联网平台提供了java SDK方便我们调用,但需要注意的是,目前java SDK不支持高级版,这也就是建议使用基础版的原因,在本教程中,服务端开发了一个Android app,装在手机上,方便我们测试
注意:这里使用的是普通版,高级版请查看另外一篇文章
Android Things阿里云物联网平台的使用(高级版)
- compile 'com.aliyun:aliyun-java-sdk-iot:4.0.0'
- compile 'com.aliyun:aliyun-java-sdk-core:3.2.10'
服务端代码如下:
- /**
- * 阿里云物联网平台服务端
- * Created by cc_want on 2018/4/11.
- */
- public class AliyunIOT {
- private String TAG ="AlibabaIOT";
-
-
- public static final String accessKey = "<填写>";
- public static final String accessSecret = "<填写>";
-
- public static final String productKey = "<填写>"; //这个是设备模版productkey
- public static final String ACTION_DRIVE = "/<填写>/<填写deviceName>/cmd";
-
- private DefaultAcsClient mClient;
-
- private static AliyunIOT mIntance;
-
- public static AliyunIOT getIntance(){
- if(mIntance == null){
- mIntance = new AliyunIOT();
- }
- return mIntance;
- }
- public void init(){
- try {
- DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
- } catch (ClientException e) {
- e.printStackTrace();
- }
- IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
- mClient = new DefaultAcsClient(profile); //初始化SDK客户端
- }
- public void open(int lightNo){
- //{"LightStatus":1}
- JSONObject json = new JSONObject();
- try {
- json.put("LightStatus",1);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- new Drive(json.toString()).execute();
- }
-
- class Drive extends AsyncTask<String,Integer,String>{
-
- String mMsg;
- Drive(String msg){
- mMsg = msg;
- }
- @Override
- protected String doInBackground(String... params) {
- PubRequest request = new PubRequest();
- request.setProductKey(productKey);
- //此处必须用Base64加密
- request.setMessageContent(Base64.encodeToString(mMsg.getBytes(),Base64.DEFAULT));
- request.setTopicFullName(ACTION_DRIVE);
- request.setQos(0); //目前支持QoS0和QoS1
- //如果不加,则报错,官方文档没写
- //Specified signature is not matched with our calculation
- request.setMethod(MethodType.POST);
- try {
- PubResponse response = mClient.getAcsResponse(request);
- Log.d(TAG,"success:"+response.getSuccess());
- Log.d(TAG,"error:"+response.getErrorMessage());
- } catch (ClientException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。