ARMmbed / mbed-os-example-lorawan

Simple LoRaWAN example application for mbed OS
Apache License 2.0
79 stars 93 forks source link

LoRaWAN event takes long to come #160

Closed doganberk95 closed 5 years ago

doganberk95 commented 5 years ago

Hi, I am trying to implement a low power sensor node using an ATmega328p processor with Arduino bootloader. I call os_runloop_once in the loop once and acquiring the EV_TXCOMPLETE can take up to a few minutes, but usually around 20-30 seconds. Due to my application, I need to keep sensors on until I get the event and this violates my low power needs. Any help/idea is appreciated. Attached you can find my related code excerpt(of course not functional due to the deleted parts):

...
volatile boolean alarm = true;
...
void onEvent (ev_t ev) {
  if (ev == EV_TXCOMPLETE) {
    Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
    set_next_alarm();
    DS3231_clear_a2f();
    if(alarm == false){
    delay(100);
    LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
  }
    // Schedule next transmission
    os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
  }
}

void do_send(osjob_t* j) {
  // Payload to send (uplink)
  alarm = false;
  digitalWrite(5, HIGH); // Turning on the mosfet the keep sensors alive
  ...
  ...
  uint8_t message[7];
  message [0] = temp >> 8;
  message [1] = temp;
  message [2] = econd >> 8;
  message [3] = econd;
  message [4] = pH;
  message [5] = random(0, 250);
  message [6] = random(0, 250);
  // Check if there is not a current TX/RX job running
  if (LMIC.opmode & OP_TXRXPEND) {
    Serial.println(F("OP_TXRXPEND, not sending"));
  } else {
    // Prepare upstream data transmission at the next possible time.
    LMIC_setTxData2(1, message, sizeof(message) - 1, 0);
    Serial.println(F("Sending uplink packet..."));
    digitalWrite(13, LOW);
  }
  // Next TX is scheduled after TX_COMPLETE event.
  digitalWrite(5, LOW); // shut the MOSFET
}

void setup() {
  pinMode(5, OUTPUT); // MOSFET Switch
  Serial.begin(115200);
  Serial.println(F("Starting..."));
  pinMode(ECPin, INPUT);
  pinMode(ECPower, OUTPUT); //Setting pin for sourcing current
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, CHANGE);

  Wire.begin();
  DS3231_init(DS3231_CONTROL_INTCN);
  DS3231_clear_a2f();
  set_next_alarm();
  sensors.begin();

  // LMIC init
  os_init();
  // Reset the MAC state. Session and pending data transfers will be discarded.
  LMIC_reset();
  // Set static session parameters.
  LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
  // Disable link check validation
  LMIC_setLinkCheckMode(0);
  // TTN uses SF9 for its RX2 window.
  LMIC.dn2Dr = DR_SF9;
  // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
  LMIC_setDrTxpow(DR_SF8, 14);
  // Start job
  do_send(&sendjob);
}
void loop() {
  if (alarm == true) {
    os_runloop_once();
  }
}
void handleInterrupt() {
  alarm = true;
}
ciarmcom commented 5 years ago

ARM Internal Ref: IOTCELL-2146

AnttiKauppila commented 5 years ago

“At this time, due to our prioritisation of work on customer projects, we are unable to devote the time to fix this issue. As an open source project, we welcome fixes to Mbed OS, so if would like to contribute a fix yourself then we would very much welcome that, please see our contribution guidelines here - https://os.mbed.com/docs/mbed-os/v5.14/contributing/workflow.html#contributions