Android Studio 2022 + MQTT连接阿里云

一、概述

本项目制作了一个手机端APP,一个遥控器,通过MQTT协议连接阿里云,实现对下位机的操控,并监控下位机的状态。使用Paho接入物联网平台,参考阿里云Paho-MQTT Android接入示例。鉴于阿里云示例项目使用的android studio版本较老,且恰逢android studio 2022.3.1版本发布,本项目就使用了最新的版本完成该项目。

二、开发步骤

1、工程创建

工程创建和之前的版本没有太大区别,这里就不详述了。

2、导入库文件

这里使用了两个库文件,一个是org.eclipse.paho.client.mqttv3-1.2.5.jar,另一个是org.eclipse.paho.android.service-1.1.1.jar。实际上,后面这个库可以不用的,这里用它的原因是mqtt库不支持自动重连,而后面这个库支持。导入方法有两种:一是如下图所示,直接下载文件包粘贴到app下的libs文件夹下,右键导入即可;另一种就是在app模块的build.gradle文件中implementation,效果是一样的。网络不好的情况还是建议使用第一种方法。

 3、添加依赖

在工程的settings.gradle中添加Paho仓库地址。

 4、添加support-v4对应的兼容包

 5、开启网络权限,添加服务

 6、代码编写

6.1布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#c0ffff"
    tools:context=".MainActivity">

    <Space
        android:layout_width="match_parent"
        android:layout_height="80dp"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="4">

        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="60dp"
            android:layout_height="match_parent"
            android:text="状态:"
            android:textSize="30dp"
            android:layout_weight="1"/>

        <TextView
            android:id="@+id/textView"
            android:layout_width="60dp"
            android:layout_height="match_parent"
            android:background="#FFFFFF"
            android:gravity="center"
            android:text=" "
            android:textSize="30dp"
            android:layout_weight="1"/>

        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

    </LinearLayout>


    <Space
        android:layout_width="match_parent"
        android:layout_height="80dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="5">

        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/btn_open"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="25dp"
            android:text="开门" />

        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>


        <Button
            android:id="@+id/btn_close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25dp"
            android:text="关门" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

    </LinearLayout>

    <Space
        android:layout_width="match_parent"
        android:layout_height="30dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:weightSum="5">

        <Button
            android:id="@+id/btn_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25dp"
            android:text="暂停" />

    </LinearLayout>

    <Space
        android:layout_width="match_parent"
        android:layout_height="30dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="5">

        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/btn_lock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25dp"
            android:text="锁门" />

        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>


        <Button
            android:id="@+id/btn_unlock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="25dp"
            android:text="解锁" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

    </LinearLayout>

</LinearLayout>

6.1运行代码

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigInteger;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**************************************************************/
public class MainActivity extends AppCompatActivity {

    /* MQTT客户端对象 */
    MqttAndroidClient mqttAndroidClient;
    final private String TAG = "AiotMqtt";
    /* 设备三元组信息 */
    final private String PRODUCTKEY = "";//填自己的
    final private String DEVICENAME = "";//填自己的
    final private String DEVICESECRET = "";//填自己的

    /* 自动Topic, 用于上报消息 */
    final private String PUB_TOPIC = "/" + PRODUCTKEY + "/" + DEVICENAME + "/user/update";
    /* 自动Topic, 用于接受消息 */
    final private String SUB_TOPIC = "/" + PRODUCTKEY + "/" + DEVICENAME + "/user/get";

    /* 阿里云Mqtt服务器域名 */
    final String host = "tcp://" + PRODUCTKEY + ".iot-as-mqtt.cn-shanghai.aliyuncs.com:1883";
    private String clientId;
    private String userName;
    private String passWord;

