LoRaWan – Estado de bateria. Archivo.ino

El módulo de desarrollo Heltec Wireless Stick Lite tiene el circuito de sensor y alimentación por baterías. El diagrama de pines muestra que el pin 13 se encuentra conectado como detector de energía, que se incorpora a las instrucciones como PowerDetection. En los nuevos modelos, el detector de energía es el pin 37.

Preparación de trama

El valor del sensor ADC para bateria es de tipo entero, entrega la cuantificación en el rango [0, 4096], por lo que un entero de 2 bytes es suficiente para los datos. El proceso de conversión a voltaje se realiza en el decodificador en Application-Server del Broker ChirpStack.

  uint16_t BateriaV = SensorBateria();
  
  appDataSize = 4 ;
  appData[3] = 0 ;
  appData[2] = 0 ;
  appData[1] = highByte(BateriaV);
  appData[0] = lowByte(BateriaV);

Intrucciones Principales

/* Adaptado a partir de:
 * HelTec Automation(TM) LoRaWAN 1.0.2 OTAA example use OTAA, CLASS A
 * Solo ESP32+LoRa series boards con licencia http://www.heltec.cn/search/);
 * https://github.com/HelTecAutomation/ESP32_LoRaWAN/tree/master/examples/OTAA_Battery_power
*/
#include <ESP32_LoRaWAN.h>
#include "Arduino.h"

/*licencia Heltec ESP32 LoRaWan http://resource.heltec.cn/search */
uint32_t  license[4] = { 0xC254CA22, 0xFB5646A9, 0xA23B184F, 0x8F613844};

/* OTAA parametros*/
uint8_t DevEui[] = { 0xc5, 0xdb, 0x5e, 0x2b, 0x64, 0xee, 0xfc, 0xae };
uint8_t AppEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t AppKey[] = { 0xf2, 0x1f, 0xff, 0x5c, 0x6f, 0xf2, 0x4c, 0xa0, 0x74, 0x73, 0xf5, 0xef, 0xf7, 0x39, 0x39, 0x10 };

/* ABP parametros*/
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;

/*LoraWan channelsmask, default channels 0-7*/ 
uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 };

DeviceClass_t  loraWanClass = CLASS_A; /*Soporte de A and C*/
uint32_t appTxDutyCycle = 15000;       /*15000; en [ms]*/
bool overTheAirActivation = true;      /*OTAA or ABP*/
bool loraWanAdr = true;                /*ADR enable*/
bool isTxConfirmed = true;             /*confirmed or unconfirmed messages */
uint8_t appPort = 2;                   /* Application port */

/* reintentos de transmisión, en caso de no recibir ack */
uint8_t confirmedNbTrials = 8;

/* Seleccionado de Arduino IDE tools */
uint8_t debugLevel = LoRaWAN_DEBUG_LEVEL;
LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;

// Mensajes por Puerto Serial
volatile boolean serial_msg = true;

// variables de sensor/actuador
// Sensor de estado de bateria, revisar modelo de módulo
// PowerDetection 13 en Wireless Stick lite, otros 37
#define Vext 21
#define PowerDetection 13

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  SPI.begin(SCK,MISO,MOSI,SS);
  Mcu.init(SS,RST_LoRa,DIO0,DIO1,license);

  //sensor de bateria
  adcAttachPin(PowerDetection);
  analogSetClockDiv(255); // 1338mS
    pinMode(Vext, OUTPUT);
    
  deviceState = DEVICE_STATE_INIT;
}

void loop() {
  
  switch( deviceState )  {
    case DEVICE_STATE_INIT:    {
      LoRaWAN.init(loraWanClass,loraWanRegion);
      break;
    }
    case DEVICE_STATE_JOIN:    {
      LoRaWAN.join();
      break;
    }
    case DEVICE_STATE_SEND:    { 
      prepareTxFrame( appPort );
      LoRaWAN.send(loraWanClass);
      deviceState = DEVICE_STATE_CYCLE;
      break;
    }
    case DEVICE_STATE_CYCLE:    {
      // Schedule next packet transmission
      txDutyCycleTime = appTxDutyCycle + randr( -APP_TX_DUTYCYCLE_RND,
                                                APP_TX_DUTYCYCLE_RND );
      LoRaWAN.cycle(txDutyCycleTime);
      deviceState = DEVICE_STATE_SLEEP;
      break;
    }
    case DEVICE_STATE_SLEEP:    {
      LoRaWAN.sleep(loraWanClass,debugLevel);
      break;
    }
    default:    {
      deviceState = DEVICE_STATE_INIT;
      break;
    }
  }
}

Preparación de trama

// Trama - integra datos
static void prepareTxFrame( uint8_t port ){
  
  uint16_t BateriaV = SensorBateria();
  
  appDataSize = 4 ;
  appData[3] = 0 ;
  appData[2] = 0 ;
  appData[1] = highByte(BateriaV);
  appData[0] = lowByte(BateriaV);
}

Lectura de sensor

uint16_t SensorBateria(){
  
  // lectura ADC Voltaje de bateria 
  digitalWrite(Vext, LOW);
  delay(10);
  uint16_t ADC_voltage = analogRead(PowerDetection);
  digitalWrite(Vext, HIGH);

  Serial.print("Voltaje Batería (V): ");
  Serial.println(ADC_voltage);

  return ADC_voltage;
}

Referencia: Heltec LoRa ESP32, https://github.com/HelTecAutomation/ESP32_LoRaWAN/blob/master/examples/OTAA_Battery_power/OTAA_Battery_power.ino

Heltec LoRa ESP32 Wireless Stick Lite, diagrama de pintes- https://resource.heltec.cn/download/Wireless_Stick_Lite/Wireless_Stick_Lite.pdf