Hi
Im trying to get my device to not do an OTAA join every time it wakes up from deep sleep. Im aware there is some topics in the forum on the same subject but I find it difficult to understand how I can implement this feature.
im using this library in my code (version 5.0.1)
My hardware is:
ESP32-C3
RFM95W
BMP280
If anyone has any tips or suggestions on how I can implement this in the code, I would really appreciate it.
This is my code so far without saving and restoring OTAA:
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_BMP280.h>
#include <esp_sleep.h> // Inkluder for deep sleep
Adafruit_BMP280 bmp; // Oppretter sensorobjekt
RTC_DATA_ATTR float lastTemperature = -1000.0;
RTC_DATA_ATTR float lastPressure = -1000.0;
RTC_DATA_ATTR float tempThreshold = 0.1;
RTC_DATA_ATTR float pressureThreshold = 1.0;
static osjob_t sendjob;
const unsigned TX_INTERVAL = 60; // 60 sekunder
// Pin mapping
const lmic_pinmap lmic_pins = {
.nss = 7,
.rxtx = LMIC_UNUSED_PIN,
.rst = 3,
.dio = {2, 21, LMIC_UNUSED_PIN},
};
static const u1_t PROGMEM APPEUI[8] = { };
void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8); }
static const u1_t PROGMEM DEVEUI[8] = { };
void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8); }
static const u1_t PROGMEM APPKEY[16] = { };
void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16); }
void printHex2(unsigned v) {
v &= 0xff;
if (v < 16)
Serial.print('0');
Serial.print(v, HEX);
}
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
case EV_SCAN_TIMEOUT:
Serial.println(F("EV_SCAN_TIMEOUT"));
break;
case EV_BEACON_FOUND:
Serial.println(F("EV_BEACON_FOUND"));
break;
case EV_BEACON_MISSED:
Serial.println(F("EV_BEACON_MISSED"));
break;
case EV_BEACON_TRACKED:
Serial.println(F("EV_BEACON_TRACKED"));
break;
case EV_JOINING:
Serial.println(F("EV_JOINING"));
break;
case EV_JOINED:
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();
}
// 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.
LMIC_setLinkCheckMode(0);
break;
case EV_JOIN_FAILED:
Serial.println(F("EV_JOIN_FAILED"));
break;
case EV_REJOIN_FAILED:
Serial.println(F("EV_REJOIN_FAILED"));
break;
case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F("Received ack"));
Serial.print(F("LMIC.txrxFlags: "));
Serial.println(LMIC.txrxFlags, HEX);
Serial.print(F("LMIC.dataLen: "));
Serial.println(LMIC.dataLen);
if (LMIC.dataLen) {
Serial.print(F("Received "));
Serial.print(LMIC.dataLen);
Serial.println(F(" bytes of payload"));
// Print the received payload
Serial.print(F("Payload: "));
for (int i = 0; i < LMIC.dataLen; i++) {
Serial.print(LMIC.frame[LMIC.dataBeg + i], HEX);
Serial.print(" ");
}
Serial.println();
// Oppdater terskelverdier fra downlink payload
if (LMIC.dataLen == 4) {
int16_t newTempThreshold = (LMIC.frame[LMIC.dataBeg] << 8) | LMIC.frame[LMIC.dataBeg + 1];
int16_t newPressureThreshold = (LMIC.frame[LMIC.dataBeg + 2] << 8) | LMIC.frame[LMIC.dataBeg + 3];
tempThreshold = newTempThreshold / 100.0;
pressureThreshold = newPressureThreshold / 10.0;
Serial.print(F("New tempThreshold: "));
Serial.println(tempThreshold);
Serial.print(F("New pressureThreshold: "));
Serial.println(pressureThreshold);
} else {
Serial.println(F("Downlink payload length is not 4 bytes"));
}
} else {
Serial.println(F("No downlink payload received"));
}
Serial.println(F("Going to deep sleep..."));
esp_sleep_enable_timer_wakeup(TX_INTERVAL * 1000000ULL); // Vekk etter TX_INTERVAL sekunder
esp_deep_sleep_start();
break;
case EV_LOST_TSYNC:
Serial.println(F("EV_LOST_TSYNC"));
break;
case EV_RESET:
Serial.println(F("EV_RESET"));
break;
case EV_RXCOMPLETE:
// data received in ping slot
Serial.println(F("EV_RXCOMPLETE"));
break;
case EV_LINK_DEAD:
Serial.println(F("EV_LINK_DEAD"));
break;
case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE"));
break;
case EV_TXSTART:
Serial.println(F("EV_TXSTART"));
break;
case EV_TXCANCELED:
Serial.println(F("EV_TXCANCELED"));
break;
case EV_RXSTART:
/* do not print anything -- it wrecks timing */
break;
case EV_JOIN_TXCOMPLETE:
Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept"));
break;
default:
Serial.print(F("Unknown event: "));
Serial.println((unsigned) ev);
break;
}
}
void do_send(osjob_t* j){
Serial.println(F("do_send called"));
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
} else {
// Les temperatur og trykk fra BMP280
float temperature = bmp.readTemperature();
float pressure = bmp.readPressure() / 100.0;
// Skriv temperatur og trykk til serial porten
Serial.print("Temp: "); Serial.print(temperature); Serial.print(" C, ");
Serial.print("Pressure: "); Serial.print(pressure); Serial.println(" hPa");
// Sjekk om terskelverdiene er oppfylt
if (abs(temperature - lastTemperature) >= tempThreshold || abs(pressure - lastPressure) >= pressureThreshold) {
// Oppdater siste verdier
lastTemperature = temperature;
lastPressure = pressure;
// Forbered data for sending
uint8_t payload[4];
int16_t tempInt = (int16_t)(temperature * 100);
int16_t pressInt = (int16_t)(pressure * 10);
payload[0] = (tempInt >> 8) & 0xFF;
payload[1] = tempInt & 0xFF;
payload[2] = (pressInt >> 8) & 0xFF;
payload[3] = pressInt & 0xFF;
// Send data on port 1
LMIC_setTxData2(1, payload, sizeof(payload), 0);
Serial.println(F("Packet queued"));
} else {
Serial.println(F("No significant change in temperature or pressure"));
// 🔴 Deep sleep selv om det ikke sendes noe
Serial.println(F("Skipping transmission, going to deep sleep..."));
esp_sleep_enable_timer_wakeup(TX_INTERVAL * 1000000ULL);
esp_deep_sleep_start();
}
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup() {
Serial.begin(9600);
Serial.println(F("Starting"));
if (!bmp.begin(0x76)) {
Serial.println(F("Could not find BMP280 sensor!"));
}
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
LMIC_reset();
// Enable ADR
LMIC_setAdrMode(1);
// Juster RX-vinduene
//LMIC_setClockError(MAX_CLOCK_ERROR * 10 / 100); // 10% clock error
// Sett data rate for RX2-vinduet
//LMIC.dn2Dr = DR_SF9;
// Start job (sending automatically starts OTAA too)
do_send(&sendjob);
}
void loop() {
//Serial.println(F("LOOP"));
os_runloop_once();
}