You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
8.0 KiB

  1. # Flashing the Xiaomi Mijia Bedside Lamp 2
  2. ## Tools needed
  3. * Allen key (2mm, 5/64") or torx (T8) screw driver
  4. * Soldering Iron
  5. * Perhaps some sticky tape
  6. * A serial to USB adapter (FTDI)
  7. * Some wires
  8. ## Warning
  9. We have writen these instructions with care, but we will give absolutely no
  10. warranty. Perhaps you will destroy your device and your computer.
  11. ## Opening the lamp, to expose the PCB
  12. Remove the rubber pads from the botton of the lamp.
  13. Unbolt the 4 screws which were hidden by the rubber pads.
  14. ![Photo of the screws](images/screws.jpg "Use an allen key or torx screw driver to remove the screws.")
  15. Remove the bottom from the device, exposing the PCB.
  16. This might take a bit of force. Just pull it up bit by bit until it pops loose.
  17. For some good pictures of disassembling this device, take a look
  18. [at this blog post](https://mysku.ru/blog/china-stores/78455.html)
  19. It is in Russian, but the translation by Google works well and moreover, the
  20. pictures are the most important thing here. If you scroll down, you will
  21. find them easily.
  22. ## Solder the wires
  23. Many of the serial to USB adapter have some header pins to which you can
  24. connect the wires of a device. Therefore, I find it most useful to take some
  25. dupont wires with a female end to them, and cut off the other end. Strip the
  26. wire on the other and, and then it can be used to solder it to the board.
  27. Solder the wires to the RX, TX, GND and GPIO0 debug pads that are shown in this
  28. photo. It is not required to solder a wire to the 3.3V debug pad. This pad
  29. is not directly connected to the 3.3V Vin of the ESP32 chip, making it a
  30. less than optimal candidate for powering the board during flashing. Instead,
  31. powering the lamp using its own power supply works best.
  32. ![Soldering points](images/Soldering_points.jpg)
  33. You can use some sticky tape to fixate the cables before soldering.
  34. ## Connect the wires to your serial to USB device
  35. The wires must be connected as follows:
  36. | Soldering point| Serial USB Adapter name |
  37. | -------------- |:------------------------:|
  38. | GND | GND |
  39. | TX | RX |
  40. | RX | TX |
  41. | GPIO0 | GND |
  42. To be able to flash the device, GPIO0 must be connected to ground while the
  43. device is booted. Therefore, connect these wires before plugging in the
  44. device's power adapter. Flashing will not work if you connect these wires
  45. when the device has already been powered up.
  46. ## When you only have one GND pin on your USB Adapter
  47. If your USB Adapter does not have multiple GND pins, then you'll have
  48. to find another way to attach GPIO0 to ground. Some options:
  49. - Use a breadbord, so you can connect the USB Adapter GND pin to a row on
  50. the bread bord, and connect the GND and GPIO0 wires of the board to that
  51. same row.
  52. - Solder a button on the board that connects GPIO0 to GND when pressed. Then
  53. you can hold down this button while plugging in the device's power supply.
  54. After booting up, you can release the button (the serial console will also
  55. mention that the device is now in flash mode). This is not the most
  56. practical solution for most people (since only one such flash operation is
  57. needed, from then on OTA - Over The Air - updates are possible), but it
  58. was a great help to me during the initial reverse engineering and firmware
  59. development.
  60. - Manually hold a wire connected to both a GND surface (e.g. the silver pad
  61. on the left of the board) and the GPIO0 debug pad, while plugging in the
  62. power supply. After booting, the wire can be removed. This is very fiddly
  63. way of doing it, but I did it a few times and it can be done.
  64. - You could opt for temporarily soldering a lead between GND and GPIO0 on
  65. the board, making GPIO0 pulled to ground permanently. It is a bit less
  66. flexible than some other options, but if you only need to do the initial
  67. backup and firmware flash of the device, then this can bee all that you
  68. need. Remove the lead after flashing is done, otherwise the device won't
  69. boot in normal mode.
  70. ## Download and install esptool
  71. See: https://github.com/espressif/esptool/blob/master/README.md#installation--dependencies
  72. ## Make a backup of the current firmware
  73. Here's an example on how to backup the original firmware from Linux. First,
  74. unplug your device's power supply, then start the esptool read_flash command:
  75. ```
  76. python esptool.py -p /dev/ttyUSB0 read_flash 0x0 0x400000 original-firmware.bin
  77. ```
  78. /dev/ttyUSB0 is the port of the usb adaper on Linux. You can find what port
  79. is used by the adapter by running `dmesg` after plugging in the USB device.
  80. On Windows this is often `COM1`, `COM2` or `COM3`.
  81. Now plug in the power supply. The output of esptool should now show that it
  82. connects to the device and downloads the firmware from it.
  83. **Caution**: You will find the WLAN SSID and Password of the latest used WiFi in
  84. this file. Therefore, keep this backup in a safe place.
  85. ## Restore the backed up firmware
  86. In case you need to rollback to the device's original firmware at some point,
  87. here's an example of how to restore the original firmware from Windows, by fully
  88. flashing it back onto the device.
  89. First, unplug your device's power supply, then start the esptool write_flash command:
  90. ```
  91. python.exe .\esptool.py --chip esp32 --port COM3 --baud 115200 write_flash 0x00 original-firmware.bin
  92. ```
  93. Make sure that GPIO0 is connected to GND and plug in the power supply.
  94. The output of esptool should now show that it connects to the device and uploads
  95. the firmware to it. Be patient after the upload reaches 100%. The output is
  96. silent for a while, but esptool tool is verifying if the firmware was uploaded
  97. correctly.
  98. After the firmware was uploaded, unplug the power, disconnect GPIO0 from GND and
  99. reconnect the power to boot into the restored firmware.
  100. ## Flash new firmware
  101. Setup an ESPHome Project (see [README.md](../README.md)),compile the firmware
  102. for the device and download the `firmware.bin` file to the device to which
  103. the serial adapter is connected.
  104. You can flash the device using esphome or esptool.
  105. I normally use the [esphome-flasher](https://github.com/esphome/esphome-flasher)
  106. tool, which is a very easy to use GUI utility app for flashing ESPHome devices:
  107. - In the app, select the COM port of your serial adapter
  108. - Also select the firmware.bin file to flash onto the device
  109. - Power up the device with GPIO0 connected to GND
  110. - Click the "Flash ESP" button to flash the firmware.
  111. If you want to flash with esptool, you can use:
  112. ```
  113. python.exe .\esptool.py --chip esp32 --port COM3 --baud 115200 write_flash 0x1000 <path\to\yourfirmware.bin>
  114. ```
  115. After flashing, power down the device, disconnect GPIO0 from GND and reconnect
  116. the power to boot into the ESPHome firmware.
  117. From here on, it is possible to flash the device OTA (over the air, which
  118. means that the firmware is uploaded over WiFi) from ESPHome. Therefore, it
  119. is now time to tuck away or remove those soldered wires and add the bottom
  120. cover back on.
  121. ## Troubleshooting flash
  122. If you have **A fatal error occurred: MD5 of file does not match data in
  123. flash!**, then make sure you are powering the board using the device's own
  124. power adapter. We've seen these errors when trying to power the board using
  125. the 3.3V debug pad.
  126. After seeing this error, user @tabacha was able to successfully flash his
  127. device using the regular power adapter and the tasmota boot loader using
  128. the following command:
  129. ```
  130. python esptool.py --chip esp32 -p /dev/ttyUSB0 --baud 115200 --before default_reset --after hard_reset write_flash -z --flash_mode dout --flash_freq 40m --flash_size detect 0x1000 bootloader_dout_40m.bin 0x8000 partitions.bin 0xe000 boot_app0.bin 0x10000 ~/Downloads/schlafzimmerbedlight.bin
  131. ```
  132. You will find the missing tasmota boot files here:
  133. https://github.com/arendst/Tasmota/tree/firmware/firmware/tasmota32/ESP32_needed_files
  134. *Note: user @tabacha was not able to use tasmota with the Bedside Lamp 2.*
  135. (remember that the [esphome-flasher](https://github.com/esphome/esphome-flasher)
  136. will give you a bit less of a hard-core experience during flashing)