TheThingsNetwork / arduino-device-lib

Arduino Library for TTN Devices
MIT License
207 stars 96 forks source link

Wake function - feature request #221

Closed savnik closed 6 years ago

savnik commented 6 years ago

A wake function to wake the device from sleep. From the data sheet this function is described as:

The module can be forced to exit from Sleep by sending a break condition followed by a 0x55 character at the new baud rate. Note that the break condition needs to be long enough not to be interpreted as a valid character at the current baud rate.

savnik commented 6 years ago

A solution to this could be:

// "emulate" break condition
    Serial1.flush();

    Serial1.begin(300);
    Serial1.write((uint8_t)0x00);
    Serial1.flush();

    delay(50);

    // set baudrate
    Serial1.begin(57600);
    Serial1.write((uint8_t)0x55);
    Serial1.flush();

That works for me with a hardware serial.

johanstokking commented 6 years ago

In the class, we don't use serial but Stream&. This is so allow for interfacing that doesn't directly rely on a serial, let alone a serial implementation: Serial(N), SerialUSB, HardwareSerial, SoftwareSerial, AltSoftSerial, etc. So begin() is not available.

Also, TheThingsNetwork class does not declare and begin the serial at all, that's up to the caller. It would be weird to add it to a wake() function I think.

BTW, are you using baudrate 300 intentionally?

alexbn71 commented 6 years ago

Could be sufficient to make "void autoBaud();" a public function and call this function from code to wake up from sleep the module.

johanstokking commented 6 years ago

@savnik @alexbn71 please advise/test keeping in mind that TheThingsNetwork library cannot call begin() as it works with the abstract Stream

alexbn71 commented 6 years ago

I know @johanstokking , in fact the function autoBaud() already defined in the library uses Stream object.

alexbn71 commented 6 years ago

I've tested this change and it works... I moved autoBaud() into public functions section then used a call ttn.autoBaud(); to wake up the module before the expiry of the sleep time.

johanstokking commented 6 years ago

OK, can you file a PR? Maybe it's nice to still have a method wake() that only calls autoBaud() to keep the interface clean and to reserve ourselves the freedom to do other magic stuff in there

alexbn71 commented 6 years ago

I perfectly agree with you, I do it...

johanstokking commented 6 years ago

Closed by #225

hallard commented 6 years ago

it's possible to call begin casting the object as HardwareSerial It's now done in PR #231

  HardwareSerial *pLora = (HardwareSerial*) modemStream;

  // Send a 0 at lower speed to be sure always received
  // as a character a 57600 baud rate
  pLora->flush();
  pLora->begin(2400);
  pLora->write((uint8_t) 0x00);
  pLora->flush();
johanstokking commented 6 years ago

This should be done in the sketch, we can't put this in the library as we can't assert modemStream is a HardwareSerial