Sonoff MiniR2 Reprogramming

Sonoff MiniR2

The Sonoff MiniR2 is a versatile mains-powered device containing an Espressif ESP8285 (essentially an ESP8266 processor plus 1MB of built-in flash memory) along with a relay output, a switch input, and a spare GPIO input.

I use a number of these, re-flashed with custom firmware, for controlling my central heating. These are my notes on reprogramming them.

Wireless Reprogramming

Configure WiFi

First, connect the Sonoff MiniR2 to your WiFi:

Determine Sonoff MiniR2's IP address by examining your router or scanning for an open port 8081. This is sometimes easier said than done. It's worth noting that you can turn the Sonoff MiniR2 off and on at this point, and it will retain your wireless credentials and reconnect to your network using the same IP address.

Unlock Sonoff DIY Programming

The easiest way without installing a REST client is using is using PowerShell to POST commands to the Sonoff MiniR2.

Get the current status:

In PowerShell:

Invoke-RestMethod -Uri "http://192.168.X.X:8081/zeroconf/info" -Method Post -ContentType "application/json" -Body '{"data": {}}' | ConvertTo-Json

Unlock Over-The-Air Programming:

In PowerShell:

Invoke-RestMethod -Uri "http://192.168.X.X:8081/zeroconf/ota_unlock" -Method Post -ContentType "application/json" -Body '{"data": {}}' | ConvertTo-Json

(Obviously replacing 192.168.X.X with the actual IP address of the Sonoff MiniR2.)

In both cases the Sonoff MiniR2 should give a JSON response that includes "error": 0, and for the info request, "otaUnlock": false or "otaUnlock": true

OTA Program the Sonoff MiniR2

Use the Sonoff DIY programming tool to upload a new firmware image. The software will automatically detect the Sonoff device you have unlocked. Select 'Brush Machine' to enter the firmware details and reprogram the device.

Important. The new firmware must either include a default WiFi SSID and password or be running a WiFi manager to allow a connection to be subsequently made to your local WiFi.

If you've got it right, the Sonoff MiniR2 will contain your new firmware. If you've got it wrong, you'll need to wire up to the chip as detailed below.

Note that if your project includes a file system image (littlefs.bin) as well as the firmware image (firmware.bin), you will need to first reprogram the Sonoff with the firmware using the Sonoff programming tool, and then upload the file system image using your usual programming tool chain (eg VSCode/PlatformIO).

Wired Reprogramming

Wired connections to Sonoff board

The connections on the Sonoff MiniR2 are, from the top board edge:

PCBFunctionWireNote
GNDGroundBlack
3v+3.3vRed
TTxOrange
RRxBrown
LOGGPIO2YellowNot required for programming. Being used here as an input.
KEY_INGPIO0N/CNot required for programming. Connected to the onboard pushbutton.

Connect Gnd, 3.3v, Tx and Rx straight through to a USB to serial adapter/programmer, without crossing Tx/Rx (ie Tx—Tx and Rx—Rx). Do not power the Sonoff MiniR2 from the mains. Power up the Sonoff device from the USB adapter/programmer whilst holding the button on the Sonoff to enter programming mode.

Download the ESPTool programming utility. No setup is required, you can use the executable directly from a command prompt. For example, if the USB serial port appears as COM6:

Get Flash Size

esptool --port COM6 flash_id

This should elicit a response like:

esptool.py v4.3
Serial port COM6
Connecting...
Detecting chip type... ESP8266
Chip is ESP8285N08
Features: WiFi, Embedded Flash
Crystal is 26MHz
MAC: 70:03:9f:0d:0e:0f
...
Detected flash size: 1MB

Download Firmware

esptool --port COM6 read_flash 0x00000 0x100000 firmware.bin

Erase Flash Memory

esptool --port COM6 erase_flash

Upload New Firmware

esptool --port COM6 write_flash -fs 1MB -fm dout 0x0 firmware.bin