Hi, below is my code,
#include “LoRaWan_APP.h”
#include “Arduino.h”
#include “Wire.h”
#define VEXT_CONTROL 1
#define DEBUG_LOGS_ENABLED 1
//LED INDICATOR
#ifndef LoraWan_RGB
#define LoraWan_RGB 0
#endif
//LOW POWER CYCLE
#define timetosleep 150 //before sleep
#define timetowake 850 //before waking
typedef enum
{
ReadVoltage,
TX,
TXBattery
}States_t;
States_t state;
static TimerEvent_t sleep;
static TimerEvent_t wakeUp;
uint8_t lowpower=0;
void onSleep(void);
void onWakeup(void);
long lastSent=0;
float sensorValue = 0.0f;
float oldSensorValue = 0.0f;
uint16_t battery = 0;
bool isTimeoutLastSent = false;
void setup() {
delay(1000);
Serial.begin(9600);
delay(1000);
boardInitMcu( );
sensorValue = 0.0f;
oldSensorValue = 0.0f;
LoraInit();
state=TX;
TimerInit( &sleep, onSleep );
TimerInit( &wakeUp, onWakeUp );
PowerDownExternalPeripherals();
onWakeUp();
}
//CUBECELL SPECIFIC SLEEP TIMING MECHANISM
void onSleep()
{
if(DEBUG_LOGS_ENABLED)Serial.println(“low power:”);
lowpower=1;
TimerSetValue( &wakeUp, timetowake );
TimerStart( &wakeUp );
}
void onWakeUp()
{
if(DEBUG_LOGS_ENABLED) Serial.println(“wakeup:”);
lowpower=0;
TimerSetValue( &sleep, timetosleep );
TimerStart( &sleep );
}
bool LoraIRQProcess()
{
//LoRa.IRQProcess;
//and some check if last message exceeded the timeout
return false;
}
void loop()
{
if(lowpower)
{
battery = GetBattery(); //added this
//if(DEBUG_LOGS_ENABLED){ Serial.println("Sleeping");}
DeepSleepDevice();
}
else
{
switch(state)
{
case TX:
{
//update the old value if it is to be sent, so that it will not consider gradual increase,
//otherwise it not sent at all if the increase in speed is very slow
oldSensorValue = sensorValue;
lastSent = millis();
state = ReadVoltage; //default state if it is not on sleep
Send();
break;
}
case TXBattery:
{
lastSent = millis();
state = ReadVoltage; //default state if it is not on sleep
SendBattery();
break;
}
case ReadVoltage:
{
//Serial.println(millis());
long tDurSinceLast = millis()-lastSent;
if(tDurSinceLast < 100)
break;
if((millis()/1000)%30 == 0) //every 60 seconds send battery reading
{
state = TXBattery;
}
else if((millis()/1000)%10 == 0)
//every 30 seconds mandatory send reading, NOTE:millis() resets after 50 days
{
Serial.println(“Sending because of 10 sec”);
turnOnRGB(COLOR_SEND,0); //blink to know that it is still active
sensorValue = analogRead(ADC2);
state = TX;
}
else if(isTimeoutLastSent) //send if time out
{
Serial.println(“Sending because of timeout”);
sensorValue = analogRead(ADC2);
state = TX;
}
else
{
sensorValue = analogRead(ADC2);
float changeValue = ((sensorValue - oldSensorValue));
float percentChange = oldSensorValue < sensorValue ? (abs(changeValue)/(sensorValue)) : (abs(changeValue)/(oldSensorValue));
if(percentChange > 0.10) //if greater than 10 percent change, send the value
{
Serial.println("Sending because of change");
state = TX;
}
else if(tDurSinceLast > 100)//lora timeout
//if lora_tx_timeout is already done, then sleep(means longer than that there is nothing to do but read sensor)
{
lowpower = 1;
}
//if none of the conditions are met, state = ReadVoltage
}
break;
}
default:
break;
}
}
isTimeoutLastSent = LoraIRQProcess(); //just simply returns if the last message was not sent
}
uint16_t GetBattery(){
pinMode(VBAT_ADC_CTL, OUTPUT);
digitalWrite(VBAT_ADC_CTL, LOW);
uint16_t voltage = getBatteryVoltage();
digitalWrite(VBAT_ADC_CTL, HIGH);
return voltage;
}
void PowerDownExternalPeripherals()
{
Wire.end();
pinMode(GPIO5,ANALOG);
pinMode(GPIO6,ANALOG);
pinMode(GPIO7,ANALOG);
pinMode(ADC,INPUT);
VextOff();
}
void DeepSleepDevice()
{
//Lora.Sleep(); //can also be down after sending packet
turnOffRGB();
PowerDownExternalPeripherals();
lowPowerHandler();
}
void VextOn()
{
#if defined(VEXT_CONTROL)
pinMode(Vext,OUTPUT);
digitalWrite(Vext, HIGH); //usually high means off but need to check per mcu
#endif
}
void VextOff()
{
#if defined(VEXT_CONTROL)
pinMode(Vext,OUTPUT);
digitalWrite(Vext, LOW); //usually high means off but need to check per mcu
#endif
}
void LoraInit()
{
//lora initialization
}
void Send()
{
//using lora
}
void SendBattery()
{
//using lora
}