Arduino RTC每8小时减去1秒

时间:2018-12-19 10:40:17

标签: c++ arduino

我将来自集成了RTC的Arduino MKR 1300的RTC用作警报,它将触发“布尔”(这是一个整数),该警报将告诉循环每分钟运行某种方法,然后每隔发送一些数据5分钟。它处于活动循环中,但是只有在循环内部时才发送数据的方法有效(不知道为什么)。问题在于,RTC显然每隔8小时就会减去1秒,几天后时间可能就会结束,而不是每隔xx:10:xx-xx:15:xx发送数据,它可能会发送xx:09的数据: xx-xx:14:xx。

代码如下:

#include <EmonLib.h>
#include <RTCZero.h>
#include <MKRWAN.h>


EnergyMonitor emon1;
EnergyMonitor emon2;
EnergyMonitor emon3;
RTCZero rtc;
LoRaModem modem;

String appEui = "1234567891011121";
String appKey = "ffffffffffffffffffffffffffffffff";

/* INITIAL_TIME */
const byte seconds = 0;
const byte minutes = 0;
const byte hours = 0;
const byte day = 17;
const byte month = 12;
const byte year = 18;


byte second_alarm = 0;
byte minute_alarm = 0;
byte hour_alarm = 0;
byte INTERVAL = 60;
int SEND_LOOP = 5;

int totalKW;
int counter= 0;
int alarm_Triggered = 0;

void setup()
{
  Serial.begin(115200);
  if (!modem.begin(EU868)) {
    Serial.println("Failed to start module");
    while (1) {}
  };
  Serial.print("Your module version is: ");
  Serial.println(modem.version());
  Serial.print("Your device EUI is: ");
  Serial.println(modem.deviceEUI());

  Serial.println("Connecting");
  int connected = modem.joinOTAA(appEui, appKey);
  if (!connected) {
    Serial.println("Something went wrong; are you indoor? Move near a window and retry");
    while (1) {}
  }
  Serial.println("Connected");

  // Set poll interval to 60 secs.
  modem.minPollInterval(60);

  analogReadResolution(9);
  emon1.current(1, 53);
  emon2.current(2, 53);
  emon3.current(3, 53);
  counter= 0;

  rtc.begin(); // initialize RTC

  rtc.setAlarmTime(hour_alarm, minute_alarm, second_alarm);
  rtc.enableAlarm(rtc.MATCH_HHMMSS);
  rtc.attachInterrupt(triggerAlarm);

  // Set the time
  rtc.setHours(hours);
  rtc.setMinutes(minutes);
  rtc.setSeconds(seconds);

  // Set the date
  rtc.setDay(day);
  rtc.setMonth(month);
  rtc.setYear(year);


}

void loop() {
  if (alarm_Triggered == 1) {
    dataMonitor();
    alarm_Triggered = 0;
  }
}

void dataMonitor() {

  int totalWatt = 0;
  unsigned long delay_send = 0;
  int sending = 0;

  double Irms1 = emon1.calcIrms(600);
  if (Irms1 < 0.3) Irms1 = 0;
  double Watt1 = Irms1 * 230;

  double Irms2 = emon2.calcIrms(600);
  if (Irms2 < 0.3) Irms2 = 0;
  double Watt2 = Irms2 * 230;

  double Irms3 = emon3.calcIrms(600);
  if (Irms3 < 0.3) Irms3 = 0;
  double Watt3 = Irms3 * 230;

  totalWatt = Watt1 + Watt2 + Watt3;
  totalKW = totalKW + totalWatt / 1000;
  Serial.println(counter);

  sendDataChecker(Irms1, Irms2, Irms3);
  setAlarm();
  counter= counter+ 1;

}

void sendDataChecker(double Irms1, double Irms2, double Irms3) {
  if (counter== SEND_LOOP) {
    double IrmsTotal = Irms1 + Irms2 + Irms3;
    String msg = "{\"id\":\"avac_aud2\",\"kW\":" + String(totalKW) + ", \"current\":" + String(IrmsTotal) + "}";
    int err;
    Serial.println("Ready to Send");
    modem.beginPacket();
    modem.print(msg);
    err = modem.endPacket(true);
    Serial.println("Sent1");
    if (err > 0) {
      //message sent correctly
      Serial.println("Sent");
      counter= 0;
      totalKW = 0;
    } else {
      Serial.println("ERR");
      counter= 0;
    }
  }
}


void setAlarm() {
  second_alarm += INTERVAL;
  if (second_alarm >= 60) {
    minute_alarm++;
    second_alarm = 0;
  }
  if (minute_alarm >= 60) {
    hour_alarm++;
    minute_alarm = 0;
  }
  if (hour_alarm >= 24) {
    hour_alarm = 0;
  }

  rtc.setAlarmTime(hour_alarm, minute_alarm, second_alarm);
}

void triggerAlarm() {
  alarm_Triggered = 1;
}

0 个答案:

没有答案