Unable to flash ESPHome to Sonoff M5

Hi,
I’m trying to flash ESPHome on Sonoff M5 switch. According to web tutorials, it should be trivial. I’m stuck with ESPHome being unable to connect to the device.

I have this programming device:
https://aliexpress.com/item/1005006127179718.html
I’ve set the connector to 3.3v and installed USB to serial driver CDM212364

Connected rt-tx, tx-rx, gnd, v3.3, and when turining on the device I keep the button pushed to enter programming mode.

From HA/ESPhome when I add new device, click connect, the interface asks for COM port (using chrome to do serial programming), but connection doesn’t succeed.

In ESPHome log there’s only a very long sequence like this, no errors:

2024-06-23 19:08:16,725 INFO 304 GET /devices (0.0.0.0) 1.64ms
2024-06-23 19:08:22,713 INFO 304 GET /devices (0.0.0.0) 1.64ms
2024-06-23 19:08:28,703 INFO 304 GET /devices (0.0.0.0) 1.41ms
2024-06-23 19:08:30,143 INFO 200 GET /devices (0.0.0.0) 1.53ms
2024-06-23 19:08:34,707 INFO 304 GET /devices (0.0.0.0) 1.65ms
2024-06-23 19:08:39,717 INFO 304 GET /devices (0.0.0.0) 1.35ms
2024-06-23 19:08:45,703 INFO 304 GET /devices (0.0.0.0) 1.61ms
2024-06-23 19:08:51,714 INFO 304 GET /devices (0.0.0.0) 1.50ms
2024-06-23 19:08:57,702 INFO 304 GET /devices (0.0.0.0) 1.46ms

Double tested that the device works normally, connected to 230V and eWeLink from smartphone, it works ok.

Any hints what else to try?

Same here. Have you found a solution?

Have You tried to connect GPIO0 to GND located nearby on board ?
I always connect pins, not pushing button.
So far working on 11 M5’s.

Yes, I already tried it. Maybe the problem is the USB2TTL; I have the same one linked by @jdukat (FT232RL Red Type-c).

I tried with esptool.py, and it gave me this error: Invalid head of packet (0xXX): Possible serial noise or corruption. (Troubleshooting - ESP32 - — esptool.py latest documentation)

Using CH340G & CP2102.

CP2102 are recognized by HAOS - can use directly on HA installed on RPi.
On PC prefer CH340G - looks more stable.

I have always flashed my ESP devices to a .bin file (manual install) and flashed using ESPHome Flasher.

I connected the TX to the RX of the USB2TTL, and it seems to be working properly. The problem might be with the M5 board.
How can I test if the TX and RX of the Sonoff M5 are working correctly?

I don’t know about the Sonoff M5, but the Sonoff labeling of +3,R,T,G has been consistent with all of the Sonoff devices I have here. And I have no experience with your particular FTDI, but I have seen one that mis-labeled the T & R. If nothing else seems to work, try connecting T-T and R-R. It can’t hurt anything.

1 Like

I’ve tested it and it’s still not working.
I’m going to buy the CH340G to see if the problem is the USB2TTL, it’s the only thing left.

Did you try the other programmer device yet?

No, I just bought 2 different programmer devices (https://aliexpress.com/item/1005006880183678.html, https://aliexpress.com/item/1005006018153062.html)

I’m waiting for the shipment to arrive, when I try them I will update you.

@thewapster any news? Have the new programmers arrived?

Not one of the new white Matter compatible M5’s is it, seems to reports on the web that these are locked firmware.

I have one of the white ones and Matter works quite well, didn’t try flashing it due to reports of not being possible. Just added mine straight to HA didn’t need to use EWeLink app.

I think all matter devices have locked firmware by default, but you got your matter anyway.
This thread is about flashing a non-matter M5 switch.

Hey, sorry for not giving any updates. The programmer device hasn’t arrived, AliExpress stuff, I guess. I’m waiting to request a refund.

After all this waiting, I’m using the switch without flashing it, and I think it’s going to stay that way.

Hey, I didn’t expect I would come back here, but I just found the solution, after trying to flash another sonoff device, having similar issues, I decided to try something looking really dangerous… I tried to flash the device with 230v ac power connected, and guess what - it worked!

I’m still using the same FTDI232, output set to 3.3v, using following procedure:

  1. Connect the FTDI cables (VCC, RX, TX, GND) but don’t connect it to the PC USB yet
  2. With ac power turned off, connect the 230v Lin and N cables
  3. Press and hold the button (my switch is 1-gang, so the gpio00 is connected to the button)
  4. Turn on AC power. You should see the blue led turned off, meaning the device is not attempting to connect to Wifi, but rather waits for the programming.
  5. After a moment, release the button.
  6. Plug in the USB cable into the PC
  7. In Home Assistant ESPHome tab, create new device and connect. Of course you need to have the usb/serial driver installed. I’m using this one: https://ftdichip.com/drivers/
    It should connect, then takes a moment creating config, programming and the device goes online with no functionality.
  8. Power off the device, disconnect the FTDI, assemble and seal the cover.
  9. Power on, the device should become online in a moment in ESPHome.
  10. Update yaml config and install it using wireless update. Done.

BTW, if you’re stuck with the yaml config, I’m copying mine below.
The one that google finds here is invalid.
Here you’ll find the ESPHome reference, but they only provide ready config for 2- and 3-gang, while the 1-gang is slightly different.
Here’s mine:

substitutions:
  friendly_name: Switch M5
  device_name: sonoff-switch-m5

esphome:
  name: ${device_name}
  friendly_name: ${friendly_name}

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

api:
  encryption:
    key: "xxx"

ota:
  - platform: esphome
    password: "xxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  on_connect:
    - light.turn_on: wifi_status_led
  on_disconnect:
    - light.turn_off: wifi_status_led

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${device_name}
    password: !secret wifi_failover_password

captive_portal:

# ---------

text_sensor:
  - platform: wifi_info
    ip_address:
      id: ip_address
      name: "IP Address"
      icon: "mdi:wan"

switch:
  - platform: gpio
    name: "Switch"
    pin: GPIO23
    id: relay_1
    on_turn_on:
      - output.turn_on: led_1
    on_turn_off:
      - output.turn_off: led_1
    restore_mode: RESTORE_DEFAULT_OFF

output:
  - platform: gpio
    id: led_1
    pin:
      number: GPIO19
      inverted: False

  - platform: ledc
    id: pwm_output
    pin: GPIO18
    frequency: 1000 Hz

light:
  - platform: status_led
    id: wifi_status_led
    internal: True
    pin:
      number: GPIO05
      inverted: True

binary_sensor:
  - platform: gpio
    name: "Button"
    pin:
      number: GPIO00
      mode: INPUT_PULLUP
      inverted: False
    on_press:
      - switch.toggle: relay_1

It’s dangerous because you are exposing line voltage to possible finger contact. What your experience tells me is that the Vcc (3V) pin is not powering the ESP correctly. When you connect Vcc (3V) and Ground only, does the LED indicate that the device is trying to connect to WiFi? (It should).

I took required safety steps to protect myself from touching the 230v line. I have a cable with additional power switch, that cuts off the power for the time of assembling the device for programming, and then put it on rubber pad before turning on the power and using isolated tool to press the button. My biggest worry was that the ac power might have propagated through the programmer into the USB port of my PC…

With 3.3v VCC only, the device doesn’t show any signs of live, but even with the 230v line attached, when powered on with the button pressed, the device goes into the programming mode and no leds flash.

Not possible.

Good to know, I wasn’t aware if this was possible or somehow guaranteed to be protected as I have software background :slight_smile: