Working in Sonoff Mini

This commit is contained in:
2021-02-02 12:51:13 -05:00
parent 33efa7052c
commit 6e2f3e071d
7 changed files with 297 additions and 13 deletions

View File

@@ -0,0 +1,68 @@
Flashing a Sonoff Mini to ESPHome as of 2021-01
Web sites used:
- Essential
- Chrome extension for RESTer
- [https://tasmota.github.io/docs/Sonoff-DIY/](https://tasmota.github.io/docs/Sonoff-DIY/) - Most of the process, but doesn't tell how to actually flash OTA
- [https://blog.lukaskukacka.com/smarthome/2020/09/09/sonoff-mini-firmware-3.6-flashing-tasmota.html](https://blog.lukaskukacka.com/smarthome/2020/09/09/sonoff-mini-firmware-3.6-flashing-tasmota.html) - Does tell how to actually flash OTA
- [https://esphome.io/devices/sonoff.html](https://esphome.io/devices/sonoff.html) - YAML for ESPHome
- Nice to know
- [https://github.com/itead/Sonoff\_Devices\_DIY\_Tools/blob/master/SONOFF%20DIY%20MODE%20Protocol%20Doc%20v2.0%20Doc.pdf](https://github.com/itead/Sonoff_Devices_DIY_Tools/blob/master/SONOFF%20DIY%20MODE%20Protocol%20Doc%20v2.0%20Doc.pdf) - Documents API (I found later)
- [https://notenoughtech.com/home-automation/sonoff-r3-diy-mode/](https://notenoughtech.com/home-automation/sonoff-r3-diy-mode/) - ota\_unlock
- [https://notenoughtech.com/home-automation/is-sonoff-diy-2-0-better/](https://notenoughtech.com/home-automation/is-sonoff-diy-2-0-better/) - DIY in general
- Not used but read
- [https://www.reddit.com/r/homeassistant/comments/di4mrk/guide\_howto\_flash\_the\_sonoff\_mini\_with\_tasmota/](https://www.reddit.com/r/homeassistant/comments/di4mrk/guide_howto_flash_the_sonoff_mini_with_tasmota/) (Old way)
- [http://dl.itead.cc/mini/C\_Instructions/MINIR2\_V1.1\_User\_manual\_20201218.pdf](http://dl.itead.cc/mini/C_Instructions/MINIR2_V1.1_User_manual_20201218.pdf) - Mini user manual (Not needed)
- [http://developers.sonoff.tech/sonoff-diy-mode-api-protocol.html](http://developers.sonoff.tech/sonoff-diy-mode-api-protocol.html) - Mini DIY mode (not really needed)
- [http://developers.sonoff.tech/sonoff-diy-mode-api-protocol.html](http://developers.sonoff.tech/sonoff-diy-mode-api-protocol.html) - Old and not applicable
Disclaimer: I don't take any responsibility for any damages or other harm caused by anything in this article. Please make sure you know what you are doing and stay safe. You are working with electric device.
Process used:
1. Create and serve your new firmware:
1. Create the firmware:
1. Method 1: Download a generic Tasmota image. You can use this to get your device flashed, and then later flash your ESPHome image OTA using the Tasmota interface
2. Method 2: Go directly to ESPHome (which I did): Using the YAML from esphome.io/devices, generate a firmware.bin file for your use. I greatly favor assigning a static IP address.
2. Serve the firmware and its checksum on an accessible file server
1. The firmware.bin file, and its sha256 hash checksum (might be optional, but I did it), must be available on your local network as a URL. If you have shell access to a publicly-accessible Linux server (I do) that works well. Alternatively, you could use Docker, mohamnag/nginx-file-browser or equivalent, to temporarily serve the file on your local machine / network. There are probably other options; I had a file server available so didn't look for them.
2. You also need the sha256 checksum of firmware.bin. On my Ubuntu box I "sudo apt install hashalot" and then used "sha256sum firmware.bin", which gave me a long string of characters. Copy-paste them somewhere for later use.
2. Connect a power cable to your Sonoff Mini. No need to open it or do anything with jumpers; just hook it up.
3. Make a list of what's connected to your router. I used "nmap -sL 192.168.1.\* | grep \(1" on my Linux box.
4. Get your Sonoff Mini's LED to blink continuously, with no discernable flash pattern. If it's flashing two shorts and a long, press and hold the button for about five seconds. If you've done things to this Sonoff before this you might have to do that twice, and you might need to power cycle it.
5. On your computer, look for a WiFi access point named ITEAD-XXXXXXXX. Connect to it with password 12345678.
6. On that same computer, open your browser and access [http://10.10.7.1/](http://10.10.7.1/)
7. You should be connected to a (ridiculously zoomed) Sonoff screen. Fill in your router's SSID and password, and click "Save."
8. If that works (you might get a "request fail" on the screen even if it worked), your Sonoff will start blinking slowly and steadily, indicating it's now connected to your network. (Also the ITEAD-XXXXXXX access point will have disappeared, and your computer might have gone back to being connected to your network).
9. Find the Sonoff's IP address, preferably by comparing the current list of things connected to your router to the list you made above. If you have a complicated network this can be a nuisance. My Sonoffs showed up as devices starting with "esp\_" or with "eWeLink". For illustration, I'll say the new Sonoff is at 192.168.86.59, but yours is likely different.
10. Use Chrome plug-in RESTer. Set it for:
1. Method: POST
2. URL: [http://192.168.86.59:8081/zeroconf/info](http://192.168.86.59:8081/zeroconf/info) [Change to your Sonoff's IP)
3. Body: {"data": {}}
4. Click "SEND." You should get back a "200 OK" plus a bunch of information about the Sonoff notably including "otaUnlock": false
11. In RESTer, change that "info" to "ota\_unlock" so my URL is now [http://192.168.86.59:8081/zeroconf/ota\_unlock](http://192.168.86.59:8081/zeroconf/ota_unlock) Click "SEND."
12. Run the RESTer "info" command again. It should now report "otaUnlock": true
13. Configure RESTer:
1. Method: POST
2. URL: [http://192.168.86.59:8081/zeroconf/ota\_flash](http://192.168.86.59:8081/zeroconf/ota_flash) (use your Sonoff's IP)
3. Body:
{
"deviceid": "",
"data": {
"downloadUrl": "http://www.myserver.com/myfilearea/firmware.bin",
"sha256sum": "187dce36882b63ddf7ea9a76f99821b97f95c5c246b23a703c92c5f22b01bc0f"
}
}
Change the entries after downloadUrl and sha256sum to match your file server and hash. Hit "SEND." Note that [https://blog.lukaskukacka.com/smarthome/2020/09/09/sonoff-mini-firmware-3.6-flashing-tasmota.html](https://blog.lukaskukacka.com/smarthome/2020/09/09/sonoff-mini-firmware-3.6-flashing-tasmota.html) includes a lot of applicable information here. Note if you get the hash wrong (e.g., include an extra space in the quotes) the new firmware will be rejected.
1. You should get an immediate "200 OK," but if you watch the access log on your server you'll see the download takes a little while (about 15 seconds for my setup). If you can't watch the server log, wait a while to be sure it finishes.
2. If your Sonoff now shows a steady blink, you're done. My new ESPHome device soon showed up in Home Assistant.

4
path_to_firmware.txt Normal file
View File

@@ -0,0 +1,4 @@
http://meneelys.com/f/firmware_sonoff_mini99.bin
76847a88e96f8053cd2b848bbd2b3ce9a95ac86d599848e272610d4e14f1a9cb firmware_sonoff_mini99.bin

16
sonoff_mini.py Normal file
View File

@@ -0,0 +1,16 @@
import requests
the_ip = "192.168.86.50"
emptybody = {"data":{}}
response = requests.post('http://'+the_ip+':8081/zeroconf/info', json = emptybody)
print(response)
print(response.json())
response = requests.post('http://'+the_ip+':8081/zeroconf/ota_unlock', json = emptybody)
print(response)
print(response.json())
response = requests.post('http://'+the_ip+':8081/zeroconf/info', json = emptybody)
print(response)
print(response.json())
flashbody = {"deviceid": "","data": {"downloadUrl": "http://meneelys.com/f/firmware_sonoff_mini99.bin","sha256sum": "76847a88e96f8053cd2b848bbd2b3ce9a95ac86d599848e272610d4e14f1a9cb"}}
response = requests.post('http://'+the_ip+':8081/zeroconf/ota_flash', json = flashbody)
print(response)
print(response.json())

View File

@@ -44,7 +44,7 @@ sensor:
binary_sensor:
- platform: gpio
pin: GPIO00
pin: GPIO00 # The built-in switch
id: reset
internal: true
filters:
@@ -56,16 +56,18 @@ binary_sensor:
- platform: gpio
name: ${device_name}_status
pin: GPIO04
pin: GPIO04 # The external terminals
id: switch_1
filters:
- delayed_on: 100ms
on_press:
then:
- switch.toggle:
id: relay_1
on_release:
then:
- switch.toggle:
id: relay_1
# on_release:
# then:
# - switch.toggle:
# id: relay_1
switch:
- platform: gpio
@@ -91,4 +93,4 @@ light:
- platform: monochromatic
name: ${device_name}_blueled
output: blue_led
internal: true # hides the Blue LED from HomeAssistant
internal: true # hides the Blue LED from HomeAssistant

View File

@@ -44,7 +44,7 @@ sensor:
binary_sensor:
- platform: gpio
pin: GPIO00
pin: GPIO00 # The built-in switch
id: reset
internal: true
filters:
@@ -56,16 +56,18 @@ binary_sensor:
- platform: gpio
name: ${device_name}_status
pin: GPIO04
pin: GPIO04 # The external terminals
id: switch_1
filters:
- delayed_on: 100ms
on_press:
then:
- switch.toggle:
id: relay_1
on_release:
then:
- switch.toggle:
id: relay_1
# on_release:
# then:
# - switch.toggle:
# id: relay_1
switch:
- platform: gpio

96
sonoff_mini43.yaml Normal file
View File

@@ -0,0 +1,96 @@
substitutions:
device_name: sonoffmini_43
device_ip: 192.168.87.43
esphome:
name: ${device_name}
platform: ESP8266
board: esp01_1m
wifi:
ssid: "Meneely"
password: "punxsutawney1"
manual_ip:
static_ip: ${device_ip}
gateway: 192.168.86.1
subnet: 255.255.255.0
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "ESPHOME"
password: "12345678"
logger:
api:
reboot_timeout: 15min
ota:
# the web_server & sensor components can be removed without affecting core functionaility.
web_server:
port: 80
sensor:
- platform: wifi_signal
name: ${device_name} Wifi Signal Strength
update_interval: 60s
- platform: uptime
name: ${device_name} Uptime
#######################################
# Device specific Config Begins Below #
#######################################
binary_sensor:
- platform: gpio
pin: GPIO00 # The built-in switch
id: reset
internal: true
filters:
- invert:
- delayed_off: 10ms
on_press:
- switch.toggle:
id: relay_1
- platform: gpio
name: ${device_name}_status
pin: GPIO04 # The external terminals
id: switch_1
filters:
- delayed_on: 100ms
on_press:
then:
- switch.toggle:
id: relay_1
# on_release:
# then:
# - switch.toggle:
# id: relay_1
switch:
- platform: gpio
name: ${device_name}_switch
icon: "mdi: lightbulb_outline"
pin: GPIO12
id: relay_1
restore_mode: restore_default_off
status_led:
pin:
number: GPIO13
inverted: true
output:
- platform: esp8266_pwm
id: blue_led
pin: GPIO13
inverted: True
light:
# the 4 lines below define the Blue LED light on Sonoff Mini, to expose in HomeAssistant remove line "internal: true"
- platform: monochromatic
name: ${device_name}_blueled
output: blue_led
internal: true # hides the Blue LED from HomeAssistant

96
sonoff_mini99.yaml Normal file
View File

@@ -0,0 +1,96 @@
substitutions:
device_name: sonoffmini_99
device_ip: 192.168.87.99
esphome:
name: ${device_name}
platform: ESP8266
board: esp01_1m
wifi:
ssid: "Meneely"
password: "punxsutawney1"
manual_ip:
static_ip: ${device_ip}
gateway: 192.168.86.1
subnet: 255.255.255.0
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "ESPHOME"
password: "12345678"
logger:
api:
reboot_timeout: 15min
ota:
# the web_server & sensor components can be removed without affecting core functionaility.
web_server:
port: 80
sensor:
- platform: wifi_signal
name: ${device_name} Wifi Signal Strength
update_interval: 60s
- platform: uptime
name: ${device_name} Uptime
#######################################
# Device specific Config Begins Below #
#######################################
binary_sensor:
- platform: gpio
pin: GPIO00 # The built-in switch
id: reset
internal: true
filters:
- invert:
- delayed_off: 10ms
on_press:
- switch.toggle:
id: relay_1
- platform: gpio
name: ${device_name}_status
pin: GPIO04 # The external terminals
id: switch_1
filters:
- delayed_on: 100ms
on_press:
then:
- switch.toggle:
id: relay_1
# on_release:
# then:
# - switch.toggle:
# id: relay_1
switch:
- platform: gpio
name: ${device_name}_switch
icon: "mdi: lightbulb_outline"
pin: GPIO12
id: relay_1
restore_mode: restore_default_off
status_led:
pin:
number: GPIO13
inverted: true
output:
- platform: esp8266_pwm
id: blue_led
pin: GPIO13
inverted: True
light:
# the 4 lines below define the Blue LED light on Sonoff Mini, to expose in HomeAssistant remove line "internal: true"
- platform: monochromatic
name: ${device_name}_blueled
output: blue_led
internal: true # hides the Blue LED from HomeAssistant