Hello, I hope someone is able to help
I'm having an issue with an arduino pro mini, being used for a very simple weather station transmitter. It reads some data from a bme280, transmits the data and then sleeps for 5 mins. The sleep is accomplished by setting an alarm on a ds3231, and then sleeping until the interrupt occurs. The above then loops.
This all seems to work fine for a while, but if you leave it running eventually it seems to end up in some kind of crash loop. The led on the Arduino blinks either brightly, or very dimly with an almost continuous flash.
I found this page a while back and used it to flash optiboot bootloader to pro mini but it still seems to encounter the same issue.
My code is as follows:
#include "LowPower.h"
#include "ds3231.h"
#include <time.h>
#include <Wire.h>
#include <RH_ASK.h>
#include <forcedClimate.h>
ForcedClimate climateSensor = ForcedClimate();
int i;
char msg[100];
const char *tempstr = "temp:";
const char *presstr = "pres:";
const char *humstr = "hum:";
const char *divstr = ":";
static const byte wakeUpPin = 2;
uint8_t sleep_period = 5;
RH_ASK driver(2000,4,3);
struct bmedata{
char temp[5];
char press[7];
char hum[6];
};
bmedata bmedataStr;
void getBmeVals(bmedata *bmedata){
climateSensor.takeForcedMeasurement();
dtostrf(climateSensor.getTemperatureCelcius(), 4, 1, bmedata->temp);
dtostrf(climateSensor.getPressure(), 6, 1, bmedata->press);
dtostrf(climateSensor.getRelativeHumidity(), 4, 1, bmedata->hum);
}
// the setup function runs once when you press reset or power the board
void setup() {
//Serial.begin(9600);
climateSensor.begin();
pinMode(LED_BUILTIN, OUTPUT);
int i;
for(i=0;i<5;i++){
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(200);
}
driver.init();
DS3231_init(DS3231_CONTROL_INTCN);
DS3231_clear_a2f();
// Configure Interrupt Pin
pinMode(wakeUpPin, INPUT_PULLUP);
digitalWrite(wakeUpPin, HIGH);
}
void wakeUp(){
}
void set_next_alarm(void)
{
struct ts t;
unsigned char wakeup_min;
DS3231_get(&t);
// calculate the minute when the next alarm will be triggered
wakeup_min = (t.min / sleep_period + 1) * sleep_period;
if (wakeup_min > 59) {
wakeup_min -= 60;
}
uint8_t flags[4] = { 0, 1, 1, 1 };
// set Alarm2. only the minute is set since we ignore the hour and day component
DS3231_set_a2(wakeup_min, 0, 0, flags);
// activate Alarm2
DS3231_set_creg(DS3231_CONTROL_INTCN | DS3231_CONTROL_A2IE| DS3231_CONTROL_BBSQW);
}
// the loop function runs over and over again forever
void loop() {
getBmeVals(&bmedataStr);
sprintf(msg, "%s%s%s%s%s%s%s%s", tempstr, bmedataStr.temp, divstr, presstr, bmedataStr.press, divstr, humstr, bmedataStr.hum);
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
set_next_alarm();
attachInterrupt(0, wakeUp, FALLING);
delay(500);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
DS3231_clear_a2f();
detachInterrupt(0);
}
Is there anything obvious in my code which eventually crash the pro mini?
Perhaps a memory leak? I've seen some people raise concerns around sprintf crashing some arduino's, which I'm using.
As this takes hours to crash (maybe even more than 24 hours) it's not easy to debug. Perhaps I need to get it to write out to an SD card as a means of saving logs to work out at which point it's crashing?
Thanks in advance for any help.