Skip to content

Commit

Permalink
Hard reset indicator and functionality
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
savnik authored and johanstokking committed Jan 14, 2019
1 parent be30091 commit 389ab70
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
10 changes: 10 additions & 0 deletions docs/TheThingsNetwork.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 20 additions & 7 deletions src/TheThingsNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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, " ");
Expand All @@ -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()
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -1035,7 +1048,7 @@ void TheThingsNetwork::sleep(uint32_t mseconds)
}

void TheThingsNetwork::wake()
{
{
autoBaud();
}

Expand All @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions src/TheThingsNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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);

Expand All @@ -88,8 +88,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);
Expand All @@ -106,7 +109,7 @@ class TheThingsNetwork
void wake();
void saveState();
void linkCheck(uint16_t seconds);
uint8_t getLinkCheckGateways();
uint8_t getLinkCheckGateways();
uint8_t getLinkCheckMargin();
};

Expand Down

0 comments on commit 389ab70

Please sign in to comment.