0

RFM9x Lora and Sleep Mode(s)? Lipo uptime very short
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

RFM9x Lora and Sleep Mode(s)? Lipo uptime very short

by pede on Sat Jan 02, 2021 10:43 am

Hi,

I red in the description of the lora board (https://www.adafruit.com/product/3231) that the feather consumes "~300uA during full sleep, ~120mA peak during +20dBm transmit, ~40mA during active radio listening". I did some further testing with bigger (2500 mAH) lipos and the sleep() function with lora feather but I couldn't reach only some days lipo runtime. It seems that I never reach the "~300uA during full sleep status. My question now I could I bring the feather in the full sleep mode and how can I wake it up if my sensor value change from false to true. In the moment I use he sleep() feature, but it seems no noticable effect.

Pete

pede
 
Posts: 28
Joined: Sun Jul 12, 2020 2:16 pm

Re: RFM9x Lora and Sleep Mode(s)? Lipo uptime very short

by siddacious on Mon Jan 04, 2021 6:15 pm

I'll page someone who can help. Please stand by.

siddacious
 
Posts: 406
Joined: Fri Apr 21, 2017 3:09 pm

Re: RFM9x Lora and Sleep Mode(s)? Lipo uptime very short

by Vmarinus on Mon Jan 04, 2021 6:35 pm

Hello,

Some time ago I played with Lorawan and The things network. I found some intersting code on the web and modified it.
The build I made runs for months on a single old 1600mAh battery from a smartphone.
I's a matter of using the good sleep library and putting in the right spot. and to keep it asleep. So put the sleep command in a loop that runs till you want it to wake. (see colored code)

To get you on the way. here is my code.
Code: Select all | TOGGLE FULL SIZE
/*******************************************************************************
 * The Things Network - Sensor Data Example
 *
 * Example of sending a valid LoRaWAN packet with DHT22 temperature and
 * humidity data to The Things Networ using a Feather M0 LoRa.
 *
 * Learn Guide: https://learn.adafruit.com/the-things-network-for-feather
 *
 * Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
 * Copyright (c) 2018 Terry Moore, MCCI
 * Copyright (c) 2018 Brent Rubell, Adafruit Industries
 *
 * Permission is hereby granted, free of charge, to anyone
 * obtaining a copy of this document and accompanying files,
 * to do whatever they want with them without any restriction,
 * including, but not limited to, copying, modification and redistribution.
 * NO WARRANTY OF ANY KIND IS PROVIDED.
 *******************************************************************************/
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <Wire.h>;
#include <Adafruit_SleepyDog.h>

//integrate the cayene payload (and an "n" to cayene)
#include <CayeneLPP.h>
CayeneLPP lpp(51);

//Sensor Library
#include <Adafruit_MPL115A2.h>
Adafruit_MPL115A2 mpl115a2;

#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

#define VBATPIN A7

//
// For normal use, we require that you edit the sketch to replace FILLMEIN
// with values assigned by the TTN console. However, for regression tests,
// we want to be able to compile these scripts. The regression tests define
// COMPILE_REGRESSION_TEST, and in that case we define FILLMEIN to a non-
// working but innocuous value.
//
#ifdef COMPILE_REGRESSION_TEST
#define FILLMEIN 0
#else
#warning "You must replace the values marked FILLMEIN with real values from the TTN control panel!"
#define FILLMEIN (#dont edit this, edit the lines that use FILLMEIN)
#endif

// This EUI must be in little-endian format, so least-significant-byte
// first. When copying an EUI from ttnctl output, this means to reverse
// the bytes. For TTN issued EUIs the last bytes should be 0xD5, 0xB3,
// 0x70.
static const u1_t PROGMEM APPEUI[8] = { xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx };
void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8);}

// This should also be in little endian format, see above.
static const u1_t PROGMEM DEVEUI[8] = { xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx };
void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);}

// This key should be in big endian format (or, since it is not really a
// number but a block of memory, endianness does not really apply). In
// practice, a key taken from the TTN console can be copied as-is.
static const u1_t PROGMEM APPKEY[16] = { xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx };

