Skip to content

Commit 433a221

Browse files
hallardjpmeijers
authored andcommitted
Fixed LoRa module not waked from sleep by autoBaud sometimes (#231)
* fixed RN2xxx not waked from sleep by autoBaud sometimes * changes requested by johan * changes requested by johan * johan request * cosmetic * HAL abstration of modemStream * Ability to use AltSoftSerial Library * Code optimization, added ~700 bytes flash for sketch * HardwareSerial only used on TheThings Node and Uno * Applied changes requested by johan * Fixed dual check * cosmetic * Added Hardware Serial only for the things node
1 parent 20d6964 commit 433a221

File tree

2 files changed

+85
-111
lines changed

2 files changed

+85
-111
lines changed

src/TheThingsNetwork.cpp

Lines changed: 71 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ uint8_t receivedPort(const char *s)
287287
return port;
288288
}
289289

290-
TheThingsNetwork::TheThingsNetwork(Stream &modemStream, Stream &debugStream, ttn_fp_t fp, uint8_t sf, uint8_t fsb)
290+
TheThingsNetwork::TheThingsNetwork(SerialType &modemStream, Stream &debugStream, ttn_fp_t fp, uint8_t sf, uint8_t fsb)
291291
{
292292
this->debugStream = &debugStream;
293293
this->modemStream = &modemStream;
@@ -387,6 +387,12 @@ size_t TheThingsNetwork::readResponse(uint8_t prefixTable, uint8_t indexTable, u
387387
return readLine(buffer, size);
388388
}
389389

390+
size_t TheThingsNetwork::checkModuleAvailable()
391+
{
392+
// Send sys get ver check we have an answer
393+
return readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_VER, buffer, sizeof(buffer));
394+
}
395+
390396
void TheThingsNetwork::autoBaud()
391397
{
392398
// Courtesy of @jpmeijers
@@ -396,19 +402,51 @@ void TheThingsNetwork::autoBaud()
396402
while (attempts-- && length == 0)
397403
{
398404
delay(100);
405+
// Send break + Autobaud
399406
modemStream->write((byte)0x00);
400407
modemStream->write(0x55);
401408
modemStream->write(SEND_MSG);
402-
sendCommand(SYS_TABLE, 0, true, false);
403-
sendCommand(SYS_TABLE, SYS_GET, true, false);
404-
sendCommand(SYS_TABLE, SYS_GET_VER, false, false);
405-
modemStream->write(SEND_MSG);
406-
length = modemStream->readBytesUntil('\n', buffer, sizeof(buffer));
409+
// check we can talk
410+
length = checkModuleAvailable();
411+
412+
// We succeeded talking to the module ?
413+
baudDetermined = (length > 0) ;
407414
}
408415
delay(100);
409416
clearReadBuffer();
410417
modemStream->setTimeout(10000);
411-
baudDetermined = true;
418+
}
419+
420+
bool TheThingsNetwork::isSleeping()
421+
{
422+
return !baudDetermined;
423+
}
424+
425+
void TheThingsNetwork::wake()
426+
{
427+
// If sleeping
428+
if (isSleeping())
429+
{
430+
// Send a 0 at lower speed to be sure always received
431+
// as a character a 57600 baud rate
432+
modemStream->flush();
433+
#ifdef HARDWARE_UART
434+
modemStream->begin(2400);
435+
#endif
436+
modemStream->write((uint8_t) 0x00);
437+
modemStream->flush();
438+
delay(20);
439+
// set baudrate back to normal and send autobaud
440+
#ifdef HARDWARE_UART
441+
modemStream->begin(57600);
442+
#endif
443+
modemStream->write((uint8_t)0x55);
444+
modemStream->flush();
445+
modemStream->write(SEND_MSG);
446+
if (checkModuleAvailable() > 0) {
447+
baudDetermined = true;
448+
}
449+
}
412450
}
413451

