EMQX configuration and Android connection

EMQX configuration

I use Windows, so I configure the Windows version.

Download on the official website first emqx
Installation is simple

1. Select the Windows version through emqx.io or github and download the.zip package to be installed.

2 decompression package

3 Open Windows command line window, cd to program directory, start EMQ X

There should be no certificate now, you need to install the certificate or emqx console will fail
Installation certificate

How to use license file:

  1. After clicking the'Download License'button, open the successful license.zip package.

  2. Copy two files (emqx.lic, emqx.key) from the compressed package to the license directory of EMQX.

  • If you install EMQX as a zip package, the license directory is `emqx/etc/'.

  • If you install EMQX as a DEB/RPM package, the license directory is'/ etc/emqx/';

  • If you install EMQX in Docker mode, the license directory is'/ opt/emqx/etc /'.

Once you're ready, you can start.

emqx start

go Browser See if there are any pages out, as follows:

If the page is loaded, it is successful

The following message publishing is implemented with python:

Need to install paho.mqtt installation package
Installation of pip staller paho.mqtt is sufficient
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))

def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.payload))
if __name__ == '__main__':
    client = mqtt.Client("mqtt")
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect("n Your ipv4 address", 1883, 600)
    while True:
    	#The theme of f is emqtt message is Hello, EMQ!
        client.publish('emqtt', payload='Hello,EMQ!', qos=0)

The following is the news subscription of Andrews.

1 Configuration Gradle

In build.gradle

repositories {
    maven {
        url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
    dependencies {
        implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
  	   implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

2 Registration Rights and Services

Add in Android Manifest

    <!-- Permissions the Application Requires -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
        <service android:name="org.eclipse.paho.android.service.MqttService">

3 Overall

4 Write subscription messages

package com.example.mqtt;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

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.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class MainActivity extends AppCompatActivity {
    MqttAndroidClient mqttAndroidClient;

    final String serverUri = "tcp://Your ipv4 address: 1883 ";
    String clientId = "robot";
    #Subscribed topics should be consistent with published topics
    final String subscriptionTopic = "emqtt";
    protected void onCreate(Bundle savedInstanceState) {
        //New client instance
        clientId = clientId + System.currentTimeMillis();
        mqttAndroidClient = new MqttAndroidClient(getApplicationContext(), serverUri, clientId);
        //Setting callback function
        mqttAndroidClient.setCallback(new MqttCallbackExtended() {
            public void connectComplete(boolean reconnect, String serverURI) {

            public void connectionLost(Throwable cause) {

            public void messageArrived(String topic, MqttMessage message) throws Exception {
            //Print subscription messages
                Log.i("mouse",new String(message.getPayload()));
                //                makeToast(new String(message.getPayload()));

            public void deliveryComplete(IMqttDeliveryToken token) {
        //Connection settings, whether to reconnect, whether to clean session
        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();

        try {
            //Connect to mqtt server
            mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
                public void onSuccess(IMqttToken asyncActionToken) {
                    makeToast("Successful connection");
                    Log.i("mouse","Successful connection");
                    //Subscribe to topic

                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    makeToast("connection failed");
                    Log.i("mouse","Connection failed!!!"+exception.getMessage());
        } catch (MqttException ex) {
    public void subscribeToTopic() {
        try {
            //Start subscribing
            mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener() {
                public void onSuccess(IMqttToken asyncActionToken) {
                    makeToast("Successful subscription" + subscriptionTopic);

                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    makeToast("Subscription failed" + subscriptionTopic);

        } catch (MqttException ex) {

    private void makeToast(String str) {
        Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();

test result

Keywords: Android Eclipse Windows Gradle

Added by karenn1 on Wed, 02 Oct 2019 18:32:16 +0300