Fixed receive bug (delay() caused dropped downlink)

pull/2/head
Max-Plastix 2021-12-07 14:06:19 -08:00
rodzic dc4a031d3d
commit 0343b80678
4 zmienionych plików z 117 dodań i 116 usunięć

Wyświetl plik

@ -72,7 +72,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define LORAWAN_PORT 2 // Port the messages will be sent to -- must match console Decoder script!
#define LORAWAN_CONFIRMED_EVERY 0 // Send confirmed message for ACK every N messages (0 means never, 1 means always)
#define LORAWAN_SF DR_SF7 // Spreading factor (recommended DR_SF7 for ttn network map purposes, DR_SF10 works for slow moving trackers)
#define LORAWAN_SF DR_SF10 // Spreading factor (recommended DR_SF7 for ttn network map purposes, DR_SF10 works for slow moving trackers)
#define LORAWAN_ADR 0 // Enable ADR
#define DEBUG_PORT Serial // Serial debug port

Wyświetl plik

@ -251,8 +251,10 @@ void update_status() {
}
void callback(uint8_t message) {
#if 1
void callback(uint8_t message)
{
static boolean seen_joined = false, seen_joining = false;
#if 0
{
snprintf(buffer, sizeof(buffer), "MSG %d\n", message);
screen_print(buffer);
@ -274,31 +276,42 @@ void callback(uint8_t message) {
if (EV_PENDING == message) Serial.println("# PENDING");
if (EV_QUEUED == message) Serial.println("# QUEUED");
/* This is confusing because JOINED is sometimes spoofed and comes early */
if (EV_JOINED == message)
seen_joined = true;
if (EV_JOINING == message)
seen_joining = true;
if (!isJoined && seen_joined && seen_joining)
{
isJoined = true;
screen_print("Joined Helium!\n");
}
if (EV_TXSTART == message) {
screen_print("Tx.. ");
screen_print("TX\n");
}
// We only want to say 'packetSent' for our packets (not packets needed for joining)
if (EV_TXCOMPLETE == message && packetQueued) {
screen_print("sent.\n");
// screen_print("sent.\n");
packetQueued = false;
packetSent = true;
}
if (EV_RXCOMPLETE == message) {
screen_print("Downlink: ");
if (EV_RXCOMPLETE == message || EV_RESPONSE == message) {
size_t len = ttn_response_len();
uint8_t data[len];
ttn_response(data, len);
for (uint8_t i = 0; i < len; i++) {
snprintf(buffer, sizeof(buffer), "%02X", data[i]);
screen_print(buffer);
}
screen_print("\n");
uint8_t port;
ttn_response(&port, data, len);
snprintf(buffer, sizeof(buffer), "Rx: %d on P%d\n", len, port);
screen_print(buffer);
Serial.print("Downlink: ");
for (int i = 0; i < len; i++)
printHex2(data[i]);
Serial.println();
/*
* Downlink format:
@ -306,28 +319,33 @@ void callback(uint8_t message) {
* 2 Bytes: Minimum Time (1 to 65535) seconds (18.2 hours) between pings, or 0 no-change
* 1 Byte: Battery voltage (2.0 to 4.5) for auto-shutoff, or 0 no-change
*/
if (len == 5) {
snprintf(buffer, sizeof(buffer), "(no changes)\n");
if (port == 1 && len == 5) {
float new_distance = (float)(data[0] << 8 | data[1]);
if (new_distance > 0.0) {
min_dist_moved = new_distance;
snprintf(buffer, sizeof(buffer), "New Dist: %.0fm\n", new_distance);
screen_print(buffer);
}
unsigned long int new_interval = data[2] << 8 | data[3];
if (new_interval) {
tx_interval_ms = new_interval * 1000;
freeze_tx_interval_ms = true;
snprintf(buffer, sizeof(buffer), "New Time: %.0lum\n", new_interval);
if (new_interval == 0xFFFF) {
freeze_tx_interval_ms = false;
tx_interval_ms = STATIONARY_TX_INTERVAL;
} else {
tx_interval_ms = new_interval * 1000;
freeze_tx_interval_ms = true;
}
snprintf(buffer, sizeof(buffer), "New Time: %.0lus\n", new_interval);
screen_print(buffer);
}
float new_low_voltage = data[4] / 2.56 + 2.0;
if (new_low_voltage) {
if (data[4]) {
float new_low_voltage = data[4] / 2.56 + 2.0;
battery_low_voltage = new_low_voltage;
snprintf(buffer, sizeof(buffer), "New LowBat: %.2fm\n", new_low_voltage);
screen_print(buffer);
}
screen_print(buffer);
}
}
}
@ -642,7 +660,7 @@ void loop() {
if (trySend()) {
// Good send
} else {
// Nothing sent. Rest
delay(100);
// Nothing sent.
// Do NOT delay() here.. the LoRa receiver and join housekeeping also needs to run!
}
}

Wyświetl plik

@ -130,81 +130,72 @@ void initDevEUI() {
// LMIC library will call this method when an event is fired
void onEvent(ev_t event) {
// Some special-case event handling
switch (event) {
case EV_JOINED: {
#ifdef SINGLE_CHANNEL_GATEWAY
forceTxSingleChannelDr();
#endif
switch(event) {
case EV_JOINED: {
#ifdef SINGLE_CHANNEL_GATEWAY
forceTxSingleChannelDr();
#endif
// Disable link check validation (automatically enabled
// during join, but because slow data rates change max TX
// size, we don't use it in this example.
if (!LORAWAN_ADR) {
LMIC_setLinkCheckMode(0); // Link check problematic if not using ADR.
// Must be set after join
// Disable link check validation (automatically enabled
// during join, but because slow data rates change max TX
// size, we don't use it in this example.
if(!LORAWAN_ADR){
LMIC_setLinkCheckMode(0); // Link check problematic if not using ADR. Must be set after join
}
Serial.println(F("EV_JOINED"));
u4_t netid = 0;
devaddr_t devaddr = 0;
u1_t nwkKey[16];
u1_t artKey[16];
LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey);
Serial.print("netid: ");
Serial.println(netid, DEC);
Serial.print("devaddr: ");
Serial.println(devaddr, HEX);
Serial.print("AppSKey: ");
for (size_t i=0; i<sizeof(artKey); ++i) {
if (i != 0)
Serial.print("-");
printHex2(artKey[i]);
}
Serial.println("");
Serial.print("NwkSKey: ");
for (size_t i=0; i<sizeof(nwkKey); ++i) {
if (i != 0)
Serial.print("-");
printHex2(nwkKey[i]);
}
Serial.println();
Preferences p;
if(p.begin("lora", false)) {
p.putUInt("netId", netid);
p.putUInt("devAddr", devaddr);
p.putBytes("nwkKey", nwkKey, sizeof(nwkKey));
p.putBytes("artKey", artKey, sizeof(artKey));
p.end();
}
break; }
case EV_TXCOMPLETE:
// Serial.println(F("EV_TXCOMPLETE"));
if (LMIC.txrxFlags & TXRX_ACK) {
// Serial.println(F("Received ack"));
_ttn_callback(EV_ACK);
}
if (LMIC.dataLen) {
// Serial.print(F("Data Received: "));
// Serial.write(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
// Serial.println();
_ttn_callback(EV_RESPONSE);
}
break;
default:
break;
}
Serial.println(F("! EV_JOINED as:"));
u4_t netid = 0;
devaddr_t devaddr = 0;
u1_t nwkKey[16];
u1_t artKey[16];
LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey);
Serial.print("netid: ");
Serial.println(netid, DEC);
Serial.print("devaddr: ");
Serial.println(devaddr, HEX);
Serial.print("AppSKey: ");
for (size_t i = 0; i < sizeof(artKey); ++i) {
if (i != 0)
Serial.print("-");
printHex2(artKey[i]);
}
Serial.println("");
Serial.print("NwkSKey: ");
for (size_t i = 0; i < sizeof(nwkKey); ++i) {
if (i != 0)
Serial.print("-");
printHex2(nwkKey[i]);
}
Serial.println();
Preferences p;
if (p.begin("lora", false)) {
p.putUInt("netId", netid);
p.putUInt("devAddr", devaddr);
p.putBytes("nwkKey", nwkKey, sizeof(nwkKey));
p.putBytes("artKey", artKey, sizeof(artKey));
p.end();
}
break;
}
case EV_TXCOMPLETE:
// Before sending the EV_TXCOMPLETE callback, send EV_ACK and EV_Response
// virtual events? Serial.println(F("! EV_TXCOMPLETE"));
if (LMIC.txrxFlags & TXRX_ACK) {
Serial.println(F("! Tx got ACK"));
_ttn_callback(EV_ACK);
}
if (LMIC.dataLen) {
Serial.print(F("! Tx got Response"));
Serial.write(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
Serial.println();
_ttn_callback(EV_RESPONSE);
}
break;
default:
break;
}
// Serial.print(F("Data Received: "));
// Serial.write(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
// Serial.println();
// Send the original message callbacks
// Send message callbacks
_ttn_callback(event);
}
@ -220,7 +211,9 @@ size_t ttn_response_len() {
return LMIC.dataLen;
}
void ttn_response(uint8_t * buffer, size_t len) {
void ttn_response(uint8_t * port, uint8_t * buffer, size_t len) {
if (port)
*port = LMIC.frame[LMIC.dataBeg - 1];
for (uint8_t i = 0; i < LMIC.dataLen; i++) {
buffer[i] = LMIC.frame[LMIC.dataBeg + i];
}
@ -377,7 +370,7 @@ void ttn_join() {
LMIC_startJoining();
}
else {
Serial.println("Rejoining saved session");
Serial.println("Reusing saved session");
LMIC_setSession(netId, devAddr, nwkKey, artKey);
// Trigger a false joined

Wyświetl plik

@ -11,29 +11,22 @@
[platformio]
src_dir = main
;[env]
;framework = arduino
;[env:upstream_develop]
;platform = https://github.com/platformio/platform-espressif32.git
[env:esp32]
platform = espressif32
framework = arduino
board = ttgo-t-beam
framework = arduino
;platform_packages =
; platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git
; note: we add src to our include search path so that lmic_project_config can override
build_flags = -Wall
-Wextra
-Wno-missing-field-initializers -O3 -Wl,-Map,.pio/build/esp32/output.map
-D ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS
-D CFG_us915=1
-D CFG_sx1276_radio=1
-D LMIC_DEBUG_LEVEL=3
-D ARDUINO_TTGO_LoRa32_v21new
-D ARDUINO_LMIC_PROJECT_CONFIG_H_SUPPRESS
-D LMIC_DEBUG_LEVEL=0
-D ARDUINO_TTGO_LoRa32_V1
; -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
; -D ARDUINO_TTGO_LoRa32_v21new
monitor_speed = 115200
monitor_port = COM17
@ -43,7 +36,4 @@ lib_deps =
mcci-catena/MCCI LoRaWAN LMIC library @ ^4.0.0
mikalhart/TinyGPSPlus @ ^1.0.2
thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays @ ^4.2.1
lewisxhe/AXP202X_Library @ ^1.1.3
;SPI
lewisxhe/AXP202X_Library @ ^1.1.3