doc:appunti:hardware:iot
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
doc:appunti:hardware:iot [2021/01/20 12:23] – [Configuring MQTT on Tasmota] niccolo | doc:appunti:hardware:iot [2021/01/20 17:07] – niccolo | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== Internet of Things: Teckin Smart Plug SP22 ====== |
- | + | ||
- | ===== Teckin Smart Plug SP22 ===== | + | |
Di queste prese intelligenti marchiate **Teckin** ne esistono almeno due versioni. Le più vecchie si basano sul chip **ESP** | Di queste prese intelligenti marchiate **Teckin** ne esistono almeno due versioni. Le più vecchie si basano sul chip **ESP** | ||
Line 20: | Line 18: | ||
* Un dispositivo, | * Un dispositivo, | ||
* Appena si **rimuove una device dalla app**, il Cloud invia al device stesso l' | * Appena si **rimuove una device dalla app**, il Cloud invia al device stesso l' | ||
- | |||
==== Smart Life App ==== | ==== Smart Life App ==== | ||
Line 188: | Line 185: | ||
State (bool, true is ON) True | State (bool, true is ON) True | ||
</ | </ | ||
+ | |||
==== Tasmota and Tuya-Convert ==== | ==== Tasmota and Tuya-Convert ==== | ||
Line 194: | Line 192: | ||
* **[[https:// | * **[[https:// | ||
* **[[https:// | * **[[https:// | ||
- | |||
- | ===== Sonoff Basic R2 ===== | ||
- | |||
- | Il vantaggio di quesi **power switch intelligenti** marchiati **Sonoff**, è che sono facilmente programmabili con il firmware **[[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. | ||
- | |||
- | {{.: | ||
- | {{.: | ||
- | |||
- | {{.: | ||
- | {{.: | ||
- | |||
- | ==== 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 === | ||
- | |||
- | Default Debian installation of the **mosquitto** server does not require a password from the connecting client. | ||
- | |||
- | **/ | ||
- | |||
- | < | ||
- | user username1 | ||
- | topic write gateway/ | ||
- | topic read gateway/ | ||
- | |||
- | user username2 | ||
- | topic readwrite gateway/ | ||
- | </ | ||
- | |||
- | **/ | ||
- | |||
- | < | ||
- | allow_anonymous false | ||
- | password_file / | ||
- | acl_file / | ||
- | </ | ||
- | |||
- | To manage a password file you can use the **mosquitto_passwd** tool, this command will set an **username** with a **passowrd** (asked interactively) into the **password file** (add a **%%-c%%** option to create the password file): | ||
- | |||
- | < | ||
- | mosquitto_passwd / | ||
- | </ | ||
- | |||
- | The file will contains **sha512** hashes. | ||
- | |||
- | ===== 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:// | ||