Skip to content

Commit 8b8a88d

Browse files
Merge pull request #189 from TheThingsNetwork/feature/autobaud
Added autoBaud() to sync baud with RN module
2 parents 79ebd00 + bfd2f3a commit 8b8a88d

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;
@@ -374,8 +375,37 @@ size_t TheThingsNetwork::readResponse(uint8_t prefixTable, uint8_t indexTable, u
374375
return readLine(buffer, size);
375376
}
376377

378+
void TheThingsNetwork::autoBaud()
379+
{
380+
// Courtesy of @jpmeijers
381+
modemStream->setTimeout(2000);
382+
uint8_t attempts = 10;
383+
size_t length = 0;
384+
while (attempts-- && length == 0)
385+
{
386+
delay(100);
387+
modemStream->write((byte)0x00);
388+
modemStream->write(0x55);
389+
modemStream->write(SEND_MSG);
390+
sendCommand(SYS_TABLE, 0, true, false);
391+
sendCommand(SYS_TABLE, SYS_GET, true, false);
392+
sendCommand(SYS_TABLE, SYS_GET_VER, false, false);
393+
modemStream->write(SEND_MSG);
394+
length = modemStream->readBytesUntil('\n', buffer, sizeof(buffer));
395+
}
396+
delay(100);
397+
clearReadBuffer();
398+
modemStream->setTimeout(10000);
399+
baudDetermined = true;
400+
}
401+
377402
void TheThingsNetwork::reset(bool adr)
378403
{
404+
if (!baudDetermined)
405+
{
406+
autoBaud();
407+
}
408+
379409
size_t length = readResponse(SYS_TABLE, SYS_RESET, buffer, sizeof(buffer));
380410

381411
// 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)