Skip to content

Commit

Permalink
margin on device status MAC command must be fine now - was always zer…
Browse files Browse the repository at this point in the history
…o. DEBUG is less verbose.
  • Loading branch information
clavisound committed Dec 10, 2024
1 parent 72c983b commit 3dac526
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 50 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ The majority of the work was done by Hendrik Hagendorn and Ideetron B.V. Thanks
- [x] old console
- [x] Join SF10 with Helium. Success everytime on 1st window but not in first attempt.
- [x] chripstack Console
- [x] Join SF8 with Helium chripstack outdors. Success on 1st window in second or third attempt.
- [x] Join SF9 with Helium chripstack outdors. Success on 1st window.
- [x] Join SF9 on Helium chripstack outdors. Success on 1st window.
- [x] Join SF8 on Helium chripstack outdors. Success on 1st window in second or third attempt.
- [x] Join SF7 on Helium and power 0dBm in different room. Success on 1st window.
- [x] Downlinks
- [x] Helium on 2nd window (SF12) always works.
- [x] SetPower
Expand All @@ -47,10 +48,7 @@ The majority of the work was done by Hendrik Hagendorn and Ideetron B.V. Thanks
- [x] NbTrans
- [x] Channel Mask
- [x] RxTimingSetup

## MAC commands added but untested

- [ ] Added battery Level to DevStatusAns. Battery status is working, I think margin is wrong.
- [x] DevStatusAns. Battery status is working, I think margin is fine.

# Untested

Expand Down
80 changes: 43 additions & 37 deletions SlimLoRa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,12 @@ void printHex(uint8_t *value, uint8_t len){
}

void SlimLoRa::printDownlink(){
Serial.print(F("\nPort Down\t: "));Serial.print(downPort);
Serial.print(F("\nfopts.length\t: "));Serial.print(f_options_length);
Serial.print(F("\nPacket Length\t: "));Serial.print(packet_length);
Serial.print(F("\nPort Down\t: ")); Serial.print(downPort);
Serial.print(F("\nfopts.length\t: ")); Serial.print(f_options_length);
Serial.print(F("\nPacket Length\t: ")); Serial.print(packet_length);
Serial.print(F("\nPayload Length\t: "));Serial.print(payload_length);
Serial.print(F("\nSNR 8bit\t: ")); Serial.print(last_packet_snrB);
Serial.print(F("\nSNR 8bit / 4\t: ")); Serial.print(last_packet_snr_);
Serial.flush();
}

