How to Flash a Sonoff device with Tasmota?

Prepare the Raspbery Pi to be able to flash the firmware into SONOFF switch
sudo systemctl stop serial-getty@AMA0.service
sudo systemctl disable serial-getty@AMA0service
sudo cp /boot/cmdline.txt /boot/cmdline.bak
sudo nano /boot/cmdline.txt
Remove the option “console=serial0,115200” from the file
Install python-pip (if not installed yet)
sudo apt-get install python-pip
Install esptool
sudo apt-get install esptool
then
pip install pyserial
pip3 install pyserial
pip install esptool
then
sudo mkdir /home/pi/sonoff
Connect the Sonoff switch to Raspberry Pi and follow this procedure to set the Sonoff switch in flashing mode.
| – Press the tactile switch on Sonoff while connecting the 3.3V cable |
| – Release the tactile switch |
Take a backup of the Switch firmware
cd /home/pi/sonoff
then
esptool.py --port /dev/ttyAMA0 read_flash 0x00000 0x100000 Sonoff_backup_01.bin
Erase the content of the flash memory
esptool.py --port /dev/ttyAMA0 erase_flash
Switch back the Sonoff in Flashing Mode (see above)
Program the Sonoff with the package downloaded from the Tasmota Releases site (see below)
esptool.py --port /dev/ttyAMA0 write_flash -fm dout -fs 1MB 0x00000 sonoff.bin (for example)
Switch back the Sonoff in Flashing Mode (see above), and run the following command:
pi@raspberrypi:~/sonoff $ python3 /usr/bin/miniterm.py /dev/ttyAMA0 115200
At the prompt, enter the following command:
wificonfig 2
The Sonoff will activate its own Access Point. Hook on it and open a browser with the IP_ADDRESS 192.168.4.1. The web page of the Sonoff device will appear.
How to configure a Sonoff flashed with Tasmota?

Click several times on Toggle button to ensure that the Sonoff device answers to the commands, confirming that the Flashing has been done successfully.
Click on Configuration

Click on Configure Module to define how the Sonoff device will be used. Refer to Tasmota reference below.

Click Save and wait for the page to reload.
Then click on Configuration.
Click on Configure Wifi to define the Wireless Network and the password.
Note: Two wireless network can be configured.
The device will try to hook on the first one, if not successful, will attempt to connect on the second one.

Click on Save. The device will hook on the Wireless network and will return to the Main Menu. Its IP_ADDRESS will be one given by the DHCP of the Wireless network. Use an IP Scanner software to detect it.
Then use this IP_ADDRESS in the browser to access the web page.
Configure the MQTT

Host: IP_ADDRESS of the machine hosting the MQTT Broker (Mosquitto for example).
Note: it can be in the form of http://example.com, provided that this url serves the port 1883 to the outside world.
CLIENT: Anything, not mandatory
USERNAME: One of the username defined in the password file pf Mosquitto. Refer to MOSQUITTO page
PASSWORD: This is the password of one of the users defined in the password file of Mosquitto.
Click Save.
Then Click on Configuration, Main Menu then Console.

Some useful commands
| Command | |
| timezone 8 | will force the device on UT+8 |
| ipaddress1 192.168.86.18 | will force the deive to take the ip_address |
| ipaddress1 restart 1 | will effectively change the ip_address |
| poweronstate | will return the value of this parameter |
| poweronstate 0 | will set the Power to OFF upon start |
| poweronstate 1 | will set the Power to ON upon start |
| poweronstate 3 | will set the Power to the last state before Off |
| powerretain | will retain the value of this parameter |
| powerretain on | will set this parameter to ON |
| powerretain off | will set this parameter to OFF |
| topic | will return the Current Topic for the siwtch |
| topic sonoff_x | will set the topic for this device to sonoff_x |