Hi, I have working with the Wifi Lora 32 V2 and a Advantech GW and all the sketch had worked very well for me .
Now, I’m taking current meausurement of the battery (Lipo 1500 mA) and realize that it always is on 50 mA, so, I don’t know if the module goes to sleep really!!
To make a test I added some lines (comments in this sketch) to wake the module up from digital interrupt and timer with esp_deep_sleep_start(); function, it work very well at sleep momment with 2.6 mA of current consumption and waking up from the sources (timer and DI).
The problem now with this new lines is the module can’t comunicate with the GW any more, I think because it goes sleep before call the Lorawan.sleep() function, that is who made some tricks (I can’t found how it work on libraries).
I don’t know if the LoraWAN library can be used with DI to wake it up, I could find any example about it.
Regards, Damian.
/*
* HelTec Automation(TM) LoRaWAN 1.0.2 OTAA example use OTAA, CLASS A
*
* Function summary:
*
* - You can use port 4 to control the LED light.
*
* - If the issued value is 1(ASCII), the lamp will be lit.
*
* - The release value is 2(ASCII) and the light will be turned off.
*
* - use internal RTC(150KHz);
*
* - Include stop mode and deep sleep mode;
*
* - 15S data send cycle;
*
* - Informations output via serial(115200);
*
* - Only ESP32 + LoRa series boards can use this library, need a license
* to make the code run(check you license here: http://www.heltec.cn/search/);
*
* You can change some definition in "Commissioning.h" and "LoRaMac- definitions.h"
*
* HelTec AutoMation, Chengdu, China.
* 成都惠利特自动化科技有限公司
* https://heltec.org
* support@heltec.cn
*
*this project also release in GitHub:
*https://github.com/HelTecAutomation/ESP32_LoRaWAN
*/
#include <ESP32_LoRaWAN.h>
#include "Arduino.h"
#include "esp_sleep.h"
/*license for Heltec ESP32 LoRaWan, quary your ChipID relevant license: http://resource.heltec.cn/search */
uint32_t license[4] = {my license};
/* OTAA para*/
uint8_t DevEui[] = { my DEVEUI};
uint8_t AppEui[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x02, 0xFB, 0x62 };
uint8_t AppKey[] = { 0x5F, 0x67, 0x66, 0xB8, 0x57, 0xD6, 0x3F, 0x78, 0x27, 0xE3, 0x5D, 0x58, 0xE7, 0x23, 0x95, 0xC0 };0
/* ABP para*/
uint8_t NwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda,0x85 };
uint8_t AppSKey[] = { 0xd7, 0x2c, 0x78, 0x75, 0x8c, 0xdc, 0xca, 0xbf, 0x55, 0xee, 0x4a, 0x77, 0x8d, 0x16, 0xef,0x67 };
uint32_t DevAddr = ( uint32_t )0x007e6ae1;
#define BUTTON_PIN_BITMASK 0x4 // GPIOs 2
/*LoraWan Class, Class A and Class C are supported*/
DeviceClass_t loraWanClass = CLASS_A;
/*the application data transmission duty cycle. value in [ms].*/
uint32_t appTxDutyCycle = 30000;
/*OTAA or ABP*/
bool overTheAirActivation = true;
/*ADR enable*/
bool loraWanAdr = true;
/* Indicates if the node is sending confirmed or unconfirmed messages */
bool isTxConfirmed = true;
/* Application port */
uint8_t appPort = 2;
uint8_t confirmedNbTrials = 4;
/*LoraWan debug level, select in arduino IDE tools.
* None : print basic info.
* Freq : print Tx and Rx freq, DR info.
* Freq && DIO : print Tx and Rx freq, DR, DIO0 interrupt and DIO1 interrupt info.
* Freq && DIO && PW: print Tx and Rx freq, DR, DIO0 interrupt, DIO1 interrupt and MCU deepsleep info.
*/
uint8_t debugLevel = LoRaWAN_DEBUG_LEVEL;
/*LoraWan region, select in arduino IDE tools*/
LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;
#define LEDPin 25 //LED light
void app(uint8_t data)
{
//El dato por lorawan viene en HEX, para ello hay que pasarlo a decimal
switch(data)
{
case 73:
{
pinMode(LEDPin,OUTPUT);
digitalWrite(LEDPin, HIGH);
break;
}
case 80:
{
pinMode(LEDPin,OUTPUT);
digitalWrite(LEDPin, LOW);
break;
}
case 51:
{
break;
}
default:
{
break;
}
}
}
//######## esta funcion recibe el dato en el Buffer[] de tamano BufferSize
void downLinkDataHandle(McpsIndication_t *mcpsIndication)
{
lora_printf("+REV DATA:%s,RXSIZE %d,PORT %d\r\n",mcpsIndication- >RxSlot?"RXWIN2":"RXWIN1",mcpsIndication->BufferSize,mcpsIndication->Port);
lora_printf("+REV DATA:");
for(uint8_t i=0;i<mcpsIndication->BufferSize;i++)
{
lora_printf("%02X",mcpsIndication->Buffer[i]);
}
lora_printf("\r\n");
lora_printf("%02x",mcpsIndication->Buffer[0]);
//Funcion app que llama a prender o apagar un led segun el dato recibido
app(mcpsIndication->Buffer[0]);
}
// Add your initialization code here
void setup()
{
//######## En caso que en deep sleep se utilice una GPIO con pull-down interna y pull-up externa esto hara que circule corriente,
//para evitar esto se debe aislar la GPIO con la funcion rtc_gpio_isolate()
pinMode(2,INPUT);
// pinMode(5,INPUT);
// pinMode(14,INPUT);
pinMode(15,INPUT);
pinMode(16,INPUT);
pinMode(17,INPUT);
//pinMode(18,INPUT);
//pinMode(19,INPUT);
//pinMode(26,INPUT);
//pinMode(27,INPUT);
pinMode(21,OUTPUT);
//Serial.begin(115200);
Serial.end();
while (!Serial);
SPI.begin(SCK,MISO,MOSI,SS);
Mcu.init(SS,RST_LoRa,DIO0,DIO1,license);
deviceState = DEVICE_STATE_INIT;
}
// The loop function is called in an endless loop
void loop()
{
switch( deviceState )
{
case DEVICE_STATE_INIT:
{
//Inicia la comunicacion LoraWAN con la Clase y la configuracion de la Frecuencia, el canal mediante enmascaramiento, Inicia el timer para el procimo envio
// Si no realizo el Join -> deviceState=DEVICE_STATE_JOIN , si el Join ya fue realizado -> deviceState=DEVICE_STATE_SEND, configurado
//para enviar el dato
LoRaWAN.init(loraWanClass,loraWanRegion);
break;
}
case DEVICE_STATE_JOIN:
{
//Realiza el Join con el servidor LoraWAN por OTAA o ABP, segun como se haya configurado, si realiza el Join va a Sleep,
// sino va a Cycle
LoRaWAN.join();
break;
}
case DEVICE_STATE_SEND:
{
/// Dato a ser enviado, donde las variables appDataSize y AppData estan definidas en la libreria
appDataSize = 10;//AppDataSize max value is 64
appData[0] = 0x00;
appData[1] = 0x01;
appData[2] = 0x02;
appData[3] = 0x03;
appData[4] = 0xFF;
appData[5] = 0x6C;
// Funcion que envia el dato appData, junto con appDataSize y appPort - El dato no puede superar los 64 bytes
LoRaWAN.send(loraWanClass);
deviceState = DEVICE_STATE_CYCLE;
break;
}
case DEVICE_STATE_CYCLE:
{
// Genera un DutyCycleTime aleatorio para el proximo envio, pero nosotros lo vamos a usar fijo, expresado en ms
txDutyCycleTime = appTxDutyCycle + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
//Lanza un Timer por txDutyCycleTime ms, el cual llegado a su fin genera un evento
LoRaWAN.cycle(txDutyCycleTime);
deviceState = DEVICE_STATE_SLEEP;
break;
}
case DEVICE_STATE_SLEEP:
{
//And before every sleep call I set the config.
//esp_sleep_enable_timer_wakeup(appTxDutyCycle*1000);
//esp_sleep_pd_config(ESP_PD_DOMAIN_MAX, ESP_PD_OPTION_OFF);
// LoRaWAN.sleep(loraWanClass,debugLevel); // llama a Mcu.sleep(classMode,debugLevel);
// esp_deep_sleep_start();
//Llama a la funcion MCU.sleep y le pasa la clase LoraWAN A o C
LoRaWAN.sleep(loraWanClass,debugLevel); // llama a Mcu.sleep(classMode,debugLevel);
break;
}
default:
{
deviceState = DEVICE_STATE_INIT;
break;
}
}
}