-->
Page 1 of 1

esp8266 rtc deepsleep reset issue

PostPosted: Mon Jun 01, 2020 12:39 am
by vinaykumar
hi,
i m working on the esp8266 ,rtc ds3231 module ,in this mainly concentrating on the power consumption for that i need to keep the esp in deep sleep for a time period ,esp is going to deepsleep but not getting reset for that time period .please do check my code and help me in any changes needed to done in the code.

/* esp is kept in deepsleep mode for every 2 hours*/


#include<WiFiManager.h>
#include<ESP8266WiFi.h>
#include<RtcDS3231.h>
#include<Wire.h>



//const char* ssid = "faculty"; // Light_WIFI
const char* ssid = "Light_WIFI";
const char* password = "light@123";

const byte interruptPin = 13;
volatile bool alarm = 0;

byte temp_Minutes = 0 , temp_Hours = 0 , temp_seconds = 0;
//unsigned char data[20] = "2020:5:27:13:55:25"; // data to write
char data[20] = "2020:5:27:13:55:25";
uint8_t distance;
char date_time_eep[20];
unsigned int addr , i , j , cnt;

/*
Static IP address configuration
*/
//IPAddress staticIP(192,168,1,22); //ESP static ip
//IPAddress gateway(192,168,1,9); //IP Address of your WiFi Router (Gateway)
//IPAddress subnet(255,255,255,0); //Subnet mask


//rtc object
RtcDS3231<TwoWire> rtcObject(Wire); //
RtcDateTime (currentTime);


void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddresspage >> 8)); // MSB
Wire.write((int)(eeaddresspage & 0xFF)); // LSB
byte c;
for ( c = 0; c < length; c++)
Wire.write(data[c]);
Wire.endTransmission();
}

void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(rdata);
Wire.endTransmission();
}

byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.read();
return rdata;


}
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
int c = 0;
for ( c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.read();

}

void connect_to_wifi()
{
// WiFi.begin(ssid, NULL);
WiFi.begin(ssid, password);
// WiFi.config(staticIP, gateway, subnet);
Serial.println(WiFi.localIP());
while(WiFi.status() != WL_CONNECTED)
{
delay(50);
Serial.print(".");
}
return;
}

ICACHE_RAM_ATTR void handleInterrupt()
{
alarm = true;
}

void delay_ds3231()
{
for(i = 0 ; i <= 1000 ; i++)
for(j = 0; j <= 2000 ; j++); ///// manul delay
//
}

void setup()
{
Wire.begin(); // initialise the connection
WiFi.mode(WIFI_OFF);
//WiFi.forceSleepBegin();
delay(1);
Serial.begin(9600);
Serial.println("I am in setup");
Serial.print("MAC: ");
Serial.println(WiFi.macAddress());
pinMode(interruptPin , INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin) , handleInterrupt , FALLING );
rtcObject.Begin();
delay(50);
/*
* reading from ds3231 eeprom
*/
RtcDateTime now = rtcObject.GetDateTime();
RtcDateTime (currentTime) = rtcObject.GetDateTime();
//Serial.println(currentTime);


// if (now <currentTime)
// {
// Serial.println("RTC is older than compile time! (Updating DateTime)");
// rtcObject.SetDateTime(currentTime);
// }

Serial.println("Reading eeprom memory");
i2c_eeprom_read_buffer(0x57,0,(byte *)date_time_eep,sizeof(date_time_eep));
Serial.print("setup date from eep : ");
Serial.println(date_time_eep);
delay(50);

if( date_time_eep[5] != '1')
{
//Serial.println (date_time_eep[5]);
Serial.println("Nothing");
}

unsigned int y , M , h , m=0, s;
int addr=0; //first address