    /************************************************************/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /* MQTT初始化 */
        mqttInit();
        /* MQTT连接 */
        mqttConnect();
        /* 通过按键发布消息 */
        Button[] arrButton = new Button[5];
        buttonInit(arrButton);
        buttonHandler(arrButton,Color.BLUE, Color.RED);

    }/* onCreate */

    /************************************************************
     *
     ************************************************************/

    /* MQTT初始化函数 */
    private void mqttInit() {
        /* 获取Mqtt建连信息clientId, username, password */
        AiotMqttOption aiotMqttOption = new AiotMqttOption().getMqttOption(PRODUCTKEY, DEVICENAME, DEVICESECRET);
        if (aiotMqttOption == null) {
            Log.e(TAG, "device info error");
        } else {
            clientId = aiotMqttOption.getClientId();
            userName = aiotMqttOption.getUsername();
            passWord = aiotMqttOption.getPassword();
        }
        /* 创建MqttAndroidClient对象, 并设置回调接口 */
        mqttAndroidClient = new MqttAndroidClient(getApplicationContext(), host, clientId);
        mqttAndroidClient.setCallback(new MqttCallback() {
            @Override
            public void connectionLost(Throwable cause) {
                Log.i(TAG, "connection lost");
            }

            @Override
            public void messageArrived(String topic, MqttMessage message) throws Exception {
                Log.i(TAG, "topic: " + topic + ", msg: " + new String(message.getPayload()));
                String payload = new String(message.getPayload());
                TextView recText = findViewById(R.id.textView);
                recText.setText(jsonParser(payload));
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken token) {

                Log.i(TAG, "msg delivered");
            }
        });
    }

    /************************************************************/

    /* MQTT连接函数 */
    private void mqttConnect() {

        /* 创建MqttConnectOptions对象并配置username和password */
        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setUserName(userName);
        mqttConnectOptions.setPassword(passWord.toCharArray());

        /* Mqtt建连 */
        try {
            mqttAndroidClient.connect(mqttConnectOptions,null, new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    Log.i(TAG, "connect succeed");

                    subscribeTopic(SUB_TOPIC);
                }

                @Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    Log.i(TAG, "connect failed");
                }
            });

        } catch (MqttException e) {
            e.printStackTrace();
        }
    } /* mqttConnect */

    /************************************************************/

    /**
     * 订阅特定的主题
     * @param topic mqtt主题
     */
    public void subscribeTopic(String topic) {
        try {
            mqttAndroidClient.subscribe(topic, 0, null, new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    Log.i(TAG, "subscribed succeed");
                }

                @Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    Log.i(TAG, "subscribed failed");
                }
            });

        } catch (MqttException e) {
            e.printStackTrace();
        }
    } /* subscribeTopic */

    /************************************************************/

    /**
     * 向默认的主题/user/update发布消息
     * @param payload 消息载荷
     */
    public void publishMessage(String payload) {
        try {
            if (!mqttAndroidClient.isConnected()) {
                mqttAndroidClient.connect();
            }

            MqttMessage message = new MqttMessage();
            message.setPayload(payload.getBytes());
            message.setQos(0);
            mqttAndroidClient.publish(PUB_TOPIC, message,null, new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    Log.i(TAG, "publish succeed!");
                }

                @Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    Log.i(TAG, "publish failed!");
                }
            });
        } catch (MqttException e) {
            Log.e(TAG, e.toString());
            e.printStackTrace();
        }
    } /* publishMessage */

    /************************************************************/

    /**
     * MQTT建连选项类,输入设备三元组productKey, deviceName和deviceSecret, 生成Mqtt建连参数clientId,username和password.
     */
    static class AiotMqttOption {
        private String username = "";//填自己的
        private String password = "";//填自己的
        private String clientId = "";//填自己的

        public String getUsername() { return this.username;}
        public String getPassword() { return this.password;}
        public String getClientId() { return this.clientId;}

        /**
         * 获取Mqtt建连选项对象
         * @param productKey 产品秘钥
         * @param deviceName 设备名称
         * @param deviceSecret 设备机密
         * @return AiotMqttOption对象或者NULL
         */
        public AiotMqttOption getMqttOption(String productKey, String deviceName, String deviceSecret) {
            if (productKey == null || deviceName == null || deviceSecret == null) {
                return null;
            }

            try {
                String timestamp = Long.toString(System.currentTimeMillis());

                // clientId
                this.clientId = productKey + "." + deviceName + "|timestamp=" + timestamp +
                        ",_v=paho-android-1.0.0,securemode=2,signmethod=hmacsha256|";

                // userName
                this.username = deviceName + "&" + productKey;

                // password
                String macSrc = "clientId" + productKey + "." + deviceName + "deviceName" +
                        deviceName + "productKey" + productKey + "timestamp" + timestamp;
                String algorithm = "HmacSHA256";
                Mac mac = Mac.getInstance(algorithm);
                SecretKeySpec secretKeySpec = new SecretKeySpec(deviceSecret.getBytes(), algorithm);
                mac.init(secretKeySpec);
                byte[] macRes = mac.doFinal(macSrc.getBytes());
                password = String.format("%064x", new BigInteger(1, macRes));
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

            return this;
        }
    } /* class AiotMqttOption */

    /************************************************************/

    /* 按键初始化函数 */
    private void buttonInit(Button[] buttons){
        for (int i = 0; i < buttons.length; ++i) {
            switch (i){
                case 0:
                    buttons[i] = findViewById(R.id.btn_open);
                    break;
                case 1:
                    buttons[i] = findViewById(R.id.btn_close);
                    break;
                case 2:
                    buttons[i] = findViewById(R.id.btn_pause);
                    break;
                case 3:
                    buttons[i] = findViewById(R.id.btn_lock);
                    break;
                case 4:
                    buttons[i] = findViewById(R.id.btn_unlock);
                    break;
            }

        }
    } /* buttonInit */

    /************************************************************/

    @SuppressLint("ClickableViewAccessibility")
    private void buttonHandler(Button[] button, int fColor, int bColor) {
        for (int i = 0; i < button.length; ++i) {
            int j = i;
            button[i].setBackgroundColor(fColor);
            button[i].setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                        button[j].setBackgroundColor(bColor);
                    } else {
                        button[j].setBackgroundColor(fColor);
                        switch (j) {
                            case 0:
                                publishMessage("{\"state\":\"OPEN\"}");
                                break;
                            case 1:
                                publishMessage("{\"state\":\"CLOSE\"}");
                                break;
                            case 2:
                                publishMessage("{\"state\":\"PAUSE\"}");
                                break;
                            case 3:
                                publishMessage("{\"state\":\"LOCK\"}");
                                break;
                            case 4:
                                publishMessage("{\"state\":\"UNLOCK\"}");
                                break;
                        }
                    }
                    return false;
                }
            });
        }
    }/* buttonHandler */

    /************************************************************/

    /* json解析函数 */
    private String jsonParser(String strIn){
        String strOut = null;
        /* 解析json数据 */
        try {
            JSONObject jsonObject = new JSONObject(strIn);
            strOut = jsonObject.getString("state");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return strOut;
    }

} /* MainActivity */

  • 3
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用SIM7000C通过MQTT连接阿里云,你需要遵循以下步骤: 1.注册阿里云账号并创建一个IoT实例。 2.在IoT实例中创建一个设备,并获取设备的三元组(ProductKey、DeviceName和DeviceSecret)。 3.在阿里云控制台中创建一个Topic,以便在设备和应用程序之间传输消息。 4.获取SIM7000C的MQTT库,例如PubSubClient,并将其添加到你的Arduino IDE中。 5.在Arduino IDE中编写代码,以便SIM7000C可以将数据发布到阿里云,并且可以从阿里云接收数据。 下面是一些示例代码,可以将其用作参考: ```C++ #include <Wire.h> #include <SoftwareSerial.h> #include <PubSubClient.h> //引入mqtt库 //SIM7000C连接的RX TX引脚 #define SIM_TX 8 #define SIM_RX 9 SoftwareSerial SerialSIM7000(SIM_TX, SIM_RX); //阿里云iot的三元组 #define PRODUCTKEY "xxxxxxxxxxxxxxxx" #define DEVICENAME "xxxxxxxxxxxxx" #define DEVICESECRET "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" //WiFi的SSID和密码 #define WIFI_SSID "your_wifi_ssid" #define WIFI_PWD "your_wifi_password" //MQTT服务器的地址和端口号 #define MQTT_SERVER "xxxxxxxxx.iot-as-mqtt.cn-shanghai.aliyuncs.com" #define MQTT_PORT 1883 //MQTT的Topic #define MQTT_TOPIC "/sys/" PRODUCTKEY "/" DEVICENAME "/thing/event/property/post" //MQTT的客户端ID #define MQTT_CLIENTID "mqtt_clientid" //MQTT的用户名和密码 #define MQTT_USERNAME PRODUCTKEY "&" DEVICENAME #define MQTT_PASSWORD DEVICESECRET //创建一个PubSubClient的实例 WiFiClient espClient; PubSubClient mqttClient(espClient); void setup() { Serial.begin(9600); SerialSIM7000.begin(115200); delay(1000); //连接SIM7000C SerialSIM7000.println("AT"); delay(1000); SerialSIM7000.println("AT+CGATT=1"); delay(5000); SerialSIM7000.println("AT+CGDCONT=1,\"IP\",\"your_apn\""); delay(1000); SerialSIM7000.println("AT+CSTT=\"your_apn\",\"your_username\",\"your_password\""); delay(5000); SerialSIM7000.println("AT+CIICR"); delay(5000); SerialSIM7000.println("AT+CIFSR"); delay(1000); //连接WiFi WiFi.begin(WIFI_SSID, WIFI_PWD); while (WiFi.status() != WL_CONNECTED) { delay(1000); } //连接MQTT服务器 mqttClient.setServer(MQTT_SERVER, MQTT_PORT); mqttClient.setCallback(callback); while (!mqttClient.connected()) { if (mqttClient.connect(MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD)) { Serial.println("MQTT connected"); } else { Serial.println("MQTT failed"); delay(5000); } } } void loop() { mqttClient.loop(); //从阿里云接收消息 if (mqttClient.connected()) { mqttClient.subscribe(MQTT_TOPIC); } //向阿里云发布数据 String payload = "{\"id\":123,\"temperature\":25,\"humidity\":50}"; mqttClient.publish(MQTT_TOPIC, payload.c_str()); delay(5000); } //MQTT的回调函数 void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } ``` 请注意,你需要将代码中的“your_apn”、“your_username”和“your_password”替换为SIM7000C所连接的运营商的APN、用户名和密码。此外,你还需要将代码中的“your_wifi_ssid”和“your_wifi_password”替换为你的WiFi网络的SSID和密码。 希望这可以帮助你开始使用SIM7000C通过MQTT连接阿里云

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值