414452
void TheThingsNetwork::reset(bool adr)
@@ -612,18 +650,16 @@ void TheThingsNetwork::showStatus()
612650
debugPrintIndex(SHOW_RX_DELAY_2, buffer);
613651
}
614652

615-
void TheThingsNetwork::configureEU868()
653+
// Puting this common function saves 238 bytes of flash
654+
void TheThingsNetwork::configureChannelsFreq(uint32_t freq, uint8_t first, uint8_t last, uint8_t first_dr)
616655
{
617-
sendMacSet(MAC_RX2, "3 869525000");
618-
sendChSet(MAC_CHANNEL_DRRANGE, 1, "0 6");
619-
620-
char buf[10];
621-
uint32_t freq = 867100000;
622656
uint8_t ch;
623-
for (ch = 0; ch < 8; ch++)
657+
char buf[10];
658+
659+
for (ch = first; ch <= last; ch++)
624660
{
625661
sendChSet(MAC_CHANNEL_DCYCLE, ch, "799");
626-
if (ch > 2)
662+
if (ch > first_dr)
627663
{
628664
sprintf(buf, "%lu", freq);
629665
sendChSet(MAC_CHANNEL_FREQ, ch, buf);
@@ -632,6 +668,13 @@ void TheThingsNetwork::configureEU868()
632668
freq = freq + 200000;
633669
}
634670
}
671+
}
672+
673+
void TheThingsNetwork::configureEU868()
674+
{
675+
sendMacSet(MAC_RX2, "3 869525000");
676+
sendChSet(MAC_CHANNEL_DRRANGE, 1, "0 6");
677+
configureChannelsFreq(867100000, 0, 8, 2);
635678
sendMacSet(MAC_PWRIDX, TTN_PWRIDX_EU868);
636679
}
637680

@@ -665,24 +708,9 @@ void TheThingsNetwork::configureAS920_923()
665708
* CH0 = 923.2MHz
666709
* CH1 = 923.4MHz
667710
*/
668-
sendMacSet(MAC_ADR, "off"); // TODO: remove when ADR is implemented for this plan
669711
sendMacSet(MAC_RX2, "2 923200000");
670712

671-
char buf[10];
672-
uint32_t freq = 922000000;
673-
uint8_t ch;
674-
for (ch = 0; ch < 8; ch++)
675-
{
676-
sendChSet(MAC_CHANNEL_DCYCLE, ch, "799");
677-
if (ch > 1)
678-
{
679-
sprintf(buf, "%lu", freq);
680-
sendChSet(MAC_CHANNEL_FREQ, ch, buf);
681-
sendChSet(MAC_CHANNEL_DRRANGE, ch, "0 5");
682-
sendChSet(MAC_CHANNEL_STATUS, ch, "on");
683-
freq = freq + 200000;
684-
}
685-
}
713+
configureChannelsFreq(922000000, 0, 8, 1);
686714
// TODO: SF7BW250/DR6 channel, not properly supported by RN2903AS yet
687715
//sendChSet(MAC_CHANNEL_DCYCLE, 8, "799");
688716
//sendChSet(MAC_CHANNEL_FREQ, 8, "922100000");
@@ -701,21 +729,7 @@ void TheThingsNetwork::configureAS923_925()
701729
sendMacSet(MAC_ADR, "off"); // TODO: remove when ADR is implemented for this plan
702730
sendMacSet(MAC_RX2, "2 923200000");
703731