// access the first address from the memory
i2c_eeprom_write_page(0x57 , 0 , (byte *)data , sizeof(data));
byte b = i2c_eeprom_read_byte(0x57, 1);
bool colon = false;
unsigned int time_date_int[10] ={0} , i = 0;
Serial.println();
while (b != 0)
{
Serial.println("outer while 1");
Serial.print("b : ");
Serial.println(b);
while((b != ':') && (b != 0))
{
Serial.print("inner while : ");
time_date_int[i] *= 10;
time_date_int[i] += (b - 48);
// Serial.print(time_date_int[i]);
// Serial.print(" inner while index : ");
// Serial.println(i);
// delay(500);
// Serial.println(i);
b = i2c_eeprom_read_byte(0x57, addr); //access an address from the memory
addr++;
}
Serial.print("outer index : ");
//Serial.println(i);
Serial.print(time_date_int[i]);
i++;
Serial.print(":");
b = i2c_eeprom_read_byte(0x57, addr);
addr++;
break;
// i2c_eeprom_read_buffer(0x57,0,(byte *)date_time_eep,sizeof(date_time_eep));
}
Serial.println();
for(i=0 ; i < 6 ; i++)
{
Serial.print(time_date_int[i]);
Serial.print(" ");
}
Serial.println();

if(time_date_int[0] >= 2020)
{
// sprintf( data, "%d:%d:%d:%d:%d:%d", 2020 , 5 , 27 , 13 , 55 , 30);
i2c_eeprom_write_page(0x57 , 0 , (byte *)data , sizeof(data));
delay(100);
Serial.println("eeprom rewriting");


for(i = 0 ; i < 10 ; i++)
time_date_int[i] = 0;
b = i2c_eeprom_read_byte(0x57, 1);
i=0;
addr = 0;
Serial.println();
while (b!=0)
{
Serial.println("outer while 2");
while((b != ':') && (b != 0))
{
Serial.println("inner while");
time_date_int[i] *= 10;
time_date_int[i] += (b - 48);
// Serial.println(i);
b = i2c_eeprom_read_byte(0x57, addr); //access an address from the memory
addr++;
}

Serial.print(i);
Serial.print(" index ");
Serial.print(time_date_int[i]);
i++;
Serial.print(":");
b = i2c_eeprom_read_byte(0x57, addr);
addr++;
}
}
m=currentTime.Minute();
Serial.print("Current Minutes : ");
Serial.println(m);
if(time_date_int[0] != currentTime.Year())
{
Serial.println("date and time are not matching and updating from ds3231 eeprom");
RtcDateTime (currentTime) = RtcDateTime(time_date_int[0]-2000 , time_date_int[1] , time_date_int[2] , time_date_int[3] , time_date_int[4] , time_date_int[5]);
rtcObject.SetDateTime(currentTime);
}
else if( m > time_date_int[4] )
{
Serial.println("date and time are updated");

}
byte temp_Minutes = 0 , temp_Hours = 0 , temp_seconds = 0;
rtcObject.Enable32kHzPin(false);
rtcObject.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmOne);
// rtcObject.SetSquareWavePin(DS3231SquareWavePin_ModeClock); // set pin mode
// rtcObject.SetSquareWavePinClockFrequency(DS3231SquareWaveClock_1Hz); // Sets Frequency
temp_seconds = currentTime.Second();
Serial.print("Current Seconds : ");
Serial.println(temp_seconds);
if(temp_seconds == 0)
{
Serial.print("Handling Seconds: ");
Serial.println(temp_seconds);
temp_seconds = 59;
}
// DS3231AlarmOne alarm1(0 , 0 , 0 , temp_seconds-1 , DS3231AlarmOneControl_SecondsMatch); /// Iterrupt at every 5 minutes
temp_Minutes = currentTime.Minute(); /// 5 minutes
Serial.print("Current Minutes: ");
Serial.println(temp_Minutes);
if((temp_Minutes+1) > 59)
{
Serial.print("Handling Minutes : ");
Serial.println(temp_Minutes);
temp_Minutes = -1;
}
// DS3231AlarmOne alarm1(0 , 0 , temp_Minutes+1 , 0 , DS3231AlarmOneControl_MinutesSecondsMatch); /// Iterrupt at every 5 minutes
temp_Hours = currentTime.Hour();
Serial.println("Current Hours: ");
Serial.println(temp_Hours);
if(temp_Hours == 0)
{
temp_Hours = 2;
Serial.println("Alarm @2am");
}
else if((temp_Hours >= 2) && (temp_Hours <= 3))
{
temp_Hours = 4;
Serial.println("Alarm @4am");
}
else if((temp_Hours >= 3) && (temp_Hours <= 5))
{
temp_Hours = 6;
Serial.println("Alarm @6am");
}
else if((temp_Hours >= 5) && (temp_Hours <= 7))
{
temp_Hours = 8;
Serial.println("Alarm @8am");
}
else if((temp_Hours >= 7) && (temp_Hours <= 9))
{
temp_Hours = 10;
Serial.println("Alarm @10am");
}
else if((temp_Hours >= 9) && (temp_Hours <= 11))
{
temp_Hours = 12;
Serial.println("Alarm @12pm");
}
else if((temp_Hours >= 11) && (temp_Hours <= 13))
{
temp_Hours = 14;
Serial.println("Alarm @2pm");
}
else if((temp_Hours >= 13) && (temp_Hours <= 15))
{
temp_Hours = 16;
Serial.println("Alarm @4pm");
}
else if((temp_Hours >= 15) && (temp_Hours <= 17))
{
temp_Hours = 18;
Serial.println("Alarm @6pm");
}
else if((temp_Hours >= 17) && (temp_Hours <= 19))
{
temp_Hours = 20;
Serial.println("Alarm @8pm");
}
else if((temp_Hours >= 19) && (temp_Hours <= 21))
{
temp_Hours = 22;
Serial.println("Alarm @10pm");
}
else if((temp_Hours >= 21) && (temp_Hours <= 23))
{
temp_Hours = 0;
Serial.println("Alarm @12am");
}

