rc-switch icon indicating copy to clipboard operation
rc-switch copied to clipboard

mySwitch.switchOff causes Exception 28

Open comdata opened this issue 10 years ago • 7 comments

Hello,

when calling the switchOff multiple times in a row it causes and Exception 28.

This is the trace from the serial console:

Exception (28): epc1=0x4020a962 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: cont sp: 3ffefd00 end: 3ffeffe0 offset: 01a0

stack>>> 3ffefea0: 3ffeea00 3ffeeafc 3ffeeefc 4020bf18
3ffefeb0: 0000003f 3ffeeafc 3ffeeefc 40206e4b
3ffefec0: 74617473 30007375 756f6800 6f436573
3ffefed0: 31006564 31303031 646f4300 203a2265
3ffefee0: 30313122 2c223130 7322090a 63746977
3ffefef0: 226f4e68 3122203a 30303030 407d0a22
3ffeff00: 00000000 00000000 00000000 4020af0a
3ffeff10: 00000000 00000000 00000000 00000002
3ffeff20: 3ffeea1f 0000003e 3ffeea5d 4020b0e5
3ffeff30: 407d004a 00001388 00001388 3ffeff70
3ffeff40: 3ffeff70 00014dd4 3ffeea10 4020dc5c
3ffeff50: 3fffdad0 00014b8f 3ffeea10 4020b672
3ffeff60: 6977732f 00686374 3ffeea10 4020b588
3ffeff70: 3ffe8901 3ffeeefc 3ffeea10 4020758d
3ffeff80: 00000000 00000000 3ffeefc0 00000000
3ffeff90: 3fffdad0 3ffeefb8 4020cce8 3ffeefb8
3ffeffa0: 3fffdad0 00000000 3ffeea10 402075b8
3ffeffb0: 00000000 00000000 3ffeefb1 402075d3
3ffeffc0: 3fffdad0 00000000 3ffeefb1 4020cd34
3ffeffd0: feefeffe feefeffe 3ffeefc0 40100718
<<<stack<<<

What could be wrong?

Kind Regards, Christoph

comdata avatar Nov 29 '16 22:11 comdata

https://github.com/esp8266/Arduino/issues/1275 is it a esp?

sui77 avatar Dec 22 '16 16:12 sui77

Yes it is a NodeMCU with an ESP 12E.

comdata avatar Dec 22 '16 16:12 comdata

i'll try to reproduce that at times

sui77 avatar Dec 22 '16 16:12 sui77

If you need anything or I should test some things I am more than willing to help.

comdata avatar Dec 22 '16 18:12 comdata

this works fine with my esp12e. can you provide your code?

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  mySwitch.enableTransmit(10);
}

void loop() {
  mySwitch.switchOff("11111", "00010");
  //delay(100);
  //Serial.println("something");
}

sui77 avatar Dec 22 '16 20:12 sui77

Please find my code below. I am using MQTT to trigger the signal.

// includes
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <FS.h>
#include <ArduinoOTA.h>
#include <RCSwitch.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

StaticJsonBuffer<200> jsonBuffer;

 char* status;
    char* houseCode;
    char* switchNo;

/**
   @brief mDNS and OTA Constants
   @{
*/
#define HOSTNAME "ESP8266-OTA-" ///< Hostename. The setup function adds the Chip ID at the end.
/// @}

/**
   @brief Default WiFi connection information.
   @{
*/
const char* ap_default_ssid = ""; ///< Default SSID.
const char* ap_default_psk = ""; ///< Default PSK.
/// @}

RCSwitch mySwitch = RCSwitch();

const char* mqtt_server =  "iot.eclipse.org"; //"192.168.1.76";

WiFiClient espClient;
PubSubClient client(espClient);

/// Uncomment the next line for verbose output over UART.
//#define SERIAL_VERBOSE

/**
   @brief Read WiFi connection information from file system.
   @param ssid String pointer for storing SSID.
   @param pass String pointer for storing PSK.
   @return True or False.

   The config file have to containt the WiFi SSID in the first line
   and the WiFi PSK in the second line.
   Line seperator can be \r\n (CR LF) \r or \n.
*/
bool loadConfig(String *ssid, String *pass)
{
  // open file for reading.
  File configFile = SPIFFS.open("/cl_conf.txt", "r");
  if (!configFile)
  {
    Serial.println("Failed to open cl_conf.txt.");

    return false;
  }

  // Read content from config file.
  String content = configFile.readString();
  configFile.close();

  content.trim();

  // Check if ther is a second line available.
  int8_t pos = content.indexOf("\r\n");
  uint8_t le = 2;
  // check for linux and mac line ending.
  if (pos == -1)
  {
    le = 1;
    pos = content.indexOf("\n");
    if (pos == -1)
    {
      pos = content.indexOf("\r");
    }
  }

  // If there is no second line: Some information is missing.
  if (pos == -1)
  {
    Serial.println("Invalid content.");
    Serial.println(content);

    return false;
  }

  // Store SSID and PSK into string vars.
  *ssid = content.substring(0, pos);
  *pass = content.substring(pos + le);

  ssid->trim();
  pass->trim();

#ifdef SERIAL_VERBOSE
  Serial.println("----- file content -----");
  Serial.println(content);
  Serial.println("----- file content -----");
  Serial.println("ssid: " + *ssid);
  Serial.println("psk:  " + *pass);
#endif

  return true;
} // loadConfig


