0
0

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.

AWSIotでのMQTT通信 WioLTE編

Posted at

3G通信でのMQTT

WioLTEでの送信

Iot機器がいつもWi-Fiなどの通信環境にあるわけではない。
SIMカードを使用した情報送信が必要になってくる。

使用機器

  • WioLTE
  • SIM(soracom)
  • usb コード

今回の環境は、自宅
つまり、電源に関しては、USBポートから安定して供給されるので省電力化の話はしません。

なぜWioLTEなのか?

esp32などの小型で汎用性の高いものを使用するほうがいい気がするが、SIMの使用が一番簡単に行えるからだ。

AWSIot

今回は、SIMでの送信でMQTTを使用する。
その際、ブローカーサーバに対して、証明書を発行し通信しないといけない。
なので、AWSIotで証明書を発行する。

手順は、AWSIoTらくらく通信方法

上記のリンク先でほとんど書いている。

 MQTT通信

通信時soracomのサーバを経由してAWSに送る。
その際、soracomが証明書を添付してAWSに送るので、マイコン送信時にコードでの記載必要ない。

testMqtt.ion

# include <WioLTEforArduino.h>
# include <WioLTEClient.h>
# include <PubSubClient.h> // https://github.com/SeeedJP/pubsubclient
# include <stdio.h>


# define APN "soracom.io"
# define USERNAME "sora"
# define PASSWORD "sora"

# define MQTT_SERVER_HOST "beam.soracom.io"
# define MQTT_SERVER_PORT (1883)

# define ID "AWS IoT"
# define OUT_TOPIC "test/wio"
# define IN_TOPIC "inTopic"


WioLTE Wio;


void setup()
{
  wio_setUP();
  connectMqtt();
}
void loop(){
  MqttClient.publish(OUT_TOPIC, "testDESU");
err:

  delay(2000);
}

void wio_setUP()
{
  delay(200);

  SerialUSB.println("### I/O Initialize.");
  Wio.Init();

  Wire.begin();
  Rtc.begin();

  SerialUSB.println("### Power supply ON.");
  Wio.PowerSupplyLTE(true);
  Wio.PowerSupplyGrove(true);
  delay(500);

  SerialUSB.println("### Turn on or reset.");
  if (!Wio.TurnOnOrReset())
  {
    SerialUSB.println("### ERROR! ###");
    return;
  }

  SerialUSB.println("### Connecting to \"" APN "\".");
  if (!Wio.Activate(APN, USERNAME, PASSWORD))
  {
    SerialUSB.println("### ERROR! ###");
    return;
  }
}

void connectMqtt()
{
  SerialUSB.println("### Connecting to MQTT server \"" MQTT_SERVER_HOST "\"");
  MqttClient.setServer(MQTT_SERVER_HOST, MQTT_SERVER_PORT);
  MqttClient.setCallback(callback);
  MqttClient.setClient(WioClient);
  if (!MqttClient.connect(ID))
  {
    SerialUSB.println("### ERROR! ###");
    return;
  }
  delay(1000);
}

WioLTEのMQTT_exampleのコードです。
これで送信してみて通信できれば完了です。

あとがき

私自身、これをやった際に、オンプレのサーバにデバイスIDではじかれる現象がありました。
なので、AWSIotを使用することになったのですが、触った感じすごく簡単だなっと思いました。データが来れば自動的にデータベースに格納してくれる。
認証周りもほどんと、外部サービスを使用しています。

ちなみに、オンプレですが、認証なしでは、送信できたんです。
ライブラリを改造しようとしましたが、修正箇所が多すぎるため断念しました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?