Expand Down Expand Up @@ -255,16 +257,16 @@ void printNOWEB(){
void SlimLoRa::setArrayEEPROM(uint16_t eepromAddr, uint8_t *arrayData, uint8_t size) {
for ( uint8_t i = 0; i < size; i++ ) {
EEPROM.update(eepromAddr + i, arrayData[i]);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
if ( i == 0 ) {
Serial.print(F("\nWRITE EEPROM Address: 0x"));Serial.print(eepromAddr + i, HEX);Serial.print(F("->0x"));Serial.print(arrayData[i], HEX);
Serial.print(F("WRITE EEPROM Address: 0x"));Serial.print(eepromAddr + i, HEX);Serial.print(F("->0x"));Serial.print(arrayData[i], HEX);
} else {
Serial.print(F(", "));Serial.print(eepromAddr + i, HEX);Serial.print(F("->0x"));Serial.print(arrayData[i], HEX);
}
#endif
}
#if DEBUG_SLIM == 1
Serial.print(F("\nWRITE: "));printHex(arrayData, size);
#if DEBUG_SLIM > 1
Serial.print(F("WRITE: "));printHex(arrayData, size);
#endif
}

Expand All @@ -279,15 +281,15 @@ void SlimLoRa::setArrayEEPROM(uint16_t eepromAddr, uint8_t *arrayData, uint8_t s
void SlimLoRa::getArrayEEPROM(uint16_t eepromAddr, uint8_t *arrayData, uint8_t size) {
for ( uint8_t i = 0; i < size; i++ ) {
arrayData[i] = EEPROM.read(eepromAddr + i);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
if ( i == 0 ) {
Serial.print(F("\nREAD EEPROM Address->Value: 0x"));Serial.print(eepromAddr + i, HEX);Serial.print(F("->0x"));Serial.print(arrayData[i], HEX);
} else {
Serial.print(F(", "));Serial.print(eepromAddr + i, HEX);Serial.print(F("->0x"));Serial.print(arrayData[i], HEX);
}
#endif
}
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
Serial.print(F("\nread: "));printHex(arrayData, size);
#endif
}
Expand Down Expand Up @@ -612,7 +614,13 @@ int8_t SlimLoRa::RfmReceivePacket(uint8_t *packet, uint8_t packet_max_length, ui
}

// SNR
#if DEBUG_SLIM == 1 // temp DEBUG to check RadioLib vs novag method
last_packet_snr_ = (int8_t) RfmRead(RFM_REG_PKT_SNR_VALUE);
last_packet_snrB = last_packet_snr_;
last_packet_snr_ /= 4;
#else
last_packet_snr_ = (int8_t) RfmRead(RFM_REG_PKT_SNR_VALUE) / 4;
#endif

// Clear interrupts
RfmWrite(RFM_REG_IRQ_FLAGS, 0xFF);
Expand Down Expand Up @@ -742,8 +750,8 @@ void SlimLoRa::RfmSendPacket(uint8_t *packet, uint8_t packet_length, uint8_t cha
// Saves memory cycles. We jump at worst case scenario EEPROM_WRITE_TX_COUNT fCnt.
if (tx_frame_counter_ % EEPROM_WRITE_TX_COUNT == 0) {
SetTxFrameCounter();
#endif
}
#endif
adr_ack_counter_++;
}

Expand Down Expand Up @@ -932,7 +940,9 @@ int8_t SlimLoRa::Join() {
channel_ = pseudo_byte_ & 0b11; // Mask with first 4 channels [0-3].
if ( channel_ == 0b11 ) { channel_ = 0b10; } // But we can join only on 3 channels: 868.100 868.300 and 868.500
*/

defaultChannel();

#if DEBUG_SLIM == 1
Serial.print(F("\nMAC before join: "));printMAC();
#endif
Expand Down Expand Up @@ -1467,25 +1477,21 @@ void SlimLoRa::ProcessFrameOptions(uint8_t *options, uint8_t f_options_length) {
pending_fopts_.fopts[pending_fopts_.length++] = 0xFF; // Unable to measure the battery level.
#endif //ARDUINO_AVR_FEATHER32U4


last_packet_snr_ = (int8_t) RfmRead(RFM_REG_PKT_SNR_VALUE);

#if DEBUG_SLIM == 1
Serial.print(F("\nlast_packet_snr_ 8bit: "));Serial.print(last_packet_snr_);
#endif
Serial.print(F("\nlast_packet_snr_ 8bit / 4: "));Serial.print(last_packet_snr_);
Serial.print(F("\nSTATUS ANS -- vbat, SNR shifts: "));Serial.print(vbat);Serial.print(F(", "));Serial.print((last_packet_snr_ & 0x80) >> 2 | last_packet_snr_ & 0x3F, BIN);

// convert to 6 bit. Code from RadioLib
if ( last_packet_snr_ < 128 ) {
last_packet_snr_ /= 4;
if ( last_packet_snrB < 128 ) {
last_packet_snrB /= 4;
} else {
last_packet_snr_ = (last_packet_snr_ - 256 ) / 4;
last_packet_snrB = (last_packet_snrB - 256 ) / 4;
}

#if DEBUG_SLIM == 1
Serial.print(F("\nSTATUS TODO: vbat / SNR shifts: "));Serial.print(vbat);Serial.print(F("/"));Serial.print((last_packet_snr_ & 0x80) >> 2 | last_packet_snr_ & 0x3F, BIN);
Serial.print(F("\nSNR / 4: "));Serial.println(last_packet_snr_);
Serial.print(F("\nSNR radiolib: "));Serial.println(last_packet_snrB);
#endif


// convert 8bit signed to 6bit signed -32 to 31
pending_fopts_.fopts[pending_fopts_.length++] = (last_packet_snr_ & 0x80) >> 2 | last_packet_snr_ & 0x3F;

i += LORAWAN_FOPT_DEV_STATUS_REQ_SIZE;
Expand Down Expand Up @@ -2783,7 +2789,7 @@ bool SlimLoRa::GetHasJoined() {

void SlimLoRa::SetHasJoined(bool value) {
eeprom_write_byte(&eeprom_lw_has_joined, value);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
Serial.print(F("\nWRITE EEPROM: joined"));
uint16_t temp = &eeprom_lw_has_joined;
#endif
Expand Down Expand Up @@ -2855,30 +2861,30 @@ void SlimLoRa::GetAppSKey(uint8_t *key) {

void SlimLoRa::SetAppSKey(uint8_t *key) {
eeprom_write_block(key, eeprom_lw_app_s_key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("WRITE app_skey: "));printHex(key, 16);
#endif
}

// FNwkSIntKey
void SlimLoRa::GetFNwkSIntKey(uint8_t *key) {
eeprom_read_block(key, eeprom_lw_f_nwk_s_int_key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("FNwkSInt: "));printHex(key, 16);
#endif
}

void SlimLoRa::SetFNwkSIntKey(uint8_t *key) {
eeprom_write_block(key, eeprom_lw_f_nwk_s_int_key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("WRITE FNwkSInt: "));printHex(key, 16);
#endif
}

// SNwkSIntKey
void SlimLoRa::GetSNwkSIntKey(uint8_t *key) {
eeprom_read_block(key, eeprom_lw_s_nwk_s_int_key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("SNwkSInt: "));printHex(key, 16);
#endif
}
Expand All @@ -2893,14 +2899,14 @@ void SlimLoRa::SetSNwkSIntKey(uint8_t *key) {
// NwkSEncKey
void SlimLoRa::GetNwkSEncKey(uint8_t *key) {
eeprom_read_block(key, eeprom_lw_nwk_s_enc_key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("NwkSEncKey: "));printHex(key, 16);
#endif
}

void SlimLoRa::SetNwkSEncKey(uint8_t *key) {
eeprom_write_block(key, eeprom_lw_nwk_s_enc_key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("WRITE NwkSEnc: "));printHex(key, 16);
#endif
}
Expand Down Expand Up @@ -3017,59 +3023,59 @@ void SlimLoRa::SetJoinNonce(uint32_t join_nonce) {
// AppSKey
void SlimLoRa::GetAppSKey(uint8_t *key) {
getArrayEEPROM(EEPROM_APPSKEY, key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("Read appSkey: "));printHex(key, 16);
#endif
}

void SlimLoRa::SetAppSKey(uint8_t *key) {
setArrayEEPROM(EEPROM_APPSKEY, key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("WRITE appSkey: "));printHex(key, 16);
#endif
}

// FNwkSIntKey
void SlimLoRa::GetFNwkSIntKey(uint8_t *key) {
getArrayEEPROM(EEPROM_FNWKSIKEY, key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("FNwkSInt: "));printHex(key, 16);
#endif
}

void SlimLoRa::SetFNwkSIntKey(uint8_t *key) {
setArrayEEPROM(EEPROM_FNWKSIKEY, key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("WRITE FNwkSInt: "));printHex(key, 16);
#endif
}

// SNwkSIntKey
void SlimLoRa::GetSNwkSIntKey(uint8_t *key) {
getArrayEEPROM(EEPROM_SNWKSIKEY, key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("SNwkSInt: "));printHex(key, 16);
#endif
}

void SlimLoRa::SetSNwkSIntKey(uint8_t *key) {
setArrayEEPROM(EEPROM_SNWKSIKEY, key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("WRITE SNwkSInt: "));printHex(key, 16);
#endif
}

// NwkSEncKey
void SlimLoRa::GetNwkSEncKey(uint8_t *key) {
getArrayEEPROM(EEPROM_NW_ENC_KEY, key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("NwkSEncKey: "));printHex(key, 16);
#endif
}

void SlimLoRa::SetNwkSEncKey(uint8_t *key) {
setArrayEEPROM(EEPROM_NW_ENC_KEY, key, 16);
#if DEBUG_SLIM == 1
#if DEBUG_SLIM > 1
printNOWEB();Serial.print(F("WRITE NwkSEnc: "));printHex(key, 16);
#endif
}
Expand Down
4 changes: 4 additions & 0 deletions SlimLoRa.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ class SlimLoRa {
uint8_t rx_symbols_ = LORAWAN_RX_MIN_SYMBOLS;
unsigned long tx_done_micros_;
int8_t last_packet_snr_;

#if DEBUG_SLIM == 1
int8_t last_packet_snrB;
#endif

uint16_t ChMask;
uint8_t NbTrans = NBTRANS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// program behaviour
#define DEBUG_INO 1 // DEBUG via Serial.print
#define PHONEY 0 // don't transmit. for DEBUGing
#define POWER 8 // Transmittion power
#define POWER 14 // Transmittion power

// pin to measure battery voltage - works with Feather32u4
#define VBATPIN A9
Expand All @@ -41,7 +41,7 @@ uint8_t joinEfforts = 5; // how many times we will try to join.
uint32_t joinStart, joinEnd, RXend, vbat, newfCnt;
uint8_t dataRate, txPower = POWER, payload[1], payload_length, vbatC;
uint8_t fport = 1;
uint8_t minutes = 5;
uint8_t minutes = 15;

SlimLoRa lora = SlimLoRa(8); // OK for feather 32u4 (CS featherpin. Aka: nss_pin for SlimLoRa). TODO: support other pin configurations.

Expand All @@ -63,7 +63,7 @@ void setup() {
lora.tx_frame_counter_ += EEPROM_WRITE_TX_COUNT;
lora.SetTxFrameCounter();

// for DEBUG only, don't use this.
// for DEBUG only, don't use those.
//lora.ForceTxFrameCounter(3);
//lora.SetDevNonce(1812);
//lora.SetJoinNonce(1812);
Expand Down Expand Up @@ -177,7 +177,7 @@ void loop() {

#if DEBUG_INO == 1
Serial.print(F("\nUsed data from Port\t: "));Serial.print(lora.downPort);
Serial.print(F("\ndownlinkSize\t: "));Serial.print(lora.downlinkSize);
Serial.print(F("\ndownlinkSize\t\t: "));Serial.print(lora.downlinkSize);
#endif

minutes = lora.downlinkData[0];
Expand All @@ -187,7 +187,7 @@ void loop() {
} // minutes

#if DEBUG_INO == 1
Serial.print(F("\nNew minutes\t: "));Serial.print(minutes);
Serial.print(F("\nNew minutes\t\t: "));Serial.print(minutes);
#endif

} else {
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SlimLoRa",
"version": "0.6.3",
"version": "0.6.4",
"description": "LoRaWAN library with OTAA join, ADR support and most important MAC commands for EU868 suitable for AVR's with 32Kbytes. It uses 14kBytes of program flash instead of 52kBytes of RadioLib or 32kBytes of LMIC. Tested avr32u4 / ATmega32u4 and HopeRF 95w (SX1276) on Adafruit Feather. ABP untested. Session is stored to EEPROM. Testers wanted and PR's for other regions. It supports downlinks.",
"keywords": "LoRaWAN, rfm95w, hoperf, SX1276, radio, communication",
"repository":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SlimLoRa
version=0.6.3
version=0.6.4
author=clavisound
maintainer=clavisound
sentence=SlimLoRa Library
Expand Down

0 comments on commit 3dac526

Please sign in to comment.