/**
   @brief Save WiFi SSID and PSK to configuration file.
   @param ssid SSID as string pointer.
   @param pass PSK as string pointer,
   @return True or False.
*/
bool saveConfig(String *ssid, String *pass)
{
  // Open config file for writing.
  File configFile = SPIFFS.open("/cl_conf.txt", "w");
  if (!configFile)
  {
    Serial.println("Failed to open cl_conf.txt for writing");

    return false;
  }

  // Save SSID and PSK.
  configFile.println(*ssid);
  configFile.println(*pass);

  configFile.close();

  return true;
} // saveConfig


/**
   @brief Arduino setup function.
*/
void setup()
{
  String station_ssid = ap_default_ssid;
  String station_psk = ap_default_psk;

  setupRCSwitch();

  Serial.begin(9600);

  delay(100);

  Serial.println("\r\n");
  Serial.print("Chip ID: 0x");
  Serial.println(ESP.getChipId(), HEX);

  // Set Hostname.
  String hostname(HOSTNAME);
  hostname += String(ESP.getChipId(), HEX);
  WiFi.hostname(hostname);

  // Print hostname.
  Serial.println("Hostname: " + hostname);
  //Serial.println(WiFi.hostname());


  // Initialize file system.
  if (!SPIFFS.begin())
  {
    Serial.println("Failed to mount file system");
    return;
  }

  // Load wifi connection information.
  if (! loadConfig(&station_ssid, &station_psk))
  {
    station_ssid = ap_default_ssid;
    station_psk = ap_default_psk;

    Serial.println("No WiFi connection information available.");
  }

  // Check WiFi connection
  // ... check mode
  if (WiFi.getMode() != WIFI_STA)
  {
    WiFi.mode(WIFI_STA);
    delay(10);
  }

  // ... Compare file config with sdk config.
  if (WiFi.SSID() != station_ssid || WiFi.psk() != station_psk)
  {
    Serial.println("WiFi config changed.");

    // ... Try to connect to WiFi station.
    WiFi.begin(station_ssid.c_str(), station_psk.c_str());

    // ... Pritn new SSID
    Serial.print("new SSID: ");
    Serial.println(WiFi.SSID());

    // ... Uncomment this for debugging output.
    //WiFi.printDiag(Serial);
  }
  else
  {
    // ... Begin with sdk config.
    WiFi.begin();
  }

  Serial.println("Wait for WiFi connection.");

  // ... Give ESP 10 seconds to connect to station.
  unsigned long startTime = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000)
  {
    Serial.write('.');
    //Serial.print(WiFi.status());
    delay(500);
  }
  Serial.println();

  // Check connection
  if (WiFi.status() == WL_CONNECTED)
  {
    // ... print IP Address
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else
  {
    Serial.println("Can not connect to WiFi station. Go into AP mode.");

    // Go into software AP mode.
    WiFi.mode(WIFI_AP);

    delay(10);

    WiFi.softAP(ap_default_ssid, ap_default_psk);

    Serial.print("IP address: ");
    Serial.println(WiFi.softAPIP());
  }

  // Start OTA server.
  ArduinoOTA.setHostname((const char *)hostname.c_str());
  ArduinoOTA.begin();

  setupMQTT();
}


void setupRCSwitch() {

  Serial.print("enabling switch");
  mySwitch.enableTransmit(4);
  Serial.print("switch enabled");

}

void switchHandler() {
  Serial.print("loop");
  // put your main code here, to run repeatedly:
  mySwitch.switchOn("11111", "00010");
  Serial.print("On");
  delay(10000);


  // Switch off
  mySwitch.switchOff("11111", "00010");
  Serial.print("Off");
  // Wait another second
  delay(10000);
  switchHandler();
}

/**
   @brief Arduino loop function.
*/
void loop()
{
  // Handle OTA server.
  ArduinoOTA.handle();
  mqttLoop();
  //yield();
}

void mqttLoop() {

  if (!client.connected()) {
    mqttReconnect();
  }

   if (client.connected()) {
    client.loop();
  }
 
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  char message[length];
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
    message[i] = (char)payload[i];
  }
  Serial.println();


  JsonObject& root = jsonBuffer.parseObject(message);

    status = (char*)root["status"].asString();
    houseCode = (char*)root["houseCode"].asString();
    switchNo = (char*)root["switchNo"].asString();

  Serial.println("Status:"+String(status));
  Serial.println("houseCode:"+String(houseCode));
  Serial.println("switchNo:"+String(switchNo));

  //setupRCSwitch();

  Serial.println(mySwitch.available());

  yield();

  if (String(status) == "1") {
    Serial.println("Switch on");
    mySwitch.switchOn(houseCode, switchNo);
  } else {
    Serial.println("Switch off");
    mySwitch.switchOff(houseCode, switchNo);
  }

  yield();
  delay(3000);
  //ESP.restart();
  Serial.print("Done Switching");
}

void setupMQTT() {

  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);


}

void mqttReconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("/status", "alive");
      // ... and resubscribe
      client.subscribe("/switch");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

This fails after some loops.

Kind Regards, Christoph

comdata avatar Dec 26 '16 18:12 comdata

I found the issue.

The declaration of StaticJsonBuffer<200> jsonBuffer; at the top was causing problems. I moved it into the callback function and now it is working.

Thanks Christoph

comdata avatar Dec 31 '16 09:12 comdata