DS3231AlarmOne alarm1(0 , temp_Hours, 0 , 0 , DS3231AlarmOneControl_HoursMinutesSecondsMatch); /// Iterrupt at every 5 minutes
delay(50);

rtcObject.SetAlarmOne(alarm1);
rtcObject.LatchAlarmsTriggeredFlags();
delay(50);
Serial.println("Moving to setup ");

}

void loop()
{
delay(50);
Serial.println("I am in loop");
// delay(500);
/* Sensor Reading */

/*
while(data[i] != 0xFF)
{
data[i] = Serial.read();
}

while(0xFF != Serial.read())
for(int i=0;i<4;i++)
{
data[i]=Serial.read();
}
distance = data[1]*256+data[2];
//Serial.print("chec = "); Serial.println(data[3]);
uint8_t checks = (data[0]+data[1]+data[2])&0xFF;
//Serial.print("checksum = ");Serial.println(checks);

if (checks == data[3]) //checksum is equals to data[3]
{
Serial.println("distance = "+String(distance));
}
else
{
Serial.println("data corrupted");
delay(100);
}
*/

// Read Sensor readings
rtcObject.LatchAlarmsTriggeredFlags();
// connecting to WIFI
WiFi.forceSleepWake();
delay(1);

//Bring up the wifi connection
WiFi.mode(WIFI_STA);
while (WiFi.status() != WL_CONNECTED)
{
connect_to_wifi();
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
}


// Updating latest date and time
if(alarm == true)
{
Serial.println("in handler");
handleAlarm(); /// interrupt when connected to I/O pin
}
//delay(1000);
//delay_ds3231();

RtcDateTime (timestamp) = rtcObject.GetDateTime();
Serial.print("time: ");
char time1[20];
sprintf(time1, "%d:%d:%d:%d:%d:%d",
timestamp.Year(),
timestamp.Month(),
timestamp.Day(),
timestamp.Hour(),
timestamp.Minute(),
timestamp.Second()
);
Serial.println(time1);
i2c_eeprom_write_page(0x57 , 0 , (byte *)time1 , sizeof(time1));
time1[0] = 0;
delay(20);
i2c_eeprom_read_buffer(0x57,0,(byte *)time1,sizeof(time1));
Serial.print("ds3231 EEPROM : ");
Serial.println(time1);
Serial.println("going to sleep \n");
Serial.println(currentTime.Minute());
delay(100);
ESP.deepSleep(0); // esp sleeps forever.
delay(10);

}//loop
void handleAlarm() /// It wont execute why because INT/SQW pin connected to ESP.RST pin through 100uf capacitor(Please connect 10k resistor series with VCC)
{
alarm = false;

RtcDateTime (timestamp) = rtcObject.GetDateTime();
00*1e6); // for 1 hour
Serial.print("time interrupt at: ");
char time1[20];

sprintf(time1, "%d:%d:%d:%d:%d:%d",
timestamp.Year(),
timestamp.Month(),
timestamp.Day(),
timestamp.Hour(),
timestamp.Minute(),
timestamp.Second()
);
cnt = 0;
Serial.println(time1);

rtcObject.LatchAlarmsTriggeredFlags();

}

thanks in advance ...