Skip to content

Commit

Permalink
[Wio BG770A] Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
matsujirushi committed Jan 22, 2025
1 parent 3e20bf9 commit 2927821
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 14 deletions.
27 changes: 14 additions & 13 deletions Wio_BG770A/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@

### セルラー通信

||スケッチ|内容|必要なもの|
|:--|:--|:--|:--|
||[cellular/cellular-status](https://github.com/SeeedJP/wio_cellular/tree/main/examples/cellular/cellular-status)|セルラー通信の状況を表示します<br>> [説明](examples/cellular/cellular-status.md)|nanoSIM|
||[cellular/transparent](https://github.com/SeeedJP/wio_cellular/tree/main/examples/cellular/transparent)|セルラーモジュールをシリアルモニタからATコマンドで操作します。<br>> [説明](examples/cellular/transparent.md)|nanoSIM|
||[cellular/shell](https://github.com/SeeedJP/wio_cellular/tree/main/examples/cellular/shell)🌶️|セルラーモジュールをシリアルモニタから操作するシェルです。<br>> [説明](examples/cellular/shell.md)|nanoSIM|
||スケッチ|内容|通信プロトコル|必要なもの|
|:--|:--|:--|:--|:--|
||[cellular/cellular-status](https://github.com/SeeedJP/wio_cellular/tree/main/examples/cellular/cellular-status)|セルラー通信の状況を表示します<br>> [説明](examples/cellular/cellular-status.md)||nanoSIM|
||[cellular/transparent](https://github.com/SeeedJP/wio_cellular/tree/main/examples/cellular/transparent)|セルラーモジュールをシリアルモニタからATコマンドで操作します。<br>> [説明](examples/cellular/transparent.md)||nanoSIM|
||[cellular/shell](https://github.com/SeeedJP/wio_cellular/tree/main/examples/cellular/shell)🌶️|セルラーモジュールをシリアルモニタから操作するシェルです。<br>> [説明](examples/cellular/shell.md)||nanoSIM|
|📝|[cellular/cellular-mqtt-pubsubclient](https://github.com/SeeedJP/wio_cellular/tree/main/examples/cellular/cellular-mqtt-pubsubclient)|MQTTブローカーへ稼働時間を送信(パブリッシュ)しながら、購読(サブスクライブ)したメッセージを受信し、シリアルモニタに表示します。|MQTT|nanoSIM|

### セルラー通信(SORACOMプラットフォーム)

||スケッチ|内容|必要なもの|
|:--|:--|:--|:--|
||[soracom/soracom-connectivity-diagnostics](https://github.com/SeeedJP/wio_cellular/tree/main/examples/soracom/soracom-connectivity-diagnostics)🌶️|SORACOMプラットフォームへの接続を確認します。<br>> [説明](examples/soracom/soracom-connectivity-diagnostics.md)|SORACOM Air for セルラー|
|📝|[soracom/soracom-uptime](https://github.com/SeeedJP/wio_cellular/tree/main/examples/soracom/soracom-uptime)|稼働時間をSORACOM Unified Endpointへ送信します。<br>> [説明](examples/soracom/soracom-uptime.md)|SORACOM Air for セルラー|
|📝|[soracom/soracom-uptime-tcpclient](https://github.com/SeeedJP/wio_cellular/tree/main/examples/soracom/soracom-uptime-tcpclient)|稼働時間をSORACOM Unified Endpointへ送信します。<br>WioCellularArduinoTcpClientクラスを使用して実装しています。<br>> [説明](examples/soracom/soracom-uptime-tcpclient.md)|SORACOM Air for セルラー|
|📝|[soracom/soracom-uptime-httpclient](https://github.com/SeeedJP/wio_cellular/tree/main/examples/soracom/soracom-uptime-httpclient)|稼働時間をIoT SIMのメタデータへ書き込みます。<br>WioCellularArduinoTcpClientクラスとHttpClientクラスを使用して実装しています。|SORACOM Air for セルラー|
|📝|[soracom/soracom-uptime-lp](https://github.com/SeeedJP/wio_cellular/tree/main/examples/soracom/soracom-uptime-lp)🌶️🌶️|稼働時間をSORACOM Unified Endpointへ送信します。PSM機能を使って電力消費を抑止しています。<br>> [説明](examples/soracom/soracom-uptime-lp.md)|SORACOM Air for セルラー|
||[application/soracom-gps-tracker](https://github.com/SeeedJP/wio_cellular/tree/main/examples/application/soracom-gps-tracker)🌶️🌶️|GPSで取得した位置情報をSORACOM Unified Endpointへ送信します。|[Grove - GPS Module](https://www.seeedstudio.com/Grove-GPS-Module.html)<br>SORACOM Air for セルラー|
||スケッチ|内容|通信プロトコル|必要なもの|
|:--|:--|:--|:--|:--|
||[soracom/soracom-connectivity-diagnostics](https://github.com/SeeedJP/wio_cellular/tree/main/examples/soracom/soracom-connectivity-diagnostics)🌶️|SORACOMプラットフォームへの接続を確認します。<br>> [説明](examples/soracom/soracom-connectivity-diagnostics.md)||SORACOM Air for セルラー|
|📝|[soracom/soracom-uptime](https://github.com/SeeedJP/wio_cellular/tree/main/examples/soracom/soracom-uptime)|稼働時間をSORACOM Unified Endpointへ送信します。<br>> [説明](examples/soracom/soracom-uptime.md)|TCP|SORACOM Air for セルラー|
|📝|[soracom/soracom-uptime-tcpclient](https://github.com/SeeedJP/wio_cellular/tree/main/examples/soracom/soracom-uptime-tcpclient)|稼働時間をSORACOM Unified Endpointへ送信します。<br>WioCellularArduinoTcpClientクラスを使用して実装しています。<br>> [説明](examples/soracom/soracom-uptime-tcpclient.md)|TCP|SORACOM Air for セルラー|
|📝|[soracom/soracom-uptime-httpclient](https://github.com/SeeedJP/wio_cellular/tree/main/examples/soracom/soracom-uptime-httpclient)|稼働時間をIoT SIMのメタデータへ書き込みます。<br>WioCellularArduinoTcpClientクラスとHttpClientクラスを使用して実装しています。|HTTP|SORACOM Air for セルラー|
|📝|[soracom/soracom-uptime-lp](https://github.com/SeeedJP/wio_cellular/tree/main/examples/soracom/soracom-uptime-lp)🌶️🌶️|稼働時間をSORACOM Unified Endpointへ送信します。PSM機能を使って電力消費を抑止しています。<br>> [説明](examples/soracom/soracom-uptime-lp.md)|TCP|SORACOM Air for セルラー|
||[application/soracom-gps-tracker](https://github.com/SeeedJP/wio_cellular/tree/main/examples/application/soracom-gps-tracker)🌶️🌶️|GPSで取得した位置情報をSORACOM Unified Endpointへ送信します。|TCP|[Grove - GPS Module](https://www.seeedstudio.com/Grove-GPS-Module.html)<br>SORACOM Air for セルラー|
118 changes: 118 additions & 0 deletions Wio_BG770A/examples/cellular/cellular-mqtt-pubsubclient.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# cellular/cellular-mqtt-pubsubclient

MQTTブローカーへ稼働時間を送信(パブリッシュ)しながら、購読(サブスクライブ)したメッセージを受信し、シリアルモニタに表示します。

## 概要

MQTTブローカー`test.mosquitto.org:1883`に接続して以下の処理を行います。

* 5分周期に、稼働時間を送信します。トピックは`WioCellular/<IMSI>/upstream`です。
* 購読したメッセージを受信して、シリアルモニタに表示します。トピックは`WioCellular/<IMSI>/downstream`です。

送受信にはMQTTを使います。

WioCellularライブラリはTLS暗号通信には対応していません。
TLSのMQTT通信をしたいときは[SORACOM Beam](https://soracom.jp/services/beam/)をご活用ください。

## MQTT通信の確認方法

### PCにメッセージを受信

Wio BG770Aから送信されたメッセージを確認するコマンドです。
`<IMSI>`の部分はSIMのIMSIに書き換えてください。)

```
mosquitto_pub -h test.mosquitto.org -p 1883 -t "WioCellular/<IMSI>/downstream" -m "<MESSAGE>"
```

### PCからメッセージを送信

Wio BG770Aへメッセージを送信するコマンドです。
`<IMSI>`の部分はSIMのIMSIに書き換えてください。)

```
mosquitto_sub -h test.mosquitto.org -p 1883 -t "WioCellular/<IMSI>/upstream"
```

## 詳細

MQTT通信PubSubClientクラスを使います。
下記コードで、PubSubClientインスタンスを使えるようにします。

```cpp
#include <WioCellular.h>
#include <PubSubClient.h>

static WioCellularArduinoTcpClient<WioCellularModule> TcpClient{ WioCellular, PDP_CONTEXT_ID };
static PubSubClient MqttClient{ TcpClient };
```
PubSubClientインスタンスには事前に
* MQTTブローカーのホスト名
* MQTTブローカーのポート番号
* キープアライブ間隔
* 購読したメッセージを受信したときに呼び出すコールバック関数
を設定します。
```cpp
MqttClient.setServer(MQTT_BROKER_HOST, MQTT_BROKER_PORT);
MqttClient.setKeepAlive(MQTT_KEEP_ALIVE);
MqttClient.setCallback(MqttClientCallback);
```

MQTTブローカーへの接続は`PubSubClient::connect()`を呼びます。
引数でクライアントIDやユーザー名、パスワードなどを指定できます。
このサンプルはクライアントIDだけを指定しています。

接続に成功したら、購読するトピックを`PubSubClient::subscribe()`で設定します。
既に設定済みであっても再接続したときは再度設定する必要があります。

```cpp
MqttClient.connect(ClientId.c_str());
MqttClient.subscribe(SubscribeTopic.c_str());
```

接続している間は、必ず定期的に`PubSubClient::loop()`を呼んでください。
購読しているメッセージの受信やキープアライブを処理するためです。

```cpp
MqttClient.loop();
```

購読したメッセージを受信すると`PubSubClient::setCallback()`で設定したコールバック関数が呼び出されます。

コールバック関数の引数でトピックとメッセージ、メッセージ長が渡されます。

```cpp
static void MqttClientCallback(char* topic, byte* payload, unsigned int length) {
...
}
```
メッセージの送信は`PubSubClient::publish()`を呼びます。
```cpp
MqttClient.publish(PublishTopic.c_str(), str.c_str());
```

このサンプルは購読したメッセージを受信するため常時接続していますが、ネットワークやサーバーの事情で接続が切れてしまうことがあります。
突然の接続断にもきちんと対処しておきましょう。

```cpp
void loop(void) {
if (!MqttClient.connected()) {
if (!MqttClient.connect(ClientId.c_str())) {
// 接続エラー
} else {
// 接続成功
}
} else {
// 接続中
WioCellular.doWork(10);
MqttClient.loop();
if (!MqttClient.connected()) break;
}
}
```
2 changes: 1 addition & 1 deletion Wio_BG770A/examples/soracom/soracom-uptime-tcpclient.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ WioCellularArduinoTcpClientクラスで送受信するスケッチです。
## 詳細

WioCellularArduinoTcpClientクラスはClientクラスを継承しているので、Clientクラスを使う通信プロトコルのライブラリを利用することができます。
例えば、[ArduinoHttpClientライブラリ](https://www.arduino.cc/reference/en/libraries/arduinohttpclient/)ライブラリや[arduino-mqttライブラリ](https://www.arduino.cc/reference/en/libraries/mqtt/)があります。
例えば、[ArduinoHttpClientライブラリ](https://docs.arduino.cc/libraries/arduinohttpclient/)ライブラリや[PubSubClientライブラリ](https://docs.arduino.cc/libraries/pubsubclient/)があります。

下記コードで、WioCellularArduinoTcpClientインスタンスを使えるようにします。

Expand Down

0 comments on commit 2927821

Please sign in to comment.