Exception (9): epc1=0x4020c89c epc2=0x00000000 epc3=0x00000000 excvaddr=0x72756363 depc=0x00000000

-1

I have been trying to implement a PID algorithm that is controlled by an Android App. The program works as expected except that sometimes it randomly pops up Exception(9) error and then restarts.

I'm not an expert in Android Debugging so if anyone can give me pointer by having a look at the debug information, that will be great.

#include <Time.h>
#include <TimeLib.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

//Connection Variables
#define FIREBASE_HOST "some-database.firebaseio.com" 
#define FIREBASE_AUTH "zxHLcCASreUrCHcDH6LuQgOEDp" 
#define WIFI_SSID "---" 
#define WIFI_PASSWORD "---" 
String WiFiSSID = "---", WiFiPassword="---";

int timer = 0, lastInstance = 0, count = 0;
static byte lastSecond = 0;
bool flag = false, secondAuth = false, firstRun = true;
uint32_t currentSeconds = 0;

//PID Variables
//Inputs and outputs
int PID_value = 0, PID_p = 0;    int PID_i = 0, PID_d = 0, temp_read_Delay = 5, raw_temp, real_temperature = 0,  maximum_firing_delay = 7400;
unsigned long previousMillis = 0, currentMillis = 0;
bool zeroCross_detected = false;
float PID_error = 0, previous_error = 0, elapsedTime, Time, timePrev, setpoint;

//Pins
#define DS18B20_PIN D4
int firing_pin = D5, zero_cross = D6;

//PID constants
//int kp = 203;   int ki= 7.2;   int kd = 1.04;
int kp = 160;   int ki= 5;   int kd = 1;

void ICACHE_RAM_ATTR zeroCrossFunction();

//.........................//
//TODO: Serial.print to Toast
//.........................//

void setup()
{
  initWiFi();
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); 

  //Define the pins
  pinMode (firing_pin,OUTPUT); 
  pinMode (zero_cross,INPUT); 
  
  attachInterrupt(digitalPinToInterrupt(zero_cross), zeroCrossFunction, RISING); 
}

void loop()
{
  if(Firebase.getBool("startCook")){
    Serial.println("Preparing the food. Please wait and leave the device powered on");
    getTimerFromFirebase();
    getTargetTempFromFirebase();
    lastInstance = now();               //For tracking timer
    count = 0; 
    currentSeconds = now() - lastInstance;
    
    while(timer > currentSeconds){
      updateTimer();
      runPIDControl();
      delay(0);
    }
    Firebase.setBool("startCook", false);
    Firebase.setInt("currentTimer", 0);
  }
  else
  {
    if(count == 0){
      Serial.println("Please select the dish to cook from the App");
      count++;
    }
  }

//  WiFiSSID = Firebase.getString("WiFi SSID");
//  WiFiPassword = Firebase.getString("WiFi Password");

  delay(2000);
}

void updateTimer(){
    Firebase.setString("message","Entered updateTimer()");
    currentSeconds = now() - lastInstance;
    if(lastSecond != second(currentSeconds))
    {
      lastSecond = second(currentSeconds);
      Serial.print("Time Elapsed: ");
      Serial.println(currentSeconds);
      Firebase.setInt("currentTimer", currentSeconds);
    }
    Firebase.setString("message","Leaving updateTimer()");
}

