doc:appunti:hardware:sonoff
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
doc:appunti:hardware:sonoff [2021/01/20 17:07] – [Flashing the Tasmota firmware] niccolo | doc:appunti:hardware:sonoff [2021/01/20 17:12] (current) – removed niccolo | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Internet of Things: Sonoff Basic R2 ====== | ||
- | |||
- | Il vantaggio di quesi **power switch intelligenti** marchiati **Sonoff**, è che sono facilmente programmabili con il firmware **[[https:// | ||
- | |||
- | The main advantage of **Sonoff** branded **smart power switches** is that they are easily programmable with the **[[https:// | ||
- | |||
- | {{.: | ||
- | |||
- | To **open the plastic case** of the Sonoff Basic R2 you have to unlock the four **snap fit joints**: there are two on each of the long edges. | ||
- | |||
- | {{.: | ||
- | |||
- | In the following pictures you can see the **pin headers** that I soldered on the PCB, that pin will be used to connect the USB to serial adapter used to **flash the Tasmota firmware**. | ||
- | |||
- | {{.: | ||
- | {{.: | ||
- | |||
- | {{.: | ||
- | {{.: | ||
- | |||
- | ===== Tools required to flash the Tasmota firmware ===== | ||
- | |||
- | - **Pin headers** to be soldered on the Sonoff Basic PCB. | ||
- | - An **USB to serial adapter**. | ||
- | - The **esptool.py** flashing tool (Python program). | ||
- | - The **tasmota.bin** firmware to be uploaded (flashed) into the Sonoff Basic. | ||
- | |||
- | You can download the **esptool.py** form the **[[https:// | ||
- | |||
- | The Tasmota firmware can be downloaded from the **[[https:// | ||
- | |||
- | ===== Flashing the Tasmota firmware ===== | ||
- | |||
- | To flash the Tasmota firmware I used a **GNU/ | ||
- | |||
- | - **Disconnect** the Sonoff Basic from **AC power**. | ||
- | - **Connect** the USB to **serial adapter** to the **Sonoff serial**, all the 4 wires are required: **3.3 Volt**, **TX**, **RX** and **GND**. The TX line of the adapter is connected to the RX line of Sonoff, and vice-versa. | ||
- | - **Execute each step** of the flashing procedure in this manner: | ||
- | * **Connect** the USB adapter to the computer **while holding down** the push button of the Sonoff device. Keeping the button pressed for 7 seconds is sufficient. Keeping the button pressed while power-on is required to put the device into a special programming mode. | ||
- | * **Execute** the required **esptool.py command** on the PC. | ||
- | * **Disconnect** the USB adapter from the PC. | ||
- | |||
- | This is the concise list of commands I used (remember: **each command** is executed after the Sonoff device is **booted in programming mode** as explained above!): | ||
- | |||
- | < | ||
- | ./ | ||
- | ./ | ||
- | ./ | ||
- | ./ | ||
- | </ | ||
- | |||
- | Running the command **%%./ | ||
- | |||
- | ^ flash_id | ||
- | ^ read_flash | ||
- | ^ erase_flash | ||
- | ^ write_flash | ||
- | |||
- | Here it is the output generated by the **write_flash** command: | ||
- | |||
- | < | ||
- | ./ | ||
- | esptool.py v3.0 | ||
- | Serial port / | ||
- | Connecting.... | ||
- | Detecting chip type... ESP8266 | ||
- | Chip is ESP8285 | ||
- | Features: WiFi, Embedded Flash | ||
- | Crystal is 26MHz | ||
- | MAC: c8: | ||
- | Uploading stub... | ||
- | Running stub... | ||
- | Stub running... | ||
- | Configuring flash size... | ||
- | Compressed 600960 bytes to 428969... | ||
- | Wrote 600960 bytes (428969 compressed) at 0x00000000 in 37.9 seconds (effective 127.0 kbit/s)... | ||
- | Hash of data verified. | ||
- | Leaving... | ||
- | Hard resetting via RTS pin... | ||
- | </ | ||
- | |||
- | After the **write_flash** command you can **power-cycle** the Sonoff device by unplugging and plugging the USB adapter again, this time **without holding the Sonoff button**. The LED on the Sonoff flashes in blue, the device starts into **Access Point mode** and stays this way for about **three minutes**, connect a smartphone or the notebook to it (search for an ESSID like **tasmota-xxxx**) and browse the address **%%http:// | ||
- | |||
- | **NOTICE** :!: I had some **problems connecting to the Sonoff Access Point** using the smartphone and using the notebook: sometimes the IP address were not assigned, other times the host 192.168.4.1 were not reachable. One time I succeded by setting the IP address manually (in the range 192.168.4.x), | ||
- | |||
- | ^ AP1 SSId/ | ||
- | ^ AP2 SSId/ | ||
- | ^ Hostname | ||
- | ^ CORS Domain | ||
- | |||
- | ==== Hardware Module Configuration ==== | ||
- | |||
- | After WiFi configuration, | ||
- | |||
- | * **Configure** the device **name and template**. Choose a name to identify this device and select the appropriate template. The template tells the firmware how to use the hardware (the relay and the LED): just use the default **Sonoff Basic (1)**. | ||
- | * **Configuration** => **Configure Template** => **Based on** => **Sonoff Basic (1)** | ||
- | * **Protect** the **web access** with a password (beware that it uses plain http, so password can be sniffed along the network): | ||
- | * **Configuration** => **Configure Other** => **Web Admin Password** | ||
- | * **Disable** the **[[https:// | ||
- | * Connecto to the web interface, **Main Menu** => **Console** and enter the command **WifiConfig 5**. This means //wait until selected AP is available again without rebooting// | ||
- | |||
- | ==== Button Reset ==== | ||
- | |||
- | If you have trouble connecting to a Tasmota device and the device have a button, you have at least two methods to try to recover. | ||
- | |||
- | ^ 6 short presses | ||
- | ^ Long press for 40 seconds | ||
- | ===== Controlling Tasmota via MQTT ===== | ||
- | |||
- | MQTT is a client/ | ||
- | |||
- | **WARNING**: | ||
- | ==== Installing a MQTT Broker ==== | ||
- | |||
- | On a GNU/Linux Debian host (version 10 Buster) I installed the following packages: | ||
- | |||
- | * **mosquitto** | ||
- | * **mosquitto-clients** | ||
- | |||
- | The daemon **mosquitto** is running and listening on port **TCP/ | ||
- | |||
- | With default configuration, | ||
- | |||
- | < | ||
- | mosquitto_sub -h mqtt.server.tld -t my_topic | ||
- | </ | ||
- | |||
- | < | ||
- | mosquitto_pub -h mqtt.server.tld -t my_topic -m " | ||
- | </ | ||
- | |||
- | On the first terminal you will get the message '' | ||
- | |||
- | ==== Configuring MQTT on Tasmota ==== | ||
- | |||
- | From the Tasmota **Main menu** => **Configuration** => **Configure MQTT**: | ||
- | |||
- | ^ Host | Hostname or IP address of the MQTT server. | ||
- | ^ Port | Default is 1883, which is MQTT TCP unencrypted. | ||
- | ^ Client | ||
- | ^ User | Username for authenticating on the MQTT broker, default is **DVES_USER**. | ||
- | ^ Password | ||
- | ^ Topic | Default is **%%tasmota_%06X%%**, | ||
- | ^ Full Topic | Default is **%%%prefix%/ | ||
- | |||
- | From the Tasmota **Main menu** => **Configuration** => **Configure other**: | ||
- | |||
- | ^ MQTT enable | ||
- | ^ Device Name | Default is // | ||
- | ^ Friendly Name 1 | Used by the autodiscovery feature, default is // | ||
- | |||
- | Here it is an example to **publish** an MQTT message asking the Sonoff device to **toggle the switch**: | ||
- | |||
- | < | ||
- | mosquitto_pub -h mqtt.server.tld | ||
- | </ | ||
- | |||
- | **NOTICE**: If you use a non-existent topic (e.g. a **wrong device name**), **no error is reported**. | ||
- | |||
- | Obviously the device understands the **TOGGLE**, **ON** and **OFF** messages. If you are **subscribed** to the proper MQTT topic, you will **receive a message** for each toggle: | ||
- | |||
- | < | ||
- | mosquitto_sub -h mqtt.server.tld -t " | ||
- | {" | ||
- | {" | ||
- | </ | ||
- | |||
- | ==== Password protect the Mosquitto broker ==== | ||
- | |||
- | A default Debian installation of the **mosquitto** server does not require a password from the connecting client. | ||
- | |||
- | To manage **a password file** you can use the **mosquitto_passwd** tool. The following example will add two users to the file: for each **username**, | ||
- | |||
- | < | ||
- | mosquitto_passwd -c / | ||
- | mosquitto_passwd / | ||
- | </ | ||
- | |||
- | The file contains **sha512** hashes; to avoid world-readability you can change the file mode to **0640**. The file is read at program start using root privileges; if the file is changed the mosquitto server must be restarted. | ||
- | |||
- | Then we define an **Access Control List** creating a **/ | ||
- | |||
- | < | ||
- | user DVES_USER | ||
- | topic readwrite +/ | ||
- | |||
- | user mqtt-client | ||
- | topic write cmnd/ | ||
- | topic read stat/ | ||
- | </ | ||
- | |||
- | In the above example we granted the **DVES_USER** (the default username used by the Tasmota firmware) the read and write rights over a topic composed by the **%%+%%** wildcard (single level wildcard), the device name **tasmota_EEB73A**, | ||
- | |||
- | This may be an over-complicated setting, in a basic scenario using a line **'' | ||
- | |||
- | Finally we tell mosquitto to use the above two files by adding a file **/ | ||
- | |||
- | < | ||
- | allow_anonymous false | ||
- | password_file / | ||
- | acl_file / | ||
- | </ | ||
- | |||
- | After restarting the server, you have to specify the correct **username** and **password** when you execute **mosquitto_pub**, | ||
- | |||
- | < | ||
- | mosquitto_pub -h mqtt.server.tld -u ' | ||
- | Connection Refused: not authorised. | ||
- | Error: The connection was refused. | ||
- | </ | ||
- | |||
- | **WARNING**: | ||
- | ===== Controlling Tasmota via HTTP GET ===== | ||
- | |||
- | The Tasmota firmware exposes an open, //admin mode//, web interface to configure and control the device status (this default can be changed). It is advisable to set a password from **Main menu** => **Configuration** => **Configure Other** => **Web Admin Password**. The default username is **admin**. | ||
- | |||
- | **WARNING**: | ||
- | |||
- | Suppose that your Tasmota device has IP address **10.0.0.114**, | ||
- | |||
- | <code bash> | ||
- | # Control the relay status: Toggle, switch it On, switch it Off. | ||
- | wget -q -O /dev/null ' | ||
- | wget -q -O /dev/null ' | ||
- | wget -q -O /dev/null ' | ||
- | </ | ||
- | |||
- | If you want to read the relay status after the command, read the output of the GET request (a JSON string): | ||
- | |||
- | <code bash> | ||
- | wget -q -O - ' | ||
- | {" | ||
- | </ | ||
- | |||
- | ===== Web References ===== | ||
- | |||
- | * **[[https:// | ||
- | * **[[https:// | ||
- | * **[[https:// | ||
doc/appunti/hardware/sonoff.1611158823.txt.gz · Last modified: 2021/01/20 17:07 by niccolo