ESP8266 WIFI SoC! General News
Come follow us on Google+ Page

User avatar
By Fabio.Pagu
#72337 Hi guys!
I have some troubles regarding esp exceptions during code execution. I'm programming the nodeMCU 1.0 using Arduino IDE. My goal is to analog-to-digital convert some values, package them e and print on COM port. Next step is to WiFi transmit this package using TCP Client-Server protocol.

This is my code:
Code: Select all#include <SPI.h>
#include <ESP8266WiFi.h>
#include <string.h>

#define PACKET_SIZE 40

// ADC-Store-Sending variables
const int AnalogIn  = A0;
int readingIn = 0;
int sendPacket = 1;
int arrayPosition = 0;
int SignalValues[PACKET_SIZE];
//String package;

// WiFi setting
char ssid[] = "WiFiname";                    // SSID of your home WiFi
char pass[] = "yourpassword";            // password of your home WiFi

IPAddress server(192,168,137,1);              // the fix IP address of the server
WiFiClient client;                            // Setup peripheral module as a client
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};    //Byte array to specify mac address
byte ip[] = {192, 168, 1, 99};                        //Byte array to specify ip address

// Control variables
int Stop = 0;
int ledPin = 15;
const int buttonPin = 13;
int buttonState = 0;         // variable for storing pushbutton status


void setup() {
  /*WiFi.begin(ssid,pass);                                      // WiFi starts association with ssdi and password
  while (WiFi.status() != WL_CONNECTED) {                     // Print "." until devices are connected
  Serial.print("Status: "); Serial.println(WiFi.status());    // Network parameters
  Serial.print("IP: ");     Serial.println(WiFi.localIP());
  Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
  Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
  Serial.print("SSID: "); Serial.println(WiFi.SSID());
  Serial.print("Signal: "); Serial.println(WiFi.RSSI());
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);                              // start transmission when button is pressed
  attachInterrupt(buttonPin, ButtonInterrupt, RISING);    // Enable interrupt for button
  timer0_write(ESP.getCycleCount() + 400000);

void loop() {
  int i; String package;
  if(buttonState) {
    if(sendPacket==PACKET_SIZE) {
      for (i=0; i<PACKET_SIZE; i++){
        package = package + ' ' + String(SignalValues[i], DEC);
      //client.connect(server, 6789);                         // Connection to the server
      //client.println(package + "\r");
      Serial.println("sEMG:" + package);
      sendPacket = 0;
      arrayPosition = 0;
      Stop = 1;
  else {
  if (Stop) {
      Stop = 0; 
      digitalWrite(ledPin, LOW);                            // to show the communication only (inverted logic)

void timer0_ISR (void){
    SignalValues[arrayPosition] = analogRead(AnalogIn);
    arrayPosition = arrayPosition + 1;
    sendPacket = sendPacket + 1;
  timer0_write(ESP.getCycleCount() + 800000);

void ButtonInterrupt()
  if(buttonState) {
    buttonState = 0; 
    digitalWrite(ledPin, LOW);                            // to show the communication
    Serial.println(buttonState);  }         //debug
  else {
    buttonState = 1; 
    digitalWrite(ledPin, HIGH);                            // to show the communication
    Serial.println(buttonState);           //debug

the ButtonInterrupt is used to control start and stop data conversion and sending. WiFi features are commented. ADC conversion is triggered by timer interrupt.
During execution some exceptions occur with related stack; I decoded them using ESP exception decoder and the result is:

3ffef4d0: 000000b0 3ffef530 3ffef530 40202cd0
3ffef4e0: 000000a5 0000009c 3fff0e68 40203368
3ffef4f0: 3ffee504 00000206 3ffef530 3ffee550
3ffef500: 3ffef53c 3ffe8350 3ffee524 40203399
3ffef510: 3ffef53c 3ffe8350 3ffef530 40202a61
3ffef520: 0000001c 3ffe8350 000000a0 40201ed3
3ffef530: 3fff0dcc 000000af 000000a5 00000000
3ffef540: 00000000 00000000 3fff10c4 000000af
3ffef550: 000000a0 00000000 00000001 40202ff5
3ffef560: 3fffdad0 00000000 3ffee548 40203020
3ffef570: feefeffe feefeffe 3ffee560 40100718

Decoding 8 results
0x40202cd0: String::changeBuffer(unsigned int) at C:\Users\fabio\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/WString.cpp line 357
0x40203368: Print::write(unsigned char const*, unsigned int) at C:\Users\fabio\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.cpp line 84
0x40203399: Print::print(String const&) at C:\Users\fabio\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.cpp line 84
0x40202a61: Print::println(String const&) at C:\Users\fabio\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Print.cpp line 84
0x40201ed3: ~StringSumHelper at C:\Users\fabio\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/WString.h line 260
: (inlined by) loop at C:\Users\fabio\Desktop\Pre-PhD\EMG_WiFi project\Total_System\final_project_v3/final_project_v3.ino line 70
0x40202ff5: esp_schedule at C:\Users\fabio\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/core_esp8266_main.cpp line 56
0x40203020: loop_wrapper at C:\Users\fabio\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/core_esp8266_main.cpp line 56
0x40100718: cont_norm at C:\Users\fabio\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/cont.S line 109

Can anyone help me to understand what is the problem?
Thanks a lot,
User avatar
By philbowles
#72383 You have a number of issues that need to be fixed:

1. You need a method to "debounce" the switch. It it not recommended to connect "bouncy" hardware to interrupt pins (see if you don't debounce it, you cannot predict the "correct" order of the various parts of the code and when they run against each other, leading to...well I think you know already...

2. You should not call Serial.<anything> from within an ISR

3. You have not declared buttonState as "volatile" nor sendPacket, nor arrayPosition

4. running delay(300); inside the main loop is likely to "throw out" any timing you THINK you might be needing.

5. You need to understand the interaction between the asynchronous events of a) the button and b) the timer and you need code to synchronise access to any common variables, for example buttonState, sendPacket arrayPosition etc etc (see point3). You also need to know why you need volatile...just declaring those variables volatile isnt going to fix the problem!

PS where you get the timer_ISR code? Why not use the Ticker library - so much easier to understand, less likely to cause ISR timing errors etc etc?
User avatar
By martinayotte
#72386 Your stacktrace seems to point to the fact that your string "package" isn't initialized and you try to concatenate something to it :
Code: Select allpackage = package + ' ' + String(SignalValues[i], DEC);

You should initialize it like this :
Code: Select allString package = String("");