Skip to content

Commit 389ab70

Browse files
savnikjohanstokking
authored andcommitted
Hard reset indicator and functionality
* Adding attempts to readLine and state marker if radio module is unresponsive * Fix spacing around brackets * Adding attempts to TheTHingsNetwork.h * No need for else when if has return * Set default in header file * decrement attempts * Proposal to handle exception when RN module does not respond * Changed variable name from radioModuleInvalidState to needsHardReset * Added hardReset function with non-blocking delay. made needsHardReset a public variable * Updating documentation for hardreset * Changed wording of unresponsive rn module in readline function * Spacing in if statement in readline function * Removing comment at readline function * Fixed spelling mistake of millis in resetHard * Replacing single qoute with double qoute to fix warning: character constant too long for its type * remove empty line * Fixed missing ( * moved adr parameter to reset and added parameter description for hardreset * Spelling mistake * Reformulated the non i/o blocking wait function to make it more understandable * Updated the way while brackets are placed in resetHard * Updated hardreset documentation to include initial pinmode setup * rewording of hardreset output pin config * Using delay(1000) instead of custom delay function to ensure that ESP8266 background operations will work
1 parent be30091 commit 389ab70

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

docs/TheThingsNetwork.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ void reset(bool adr);
2828

2929
- `bool adr`: Enable/disable Adaptive Data Rate.
3030

31+
## Method: `hardReset`
32+
33+
Performs a hardware reset of the RN module. Input parameter is the pin which the reset pin from the module is connected to. This does clear saved state, e.g. provisioned keys.
34+
35+
```c
36+
void hardReset(uint8_t resetPin);
37+
```
38+
39+
- `uint8_t resetPin`: The output pin that is connected to the module's reset pin. The output pin should be configured as output and set to high by the user.
40+
3141
## Method: `getHardwareEui`
3242
3343
Gets the unique hardware EUI, often used as the DevEUI.

src/TheThingsNetwork.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,19 @@ void TheThingsNetwork::clearReadBuffer()
357357
}
358358
}
359359

360-
size_t TheThingsNetwork::readLine(char *buffer, size_t size)
360+
size_t TheThingsNetwork::readLine(char *buffer, size_t size, uint8_t attempts)
361361
{
362362
size_t read = 0;
363-
while (read == 0)
363+
while (!read && attempts--)
364364
{
365365
read = modemStream->readBytesUntil('\n', buffer, size);
366366
}
367+
if (attempts<=0)
368+
{ // If attempts is activated return 0 and set RN state marker
369+
this->needsHardReset = true; // Inform the application about the radio module is not responsive.
370+
debugPrintLn("No response from RN module.");
371+
return 0;
372+
}
367373
buffer[read - 1] = '\0'; // set \r to \0
368374
return read;
369375
}
@@ -417,7 +423,7 @@ void TheThingsNetwork::reset(bool adr)
417423
size_t length = readResponse(SYS_TABLE, SYS_RESET, buffer, sizeof(buffer));
418424

419425
autoBaud();
420-
length = readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_VER, buffer, sizeof(buffer));
426+
length = readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_VER, buffer, sizeof(buffer));
421427

422428
// buffer contains "RN2xx3[xx] x.x.x ...", splitting model from version
423429
char *model = strtok(buffer, " ");
@@ -436,6 +442,13 @@ void TheThingsNetwork::reset(bool adr)
436442
sendMacSet(MAC_ADR, "off");
437443
}
438444
this->adr = adr;
445+
this->needsHardReset = false;
446+
}
447+
448+
void TheThingsNetwork::resetHard(uint8_t resetPin){
449+
digitalWrite(resetPin, LOW);
450+
delay(1000);
451+
digitalWrite(resetPin, HIGH);
439452
}
440453

441454
void TheThingsNetwork::saveState()
@@ -776,8 +789,8 @@ void TheThingsNetwork::configureKR920_923()
776789
void TheThingsNetwork::configureIN865_867()
777790
{
778791
sendMacSet(MAC_ADR, "off"); // TODO: remove when ADR is implemented for this plan
779-
sendMacSet(MAC_RX2, "2 866550000"); // SF10
780-
792+
sendMacSet(MAC_RX2, "2 866550000"); // SF10
793+
781794
// Disable the three default LoRaWAN channels
782795
sendChSet(MAC_CHANNEL_STATUS, 0, "off");
783796
sendChSet(MAC_CHANNEL_STATUS, 1, "off");
@@ -1035,7 +1048,7 @@ void TheThingsNetwork::sleep(uint32_t mseconds)
10351048
}
10361049

10371050
void TheThingsNetwork::wake()
1038-
{
1051+
{
10391052
autoBaud();
10401053
}
10411054

@@ -1051,7 +1064,7 @@ void TheThingsNetwork::linkCheck(uint16_t seconds)
10511064
modemStream->write(buffer);
10521065
modemStream->write(SEND_MSG);
10531066
debugPrintLn(buffer);
1054-
waitForOk();
1067+
waitForOk();
10551068
}
10561069

10571070
uint8_t TheThingsNetwork::getLinkCheckGateways()

src/TheThingsNetwork.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class TheThingsNetwork
6161
void (*messageCallback)(const uint8_t *payload, size_t size, port_t port);
6262

6363
void clearReadBuffer();
64-
size_t readLine(char *buffer, size_t size);
64+
size_t readLine(char *buffer, size_t size, uint8_t attempts = 3);
6565
size_t readResponse(uint8_t prefixTable, uint8_t indexTable, uint8_t index, char *buffer, size_t size);
6666
size_t readResponse(uint8_t table, uint8_t index, char *buffer, size_t size);
6767

@@ -88,8 +88,11 @@ class TheThingsNetwork
8888
void sendGetValue(uint8_t table, uint8_t prefix, uint8_t index);
8989

9090
public:
91+
bool needsHardReset = false;
92+
9193
TheThingsNetwork(Stream &modemStream, Stream &debugStream, ttn_fp_t fp, uint8_t sf = TTN_DEFAULT_SF, uint8_t fsb = TTN_DEFAULT_FSB);
9294
void reset(bool adr = true);
95+
void resetHard(uint8_t resetPin);
9396
void showStatus();
9497
size_t getHardwareEui(char *buffer, size_t size);
9598
size_t getAppEui(char *buffer, size_t size);
@@ -106,7 +109,7 @@ class TheThingsNetwork
106109
void wake();
107110
void saveState();
108111
void linkCheck(uint16_t seconds);
109-
uint8_t getLinkCheckGateways();
112+
uint8_t getLinkCheckGateways();
110113
uint8_t getLinkCheckMargin();
111114
};
112115

0 commit comments

Comments
 (0)