704-
char buf[10];
705-
uint32_t freq = 923600000;
706-
uint8_t ch;
707-
for (ch = 0; ch < 8; ch++)
708-
{
709-
sendChSet(MAC_CHANNEL_DCYCLE, ch, "799");
710-
if (ch > 1)
711-
{
712-
sprintf(buf, "%lu", freq);
713-
sendChSet(MAC_CHANNEL_FREQ, ch, buf);
714-
sendChSet(MAC_CHANNEL_DRRANGE, ch, "0 5");
715-
sendChSet(MAC_CHANNEL_STATUS, ch, "on");
716-
freq = freq + 200000;
717-
}
718-
}
732+
configureChannelsFreq(923600000, 0, 8, 1);
719733
// TODO: SF7BW250/DR6 channel, not properly supported by RN2903AS yet
720734
//sendChSet(MAC_CHANNEL_DCYCLE, 8, "799");
721735
//sendChSet(MAC_CHANNEL_FREQ, 8, "924500000");
@@ -734,18 +748,7 @@ void TheThingsNetwork::configureKR920_923()
734748
sendChSet(MAC_CHANNEL_STATUS, 0, "off");
735749
sendChSet(MAC_CHANNEL_STATUS, 1, "off");
736750

737-
char buf[10];
738-
uint32_t freq = 922100000;
739-
uint8_t ch;
740-
for (ch = 2; ch < 9; ch++)
741-
{
742-
sendChSet(MAC_CHANNEL_DCYCLE, ch, "799");
743-
sprintf(buf, "%lu", freq);
744-
sendChSet(MAC_CHANNEL_FREQ, ch, buf);
745-
sendChSet(MAC_CHANNEL_DRRANGE, ch, "0 5");
746-
sendChSet(MAC_CHANNEL_STATUS, ch, "on");
747-
freq = freq + 200000;
748-
}
751+
configureChannelsFreq(922100000, 2, 9, 0);
749752
sendMacSet(MAC_PWRIDX, TTN_PWRIDX_KR920_923);
750753
}
751754

