From 3dac526742218a346db07f99881c38d97cf18143 Mon Sep 17 00:00:00 2001 From: clavisound Date: Tue, 10 Dec 2024 23:26:07 +0200 Subject: [PATCH] margin on device status MAC command must be fine now - was always zero. DEBUG is less verbose. --- README.md | 10 +-- SlimLoRa.cpp | 80 ++++++++++--------- SlimLoRa.h | 4 + ...imLoRa-feather32u4-battery-complicated.ino | 10 +-- library.json | 2 +- library.properties | 2 +- 6 files changed, 58 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index cc4359a..78ed2a1 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/SlimLoRa.cpp b/SlimLoRa.cpp index ae48337..592d5a3 100644 --- a/SlimLoRa.cpp +++ b/SlimLoRa.cpp @@ -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(); } @@ -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 } @@ -279,7 +281,7 @@ 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 { @@ -287,7 +289,7 @@ void SlimLoRa::getArrayEEPROM(uint16_t eepromAddr, uint8_t *arrayData, uint8_t s } #endif } -#if DEBUG_SLIM == 1 +#if DEBUG_SLIM > 1 Serial.print(F("\nread: "));printHex(arrayData, size); #endif } @@ -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); @@ -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_++; } @@ -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 @@ -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; @@ -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 @@ -2855,7 +2861,7 @@ 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 } @@ -2863,14 +2869,14 @@ void SlimLoRa::SetAppSKey(uint8_t *key) { // 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 } @@ -2878,7 +2884,7 @@ void SlimLoRa::SetFNwkSIntKey(uint8_t *key) { // 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 } @@ -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 } @@ -3017,14 +3023,14 @@ 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 } @@ -3032,14 +3038,14 @@ void SlimLoRa::SetAppSKey(uint8_t *key) { // 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 } @@ -3047,14 +3053,14 @@ void SlimLoRa::SetFNwkSIntKey(uint8_t *key) { // 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 } @@ -3062,14 +3068,14 @@ void SlimLoRa::SetSNwkSIntKey(uint8_t *key) { // 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 } diff --git a/SlimLoRa.h b/SlimLoRa.h index 41e500e..d4aef3e 100644 --- a/SlimLoRa.h +++ b/SlimLoRa.h @@ -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; diff --git a/examples/SlimLoRa-feather32u4-battery-complicated/SlimLoRa-feather32u4-battery-complicated.ino b/examples/SlimLoRa-feather32u4-battery-complicated/SlimLoRa-feather32u4-battery-complicated.ino index da5fa24..1756fc3 100644 --- a/examples/SlimLoRa-feather32u4-battery-complicated/SlimLoRa-feather32u4-battery-complicated.ino +++ b/examples/SlimLoRa-feather32u4-battery-complicated/SlimLoRa-feather32u4-battery-complicated.ino @@ -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 @@ -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. @@ -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); @@ -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]; @@ -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 { diff --git a/library.json b/library.json index 5842460..426d562 100644 --- a/library.json +++ b/library.json @@ -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": diff --git a/library.properties b/library.properties index acdab2b..2fbffd6 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SlimLoRa -version=0.6.3 +version=0.6.4 author=clavisound maintainer=clavisound sentence=SlimLoRa Library