diff --git a/docs/TheThingsNetwork.md b/docs/TheThingsNetwork.md index 94130b80..c23384b7 100644 --- a/docs/TheThingsNetwork.md +++ b/docs/TheThingsNetwork.md @@ -28,6 +28,16 @@ void reset(bool adr); - `bool adr`: Enable/disable Adaptive Data Rate. +## Method: `hardReset` + +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. + +```c +void hardReset(uint8_t resetPin); +``` + +- `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. + ## Method: `getHardwareEui` Gets the unique hardware EUI, often used as the DevEUI. diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index c6b4b42c..7b06eb11 100644 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -357,13 +357,19 @@ void TheThingsNetwork::clearReadBuffer() } } -size_t TheThingsNetwork::readLine(char *buffer, size_t size) +size_t TheThingsNetwork::readLine(char *buffer, size_t size, uint8_t attempts) { size_t read = 0; - while (read == 0) + while (!read && attempts--) { read = modemStream->readBytesUntil('\n', buffer, size); } + if (attempts<=0) + { // If attempts is activated return 0 and set RN state marker + this->needsHardReset = true; // Inform the application about the radio module is not responsive. + debugPrintLn("No response from RN module."); + return 0; + } buffer[read - 1] = '\0'; // set \r to \0 return read; } @@ -417,7 +423,7 @@ void TheThingsNetwork::reset(bool adr) size_t length = readResponse(SYS_TABLE, SYS_RESET, buffer, sizeof(buffer)); autoBaud(); - length = readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_VER, buffer, sizeof(buffer)); + length = readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_VER, buffer, sizeof(buffer)); // buffer contains "RN2xx3[xx] x.x.x ...", splitting model from version char *model = strtok(buffer, " "); @@ -436,6 +442,13 @@ void TheThingsNetwork::reset(bool adr) sendMacSet(MAC_ADR, "off"); } this->adr = adr; + this->needsHardReset = false; +} + +void TheThingsNetwork::resetHard(uint8_t resetPin){ + digitalWrite(resetPin, LOW); + delay(1000); + digitalWrite(resetPin, HIGH); } void TheThingsNetwork::saveState() @@ -776,8 +789,8 @@ void TheThingsNetwork::configureKR920_923() void TheThingsNetwork::configureIN865_867() { sendMacSet(MAC_ADR, "off"); // TODO: remove when ADR is implemented for this plan - sendMacSet(MAC_RX2, "2 866550000"); // SF10 - + sendMacSet(MAC_RX2, "2 866550000"); // SF10 + // Disable the three default LoRaWAN channels sendChSet(MAC_CHANNEL_STATUS, 0, "off"); sendChSet(MAC_CHANNEL_STATUS, 1, "off"); @@ -1035,7 +1048,7 @@ void TheThingsNetwork::sleep(uint32_t mseconds) } void TheThingsNetwork::wake() -{ +{ autoBaud(); } @@ -1051,7 +1064,7 @@ void TheThingsNetwork::linkCheck(uint16_t seconds) modemStream->write(buffer); modemStream->write(SEND_MSG); debugPrintLn(buffer); - waitForOk(); + waitForOk(); } uint8_t TheThingsNetwork::getLinkCheckGateways() diff --git a/src/TheThingsNetwork.h b/src/TheThingsNetwork.h index 92f84e13..f961ff51 100644 --- a/src/TheThingsNetwork.h +++ b/src/TheThingsNetwork.h @@ -57,7 +57,7 @@ class TheThingsNetwork void (*messageCallback)(const uint8_t *payload, size_t size, port_t port); void clearReadBuffer(); - size_t readLine(char *buffer, size_t size); + size_t readLine(char *buffer, size_t size, uint8_t attempts = 3); size_t readResponse(uint8_t prefixTable, uint8_t indexTable, uint8_t index, char *buffer, size_t size); size_t readResponse(uint8_t table, uint8_t index, char *buffer, size_t size); @@ -84,8 +84,11 @@ class TheThingsNetwork void sendGetValue(uint8_t table, uint8_t prefix, uint8_t index); public: + bool needsHardReset = false; + TheThingsNetwork(Stream &modemStream, Stream &debugStream, ttn_fp_t fp, uint8_t sf = TTN_DEFAULT_SF, uint8_t fsb = TTN_DEFAULT_FSB); void reset(bool adr = true); + void resetHard(uint8_t resetPin); void showStatus(); size_t getHardwareEui(char *buffer, size_t size); size_t getAppEui(char *buffer, size_t size); @@ -102,7 +105,7 @@ class TheThingsNetwork void wake(); void saveState(); void linkCheck(uint16_t seconds); - uint8_t getLinkCheckGateways(); + uint8_t getLinkCheckGateways(); uint8_t getLinkCheckMargin(); };