Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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

ma2shita
(松下 享平) IoT通信プラットフォーム「ソラコム」のエバンジェリストで年間140回以上の講演を通じ #IoT の事例や技術情報を日々紹介。LPWAの選び方や共著で「公式ガイドブック SORACOM プラットフォーム」を書いています。2020年の #AWS IoT Heroです。"Max"はニックネームです、フォローやメッセージはお気軽に! Twitterは @ma2shita です。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした