1

I'm having this weird issue where my data Send interval gets screwed up when the monitored current goes above 27A. Basically, it keeps adding several seconds to my interval, here's an example:

Database:

        Current    |   Time
        16A         |   10:00:30
        16A         |   10:10:30
        16A         |   10:20:30
        ....
        16A         |   12:00:30
        27A         |   12:10:35
        27A         |   12:20:40
        27A         |   12:30:45
        .....
        27A         |   13:21:05
        16A         |   13:31:05
        16A         |   13:41:05
        ....

The arduino I'm using is an MKR 1300 with RTC integrated. And here's the code for visbility: Also, by the way, the RTC library is RTCZero.

/* 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;
int SEND_LOOP = 10; //5

float totalKW;
int time_running = 0;
int alarm_Triggered = 0;
int correction_var = 0;
long seq_value = 0;

void setup()
  LoRa Network agreement code (removed)

  analogReadResolution(9);
  emon1.current(1, 90.5);  
  emon2.current(2, 90.3);
  emon3.current(3, 90.3);
  time_running = 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() {

  float totalWatt = 0;

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

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

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

  totalWatt = Watt1 + Watt2 + Watt3;
  totalKW = totalKW + totalWatt / 1000;

  sendDataChecker();
  setAlarm();
  time_running = time_running + 1;

}

void sendDataChecker() {
  if (time_running >= SEND_LOOP) {
    String msg = String(seq_value)+";"+String(totalKW);
    int err;
    modem.beginPacket();
    modem.print(msg);
    err = modem.endPacket(true);
    if (err > 0) {
      //message sent correctly
      time_running = 0;
      totalKW = 0;
      seq_value++;
    } else {
      Serial.println("ERR");
      time_running = 0;
    }

    delay(1000);

    if (!modem.available()) {
      //Now downlink message
      return;
    } else {
      char rcv[64] {};
      int i = 0;
      while (i < 64 && modem.available()) {
        rcv[i++] = (char)modem.read();
      }
      String data_received = rcv;
      if (data_received == "") {
        Serial.println("Null");
      } else {
        //JsonObject& root = jsonBuffer.parseObject(data_received);
        correction_var = data_received.toInt();
      }
    }
  }
}


void setAlarm() {
  int INTERVAL = 60; //60
  second_alarm += INTERVAL;
  minute_alarm += second_alarm / 60;
  second_alarm %= 60;
  hour_alarm += minute_alarm / 60;
  minute_alarm %= 60;
  hour_alarm %= 24;

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

void correct() {
  if (correction_var == 0) {
    return;
  }
  if (correction_var == 1) {
    rtc.setSeconds(rtc.getSeconds() + 1);
    correction_var = 0;
    return;
  }
  if (correction_var == -1) {
    if ((rtc.getSeconds() - 1) < 1) {
      rtc.setSeconds(0);
      correction_var = 0;
      return;
    } else {
      rtc.setSeconds(rtc.getSeconds() - 1);
      correction_var = 0;
      return;
    }
  }
  correction_var = 0;
  return;
}

void triggerAlarm() {
  alarm_Triggered = 1;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.