# sonoff-flasher

An AlpineLinux image targeted at raspberry pi zero W for sonoff-diy autoflashing with Tasmota.


## BIG FAT WARNING
** this is only a proof of concept, that's no guarantee whatsoever that it's working as intended  **

** there's a big chance to brick your device **

** as with anything that works with mains voltage, there's also risk of injury **

** go on if you're ok with all that, and also don't blame me if something goes wrong **



## installation instructions

### requirements

* internet connection 
* raspberry pi zero W (pay attention as the non-wifi version is very similar)
* a small microsd card (1GB or more will go)
* an USB to microUSB cable (to give power to the rPi)

### instructions

1. [Download](https://alpinelinux.org/downloads/)  the AlpineLinux image for raspberry (armhf architecture) from [here](http://dl-cdn.alpinelinux.org/alpine/v3.10/releases/armhf/alpine-rpi-3.10.3-armhf.tar.gz) 
2. Format your microSD card as FAT32
3. Unpack the downloaded AlpineLinux into the microSD card
4. Copy all the files from [this repo](https://git.asperti.com/paspo/sonoff-flasher)'s rootfs directory into the microSD card (you have to replace **cmdline.txt** with the new one when asked)
5. unmount the card and place it into the rPi

## usage

Power on the raspberry with the microSD card you just created and wait a minute for the OS to load.

Disconnect all the cables from your brand new Sonoff-mini, open it and insert the "DIY jumper" in the proper place. Close it, reconnect the wires and power it on.

In 2-3 minutes you should see a new wifi called "SONOFF-xxxx".

## info on sonoff DIY mode
When you enable the "DIY mode" for a sonoff, you should create a new wireless network called "sonoffDiy" with password "20170618sn", the sonoff connects with that network and you have the possibility to flash a different firmware using the "itead diy tool".


More info on that procedure [here](https://github.com/arendst/Tasmota/wiki/Sonoff-DIY).

## how does this thing works
* this tool creates the "sonoffDiy" wireless network (that's why we need a zero-W) with DHCP and DNS
* when a sonoff-mini connects, it assigns an IP address
* it scans the network (via avahi-browser) to get the IP address of the sonoff and its ID
* it the uses these informations to check if the sonoff is responding to zeroconf requests
* if it does, it requests an "ota-unlock": normally the firmware flashing is not enabled and when asked, the sonoff-mini "phones home" to signal that a OTA unlock has been requested for that specific ID.
* we don't have internet connection, so the raspberry tells the sonoff "I'm itead and you can enable OTA"
* then the raspberry requests a firmware upgrade, telling the sonoff to pick the firmware directly from the raspberry webserver
* the sonoff downloads the firmware, flashes it and reboots
* we now have a new tasmotized friend!
(you can remove the jumper now)

## more info
You can use your PC to power the raspberry, through the OTG port. If you do that, you have an emulated USB ethernet, with an IP address different from that on the wireless side.

SSH is enabled with root user and the default password is "root" (we need little to no security, as we won't need the raspberry anymore after the flashing)

The IP address on the USB side is: **10.111.101.1**

The IP address on the WIRELESS side is: **10.101.111.1**

You can find a log of the flashing script in **/root/flasher.log**

Everything is cleared at each reboot as the whole system is running from RAM. If you want to keep some modifications (e.g. an authorized_keys for ssh autologin) take a look at how it's done [here](https://wiki.alpinelinux.org/wiki/Alpine_local_backup) 