void os_getDevKey (u1_t* buf) {  memcpy_P(buf, APPKEY, 16);}

// payload to send to TTN gateway
static uint8_t payload[5];
static osjob_t sendjob;

[color=#FFFF00]// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 600;[/color]

// Pin mapping for Adafruit Feather M0 LoRa
const lmic_pinmap lmic_pins = {
    .nss = 8,
//      .nss = 11,
      .rxtx = LMIC_UNUSED_PIN,
    .rst = 4,
//    .rst = 12,
      .dio = {3, 6, LMIC_UNUSED_PIN},
//      .dio = {5, 6, LMIC_UNUSED_PIN},
      .rxtx_rx_active = 0,
    .rssi_cal = 8,              // LBT cal for the Adafruit Feather M0 LoRa, in dB
    .spi_freq = 8000000,
};


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("artKey: ");
              for (size_t i=0; i<sizeof(artKey); ++i) {
                if (i != 0)
                  Serial.print("-");
                Serial.print(artKey[i], HEX);
              }
              Serial.println("");
              Serial.print("nwkKey: ");
              for (size_t i=0; i<sizeof(nwkKey); ++i) {
                      if (i != 0)
                              Serial.print("-");
                      Serial.print(nwkKey[i], HEX);
              }
              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;
        /*
        || This event is defined but not used in the code. No
        || point in wasting codespace on it.
        ||
        || case EV_RFU1:
        ||     Serial.println(F("EV_RFU1"));
        ||     break;
        */
        case EV_JOIN_FAILED:
            Serial.println(F("EV_JOIN_FAILED"));
            break;
        case EV_REJOIN_FAILED:
            Serial.println(F("EV_REJOIN_FAILED"));
            break;
            break;
        case EV_TXCOMPLETE:
            Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
            if (LMIC.txrxFlags & TXRX_ACK)
              Serial.println(F("Received ack"));
            if (LMIC.dataLen) {
              Serial.println(F("Received "));
              Serial.println(LMIC.dataLen);
              Serial.println(F(" bytes of payload"));
            }

            // Schedule next transmission
            //os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
            do_send(&sendjob);
            [color=#FFFF00]for (int i=0; i<int(TX_INTERVAL/8); i++) {
            // Use library from https://github.com/rocketscream/Low-Power
            //LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
            // going to deepsleep VMA
              digitalWrite(LED_BUILTIN, LOW); // Show we're asleep
              Watchdog.sleep(8000);
              digitalWrite(LED_BUILTIN, HIGH); // Show we're awake again
            }[/color]
            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;
        /*
        || This event is defined but not used in the code. No
        || point in wasting codespace on it.
        ||
        || case EV_SCAN_FOUND:
        ||    Serial.println(F("EV_SCAN_FOUND"));
        ||    break;
        */
        case EV_TXSTART:
            Serial.println(F("EV_TXSTART"));
            break;
        default:
            Serial.print(F("Unknown event: "));
            Serial.println((unsigned) ev);
            break;
    }
}

void do_send(osjob_t* j){
    // Check if there is not a current TX/RX job running
    if (LMIC.opmode & OP_TXRXPEND) {
        Serial.println(F("OP_TXRXPEND, not sending"));
    } else {
        // reset lpp for Cayene
        lpp.reset();
       
        // read the temperature from the mp1152a
        float temperature = 0;
        float pressure = 0;
        mpl115a2.getPT(&pressure,&temperature);
       
        Serial.print("Temperature: (°C):"); Serial.print(temperature);
        Serial.println(" *C");
        lpp.addTemperature(1, temperature);

        pressure = pressure * 10;
        Serial.print("press (hPa):");
        Serial.println(pressure);
        lpp.addBarometricPressure(2, pressure);

        VL53L0X_RangingMeasurementData_t measure;
        lox.rangingTest(&measure, false);
        float distance = 0;
        if (measure.RangeStatus != 4) {  // phase failures have incorrect data
          distance = measure.RangeMilliMeter;
          Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
        } else {
          Serial.println(" out of range ");
        }
        Serial.print("distance that will be sent: ");
        distance = distance/100; //distance in dm
        Serial.println(distance);
        lpp.addAnalogInput(3, distance);

        float measuredvbat = analogRead(VBATPIN);
        measuredvbat *= 2;    // we divided by 2, so multiply back
        measuredvbat *= 3.3;  // Multiply by 3.3V, our reference voltage
        measuredvbat /= 1024; // convert to voltage
        Serial.print("VBat (V) :" );
        Serial.println(measuredvbat);
        lpp.addAnalogInput(4, measuredvbat);

        // prepare upstream data transmission at the next possible time.
        // transmit on port 1 (the first parameter); you can use any value from 1 to 223 (others are reserved).
        // don't request an ack (the last parameter, if not zero, requests an ack from the network).
        // Remember, acks consume a lot of network resources; don't ask for an ack unless you really need it.
        Serial.print("buf size: ");
        Serial.println(lpp.getSize());
        Serial.print("Error: ");
        Serial.println(lpp.getError());
       
        LMIC_setTxData2(1, lpp.getBuffer(), lpp.getSize(), 0);
    }
    // Next TX is scheduled after TX_COMPLETE event.
}

