Skip to content

Commit a0e66da

Browse files
committed
Fixed error management and lora_band configuration
1 parent 2d3b983 commit a0e66da

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

src/Arduino_LoRaConnectionHandler.cpp

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000; /* NOT USED
3333
CTOR/DTOR
3434
******************************************************************************/
3535

36-
LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_appkey) :
36+
LoRaConnectionHandler::LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band band) :
3737
appeui(_appeui),
3838
appkey(_appkey),
39+
band(band),
3940
lastConnectionTickTime(millis()),
4041
connectionTickTimeInterval(CHECK_INTERVAL_IDLE),
4142
keepAlive(false),
4243
_on_connect_event_callback(NULL),
4344
_on_disconnect_event_callback(NULL),
4445
_on_error_event_callback(NULL) {
46+
netConnectionState = NetworkConnectionState::INIT;
4547
}
4648

4749
/******************************************************************************
@@ -89,13 +91,50 @@ void LoRaConnectionHandler::write(const uint8_t *buf, size_t size) {
8991
modem.beginPacket();
9092
modem.write(buf, size);
9193
err = modem.endPacket(true);
92-
if (err > 0) {
93-
Serial.println("Message sent correctly!");
94-
} else {
95-
Serial.println("Error sending message :(");
96-
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
97-
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
98-
}
94+
if (err != size) {
95+
switch (err) {
96+
case -1: {
97+
Serial.println("Message length is bigger than max LoRa packet!");
98+
Serial.println(err);
99+
}
100+
break;
101+
case -2: {
102+
Serial.println("Message ack was not recieved, the message could not be delivered");
103+
}
104+
break;
105+
case 2: {
106+
Serial.println("LoRa generic error (LORA_ERROR)");
107+
}
108+
break;
109+
case 3: {
110+
Serial.println("LoRa malformed param error (LORA_ERROR_PARAM");
111+
}
112+
break;
113+
case 4: {
114+
Serial.println("LoRa chip is busy (LORA_ERROR_BUSY)");
115+
}
116+
break;
117+
case 5: {
118+
Serial.println("LoRa chip overflow error (LORA_ERROR_OVERFLOW)");
119+
}
120+
break;
121+
case 6: {
122+
Serial.println("LoRa no network error (LORA_ERROR_NO_NETWORK)");
123+
}
124+
break;
125+
case 7: {
126+
Serial.println("LoRa rx error (LORA_ERROR_RX)");
127+
}
128+
break;
129+
case 8: {
130+
Serial.println("LoRa unknown error (LORA_ERROR_UNKNOWN)");
131+
}
132+
break;
133+
}
134+
}
135+
else {
136+
Serial.println("Message sent correctly!");
137+
}
99138
}
100139

101140
int LoRaConnectionHandler::read() {
@@ -117,16 +156,18 @@ void LoRaConnectionHandler::update() {
117156
switch (netConnectionState) {
118157
case NetworkConnectionState::INIT: {
119158
Debug.print(DBG_VERBOSE, "::INIT");
120-
159+
if (!modem.begin(band)) {
160+
Debug.print(DBG_VERBOSE, "Failed to start module");
161+
changeConnectionState(NetworkConnectionState::ERROR);
162+
};
121163
delay(1000);
122164

123165
changeConnectionState(NetworkConnectionState::CONNECTING);
124166
}
125167
break;
126168
case NetworkConnectionState::CONNECTING: {
127169
Debug.print(DBG_VERBOSE, "::CONNECTING");
128-
networkStatus = modem.joinOTAA(appeui, appkey);;
129-
170+
networkStatus = modem.joinOTAA(appeui, appkey);
130171
if (networkStatus != true) {
131172
changeConnectionState(NetworkConnectionState::ERROR);
132173
return;

src/Arduino_LoRaConnectionHandler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
class LoRaConnectionHandler : public LPWANConnectionHandler {
3434
public:
35-
LoRaConnectionHandler(const char *_appeui, const char *_appkey);
35+
LoRaConnectionHandler(const char *_appeui, const char *_appkey, _lora_band = EU868);
3636

3737
void init();
3838
unsigned long getTime();
@@ -68,6 +68,7 @@ class LoRaConnectionHandler : public LPWANConnectionHandler {
6868

6969
LoRaModem modem;
7070
const char *appeui, *appkey;
71+
_lora_band band;
7172
unsigned long lastConnectionTickTime;
7273

7374
int connectionTickTimeInterval;

0 commit comments

Comments
 (0)