TheThingsNetwork / arduino-device-lib

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

API: Default to Serial and Serial1 #5

Closed FokkeZB closed 8 years ago

FokkeZB commented 8 years ago

The library obviously has to work with any Arduino device, but we can simply sketches for The Things Uno and Node by defaulting to Serial for debug and Serial1 for the modem.

Current:

#define debugSerial Serial
#define loraSerial Serial1

void setup() {
  debugSerial.begin(115200);
  loraSerial.begin(57600);

  ttn.init(loraSerial, debugSerial);

  ttn.showStatus();
}

For TT Uno/Node this could be

void setup() {
  ttn.showStatus();
}

If you'd need to override for an other device you'd still call ttn.init():

ttn.init(loraSerial, debugSerial);

Since it would feel to not use init() if you'd use defaults, or if you only need to override one of the two it might be better to dump init() all together and have explicit setters:

ttn.setDebugStream(debugSerial);
ttn.setModemStream(debugSerial);
FokkeZB commented 8 years ago

Maybe initFor would be a better name.

@johanstokking am I right there are three "variables" the library needs to know to work on any Arduino:

Since we can detect the module and Serial is always for USB we only really need to know the Serial Port the LoRaWAN module is on right?

If I'm right then I think we could just remove the init() function and add methods to override the Serial Port for the LoRaWAN module in case you don't have it on Serial1 (or would like to use a different baud rate)

ttn.setModemSerial(modemSerial);

This and removing reset() will simplify a lot:

#include <TheThingsNetwork.h>

const byte appEui[8] = {0x70, 0xB3, 0xD5, 0x7E, 0xE0, 0xE0, 0x01, 0x4A1};
const byte appKey[16] = {0x73, 0x6D, 0x24, 0xD2, 0x69, 0xBE, 0xE3, 0xAE, 0x0E, 0xCE, 0xF0, 0xBB, 0x6C, 0xA4, 0xBA, 0xFE};

TheThingsNetwork ttn;

void setup() {
  while(!ttn.join(appEui, appKey));

  ttn.showStatus();
}

void loop() {
  ttn.sendBytes(payload, sizeof(payload));

  delay(20000);
}
johanstokking commented 8 years ago

The challenge here is that the entire Serial1 keyword, regardless of initialization, is not known on for example the Arduino Uno board. The only way to use it, is either by passing it from the user sketch, or a #define before including the header, so that the header can conditionally reference Serial1.

FokkeZB commented 8 years ago

But we can test if Serial1 exists right? So if it does we can use that (which would work for our Uno and Node). Same goes for Serial for debug. And then we'd have setDebugSerial() and setModemSerial() to override. We could postpone calling .begin() to reset() (which is called from both personalize() and join() now) so the user has every change to override before we actual use them.

johanstokking commented 8 years ago

Yes, but I don't know if it's good to assume that if Serial1 is defined, then that is automatically the modem serial. But indeed, we can have the overrides.

FokkeZB commented 8 years ago

I think it's fine to optimise (minimise) the library for our Uno and Node and have overrides for other devices if needed.

johanstokking commented 8 years ago

There are too many uncertainties and it may cause more issues than that it solves a problem (if any)