ESP-12F intermittent problems

I made several projects with ESP-12E and ESP-12F several months ago and I managed to solve all the power problems and the pullup/pulldown requirements. Those projects still work fine and are very stable. One month ago, I received a new batch of ESP-12F form Aliexpress and the logo on top is not identical to the former one, see here. The first problem I faced is that, as opposed to the indication "QIO" reported on the back, it only works in DIO mode. Once I found the correct setings, it flashed OK and I never again got a reset or crash.

But I am facing a new, worst, problem: ANY sketches I load, even those of the still working (on other chips) ones, I see the sketch running (regular Serial.print messages are shown as expected) but the behaviour of the WiFi is intermittent.

In other words:
The ESP connects always to the access point and the authentication works fine. Let's now focus on PING from a PC to ESP, while all the WiFi behaviour is basically the same:

Sometimes it answers forever to ping: very rare, but happens.
Sometimes it never answers to ping, but the ARP is solved (arp -d before ping, arp -a after ping timeout show the correct MAC).
Sometimes it answer to the first ping, then I have a 5-10 seconds timeout, then it works forever.
Sometimes it answer to the first ping, then it stops to answer forever.
Sometimes it answer to the first ping and resumes to answer after several minutes.
Once it decides to answer, it answer forever.

AT his point, I exclude a sketch problem and I assume
A) A power problem.
B) Defective ESP-12F (all the four behave exactly the same).

As for the power, to be sure to have all the problems already faced fixed, I made this basic test board that can be expanded on a breadboard.

Please note, under the ESP there is a 1000uF electrolytic cap and the pullup/pulldown resistors, the regulator is an LM317 and the pot is to adjust the 3.3V. The toggle switch is to disconnect DTR and RTS, while under the ESP there is also a female header to connect the USB/Serial adapter. The wires from the LM317 to the ESP are 1mm diameter.
I tried 3 different power supplies: wall 12V/2A, bench P.S.U. 12V/5A, P.C. power supply, 12V rail.

My main question is: are you aware of faulty chips from the Chinese market? Any other suggestions?

clear the flash with esptool erase_flash. The new Espressif SDK used in arduino esp8266 core 2.4.1 has problems with old settings remembered on the flash. If it doesn't help, erase the flash again and use lwip 1.4 in Tools menu settings for next uploads.

Yes, I forgot to add that, off course, I cleared the flash with Esptool gui v3.6.4, with esptool.py v2.3.1 erase_flash command and I also uploaded a full 4MB file of 0xFFs and a full 4MB file of 0x00s (even with the -no_compress flag). By the way, I guess that should it be a software/firmware/configuration problem, it should behave every time exactly the same, isn't it?

and which esp8266 arduino core version you have and which lwip version you use?

I must admit, I am a little bit confused on "arduino core" version! IDE is 1.8.5, Arduino AVR Boards is 1.6.21, ESP8266 is 2.4.1. Is that what you ask for?

As for lwip, it WAS set to V2 Lower memory, I tried both V2 Higher bandwidth, 1.4 Higher bandwidth and finally v1.4 Compile from source. Before every change, I issued a clear_flash. The first 3 show the usual (for me!) problem. The last, compile from source, fails in compilation.

resca:
I must admit, I am a little bit confused on "arduino core" version! IDE is 1.8.5, Arduino AVR Boards is 1.6.21, ESP8266 is 2.4.1. Is that what you ask for?

As for lwip, it WAS set to V2 Lower memory, I tried both V2 Higher bandwidth, 1.4 Higher bandwidth and finally v1.4 Compile from source. Before every change, I issued a clear_flash. The first 3 show the usual (for me!) problem. The last, compile from source, fails in compilation.

