LoginSignup
2

More than 3 years have passed since last update.

ちょっとだけWio LTEのサンプルコードを改善してみた

Last updated at Posted at 2019-04-29

何をしたか?

Wio LTEで、従来からの起動→SORACOM接続に加えて、
NTP同期、IMSI/IMEI/ICCID/電話番号取得、日本時間取得(UTC→JST)をやってみた。

+シーケンスごとのOK/NGをわかりやすく表示させてみた。

どんなイメージ?

こんな感じ。

--- START ---------------------------------------------------
### I/O Initialize.
### Power supply ON.
### Turn on or reset. -> OK
### Connecting to "soracom.io". -> OK
### GetPhoneNumber. -> OK
PhoneNumber:02012345678
### Sync time.(ntp.nict.jp) -> OK
### GetIMEI. -> OK
IMEI:861108031234567
### GetIMSI. -> OK
IMSI:440123456789012
### GetICCID. -> OK
ICCID:8981123456789012345
--- COMPLETE ------------------------------------------------

### Get time.
JST:2019/04/30 04:29:05
### Get RSSI.
RSSI:### ERROR! ###

### Get time.
JST:2019/04/30 04:29:15
### Get RSSI.
RSSI:-69

サンプルコード

WioLTE_init.ino
#include <WioLTEforArduino.h>
#include <time.h> //for getWioTime()
WioLTE Wio;

char imei[50];
char imsi[50];
char iccid[50];
char phonenumber[50];

void setup() {
  //開始メッセージ
  delay(200);
  SerialUSB.println("");
  SerialUSB.println("--- START ---------------------------------------------------");

  //IO初期化
  SerialUSB.println("### I/O Initialize.");
  Wio.Init();

  //LTEモジュールへの電源供給開始
  SerialUSB.println("### Power supply ON.");
  Wio.PowerSupplyLTE(true);
  delay(500);

  //LTEモジュールの起動
  SerialUSB.print("### Turn on or reset.");
  if (!Wio.TurnOnOrReset()) {
    SerialUSB.println(" -> NG");
    return;
  }else{
    SerialUSB.println(" -> OK");
  }

  //SORACOMへの接続
  SerialUSB.print("### Connecting to \"soracom.io\".");
  if (!Wio.Activate("soracom.io","sora","sora")) {
    SerialUSB.println(" -> NG");
    return;
  }else{
    SerialUSB.println(" -> OK");
    //PhoneNumber取得(回線接続時のみ)
    SerialUSB.print("### GetPhoneNumber.");
    if (!Wio.GetPhoneNumber(phonenumber, sizeof(phonenumber))) {
      SerialUSB.println(" -> NG");
      return;
    }else{
      SerialUSB.println(" -> OK");
      SerialUSB.print("PhoneNumber:");
      SerialUSB.println(phonenumber);
    }
  }

  //時刻同期(ntp.nict.jp)
  SerialUSB.print("### Sync time.(ntp.nict.jp)");
  if (!Wio.SyncTime("ntp.nict.jp")) {
    SerialUSB.println(" -> NG");
    return;
  }else{
    SerialUSB.println(" -> OK");
  }

  //IMEI取得
  SerialUSB.print("### GetIMEI.");
  if (!Wio.GetIMEI(imei, sizeof(imei))) {
    SerialUSB.println(" -> NG");
    return;
  }else{
    SerialUSB.println(" -> OK");
    SerialUSB.print("IMEI:");
    SerialUSB.println(imei);
  }

  //IMSI取得
  SerialUSB.print("### GetIMSI.");
  if (!Wio.GetIMSI(imsi, sizeof(imsi))) {
    SerialUSB.println(" -> NG");
    return;
  }else{
    SerialUSB.println(" -> OK");
    SerialUSB.print("IMSI:");
    SerialUSB.println(imsi);
  }

  //ICCID取得
  SerialUSB.print("### GetICCID.");
  if (!Wio.GetICCID(iccid, sizeof(iccid))) {
    SerialUSB.println(" -> NG");
    return;
  }else{
    SerialUSB.println(" -> OK");
    SerialUSB.print("ICCID:");
    SerialUSB.println(iccid);
  }

  //完了
  SerialUSB.println("--- COMPLETE ------------------------------------------------");
  SerialUSB.println("");
}

void loop() {
  //現在時刻を取得
  SerialUSB.println("### Get time.");
  SerialUSB.print("JST:");
  getWioTime();

  //RSSIを取得
  SerialUSB.println("### Get RSSI.");
  SerialUSB.print("RSSI:");
  getWioRSSI();
  SerialUSB.println("");
  delay(10000);
}

void getWioTime(){
  struct tm now;
  time_t timer;
  struct tm *t_now;
  char time_str[50];

  if (!Wio.GetTime(&now)) {
    SerialUSB.println("### ERROR! ###");
    return;
  }else{
    timer=mktime(&now);
    timer+=3600*9;//JST(+9Hour)
    t_now=localtime(&timer);
    strftime(time_str,sizeof(time_str),"%Y/%m/%d %H:%M:%S",t_now);
    SerialUSB.println(time_str);
  }
}

void getWioRSSI(){
  int rssi = Wio.GetReceivedSignalStrength();
  if (rssi == -999) {
    SerialUSB.println("### ERROR! ###");
    return;
  }else{
    SerialUSB.println(rssi);
  }
}

最後に

GW中だからって夜更かししたら止まらなくなった。危ない危ない。
あと何故か1回目のRSSI取得に失敗する。うーん。

参考

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
2