WioLTE를 사용해 보았다.

14582 단어 iftttArduinoWioLTE
WioLTE를 사용해 보았으므로 비망록

조작반의 동작을 감시해, 변화가 있으면 시간과 점등 상황을 전하는 프로그램.
프레임은 3D 프린터로 작성. 100균의 자석으로 반에 달라붙도록 하고 있다.
(추가로 빛이 들어가지 않도록, 스폰지 사용과 아크릴의 대각선 부분은 비닐 테이프로 덮는 것을 했다.)

시간을 정확하게 하기 위해 계측 중에는 (LTE를 On으로 할 필요가 있다) GetTime을 이용하지 않고 Grove 고정밀 RTC를 사용하고 있다.
iMuto M5185Wh/50000mAh로 약 3주간 가지는 것 같습니다.
h tp : // 아 mz 응. 아시아/d/fCh64

막힌 곳



TurnOnorReset에서 에러가 반환되고 있는 것이 있었으므로, 에러가 나오면 5회까지 반복하도록(듯이) 했다. 한 번은 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로 변환하는 곳 ...

좋은 웹페이지 즐겨찾기