WioLTEを使ってみた。


WioLTEを使ってみましたので備忘録

操作盤の動作を監視し、変化が有ったら時間と点灯状況を伝えるプログラム。
枠は3Dプリンタで作成。100均のマグネットで盤にくっつくようにしている。
(追加で光が入らないように、スポンジ使用とアクリルの斜めの部分はビニールテープで覆うことをした。)

時間を正確にするために計測中には(LTEをOnにする必要がある)GetTimeを利用せずGrove 高精度RTCを使用している。
iMuto M5185Wh/50000mAhで約3週間持ちそうです。
http://amzn.asia/d/fChHN64

詰まったところ

TurnOnorResetでエラーが返っていることがあったので、エラーが出たら5回まで繰り返すようにした。1回はRetryするようにした。

プログラム

#include <WioLTEforArduino.h>
#include <Wire.h>
#include "PCF85063TP.h"
#include "stdio.h"

#define UseSD false

#define APN               "APN"
#define USERNAME          "USER_NAME"
#define PASSWORD          "PASSWORD"

#define WEBHOOK_EVENTNAME "IFTTT_EVENT_NAME"
#define WEBHOOK_KEY       "IFTTT_KEY"
#define WEBHOOK_URL       "https://maker.ifttt.com/trigger/"WEBHOOK_EVENTNAME"/with/key/"WEBHOOK_KEY

#define LED1 WIOLTE_D19
#define LED2 WIOLTE_D20
#define LED3 WIOLTE_D39
#define LED4 WIOLTE_D38

#define PUMP1_ON WIOLTE_A5
#define PUMP1_OFF WIOLTE_A4
#define PUMP2_ON WIOLTE_A7
#define PUMP2_OFF WIOLTE_A6

WioLTE Wio;
PCD85063TP clockx;//define a object of PCD85063TP class
bool Pump1_On_Status = false, Pump1_Off_Status = false, Pump2_On_Status = false, Pump2_Off_Status = false;

