Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By dnts
#24773 I don't know if this topic belongs here but since it was my last resort for dealing with reboots - well, here it is.
I have a nice sketch for a kitchen timer with web access so I can monitor and control it from my mobile. All is nice and dandy but it just reboots now and then. It can run for 8 hours straight and then get a WDT reset.. or run for 10 seconds and reboot. Now.. a kitchen timer should never loose count! or I will get overcooked pasta. I didn't find any apparent cause (power supply is OK.. code appears smooth) so I decided I need persistence between reboots. The standard RAM is cleared following reset so it's a no-go. But lo and behold! the ESP has an RTC RAM. This RAM, accessible via the "system_rtc_mem_read" and "system_rtc_mem_write" API calls can hold enough data to memorize the state of the system. Note that actual modifiable RAM starts at an offset of 28 bytes from the base address. I put a signature in the RAM, my main states and variables, and a checksum. When the "setup" code runs, it checks for the signature and a valid checksum and then modifies the boot values accordingly. My main loop just updates the RTC RAM on a regular basis so that any crash of the ESP can be survived.
These are my 2 cents for today.
User avatar
By dnts
#24808 Here's the code.. extremely straightforward.
Code: Select allextern "C" {
#include "user_interface.h"
}

//commit state to RTC RAM. Load state from RTC RAM
#define RTC_BASE 28
#define STATE_SIZE 24

void putInRTC(void)
{
  //put the start time, target time and mode into RTC RAM and put signature as well
  unsigned char buf[STATE_SIZE];
  buf[0]='N';
  buf[1]='T';
  buf[2]='S';
  buf[3]=0;
  buf[4]=(unsigned char)tmode;
  buf[5]=(unsigned char)tmode;
  buf[6]=(unsigned char)tmode;
  buf[7]=(unsigned char)tmode;
  buf[8]=(unsigned char)((dest_time>>24)&0xff);
  buf[9]=(unsigned char)((dest_time>>16)&0xff);
  buf[10]=(unsigned char)((dest_time>>8)&0xff);
  buf[11]=(unsigned char)((dest_time)&0xff);
  buf[12]=(unsigned char)((start_time>>24)&0xff);
  buf[13]=(unsigned char)((start_time>>16)&0xff);
  buf[14]=(unsigned char)((start_time>>8)&0xff);
  buf[15]=(unsigned char)((start_time)&0xff);
  buf[16]=0;
  int j;
  for (j=0;j<16;j++) buf[16]+=buf[j]; //checksum
  system_rtc_mem_write(RTC_BASE,buf,STATE_SIZE); 
}

boolean getFromRTC(void)
{
  unsigned char buf[STATE_SIZE];
  system_rtc_mem_read(RTC_BASE,buf,STATE_SIZE); 
  unsigned char temp=0;
  int j;
  for (j=0;j<16;j++) temp+=buf[j]; //checksum
  if (temp!=buf[16]) return false;
  if (buf[0]!='N') return false;
  if (buf[1]!='T') return false;
  if (buf[2]!='S') return false;
  if (buf[4]!=buf[5]) return false;
  if (buf[4]!=buf[6]) return false;
  if (buf[4]!=buf[7]) return false;
  tmode=(enum MODE)buf[4];
  dest_time=(time_t)(((buf[8]*256+buf[9])*256+buf[10])*256+buf[11]);
  start_time=(time_t)(((buf[12]*256+buf[13])*256+buf[14])*256+buf[15]);
  return (true);
}