Flashing ESP gives error message: "Unexpected error: The firmware binary is invalid (magic byte=23, should be E9)"

I try to flash a Sonoff D1 with ESP-home but I get the error above.

I found a tutorial Sonoff D1 Dimmer running ESPHome | JeffResc that I followed but as I flash it with ESP-home flasher I get the error.

I also tried and flash it from inside ESP-home with the wired solution but it says “Error fetching configuration information”.

The yaml is this:

# Set variables such as the device's name and friendly name
# The devicename is used internally which the friendly name is shown in the user interface
substitutions:
  devicename: sonoff_d1_matilda
  friendly_name: Dimmer_Matilda

esphome:
  name: $devicename
  platform: ESP8266
  board: esp01_1m
  # Include our custom code
  includes:
    - d1_dimmer_no_rf.h

# TODO: Set these as your own Wi-Fi credentials
wifi:
  ssid: "something"
  password: "something"
  fast_connect: true
  ap:
    ssid: ESP-${devicename}
    password: "XgRIzTxPcREB"

captive_portal:

# TODO: Set these as your own web server credentials
web_server:
  port: 80
#  auth:
#    username: !secret web_username
#    password: !secret web_password

# Make sure to disable serial logging as it will interfere with our serial connection
logger:
  baud_rate: 0

# TODO: Set your own API credentials
api:
  password: "Sonoff_dimmer_Matilda"

# TODO: Set your own OTA credentials
ota:
  password: "Sonoff_dimmer_Matilda"
  safe_mode: True

# Uses the onboard LED as status indicator
status_led:
  pin:
    number: GPIO13
    inverted: True

# Report basic device information like it's Wi-Fi signal strength and uptime
sensor:
  - platform: wifi_signal
    name: ${friendly_name} WiFi Signal
    update_interval: 60s
  - platform: uptime
    name: ${friendly_name} Uptime
    filters:
      - lambda: return x / 60.0;
    unit_of_measurement: minutes

# Basic switch to allow you to restart the device remotely
switch:
  - platform: restart
    name: ${friendly_name} Restart

binary_sensor:
  - platform: status
    name: "Sonoff D1 Matilda Status"

# Define our custom light component
light:
  - platform: custom
    lambda: |-
      auto dimmer_light = new Sonoff_D1_Dimmer();
      App.register_component(dimmer_light);
      return {dimmer_light};
    lights:
      - name: $friendly_name
        id: main
        restore_mode: RESTORE_DEFAULT_OFF
        gamma_correct: 0      

Essentially unchanged from what the blog says.
Only thing I have changed is to disable the web server auth and changed the wifi credentials.

Anyone know what the error means?

It’s connected gnd -gnd
tx - rx
rx - tx
vcc - vcc

it seems there is errors in the .h file.

