6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

M5Stack と PubSubClient で SORACOM Beam 越しに MQTT で Amazon MQ と Pub/Sub する

Last updated at Posted at 2019-05-25

Qiita 内の Amazon MQ エントリー数 No.1 (5/25時点) の松下です。

ワイもく! Vol.8 の中の 「第5回 M5Stack もくもく会」に参加、M5Stack と PubSubClient で SORACOM Beam 越しに MQTT で Amazon MQ と Pub/Sub を試してみました。

構成

残念なことに 通信部分はモバイルルーターを使ってます (^^;;;

全体構成図

実装

以下、実装です。

コード(スケッチ)

#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <M5Stack.h>

char *ssid = "SSID";
char *password = "Pass";

#define __VERSION__   "1.0.0"
#define LOOP_INTERVAL (6000)

char *THING_NAME = "M5Stack";

////////////////////////////////////////////////////////////////////////////////

WiFiClient netClient;
PubSubClient MqttClient;

void callback(char* topic, byte* payload, unsigned int length) {
    String buf_t = String(topic);
    Serial.print("Incoming: "); Serial.println(buf_t);
    payload[length] = '\0';
    String buf_s = String((char*) payload);
    M5.Lcd.clear();
    M5.Lcd.setCursor(10, 10);
    if (length == 1) {
        int n = buf_s.toInt();
        M5.Lcd.setTextSize(1);
        M5.Lcd.printf("setTextSize = %d\n", n);
        M5.Lcd.setTextSize(n);
    } else {
        M5.Lcd.clear();
        int src_len = buf_s.length() + 1;
        char s[src_len];
        buf_s.toCharArray(s, src_len);
        M5.Lcd.printf(s);
    }
}

void setup_network() {
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println();
    Serial.println("WiFi Connected.");
}

void connect() {
    setup_network();
    Serial.print("ThingName(mqtt_id): "); Serial.println(THING_NAME);
    MqttClient.setServer("beam.soracom.io", 1883);
    MqttClient.setCallback(callback);
    MqttClient.setClient(netClient);
    if (!MqttClient.connect(THING_NAME)) {
        Serial.println(MqttClient.state());
    }
    MqttClient.subscribe("m5stack/sub/#");
}

void setup() {
    Serial.begin(115200);
    M5.begin();
    connect();
    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setTextColor(WHITE);
    M5.Lcd.setTextSize(1);
    M5.Lcd.printf("Ready");
}

void loop() {
    unsigned long next = millis();
    while (millis() < next + LOOP_INTERVAL) {
        MqttClient.loop();
    }
    Serial.println("loop");
    MqttClient.publish("m5stack/ping", "(^^)");
}

Amazon MQ と SORACOM Beam

詳しくはこちらで。

おわりに

うーん、M5Stack にセルラー通信が入ってくれると嬉しい。ケースには入ってるし、モニターもついてるし、電池も入ってるし!!

ワイもく!さんは「コラボ推奨」とのことで、みんなでワイワイやれたのは楽しかったです!

EoT

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?