void setup() {
    delay(5000);
//    while (! Serial);
    digitalWrite(13, LOW);
    Serial.begin(9600);
    Serial.println(F("Starting"));
    mpl115a2.begin();
    lox.begin();

    // LMIC init
    os_init();
    // Reset the MAC state. Session and pending data transfers will be discarded.
    LMIC_reset();
    // Disable link-check mode and ADR, because ADR tends to complicate testing.
    LMIC_setLinkCheckMode(0);
    // Set the data rate to Spreading Factor 7.  This is the fastest supported rate for 125 kHz channels, and it
    // minimizes air time and battery power. Set the transmission power to 14 dBi (25 mW).
    LMIC_setDrTxpow(DR_SF7,14);
    // in the US, with TTN, it saves join time if we start on subband 1 (channels 8-15). This will
    // get overridden after the join by parameters from the network. If working with other
    // networks or in other regions, this will need to be changed.
//    LMIC_selectSubBand(1);
    #if defined(CFG_eu868)
      Serial.println("European Channels");
      for (int i = 1; i <= 8; i++) LMIC_disableChannel(i);
    #elif defined(CFG_us915)
    // NA-US channels 0-71 are configured automatically
    // but only one group of 8 should (a subband) should be active
    // TTN recommends the second sub band, 1 in a zero based count.
    // https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json
      LMIC_selectSubBand(1);
    #endif
   
    // Start job (sending automatically starts OTAA too)
    do_send(&sendjob);
}

void loop() {
  // we call the LMIC's runloop processor. This will cause things to happen based on events and time. One
  // of the things that will happen is callbacks for transmission complete or received messages. We also
  // use this loop to queue periodic data transmissions.  You can put other things here in the `loop()` routine,
  // but beware that LoRaWAN timing is pretty tight, so if you do more than a few milliseconds of work, you
  // will want to call `os_runloop_once()` every so often, to keep the radio running.
  os_runloop_once();

}


Attention Spamchecking refues to use Cayene with two "n" so i removed one "n" in the code in order to submit it.

Vmarinus
 
Posts: 9
Joined: Tue Dec 18, 2018 4:59 am

Re: RFM9x Lora and Sleep Mode(s)? Lipo uptime very short

by adafruit2 on Tue Jan 05, 2021 12:07 am

the radio chip has sleep modes, you also have to put the mainboard into sleep. deep sleep in circuitpython is new, but heres some info https://learn.adafruit.com/deep-sleep-w ... cuitpython
here's an arduino library https://github.com/adafruit/Adafruit_SleepyDog

adafruit2
Site Admin
 
Posts: 19647
Joined: Fri Mar 11, 2005 7:36 pm

Re: RFM9x Lora and Sleep Mode(s)? Lipo uptime very short

by pede on Wed Jan 06, 2021 6:46 am

adafruit2 wrote:the radio chip has sleep modes, you also have to put the mainboard into sleep. deep sleep in circuitpython is new, but heres some info https://learn.adafruit.com/deep-sleep-w ... cuitpython
here's an arduino library https://github.com/adafruit/Adafruit_SleepyDog

