Serial port error after migrating code from Arduino to ESP8266

Hello! I'm trying to measure durations of different signal levels and send it via serial port to PC.
The code I'm trying to run:

inline bool readBit(uint32_t prescaler) {
  uint32_t count = 0;

  pinMode(swim, INPUT);

  while (digitalRead(swim) != LOW);

  while (digitalRead(swim) != HIGH) {count++;}

  illegal_period = false;

  //if(illegal_period){
    String lama = String(count) + '\n';
    char lama_array[30] = {0};
    lama.toCharArray(lama_array, 30);
    Serial.write(lama_array);
  //}

  if (count > (short_phase)) return LOW;
  else return HIGH;
}

On arduino it works but gives inadequate values because of Arduino runs code on frequency that is sufficiently lower than HSI's 16 MHz (16MHz is according to datasheet).

Then I tried to run the same code on ESP8266 but it does not work and instead of values writes following:

12:20:25.222 ->
12:20:25.222 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
12:20:25.222 ->
12:20:25.222 -> Soft WDT reset
12:20:25.222 ->
12:20:25.222 -> Exception (4):
12:20:25.222 -> epc1=0x40100314 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
12:20:25.255 ->
12:20:25.255 -> >>>stack>>>
12:20:25.255 ->
12:20:25.255 -> ctx: cont
12:20:25.255 -> sp: 3ffffde0 end: 3fffffd0 offset: 0160
12:20:25.255 -> 3fffff40: 00000000 00000001 00000004 401002bc
12:20:25.255 -> 3fffff50: 402011c5 3ffe85d8 3ffe8661 402011d0

12:20:25.296 -> <<<stack<<<
12:20:25.296 ->
12:20:25.296 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
12:20:25.338 ->
12:20:25.338 -> ets Jan 8 2013,rst cause:2, boot mode:(3,6)
12:20:25.338 ->
12:20:25.338 -> load 0x4010f000, len 3424, room 16
12:20:25.338 -> tail 0
12:20:25.338 -> chksum 0x2e
12:20:25.338 -> load 0x3fff20b8, len 40, room 8
12:20:25.338 -> tail 0
12:20:25.338 -> chksum 0x2b
12:20:25.338 -> csum 0x2b
12:20:25.338 -> v00042d50
12:20:25.338 -> ~ld
12:20:25.370 -> ����o�;��o|�l� dc� �|r�l�o��o�l��s�l�$� �

How to fix this on ESP8266 and make it work?

There is an exception encoder for ESP8266 that you can install for IDE 1.x (not sure if there is already an equivalent for IDE 2.x).

If you post your full code, somebody can have a look if there are obvious mistakes in your code; I'm not familiar enough with ESP8266 to advise on that.

Next time, please post errors using code tags.

Exception 4: Level1Interrupt: Level-1 interrupt as indicated by set level-1 bits in the INTERRUPT register
PC: 0x4010031c: __digitalRead(uint8_t) at C:\Users\hqoffice\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_wiring_digital.cpp:98
EXCVADDR: 0x00000000

Decoding stack results
0x401002bc:  is in __digitalWrite(uint8_t, uint8_t) (C:\Users\hqoffice\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_wiring_digital.cpp:87).
0x402011c5:  is in writeBit(bool, unsigned int) (C:\Users\hqoffice\Documents\Arduino\Sewer\Sewer/Sewer.ino:71).
0x402011d0:  is in writeBit(bool, unsigned int) (C:\Users\hqoffice\Documents\Arduino\Sewer\Sewer/Sewer.ino:72).
0x40201262:  is in writeByte(unsigned char) (C:\Users\hqoffice\Documents\Arduino\Sewer\Sewer/Sewer.ino:101).
0x40201654: activation_sequence() at C:\Users\hqoffice\Documents\Arduino\Sewer\Sewer\Sewer.ino:240
0x40201787: loop() at C:\Users\hqoffice\Documents\Arduino\Sewer\Sewer\Sewer.ino:382
0x402017d8: loop() at C:\Users\hqoffice\Documents\Arduino\Sewer\Sewer\Sewer.ino:392
0x40202338: loop_wrapper() at C:\Users\hqoffice\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp:258

Have you tried that function with just the digitalRead counter and without the String stuff?

Btw, why not replace the String thing with sprintf:

char lama_array[30];
sprintf(lama_array, "%d\n", count);

lama_array can be dimensioned smaller in this example due to the max value of uint32_t.

I think that you need to use yield() in those while loops; the "soft wdt reset" might indicate that.

2 Likes

Can you write an example applied to code in start topic?

becomes

while (digitalRead(swim) != LOW) {
  yield();
}
1 Like

Tried. It removes error. Thank you.

But it's revealed that ESP8266 is incompatible with interesting for me 5V logic and goes into continously repeated reset when voltage on pins goes higher than 3.6 V

You can use a 3.3V microcontroller in a 5V context if you translate the logic levels from 3.3V to 5V and vice versa. There are small modules for this, and you can make them from discrete components as well. So don't let this stop you; it's easily overcome in virtually all instances.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.