Compiling /data/test/.pioenvs/test/src/main.cpp.o
In file included from src/main.cpp:12:0:
src/d1_dimmer_no_rf.h:4:1: error: expected class-name before '{' token
 {
 ^
src/d1_dimmer_no_rf.h:16:3: error: 'LightTraits' does not name a type
   LightTraits get_traits() override
   ^
In file included from src/main.cpp:12:0:
src/d1_dimmer_no_rf.h:44:20: error: 'LightState' has not been declared
   void write_state(LightState *state) override
                    ^
src/d1_dimmer_no_rf.h:44:8: error: 'void Sonoff_D1_Dimmer::write_state(int*)' marked override, but does not override
   void write_state(LightState *state) override
        ^
src/d1_dimmer_no_rf.h: In member function 'void Sonoff_D1_Dimmer::write_state(int*)':
src/d1_dimmer_no_rf.h:50:12: error: request for member 'current_values_as_binary' in '* state', which is of non-class type 'int'
     state->current_values_as_binary(&binary);
            ^
src/d1_dimmer_no_rf.h:51:12: error: request for member 'current_values_as_brightness' in '* state', which is of non-class type 'int'
     state->current_values_as_brightness(&brightness);
            ^
*** [/data/test/.pioenvs/test/src/main.cpp.o] Error 1
========================== [FAILED] Took 8.18 seconds ==========================

Anyone know how to deal with those errors?

d1_dimmer_no_rf.h:

/*
  d1_dimmer_no_rf.h - Sonoff D1 Dimmer support for ESPHome

  Copyright © 2020 Jeff Rescignano

  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  and associated documentation files (the “Software”), to deal in the Software without
  restriction, including without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
  the Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all copies or
  substantial portions of the Software.

  THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  -----

  If modifying this file, in addition to the license above, please ensure to include links back to the original code:
  https://jeffresc.dev/blog/2020-10-10
  https://github.com/JeffResc/Sonoff-D1-Dimmer
  https://github.com/arendst/Tasmota/blob/2d4a6a29ebc7153dbe2717e3615574ac1c84ba1d/tasmota/xdrv_37_sonoff_d1.ino#L119-L131

  -----

  THANK YOU!
  Thanks to the team over at Tasmota for providing the serial codes to control the dimmer!

  View the source: https://github.com/arendst/Tasmota/blob/2d4a6a29ebc7153dbe2717e3615574ac1c84ba1d/tasmota/xdrv_37_sonoff_d1.ino#L119-L131
*/

#include "esphome.h"

class Sonoff_D1_Dimmer : public Component, public LightOutput
{
private:
  bool lastBinary;
  int lastBrightness;

public:
  void setup() override
  {
    // Start a serial connection when the script is setup
    Serial.begin(9600);
  }
  // Set the device's traits
  LightTraits get_traits() override
  {
    auto traits = LightTraits();
    traits.set_supported_color_modes({light::ColorMode::BRIGHTNESS});
    return traits;
  }

  void control_dimmer(const bool & binary, const int & brightness) {
    if (binary != lastBinary || brightness != lastBrightness)
    {
      // Include our basic code from the Tasmota project, thank you again!
      //                     0    1    2    3    4    5    6    7    8    9    10   11   12   13   14   15   16
      uint8_t buffer[17] = {0xAA, 0x55, 0x01, 0x04, 0x00, 0x0A, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00};

      buffer[6] = binary;
      buffer[7] = brightness;

      for (uint32_t i = 0; i < sizeof(buffer); i++)
      {
        if ((i > 1) && (i < sizeof(buffer) - 1))
        {
          buffer[16] += buffer[i];
        }
        Serial.write(buffer[i]);
      }
    }
  }

  void write_state(LightState *state) override
  {
    bool binary;
    float brightness;

    // Fill our variables with the device's current state
    state->current_values_as_binary(&binary);
    state->current_values_as_brightness(&brightness);

    // Convert ESPHome's brightness (0-1) to the device's internal brightness (0-100)
    const int calculatedBrightness = round(brightness * 100);

    ESP_LOGD("custom", "Interpreting brightness %f as %d", brightness, calculatedBrightness);
    control_dimmer(binary, calculatedBrightness);
  }
};

Sonoff devices are typically ESP8285 devices?

I believe so?

Why would that be an issue with the .h file then?
Is that compiled differently?

different processor, different hardware, different registers, different .h file.
Give it a try with esp8285 and see what happens.

I don’t think 8285 is valid.
Using the ESP-Home editor it gives an error in that line.

But after a few more attempts it just worked.
And let me be clear that I had already tried it probably 10 times by the time I posted the question.
It’s unknown why it all of the sudden worked.

This comes from an earlier ESPHome support post:

esphome:
  platform: ESP8266
  board: esp8285

I did not try that.
I just used platform 8285.

It seems to run fine on 8266 also, haven’t tried to connect it to a light yet though.
But if it doesn’t work then I know how I can change the config.
Thanks!

1 Like

There is little difference between 8285 and 8266 :

  1. ESP8285 integrates 1MB Flash in DOUT mode. ESP8266 requires external Flash;
  2. ESP8285 has more GPIO9 and GPIO10 for users than ESP8266;

For the rest they are the same.