mqtt x python, Java, Android 備忘録

## 概要

まずはmacでmqttをインストール、サービスを立ち上げる
https://qiita.com/kenmaro/items/0b50fdef22dd234c0842

そのあとに、実際にpython, Java, Android studio を使って
`pub`, `sub`などをやってみる。


## Python

```pub.py
import paho.mqtt.client as mqtt
import time

client = mqtt.Client()
client.connect('localhost', 1883, keepalive=60)
client.loop_start()
while True:
  client.publish('world/darai0512', 'test')
  time.sleep(1)
```

```sub.py
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, respons_code):
  print('connected')
  client.subscribe('world/#')

def on_message(client, userdata, msg):
  print(msg.topic + ' ' + str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect('localhost', 1883, keepalive=60)
client.loop_forever()
```


## Java

mac に `brew install gradle`
としてJavaのビルドツール`gradle`をインストールした後、
https://qiita.com/shohei1913/items/b355ad7d1bb27141176b
こちらの記事に従う。
https://github.com/ShoheiMitani/mqtt
ここにgit repoも準備されている。ありがとうございます。


## Android

更新中


```MainActivity.java
package com.example.mq2;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;

import android.view.Menu;
import android.view.MenuItem;

import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;

import android.content.Context;
import android.util.Log;

import android.content.Intent;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {
    private String TAG = "test";
    private MqttAndroidClient mqttAndroidClient;

    //Publish設定
    final String broker = "tcp://xxxxxxxxxx:1883";
    final String topic = "top/second/third";
    final int qos = 1;
    final String clientId = "Publisher01";
    //Publishするメッセージ内容
    String content = "message to you";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mqttAndroidClient = new MqttAndroidClient(this, broker, clientId);
        MqttConnectOptions options = new MqttConnectOptions();
//        options.setCleanSession(false);

        Log.d(TAG, "test2");

        try {
            mqttAndroidClient.connect(options, null, new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken iMqttToken) {
                    Log.d(TAG, "onSuccess");

                    try {

                        IMqttDeliveryToken token = mqttAndroidClient.publish(topic, content.getBytes(), 0, true);
                        Log.d(TAG, "publish");
                    } catch (MqttException e) {
                        Log.d(TAG, e.toString());
                    }
                }

                @Override
                public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
                    Log.d(TAG, "onFailure");
                }
            });
        } catch (MqttException e) {
            e.printStackTrace();
        }

        Log.d(TAG, "Connected and publishing message: qos -> " +  qos +  " message -> " + content);



        Log.d(TAG, "published message");


    }
```