Hi,

many thx I know this stuff, but deepsleep isn't support on lora feathers and don't want to switch zu C.

Peter

pede
 
Posts: 28
Joined: Sun Jul 12, 2020 2:16 pm

Re: RFM9x Lora and Sleep Mode(s)? Lipo uptime very short

by pede on Wed Jan 06, 2021 6:47 am

Vmarinus wrote:Hello,

Some time ago I played with Lorawan and The things network. I found some intersting code on the web and modified it.
The build I made runs for months on a single old 1600mAh battery from a smartphone.
I's a matter of using the good sleep library and putting in the right spot. and to keep it asleep. So put the sleep command in a loop that runs till you want it to wake. (see colored code)

To get you on the way. here is my code.
Code: Select all | TOGGLE FULL SIZE
/*******************************************************************************
 * The Things Network - Sensor Data Example
 *
 * Example of sending a valid LoRaWAN packet with DHT22 temperature and
 * humidity data to The Things Networ using a Feather M0 LoRa.
 *
 * Learn Guide: https://learn.adafruit.com/the-things-network-for-feather
 *
 * Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
 * Copyright (c) 2018 Terry Moore, MCCI
 * Copyright (c) 2018 Brent Rubell, Adafruit Industries
 *
 * Permission is hereby granted, free of charge, to anyone
 * obtaining a copy of this document and accompanying files,
 * to do whatever they want with them without any restriction,
 * including, but not limited to, copying, modification and redistribution.
 * NO WARRANTY OF ANY KIND IS PROVIDED.
 *******************************************************************************/
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <Wire.h>;
#include <Adafruit_SleepyDog.h>

//integrate the cayene payload (and an "n" to cayene)
#include <CayeneLPP.h>
CayeneLPP lpp(51);

//Sensor Library
#include <Adafruit_MPL115A2.h>
Adafruit_MPL115A2 mpl115a2;

#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

#define VBATPIN A7

//
// For normal use, we require that you edit the sketch to replace FILLMEIN
// with values assigned by the TTN console. However, for regression tests,
// we want to be able to compile these scripts. The regression tests define
// COMPILE_REGRESSION_TEST, and in that case we define FILLMEIN to a non-
// working but innocuous value.
//
#ifdef COMPILE_REGRESSION_TEST
#define FILLMEIN 0
#else
#warning "You must replace the values marked FILLMEIN with real values from the TTN control panel!"
#define FILLMEIN (#dont edit this, edit the lines that use FILLMEIN)
#endif

// This EUI must be in little-endian format, so least-significant-byte
// first. When copying an EUI from ttnctl output, this means to reverse
// the bytes. For TTN issued EUIs the last bytes should be 0xD5, 0xB3,
// 0x70.
static const u1_t PROGMEM APPEUI[8] = { xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx };
void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8);}

// This should also be in little endian format, see above.
static const u1_t PROGMEM DEVEUI[8] = { xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx };
void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);}

// This key should be in big endian format (or, since it is not really a
// number but a block of memory, endianness does not really apply). In
// practice, a key taken from the TTN console can be copied as-is.
static const u1_t PROGMEM APPKEY[16] = { xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx };

void os_getDevKey (u1_t* buf) {  memcpy_P(buf, APPKEY, 16);}

// payload to send to TTN gateway
static uint8_t payload[5];
static osjob_t sendjob;

