Skip to content

Commit bfd2f3a

Browse files
committed
Added autoBaud() to sync baud with RN module
1 parent 9a06bf6 commit bfd2f3a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/TheThingsNetwork.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ TheThingsNetwork::TheThingsNetwork(Stream &modemStream, Stream &debugStream, ttn
287287
{
288288
this->debugStream = &debugStream;
289289
this->modemStream = &modemStream;
290+
this->modemStream->setTimeout(10000);
290291
this->fp = fp;
291292
this->sf = sf;
292293
this->fsb = fsb;
@@ -369,8 +370,37 @@ size_t TheThingsNetwork::readResponse(uint8_t prefixTable, uint8_t indexTable, u
369370
return readLine(buffer, size);
370371
}
371372

373+
void TheThingsNetwork::autoBaud()
374+
{
375+
// Courtesy of @jpmeijers
376+
modemStream->setTimeout(2000);
377+
uint8_t attempts = 10;
378+
size_t length = 0;
379+
while (attempts-- && length == 0)
380+
{
381+
delay(100);
382+
modemStream->write((byte)0x00);
383+
modemStream->write(0x55);
384+
modemStream->write(SEND_MSG);
385+
sendCommand(SYS_TABLE, 0, true, false);
386+
sendCommand(SYS_TABLE, SYS_GET, true, false);
387+
sendCommand(SYS_TABLE, SYS_GET_VER, false, false);
388+
modemStream->write(SEND_MSG);
389+
length = modemStream->readBytesUntil('\n', buffer, sizeof(buffer));
390+
}
391+
delay(100);
392+
clearReadBuffer();
393+
modemStream->setTimeout(10000);
394+
baudDetermined = true;
395+
}
396+
372397
void TheThingsNetwork::reset(bool adr)
373398
{
399+
if (!baudDetermined)
400+
{
401+
autoBaud();
402+
}
403+
374404
size_t length = readResponse(SYS_TABLE, SYS_RESET, buffer, sizeof(buffer));
375405

376406
// buffer contains "RN2xx3 1.x.x ...", splitting model from version

src/TheThingsNetwork.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class TheThingsNetwork
4242
uint8_t sf;
4343
uint8_t fsb;
4444
char buffer[512];
45+
bool baudDetermined = false;
4546
void (*messageCallback)(const uint8_t *payload, size_t size, port_t port);
4647

4748
void clearReadBuffer();
@@ -53,6 +54,7 @@ class TheThingsNetwork
5354
void debugPrintMessage(uint8_t type, uint8_t index, const char *value = NULL);
5455

5556
void reset(bool adr = true);
57+
void autoBaud();
5658
void configureEU868(uint8_t sf);
5759
void configureUS915(uint8_t sf, uint8_t fsb);
5860
void configureChannels(uint8_t sf, uint8_t fsb);

0 commit comments

Comments
 (0)