Hi, after testing the esp32 DOIT Devkit v1 and seeing that the battery was very short in deep-sleep mode I decided to try the esp32 from the manufacturer Firebeetle. The difference in battery life is quite considerable but I have a problem that I don't understand and I'm not able to solve.
My project consists of calculating the distance to an object by means of an ultrasound and sending that measurement through Sigfox so that it reaches a web page. In order to save battery power I have to make the emission every hour and after sending it the microcontroller goes into deep-sleep mode. My problem is that when he wakes up, instead of taking a single measurement, there are times when he takes up to 10 or 15 measurements in a row every 10 seconds, consuming a lot of battery power. Does anyone know what could be happening? The only theory I can come up with is that something is interfering with his sleep, but I don't really understand what that is.
I'm enclosing the code in case someone can help me
#include <HardwareSerial.h>
#include "esp_sleep.h"
#define uS_TO_S_FACTOR 1000000ULL
#define TIME_TO_SLEEP 3600
#define LECTURAS 10
#define TAM_TRAMA_SIGFOX 4
HardwareSerial Sigfox(1);
int SigfoxBits = 9600;
uint8_t sigfoxMsg[TAM_TRAMA_SIGFOX];
const int
pwPin1 = 13;
long sensor1, cm;
void read_sensor()
{
int acumulador =0;
for (int i =0; i<LECTURAS; i++)
{
delay(200);
acumulador=acumulador + ((int)pulseIn(pwPin1, HIGH) / 58);
}
cm=acumulador/LECTURAS;
}
String sendMessage(uint8_t SigfoxMsg[], int bufferSize) {
String status = "";
char SigfoxBuffer;
// Send AT$SF=xx to WISOL to send XX (payload data of size 1 to 12 bytes)
Sigfox.print("AT$SF=");
for(int i= 0;i<bufferSize;i++){
if (SigfoxMsg[i]<0x10)
{
Sigfox.print("0");
}
Sigfox.print(String(SigfoxMsg[i], HEX));
}
Sigfox.print("\r");
while (!Sigfox.available()){
delay(10);
}
while(Sigfox.available()){
SigfoxBuffer = (char)Sigfox.read();
status += SigfoxBuffer;
delay(10);
}
return status;
}
void setup () {
Serial.begin(115200);
pinMode(pwPin1, INPUT);
Sigfox.begin(9600, SERIAL_8N1, 16, 17);
delay(300);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP*uS_TO_S_FACTOR);
}
void loop () {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
if (wakeup_reason == ESP_SLEEP_WAKEUP_TIMER)
{
read_sensor();
sigfoxMsg[0] = cm >> 24;
sigfoxMsg[1] = cm >> 16;
sigfoxMsg[2] = cm >> 8;
sigfoxMsg[3] = cm;
sendMessage(sigfoxMsg, TAM_TRAMA_SIGFOX);
}
esp_deep_sleep_start();
}