[color=#FFFF00]// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 600;[/color]

// Pin mapping for Adafruit Feather M0 LoRa
const lmic_pinmap lmic_pins = {
    .nss = 8,
//      .nss = 11,
      .rxtx = LMIC_UNUSED_PIN,
    .rst = 4,
//    .rst = 12,
      .dio = {3, 6, LMIC_UNUSED_PIN},
//      .dio = {5, 6, LMIC_UNUSED_PIN},
      .rxtx_rx_active = 0,
    .rssi_cal = 8,              // LBT cal for the Adafruit Feather M0 LoRa, in dB
    .spi_freq = 8000000,
};


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("artKey: ");
              for (size_t i=0; i<sizeof(artKey); ++i) {
                if (i != 0)
                  Serial.print("-");
                Serial.print(artKey[i], HEX);
              }
              Serial.println("");
              Serial.print("nwkKey: ");
              for (size_t i=0; i<sizeof(nwkKey); ++i) {
                      if (i != 0)
                              Serial.print("-");
                      Serial.print(nwkKey[i], HEX);
              }
              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;
        /*
        || This event is defined but not used in the code. No
        || point in wasting codespace on it.
        ||
        || case EV_RFU1:
        ||     Serial.println(F("EV_RFU1"));
        ||     break;
        */
        case EV_JOIN_FAILED:
            Serial.println(F("EV_JOIN_FAILED"));
            break;
        case EV_REJOIN_FAILED:
            Serial.println(F("EV_REJOIN_FAILED"));
            break;
            break;
        case EV_TXCOMPLETE:
            Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
            if (LMIC.txrxFlags & TXRX_ACK)
              Serial.println(F("Received ack"));
            if (LMIC.dataLen) {
              Serial.println(F("Received "));
              Serial.println(LMIC.dataLen);
              Serial.println(F(" bytes of payload"));
            }

            // Schedule next transmission
            //os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
            do_send(&sendjob);
            [color=#FFFF00]for (int i=0; i<int(TX_INTERVAL/8); i++) {
            // Use library from https://github.com/rocketscream/Low-Power
            //LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
            // going to deepsleep VMA
              digitalWrite(LED_BUILTIN, LOW); // Show we're asleep
              Watchdog.sleep(8000);
              digitalWrite(LED_BUILTIN, HIGH); // Show we're awake again
            }[/color]
            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;
        /*
        || This event is defined but not used in the code. No
        || point in wasting codespace on it.
        ||
        || case EV_SCAN_FOUND:
        ||    Serial.println(F("EV_SCAN_FOUND"));
        ||    break;
        */
        case EV_TXSTART:
            Serial.println(F("EV_TXSTART"));
            break;
        default:
            Serial.print(F("Unknown event: "));
            Serial.println((unsigned) ev);
            break;
    }
}

void do_send(osjob_t* j){
    // Check if there is not a current TX/RX job running
    if (LMIC.opmode & OP_TXRXPEND) {
        Serial.println(F("OP_TXRXPEND, not sending"));
    } else {
        // reset lpp for Cayene
        lpp.reset();
       
        // read the temperature from the mp1152a
        float temperature = 0;
        float pressure = 0;
        mpl115a2.getPT(&pressure,&temperature);
       
        Serial.print("Temperature: (°C):"); Serial.print(temperature);
        Serial.println(" *C");
        lpp.addTemperature(1, temperature);

        pressure = pressure * 10;
        Serial.print("press (hPa):");
        Serial.println(pressure);
        lpp.addBarometricPressure(2, pressure);

        VL53L0X_RangingMeasurementData_t measure;
        lox.rangingTest(&measure, false);
        float distance = 0;
        if (measure.RangeStatus != 4) {  // phase failures have incorrect data
          distance = measure.RangeMilliMeter;
          Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
        } else {
          Serial.println(" out of range ");
        }
        Serial.print("distance that will be sent: ");
        distance = distance/100; //distance in dm
        Serial.println(distance);
        lpp.addAnalogInput(3, distance);

        float measuredvbat = analogRead(VBATPIN);
        measuredvbat *= 2;    // we divided by 2, so multiply back
        measuredvbat *= 3.3;  // Multiply by 3.3V, our reference voltage
        measuredvbat /= 1024; // convert to voltage
        Serial.print("VBat (V) :" );
        Serial.println(measuredvbat);
        lpp.addAnalogInput(4, measuredvbat);

        // prepare upstream data transmission at the next possible time.
        // transmit on port 1 (the first parameter); you can use any value from 1 to 223 (others are reserved).
        // don't request an ack (the last parameter, if not zero, requests an ack from the network).
        // Remember, acks consume a lot of network resources; don't ask for an ack unless you really need it.
        Serial.print("buf size: ");
        Serial.println(lpp.getSize());
        Serial.print("Error: ");
        Serial.println(lpp.getError());
       
        LMIC_setTxData2(1, lpp.getBuffer(), lpp.getSize(), 0);
    }
    // Next TX is scheduled after TX_COMPLETE event.
}