void runPIDControl(){
  Firebase.setString("message","Entered runPIDControl()");
  currentMillis = millis();           //Save the value of time before the loop
  if(currentMillis - previousMillis >= temp_read_Delay){
    previousMillis += temp_read_Delay;              //Increase the previous time for next loop

    if(ds18b20_read(&raw_temp)) {
        real_temperature = (float)raw_temp / 16;     // Convert temperature raw value into degree Celsius (temp in °C = raw/16)
    }
    PID_error = setpoint - real_temperature;        //Calculate the pid ERROR
    Serial.print("Current:");
    Serial.print(real_temperature);
    Serial.print(", Target:");
    Serial.print(setpoint);
    
    if(PID_error > 30)                              //integral constant will only affect errors below 30ºC             
    {PID_i = 0;}
    
    PID_p = kp * PID_error;                         //Calculate the P value
    PID_i = PID_i + (ki * PID_error);               //Calculate the I value
    timePrev = Time;                    // the previous time is stored before the actual time read
    Time = millis();                    // actual time read
    elapsedTime = (Time - timePrev) / 1000;   
    PID_d = kd*((PID_error - previous_error)/elapsedTime);  //Calculate the D value
    PID_value = PID_p + PID_i + PID_d;                      //Calculate total PID value

    Serial.print(", PID:");
    Serial.println(PID_value);
    
    if(PID_value < 0)
    {      PID_value = 0;       }
    if(PID_value > maximum_firing_delay)
    {      PID_value = maximum_firing_delay;    }


    Firebase.setInt("PID Power", PID_value);
    previous_error = PID_error; //Remember to store the previous error. 
  }
  Firebase.setString("message","Leaving runPIDControl()");
}

void ICACHE_RAM_ATTR zeroCrossFunction(){
  delayMicroseconds(maximum_firing_delay - PID_value); //This delay controls the power
  digitalWrite(firing_pin,HIGH);
  delayMicroseconds(100);
  digitalWrite(firing_pin,LOW);
}

void getTargetTempFromFirebase(){
  setpoint = Firebase.getFloat("tempToMaintain");
  
}

void getTimerFromFirebase(){
  timer = Firebase.getInt("timerInSecs");
  Serial.print("Running for ");
  Serial.print(timer);
  Serial.println(" seconds"); 
}

void initWiFi(){
  int multipleTries = 0;
  Serial.begin(9600);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 
  Serial.println("Connecting"); 
  Serial.println("First Authentication");
  while (WiFi.status() != WL_CONNECTED) { 
    Serial.print("."); 
    delay(500);
    multipleTries ++;
    if(multipleTries > 50){
      secondAuth = true;
    }
    if(secondAuth){
      Serial.println();
      Serial.println("Second Authentication");
      break;
    }
  }
  Serial.println();
  if(secondAuth){
    WiFi.begin(WiFiSSID, WiFiPassword); 
    Serial.print("Connecting"); 
    while (WiFi.status() != WL_CONNECTED) { 
      Serial.print("."); 
      delay(500);
    }
    Serial.println(); 
      
  }
  Serial.print("Connected: "); 
  Serial.println(WiFi.localIP());
}

//DS18B20 Functions
bool ds18b20_start(){
  bool ret = 0;
  digitalWrite(DS18B20_PIN, LOW);     // Send reset pulse to the DS18B20 sensor
  pinMode(DS18B20_PIN, OUTPUT);
  delayMicroseconds(500);             // Wait 500 us
  pinMode(DS18B20_PIN, INPUT);
  delayMicroseconds(100);             //wait to read the DS18B20 sensor response
  if (!digitalRead(DS18B20_PIN)) {
    ret = 1;                          // DS18B20 sensor is present
    delayMicroseconds(400);           // Wait 400 us
  }
  return(ret);
}
 
void ds18b20_write_bit(bool value){
  digitalWrite(DS18B20_PIN, LOW);
  pinMode(DS18B20_PIN, OUTPUT);
  delayMicroseconds(2);
  digitalWrite(DS18B20_PIN, value);
  delayMicroseconds(80);
  pinMode(DS18B20_PIN, INPUT);
  delayMicroseconds(2);
}
 
void ds18b20_write_byte(byte value){
  byte i;
  for(i = 0; i < 8; i++)
    ds18b20_write_bit(bitRead(value, i));
}
 
bool ds18b20_read_bit(void) {
  bool value;
  digitalWrite(DS18B20_PIN, LOW);
  pinMode(DS18B20_PIN, OUTPUT);
  delayMicroseconds(2);
  pinMode(DS18B20_PIN, INPUT);
  delayMicroseconds(5);
  value = digitalRead(DS18B20_PIN);
  delayMicroseconds(100);
  return value;
}
 