2.4.1, for me only lwip 1.4 works reliably (I didn't try the compiled version).

I managed to enable debug on Serial, where I have also some of my messages. Here it is quite clear, there is "something" wrong just in the WiFi protocol. I am on the way to try to understand the meaning of "bcn_timout,ap_probe_send_start" and, subsequently, "ap_probe_send over, rest wifi status to disassoc". May be my A.P. needs a different configuration for newly compiled modules? Unfortunately the only action I cannot perform is to load just compiled sketches to the old and working ESP's!

Follows the full debug output. I placed an * in front of my personal messages. 10.3.50.5 is the PC that is trying to ping ESP forever.

SDK:2.2.1(cfd48f3)/Core:2.4.1/lwIP:1.4.0rc2

**Starting!
bcn 0
del if1
usl
mode : sta(60:01:94:01:6c:5a)
add if0
wifi evt: 8
wifi evt: 2
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 2
cnt

connected with MikroTik, channel 11
dhcp client start...
wifi evt: 0
ip:10.3.50.59,mask:255.255.255.0,gw:10.3.50.11
wifi evt: 3
**Connected to: MikroTik
**IP: 10.3.50.59
**Channel: 11
**MAC: 60:01:94:01:6c:5a
**UDP listening on port: 1986
**Newphone ready!
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
bcn_timout,ap_probe_send_start
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
ap_probe_send over, rest wifi status to disassoc
state: 5 -> 0 (1)
rm 0
wifi evt: 1
STA disconnect: 200
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
scandone
no MikroTik found, reconnect after 1s
wifi evt: 1
STA disconnect: 201
reconnect
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
scandone
no MikroTik found, reconnect after 1s
wifi evt: 1
STA disconnect: 201
reconnect
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!
scandone
no MikroTik found, reconnect after 1s
wifi evt: 1
STA disconnect: 201
reconnect
[hostByName] Host: 10.3.50.5 is a IP!
[hostByName] Host: 10.3.50.5 is a IP!

As you can see, it connects fine, authenticates, gets IP from DHCP, etc. In addition, as far as my A.P. sa concerned, it never disconnects!

the esptool you use to erase flash is a newest version? does it detect the real flash size?

I used bot the real python script with Linux and the .exe version with windows. Both are V2.3.1.
Yes, esptool detects the correct amount of flash.

Update: I had two problems!

First of all, the old and working sketches no longer work due the lwip 2.0 problem you pointed out. So, I managed to see those sketches working smoothly on the new chips. Problem solved!

At that point I focused on the new sketch, still not working, and I discovered where the problem is!

Should I call analogRead(A0), WiFi crashes. Solid problem: remove that line and the sketch works fine, insert it and WiFi ceases to work. Of course, I placed ADC_MODE(ADC_TOUT) in front of any functions.

I made several "variations": analogRead(0), analogRead(17), system_adc_read(): always the same problem. Finally I grounded the ADC pin (it was floating) with no better results.

Just as a useless (for me) final test, I set ADC_MODE(ADC_VCC) and, once I call ESP.getVcc() I have the usual crash of WiFi.

This may be a known problem?

resca:
I used bot the real python script in Linux and the .exe version with windows. Bot are V2.3.1.
Yes, i guess esptool detects the correct amount of flash:

C:\Source\Arduino>esptool.py --chip esp8266 --baud 230400 --port COM10 flash_id

esptool.py v2.3.1
Connecting....
Chip is ESP8266EX
Features: WiFi
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 230400
Changed.
Manufacturer: e0
Device: 4016
Detected flash size: 4MB
Hard resetting via RTS pin...

C:\Source\Arduino>esptool.py --chip esp8266 --baud 230400 --port COM10 erase_flash
esptool.py v2.3.1
Connecting....
Chip is ESP8266EX
Features: WiFi
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 230400
Changed.
Erasing flash (this may take a while)...
Chip erase completed successfully in 9.9s
Hard resetting via RTS pin...

yes, it is as it should be.

the Espressif Flash Download Tool has a page for internal esp8266 settings. They are uploaded in esp-init.bin.

The arduino package has settings hardcoded and the settings are joined to sketch bin.

I updated my previous post while you were answering, sorry. If you like, see above: partially solved!

sorry, I have no experience with the analog pin of the esp8266

now I had this problem with analogRead causing WiFi disconnection and I didn't remember this thread until I googled it