void setup()
{
  int count = 0;
  SerialUSB.println("");
  SerialUSB.println("--- START --------------------------------------");
  SerialUSB.println("****** Power Setting ***********************");
  bool ErrorFlag = false;
  bool SuccessFlag = false;
  struct tm now;

  Wio.Init();
  Wio.LedSetRGB(0, 0, 128);
  Wio.PowerSupplyLTE(true);
  Wio.PowerSupplyGNSS(false);
  Wio.PowerSupplySD(UseSD);
  Wio.PowerSupplyGrove(true);
  delay(1000);
  clockx.begin();

  SerialUSB.println("****** I/O Initialize ***********************");
  pinMode(PUMP1_ON, INPUT_ANALOG);
  pinMode(PUMP1_OFF, INPUT_ANALOG);
  pinMode(PUMP2_ON, INPUT_ANALOG);
  pinMode(PUMP2_OFF, INPUT_ANALOG);
  pinMode(LED1, OUTPUT); digitalWrite(LED1, HIGH);
  pinMode(LED2, OUTPUT); digitalWrite(LED2, HIGH);
  pinMode(LED3, OUTPUT); digitalWrite(LED3, HIGH);
  pinMode(LED4, OUTPUT); digitalWrite(LED4, HIGH);
  SerialUSB.println("****** LTE Initialize ***********************");
  SerialUSB.println("### Turn on or reset(1st).");
  count = 0;
  SuccessFlag = false;
  while (!SuccessFlag) {
    if (!Wio.TurnOnOrReset()) {
      Wio.LedSetRGB(128, 0, 0);
      SerialUSB.print("### ERROR!(");
      SerialUSB.print(count, DEC);
      SerialUSB.println(":TurnOnOrReset) ###");
      ErrorFlag = true;
      delay(500);
      count++;
      if (count > 5)SuccessFlag = true;
    } else {
      SuccessFlag = true;
      ErrorFlag = false;
    }
  }

  if (ErrorFlag == false) {
    digitalWrite(LED1, LOW);
    SerialUSB.println("### Connecting to \""APN"\".(1st)");
    count = 0;
    SuccessFlag = false;
    while (!SuccessFlag) {
      if (!Wio.Activate(APN, USERNAME, PASSWORD)) {
        Wio.LedSetRGB(128, 0, 0);
        SerialUSB.print("### ERROR!(");
        SerialUSB.print(count, DEC);
        SerialUSB.println(":Activate) ###");
        ErrorFlag = true;
        delay(500);
        count++;
        if (count > 5)SuccessFlag = true;
      } else {
        ErrorFlag = false;
        SuccessFlag = true;
      }
    }
  }

  //時刻取得(NTP)
  if (ErrorFlag == false) {
    digitalWrite(LED2, LOW);
    SerialUSB.println("### Sync time.(1st)");
    count = 0;
    SuccessFlag = false;
    while (!SuccessFlag) {
      if (!Wio.SyncTime("ntp.nict.jp")) {
        Wio.LedSetRGB(0, 0, 128);
        SerialUSB.print("### ERROR!(");
        SerialUSB.print(count, DEC);
        SerialUSB.println(":Sync) ###");
        ErrorFlag = true;
        delay(500);
        count++;
        if (count > 5)SuccessFlag = true;
      } else {
        SuccessFlag = true;
        ErrorFlag = false;
      }
    }
  }

  //SetRTC
  if (ErrorFlag == false) {
    digitalWrite(LED3, LOW);
    SerialUSB.println("### Get time. 1st");
    count = 0;
    SuccessFlag = false;
    while (!SuccessFlag) {
      if (!Wio.GetTime(&now)) {
        Wio.LedSetRGB(0, 0, 128);
        SerialUSB.print("### ERROR!(");
        SerialUSB.print(count, DEC);
        SerialUSB.println(":Get Time) ###");
        ErrorFlag = true;
        delay(500);
        count++;
        if (count > 5)SuccessFlag = true;
      } else {
        ErrorFlag = false;
        SuccessFlag = true;
      }
    }
  }
  if (ErrorFlag == false) {
    digitalWrite(LED4, LOW);
    SerialUSB.println("### Set RTC.(1st)");
    int year = now.tm_year + 1900;
    byte month = now.tm_mon + 1;
    byte day = now.tm_mday;
    byte hour = now.tm_hour;
    byte minute = now.tm_min;
    byte second = now.tm_sec;
    //UTC-->JST
    hour += 9;
    if (hour > 23) {
      day += 1;
      hour -= 24;
    }
    //Set RTC
    clockx.stopClock();
    clockx.fillByYMD(year, month, day);
    clockx.fillByHMS(hour, minute, second);
    clockx.setTime();//write time to the RTC chip
    clockx.startClock();
    uint8_t ret = clockx.calibratBySeconds(0, -0.000041);
    SerialUSB.print("### Set Time :");
    SerialUSB.print(year, DEC);
    SerialUSB.print("/");
    SerialUSB.print(month, DEC);
    SerialUSB.print("/");
    SerialUSB.print(day, DEC);
    SerialUSB.print(" ");
    SerialUSB.print(hour, DEC);
    SerialUSB.print(":");
    SerialUSB.print(minute, DEC);
    SerialUSB.print(":");
    SerialUSB.println(second, DEC);
  }
  SerialUSB.println("### Setup completed.");
  //Wio.Deactivate();  // Deactivate a PDP context. Added at v1.1.9
  SerialUSB.println("### DeActivate");
  count = 0;
  SuccessFlag = false;
  while (!SuccessFlag) {
    if (!Wio.Deactivate()) {
      Wio.LedSetRGB(0, 0, 128);
      SerialUSB.print("### ERROR!(");
      SerialUSB.print(count, DEC);
      SerialUSB.println(":Deactivate) ###");
      ErrorFlag = true;
      delay(500);
      count++;
      if (count > 5)SuccessFlag = true;
    } else {
      SuccessFlag = true;
      ErrorFlag = false;
    }
  }


  //Wio.TurnOff(); // Shutdown the LTE module. Added at v1.1.6
  SerialUSB.println("### TurnOff");
  count = 0;
  SuccessFlag = false;
  while (!SuccessFlag) {
    if (!Wio.TurnOff()) {
      Wio.LedSetRGB(0, 0, 128);
      SerialUSB.print("### ERROR!(");
      SerialUSB.print(count, DEC);
      SerialUSB.println(":TurnOff) ###");
      ErrorFlag = true;
      delay(500);
      count++;
      if (count > 5)SuccessFlag = true;
    } else {
      SuccessFlag = true;
      ErrorFlag = false;
    }
  }
  Wio.PowerSupplyLTE(false); // Turn the power supply to LTE module off
  if (ErrorFlag == true)delay(20000);
  Wio.LedSetRGB(0, 0, 0);
}