byte ds18b20_read_byte(void) {
  byte i, value;
  for(i = 0; i  <8; i++)
    bitWrite(value, i, ds18b20_read_bit());
  return value;
}
 
bool ds18b20_read(int *raw_temp_value) {
  if (!ds18b20_start())                     // Send start pulse
    return(0);                              // Return 0 if error
  ds18b20_write_byte(0xCC);                 // Send skip ROM command
  ds18b20_write_byte(0x44);                 // Send start conversion command
  while(ds18b20_read_byte() == 0);          // Wait for conversion complete
  if (!ds18b20_start())                     // Send start pulse
    return(0);                              // Return 0 if error
  ds18b20_write_byte(0xCC);                 // Send skip ROM command
  ds18b20_write_byte(0xBE);                 // Send read command
  *raw_temp_value = ds18b20_read_byte();    // Read temperature LSB byte and store it on raw_temp_value LSB byte
  *raw_temp_value |= (unsigned int)(ds18b20_read_byte() << 8);     // Read temperature MSB byte and store it on raw_temp_value MSB byte
  return(1);                                // OK --> return 1
}

Here is my stack trace:

Exception (9):
epc1=0x4020c89c epc2=0x00000000 epc3=0x00000000 excvaddr=0x72756363 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffc60 end: 3fffffc0 offset: 0190
3ffffdf0:  00000000 00000001 3ffffeb4 3ffeebf0  
3ffffe00:  0000000b 3ffeebb8 3fff0488 4020687c  
3ffffe10:  3ffe8871 000001bb 3fff0488 402077a4  
3ffffe20:  3ffe8925 0000003b 3fff0488 40207bd4  
3ffffe30:  0000003b 3ffeebb8 0000002e 40203fe0  
3ffffe40:  3ffffe88 0000003b 3ffffe70 40208668  
3ffffe50:  3ffeebb4 3ffffe70 3ffe8925 3ffffef0  
3ffffe60:  3ffeebb4 3ffffeb4 3fff0484 402059b3  
3ffffe70:  3fff01dc 003b003f 00fffeb4 3fff051c  
3ffffe80:  0040004f 00ff3f44 3fff06ec 0020002f  
3ffffe90:  00004b20 3ffffed8 3fff3f47 3ffffef0  
3ffffea0:  3ffeebb4 3ffe8921 3fff02cc 4020361c  
3ffffeb0:  00000011 3fff0768 00000020 3fff02cc  
3ffffec0:  3fff3f44 3ffffef0 3fff3f44 40203fa4  
3ffffed0:  3fffff00 00000010 00000004 3fff02cc  
3ffffee0:  3fff3f44 3ffffef8 3ffeebb0 402057d4  
3ffffef0:  3fff06e0 00000003 3fff05d0 3fff3f47  
3fffff00:  3ffe8600 3ffeec10 40100248 3ffe879f  
3fffff10:  00000003 3fffff40 3fffff60 4020861f  
3fffff20:  0000000c 3ffeec10 3ffe89b3 3ffe879f  
3fffff30:  00000011 3ffeec10 3ffeea14 40205829  
3fffff40:  00000004 0000022e 3ffe86db 4020869e  
3fffff50:  3ffeea14 3ffeec10 3ffeea14 40201088  
3fffff60:  3fff05ac 000c000f 00feea1c 40209c92  
3fffff70:  3ffeea14 3ffeebb0 3ffeea24 40201717  
3fffff80:  72617400 6f6f4374 8000006b 4010019c  
3fffff90:  3fffdad0 00000000 3ffeec4c 3ffeec8c  
3fffffa0:  3fffdad0 00000000 3ffeec4c 402094e8  
3fffffb0:  feefeffe feefeffe 3ffe859c 40100df5  

<<<stack<<<

This is the output from EspExceptionDecoder:

exception
arduino
arduino-ide
nodemcu
arduino-esp8266
asked on Stack Overflow Jul 14, 2020 by Ashar

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0