void setup() {
    delay(5000);
//    while (! Serial);
    digitalWrite(13, LOW);
    Serial.begin(9600);
    Serial.println(F("Starting"));
    mpl115a2.begin();
    lox.begin();

    // LMIC init
    os_init();
    // Reset the MAC state. Session and pending data transfers will be discarded.
    LMIC_reset();
    // Disable link-check mode and ADR, because ADR tends to complicate testing.
    LMIC_setLinkCheckMode(0);
    // Set the data rate to Spreading Factor 7.  This is the fastest supported rate for 125 kHz channels, and it
    // minimizes air time and battery power. Set the transmission power to 14 dBi (25 mW).
    LMIC_setDrTxpow(DR_SF7,14);
    // in the US, with TTN, it saves join time if we start on subband 1 (channels 8-15). This will
    // get overridden after the join by parameters from the network. If working with other
    // networks or in other regions, this will need to be changed.
//    LMIC_selectSubBand(1);
    #if defined(CFG_eu868)
      Serial.println("European Channels");
      for (int i = 1; i <= 8; i++) LMIC_disableChannel(i);
    #elif defined(CFG_us915)
    // NA-US channels 0-71 are configured automatically
    // but only one group of 8 should (a subband) should be active
    // TTN recommends the second sub band, 1 in a zero based count.
    // https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json
      LMIC_selectSubBand(1);
    #endif
   
    // Start job (sending automatically starts OTAA too)
    do_send(&sendjob);
}

void loop() {
  // we call the LMIC's runloop processor. This will cause things to happen based on events and time. One
  // of the things that will happen is callbacks for transmission complete or received messages. We also
  // use this loop to queue periodic data transmissions.  You can put other things here in the `loop()` routine,
  // but beware that LoRaWAN timing is pretty tight, so if you do more than a few milliseconds of work, you
  // will want to call `os_runloop_once()` every so often, to keep the radio running.
  os_runloop_once();

}


Attention Spamchecking refues to use Cayene with two "n" so i removed one "n" in the code in order to submit it.



Hi,

many thx all my lora stuff is on circuitpython, so I don't want to switch to C :-(

pede
 
Posts: 28
Joined: Sun Jul 12, 2020 2:16 pm

Re: RFM9x Lora and Sleep Mode(s)? Lipo uptime very short

by jerryn on Tue Jan 12, 2021 9:00 am

Peter,
This is still an early "work in progress" bu tI think it may be of interest to you.
I have an an RFM9x breakout board connected to a Unexpected Maker FeatherS2
the RFM9x is powered by the LDO2 pin from the feathers2

I have a pushbutton switch connecting Pin IO5 to Ground w[hen pressed.

The following code transmits a message every 5 seconds as long as the button is pressed - then goes to sleep if the button is released.
I am still learning about how to best use the Pin Alarm functions but this seems to be a good first start and seemed like it might be relevant to your project.

I am using the "absolute Newest" build of CircuitPython from https://circuitpython.org/board/unexpectedmaker_feathers2/
I think you need at least the 6.1.0-rc.0 for the pin Alarm functions.

I hope it is a useful example,
Jerry

Code: Select all | TOGGLE FULL SIZE
import alarm
import alarm.pin
import time
import board
import digitalio
import adafruit_rfm9x


if alarm.wake_alarm:
    #print("awake", alarm.wake_alarm, alarm.wake_alarm.pin)
    print("awake", alarm.wake_alarm)
    alarm.sleep_memory[0] += 1
else:
    print("no wake up alarm")
    alarm.sleep_memory[0] = 0

#turn on the LED while it is awake
led=digitalio.DigitalInOut(board.LED)
led.switch_to_output()
led.value = True

# this is the alarm pin
pin=digitalio.DigitalInOut(board.IO5)
pin.pull  = digitalio.Pull.UP


print("count:", alarm.sleep_memory[0])

# Define radio parameters.
RADIO_FREQ_MHZ = 915.0  # Frequency of the radio in Mhz. Must match your
# module! Can be a value like 915.0, 433.0, etc.

# Define pins connected to the chip.
CS = digitalio.DigitalInOut(board.IO3)
RESET = digitalio.DigitalInOut(board.IO7)

# Initialize SPI bus.
#spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
spi = board.SPI()

# Initialze RFM radio
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)