@@ -865,24 +868,13 @@ bool TheThingsNetwork::sendChSet(uint8_t index, uint8_t channel, const char *val
865868
{
866869
clearReadBuffer();
867870
char ch[5];
868-
if (channel > 9)
869-
{
870-
ch[0] = ((channel - (channel % 10)) / 10) + 48;
871-
ch[1] = (channel % 10) + 48;
872-
ch[2] = '\0';
873-
}
874-
else
875-
{
876-
ch[0] = channel + 48;
877-
ch[1] = '\0';
878-
}
871+
sprintf(ch, "%d ", channel);
879872
debugPrint(F(SENDING));
880873
sendCommand(MAC_TABLE, MAC_PREFIX, true);
881874
sendCommand(MAC_TABLE, MAC_SET, true);
882875
sendCommand(MAC_GET_SET_TABLE, MAC_CH, true);
883876
sendCommand(MAC_CH_TABLE, index, true);
884877
modemStream->write(ch);
885-
modemStream->write(" ");
886878
modemStream->write(value);
887879
modemStream->write(SEND_MSG);
888880
debugPrint(channel);
@@ -910,44 +902,16 @@ bool TheThingsNetwork::sendPayload(uint8_t mode, uint8_t port, uint8_t *payload,
910902
sendCommand(MAC_TABLE, MAC_PREFIX, true);
911903
sendCommand(MAC_TABLE, MAC_TX, true);
912904
sendCommand(MAC_TX_TABLE, mode, true);
913-
char sport[4];
914-
if (port > 99)
915-
{
916-
sport[0] = ((port - (port % 100)) / 100) + 48;
917-
sport[1] = (((port % 100) - (port % 10)) / 10) + 48;
918-
sport[2] = (port % 10) + 48;
919-
sport[3] = '\0';
920-
}
921-
else if (port > 9)
922-
{
923-
sport[0] = ((port - (port % 10)) / 10) + 48;
924-
sport[1] = (port % 10) + 48;
925-
sport[2] = '\0';
926-
}
927-
else
928-
{
929-
sport[0] = port + 48;
930-
sport[1] = '\0';
931-
}
932-
modemStream->write(sport);
933-
modemStream->print(" ");
934-
debugPrint(sport);
935-
debugPrint(F(" "));
905+
char buf[5];
906+
sprintf(buf, "%d ", port);
907+
modemStream->write(buf);
908+
debugPrint(buf);
936909
uint8_t i = 0;
937910
for (i = 0; i < length; i++)
938911
{
939-
if (payload[i] < 16)
940-
{
941-
modemStream->print("0");
942-
modemStream->print(payload[i], HEX);
943-
debugPrint(F("0"));
944-
debugPrint(payload[i], HEX);
945-
}
946-
else
947-
{
948-
modemStream->print(payload[i], HEX);
949-
debugPrint(payload[i], HEX);
950-
}
912+
sprintf(buf, "%02X", payload[i] );
913+
modemStream->print(buf);
914+
debugPrint(buf);
951915
}
952916
modemStream->write(SEND_MSG);
953917
debugPrintLn();
@@ -969,11 +933,9 @@ void TheThingsNetwork::sleep(uint32_t mseconds)
969933
modemStream->write(buffer);
970934
modemStream->write(SEND_MSG);
971935
debugPrintLn(buffer);
972-
}
973936

974-
void TheThingsNetwork::wake()
975-
{
976-
autoBaud();
937+
// to be determined back on wake up
938+
baudDetermined = false;
977939
}
978940

979941
void TheThingsNetwork::linkCheck(uint16_t seconds)

src/TheThingsNetwork.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020

2121
#define TTN_BUFFER_SIZE 300
2222

23+
// The Things Products devices
24+
// Things Node only need this, we won't impact Things Uno user
25+
#if defined(ARDUINO_THINGS_NODE)
26+
typedef HardwareSerial SerialType;
27+
#define HARDWARE_UART
28+
#else
29+
typedef Stream SerialType;
30+
#endif
31+
2332
typedef uint8_t port_t;
2433

2534
enum ttn_response_t
@@ -42,7 +51,7 @@ enum ttn_fp_t
4251
class TheThingsNetwork
4352
{
4453
private:
45-
Stream *modemStream;
54+
SerialType *modemStream;
4655
Stream *debugStream;
4756
ttn_fp_t fp;
4857
uint8_t sf;
@@ -60,7 +69,9 @@ class TheThingsNetwork
6069
void debugPrintIndex(uint8_t index, const char *value = NULL);
6170
void debugPrintMessage(uint8_t type, uint8_t index, const char *value = NULL);
6271

72+
size_t checkModuleAvailable();
6373
void autoBaud();
74+
void configureChannelsFreq(uint32_t freq, uint8_t first, uint8_t last, uint8_t first_dr);
6475
void configureEU868();
6576
void configureUS915(uint8_t fsb);
6677
void configureAS920_923();
@@ -78,7 +89,7 @@ class TheThingsNetwork
7889
void sendGetValue(uint8_t table, uint8_t prefix, uint8_t index);
7990

8091
public:
81-
TheThingsNetwork(Stream &modemStream, Stream &debugStream, ttn_fp_t fp, uint8_t sf = TTN_DEFAULT_SF, uint8_t fsb = TTN_DEFAULT_FSB);
92+
TheThingsNetwork(SerialType &modemStream, Stream &debugStream, ttn_fp_t fp, uint8_t sf = TTN_DEFAULT_SF, uint8_t fsb = TTN_DEFAULT_FSB);
8293
void reset(bool adr = true);
8394
void showStatus();
8495
size_t getHardwareEui(char *buffer, size_t size);
@@ -93,6 +104,7 @@ class TheThingsNetwork
93104
ttn_response_t sendBytes(const uint8_t *payload, size_t length, port_t port = 1, bool confirm = false, uint8_t sf = 0);
94105
ttn_response_t poll(port_t port = 1, bool confirm = false);
95106
void sleep(uint32_t mseconds);
107+
bool isSleeping();
96108
void wake();
97109
void saveState();
98110
void linkCheck(uint16_t seconds);

0 commit comments

Comments
 (0)