NodeMCU ESP8266 cannot upload sketch - timeout error

1.7k views Asked by At

Using arduino-cli, the arduino-ide, or directly the esptool.py (from https://github.com/espressif/esptool) I obtain the following error when uploading a simple (correct) sketch.

x@y:~/blink$ ~/tools/arduino-cli upload -p /dev/ttyUSB0 --fqbn esp8266:esp8266:nodemcuv2
esptool.py v3.1
Serial port /dev/ttyUSB0
Connecting........_____....._____....._____....._____....._____....._____....._____
Traceback (most recent call last):
  File "/home/x/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/upload.py", line 66, in <module>
    esptool.main(cmdline)
  File "/home/x/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/esptool/esptool.py", line 4074, in main
    esp = esp or get_default_connected_device(ser_list, port=args.port, connect_attempts=args.connect_attempts,
  File "/home/x/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/esptool/esptool.py", line 121, in get_default_connected_device
    _esp.connect(before, connect_attempts)
  File "/home/x/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/tools/esptool/esptool.py", line 639, in connect
    raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
Error during Upload: Failed uploading: uploading error: exit status 1 
esptool.py v3.1
1

There are 1 answers

0
65537 On BEST ANSWER

The problem was that my last apt upgrade updated the kernel from the 5.4.0-86-generic to 5.4.0-88-generic (I checked the update in /var/log/apt/history.log.1.gz). The driver CH341 in 5.4.0-88 is not working properly (the ESP I'm using require CH340G drivers). The solution is to go back to the 5.4.0-86 drivers and in the following I list what I've done.

  1. disconnect any ESP or board connected to any USB. Then test that the CH341 module (which allows the serial communication with the EPS) is loaded. You can use this code to test the correct unload/load of the module in steps 2, 3.
x@y:~$ lsmod | egrep serial
usbserial              53248  2 cp210x,ch341
  1. here is where modules of the current (5.4.0-88) and previous (5.4.0-86) kernel are
x@y:~$ ls -larh /lib/modules/
total 164K
drwxr-xr-x  6 root root 4,0K set 29 09:19 5.4.0-88-generic
drwxr-xr-x  6 root root 4,0K set 23 09:45 5.4.0-86-generic
[...]
  1. unload current (5.4.0-88) CH341 module with the command
sudo rmmod /lib/modules/5.4.0-88-generic/kernel/drivers/usb/serial/ch341.ko
  1. load previous (5.4.0-86) CH341 module with the command
sudo insmod /lib/modules/5.4.0-86-generic/kernel/drivers/usb/serial/ch341.ko

That's it :) You should now be able to upload a new sketch to your ESP8266.