void loop()
{
  char data[100];
  int status;
  int count;
  bool flag = false;
  bool ErrorFlag = false;
  bool SuccessFlag = false;
  bool Pump1_On_Status_Now = false, Pump1_Off_Status_Now = false, Pump2_On_Status_Now = false, Pump2_Off_Status_Now = false;

  int a1 = analogRead(PUMP1_ON);
  int a2 = analogRead(PUMP1_OFF);
  int a3 = analogRead(PUMP2_ON);
  int a4 = analogRead(PUMP2_OFF);
  if (a1 > a2) {
    Pump1_On_Status_Now = true;
    Pump1_Off_Status_Now = false;
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  } else {
    Pump1_On_Status_Now = false;
    Pump1_Off_Status_Now = true;
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
  }
  if (a3 > a4) {
    Pump2_On_Status_Now = true;
    Pump2_Off_Status_Now = false;
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, LOW);
  } else {
    Pump2_On_Status_Now = false;
    Pump2_Off_Status_Now = true;
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, HIGH);
  }

  if (Pump1_On_Status_Now != Pump1_On_Status) {
    Pump1_On_Status = Pump1_On_Status_Now;
    flag = true;
  }
  if (Pump1_Off_Status_Now != Pump1_Off_Status) {
    Pump1_Off_Status = Pump1_Off_Status_Now;
    flag = true;
  }
  if (Pump2_On_Status_Now != Pump2_On_Status) {
    Pump2_On_Status = Pump2_On_Status_Now;
    flag = true;
  }
  if (Pump2_Off_Status_Now != Pump2_Off_Status) {
    Pump2_Off_Status = Pump2_Off_Status_Now;
    flag = true;
  }

  if (flag == true) {
    clockx.getTime();
    sprintf(data, "{\"value1\":\"%d/%d/%d %d:%d:%d;%d;%d;%d;%d\"}",
            clockx.year + 2000,
            clockx.month,
            clockx.dayOfMonth,
            clockx.hour,
            clockx.minute,
            clockx.second,
            Pump1_On_Status_Now,
            Pump1_Off_Status_Now,
            Pump2_On_Status_Now,
            Pump2_Off_Status_Now
           );

    //post IFTTT
    SerialUSB.println("### Power supply ON. 1st");
    Wio.PowerSupplyLTE(true);
    delay(1000);
    SerialUSB.println("### Turn on or reset. 1st");
    count = 0;
    SuccessFlag = false;
    while (!SuccessFlag) {
      if (!Wio.TurnOnOrReset()) {
        SerialUSB.print("### ERROR!(");
        SerialUSB.print(count, DEC);
        SerialUSB.println(":TurnOff) ###");
        ErrorFlag = true;
        delay(500);
        count++;
        if (count > 5)SuccessFlag = true;
      } else {
        SuccessFlag = true;
        ErrorFlag = false;
      }
    }

    if (ErrorFlag == false) {
      SerialUSB.println("### Connecting to \""APN"\". 1st");
      count = 0;
      SuccessFlag = false;
      while (!SuccessFlag) {
        if (!Wio.Activate(APN, USERNAME, PASSWORD)) {
          SerialUSB.print("### ERROR! (");
          SerialUSB.print(count, DEC);
          SerialUSB.println(":Activate) ###");
          ErrorFlag = true;
          delay(500);
          count++;
          if (count > 5)SuccessFlag = true;
        } else {
          SuccessFlag = true;
          ErrorFlag = false;
        }
      }
    }

    if (ErrorFlag == false) {
      SerialUSB.println("### Post. 1st");
      SerialUSB.print("Post:");
      SerialUSB.println(data);
      count = 0;
      SuccessFlag = false;
      while (!SuccessFlag) {
        if (!Wio.HttpPost(WEBHOOK_URL, data, &status)) {
          SerialUSB.print("### ERROR!(");
          SerialUSB.print(count, DEC);
          SerialUSB.println(":Post) ###");
          ErrorFlag = true;
          delay(1000);
          count++;
          if (count > 5)SuccessFlag = true;
        } else {
          SuccessFlag = true;
          ErrorFlag = false;
        }
      }
      SerialUSB.print("Status(200=Sucess):");
      SerialUSB.println(status);
      if (status != 200)ErrorFlag = true;
    }

    if (ErrorFlag) {
      SerialUSB.println("************** Retry Post Data *****************");
      delay(600000); //1minute
      SerialUSB.println("### Turn on or reset. Retry");
      count = 0;
      SuccessFlag = false;
      while (!SuccessFlag) {
        if (!Wio.TurnOnOrReset()) {
          SerialUSB.print("### ERROR!(");
          SerialUSB.print(count, DEC);
          SerialUSB.println(":TurnOff) ###");
          ErrorFlag = true;
          delay(500);
          count++;
          if (count > 5)SuccessFlag = true;
        } else {
          SuccessFlag = true;
          ErrorFlag = false;
        }
      }

      if (ErrorFlag == false) {
        SerialUSB.println("### Connecting to \""APN"\". Retry");
        count = 0;
        SuccessFlag = false;
        while (!SuccessFlag) {
          if (!Wio.Activate(APN, USERNAME, PASSWORD)) {
            SerialUSB.print("### ERROR! (");
            SerialUSB.print(count, DEC);
            SerialUSB.println(":Activate) ###");
            ErrorFlag = true;
            delay(500);
            count++;
            if (count > 5)SuccessFlag = true;
          } else {
            SuccessFlag = true;
            ErrorFlag = false;
          }
        }
      }

      if (ErrorFlag == false) {
        SerialUSB.print("### Post. Retry");
        SerialUSB.print("Post:");
        SerialUSB.println(data);
        count = 0;
        SuccessFlag = false;
        while (!SuccessFlag) {
          if (!Wio.HttpPost(WEBHOOK_URL, data, &status)) {
            SerialUSB.print("### ERROR!(");
            SerialUSB.print(count, DEC);
            SerialUSB.println(":Post) ###");
            ErrorFlag = true;
            delay(1000);
            count++;
            if (count > 5)SuccessFlag = true;
          } else {
            SuccessFlag = true;
            ErrorFlag = false;
          }
        }
        SerialUSB.print("Status(200=Sucess):");
        SerialUSB.println(status);
      }
    }

    SerialUSB.println("### Power supply OFF.");
    //Wio.Deactivate();  // Deactivate a PDP context. Added at v1.1.9
    SerialUSB.println("### DeActivate");
    count = 0;
    SuccessFlag = false;
    while (!SuccessFlag) {
      if (!Wio.Deactivate()) {
        SerialUSB.print("### ERROR!(");
        SerialUSB.print(count, DEC);
        SerialUSB.println(":Deactivate) ###");
        ErrorFlag = true;
        delay(500);
        count++;
        if (count > 5)SuccessFlag = true;
      } else {
        SuccessFlag = true;
        ErrorFlag = false;
      }
    }
    //Wio.TurnOff(); // Shutdown the LTE module. Added at v1.1.6
    SerialUSB.println("### TurnOff");
    count = 0;
    SuccessFlag = false;
    while (!SuccessFlag) {
      if (!Wio.TurnOff()) {
        SerialUSB.print("### ERROR!(");
        SerialUSB.print(count, DEC);
        SerialUSB.println(":TurnOff) ###");
        ErrorFlag = true;
        delay(500);
        count++;
        if (count > 5)SuccessFlag = true;
      } else {
        SuccessFlag = true;
        ErrorFlag = false;
      }
    }
    Wio.PowerSupplyLTE(false); // Turn the power supply to LTE module off
   }
  delay(1000);
}

済みません・・・プログラムを見ての通りうまく開始できない時間が有ります。月終わりの14時以降?UTCからJSTに変換するところ...