rfm9x.tx_power = 23


# send a broadcast mesage as long as the pin is low
while not pin.value:
    rfm9x.send(bytes("I'm awake {}".format(alarm.sleep_memory[0]), "UTF-8"))
    time.sleep(5.)

#free the alarm pin
pin.deinit()

rfm9x.sleep()
print("packet sent - rfm9x sleeping")

#create an alarm on Pin IO5
pin_alarm = alarm.pin.PinAlarm(pin=board.IO5, value=False, pull=True)


print("about to deep_sleep")

# exit and set the alarm
alarm.exit_and_deep_sleep_until_alarms(pin_alarm)

jerryn
 
Posts: 1371
Joined: Sat Sep 14, 2013 9:05 am

Re: RFM9x Lora and Sleep Mode(s)? Lipo uptime very short

by jerryn on Tue Jan 12, 2021 12:32 pm

FYI -- I also tried using an RFM9x feather wing - powered normally (via LDO1) and it works as well.
I also ran this on a metro_esp32s2 as well with the same results.

jerryn
 
Posts: 1371
Joined: Sat Sep 14, 2013 9:05 am

Re: RFM9x Lora and Sleep Mode(s)? Lipo uptime very short

by pede on Wed Jan 13, 2021 4:26 am

jerryn wrote:FYI -- I also tried using an RFM9x feather wing - powered normally (via LDO1) and it works as well.
I also ran this on a metro_esp32s2 as well with the same results.

Hi Jerry,

many thanks for your code an hints. I wait every on my S2 boards from Pimoroni. It's rather difficult in Europe (Germany) to order it caused by the Brexit chaos. Shipping time is unfortunatelly much longer than in the past. As soon as I've got, I will start working on it.
I would like to exchange my experience with as you like.
Have a nice day wherever you are ...

regardly

Peter

pede
 
Posts: 28
Joined: Sun Jul 12, 2020 2:16 pm

Re: RFM9x Lora and Sleep Mode(s)? Lipo uptime very short

by pede on Thu Jan 14, 2021 3:15 pm

jerryn wrote:FYI -- I also tried using an RFM9x feather wing - powered normally (via LDO1) and it works as well.
I also ran this on a metro_esp32s2 as well with the same results.

Hi Jerry,

Hi, just got my f2 feather, after some click tries I see the UFTHRS2BOOT drive on my mac. I see a current.uf2 file and an index.htm and info_uf2.tx file, but when I try to copy the newest circuitpython.uf2 file on it (https://adafruit-circuit-python.s3.amaz ... 7015d7.uf2) I see short copy message starting the copy-process then after 15 seconds no copy and the drive disappears? the F2 reboots with the old uf2 (4.2 MB) Interessting the current.uf2 is double big as the newest adafruit.uf2. Did you use a Mac too? Any ideas whats wrong? Thx PD

pede
 
Posts: 28
Joined: Sun Jul 12, 2020 2:16 pm

Re: RFM9x Lora and Sleep Mode(s)? Lipo uptime very short

by jerryn on Fri Jan 15, 2021 6:52 am

When you copy the .uf2 file tot he the UFTHRS2BOOT drive, the UFTHRS2BOOT drive should "disappear" and the system should remount as a drive named CIRCUITPY when it ti s running CircuitPython. Does this happen?

THe UFTHRS2BOOT drive is not a "real" file system the .UF2 file you copy to it will always appear as CURRENT.UF2

I have used both a Mac and Linux system to connect to my Feather S2.

jerryn
 
Posts: 1371
Joined: Sat Sep 14, 2013 9:05 am

Please be positive and constructive with your questions and comments.