Updated Python HTTP OTA Server (markdown)

Michael Ingraham 2019-10-06 06:53:55 -04:00
parent 33fc6a4409
commit 99db6ad14f
1 changed files with 70 additions and 89 deletions

@ -1,131 +1,112 @@
## Introduction
Sonoff-Tasmota firmware can be ugraded using 'Firmware Upgrade' option on main menu and selecting an OTA URL to fetch (and flash) a new firmware on the device.
Tasmota firmware can be upgraded using 'Firmware Upgrade' option on main menu and selecting an OTA URL to fetch (and flash) a new firmware on the device.
<p align="center">
<img src="https://github.com/arendst/arendst.github.io/blob/master/media/fw-upgrade-menu.png">
</p>
To use this nice feature users need to deploy an HTTP server with Apache, Nginx or other software solutions switching
to superuser privileges and installing additional packages (and libraries) on his own Linux/Windows box.
To use this feature, firmware files need to be hosted on an HTTP web server (e.g., http://thehackbox.org/tasmota). Alternatively, users can to deploy a local HTTP server with Apache, Nginx, or other software solutions.
Python makes available Flask (micro)framework (http://flask.pocoo.org) that can be used at different levels of complexity due to its modular architecture. A simple HTTP server (hello-world) can be developed only by *six* lines of Python code (http://flask.pocoo.org/docs/1.0/quickstart/#a-minimal-application).
Python makes available the [Flask (micro)framework](http://flask.pocoo.org) that can be used at different levels of complexity due to its modular architecture. [A simple HTTP server](http://flask.pocoo.org/docs/1.0/quickstart/#a-minimal-application) (hello-world) can be developed with only *six* lines of Python code.
A Flask application to offer Sonoff-Tasmota firmware images for OTA upgrades is available in 'tools' directory (https://github.com/arendst/Sonoff-Tasmota/tree/development/tools/fw_server/fw-server.py).
A Flask application to offer Tasmota firmware images for OTA upgrades is available as part of the available [tools](https://github.com/arendst/Sonoff-Tasmota/tree/development/tools/fw_server/).
## Requirements
* Switching to superuser privileges and installing additional packages (and libraries) on a Linux/Windows box.
* Python3 (follow instruction related to your operating system)
* netifaces and Flask libraries - can be installed by 'pip' package manager:
```
pip install netifaces flask
```
```
pip install netifaces flask
```
## Instructions:
Copy Tasmota firmware binary files in 'tools/fw' directory. A set of pre-built firmware binary files can be downloaded from the Tasmota [repository](http://thehackbox.org/tasmota).
Copy Sonoff-Tasmota firmware binary files in 'tools/fw' directory.
A set of prebuilt files can be downloaded by Sonoff-Tasmota release page:
* https://github.com/arendst/Sonoff-Tasmota/releases
Configure your Sonoff-Tasmota device with your fw-server URL:
Configure your Tasmota device with your fw-server URL:
```
Firmware Upgrade -> Upgrade by web server:
http://<ip_address>:5000/sonoff-minimal.bin
http://<ip_address>:5000/sonoff.bin
```
or use the following commands:
or use the following command:
```
otaurl http://<ip_address>:5000/sonoff-minimal.bin
upgrade 1
Backlog OTAURL http://<ip_address>:5000/sonoff.bin; Upgrade 1
```
## Usage:
## Usage
To start Python HTTP server:
```
python fw-server.py -d <net_iface> (default: eth0)
`python fw-server.py -d <net_iface> (default: eth0)`
or
python fw-server.py -i <ip_address>
```
`python fw-server.py -i <ip_address>`
Example:
```
python fw-server.py -d wlan0
`python fw-server.py -d wlan0`
or
python fw-server.py -i 192.168.1.1
```
`python fw-server.py -i 192.168.1.1`
## Note
On Windows operating system it is advisable to use '-i' option because Windows uses UUID name for network interfaces that are not so easy to type...
**Note:** On Windows it is advisable to use '-i' option because Windows uses UUID naming for network interfaces that are difficult to enter.
## Linux server:
If your MQTT broker is hosted on a local server, you may want Tasmota OTA to work the same server as a service. Here is how to achieve this:
If your MQTT broker is hosted on a local server, you may want Tasmota OTA to work the same, as a service. Here is how to achieve this:
- Copy the python script on the linux server
```
$ sudo su
# mkdir /srv/tasmota/fw_server/fw
# cd /srv/tasmota/fw_server
# wget https://github.com/arendst/Sonoff-Tasmota/tree/development/tools/fw_server/fw-server.py
```
- Copy the python script on the Linux server
```
$ sudo su
# mkdir /srv/tasmota/fw_server/fw
# cd /srv/tasmota/fw_server
# wget https://github.com/arendst/Sonoff-Tasmota/tree/development/tools/fw_server/fw-server.py
```
- Create a new file '/etc/systemd/system/tasmota.service' and copy those lines :
```
[Unit]
Description=Local OTA server for Tasmota
Requires=network.target
After=network.target multi-user.target
```
[Unit]
Description=Local OTA server for Tasmota
Requires=network.target
After=network.target multi-user.target
[Service]
#User=XYZ
Type=idle
ExecStart=/usr/bin/python /srv/tasmota/fw_server/fw-server.py -d wlan0
Restart=on-failure
[Service]
#User=XYZ
Type=idle
ExecStart=/usr/bin/python /srv/tasmota/fw_server/fw-server.py -d wlan0
Restart=on-failure
[Install]
WantedBy=multi-user.target
```
[Install]
WantedBy=multi-user.target
```
- User rights
We have created those files and directories as `root` but it is not desirable, the option `User=XYZ` should be uncommented and `XYZ` replaced by you usernane.
We have created those files and directories as `root` but it is not desirable, the option `User=XYZ` should be uncommented and `XYZ` replaced by you usernane.
In the following commands too, replace 4 time `XYZ` by your username :
```
# chown -hR XYZ:XYZ /srv/tasmota
# chown XYZ:XYZ /etc/systemd/system/tasmota.service
```
In the following commands too, replace 4 time `XYZ` by your username :
```
# chown -hR XYZ:XYZ /srv/tasmota
# chown XYZ:XYZ /etc/systemd/system/tasmota.service
```
- Run the service
```
# systemctl daemon-reload
# systemctl enable tasmota.service
# systemctl start tasmota.service
```
- Check the service is active and runnning.
```
# systemctl status tasmota.service
```
```
# systemctl daemon-reload
# systemctl enable tasmota.service
# systemctl start tasmota.service
```
- Check the service is active and running.
```
# systemctl status tasmota.service
```
- Test of the server
To feed the Linux server, we're now able to copy new firmware files in the folder `/srv/tasmota/fw_server/fw` with `XYZ` user rights. It can be done using `scp` or `samba` for instance. Upload `firmware.bin` into the folder. The service can be tested from any browser by issuing the address `http://<ip_address>:5000/firmware.bin` were `<ip_address>` is the address of the Linux server.
To feed the linux server, we're now able to copy new firmwares in the folder `/srv/tasmota/fw_server/fw` with `XYZ` user rights. It can be done using `scp` or `samba` for instance.
Let's upload `firmware.bin` into the folder, the service can be tested from any browser by issuing the address `http://<ip_address>:5000/firmware.bin` were `<ip_address>` is the adress of the linux server.
If you reboot the server, the service should startup again.
- After a power failure, local HTTP OTA server may become unresponsive
If your notice that the OTA mechanism is broken, try at first to download the served file from a browser. If you can't, then check the status of the service as said above. After a power failure, it may happen that your wlan0 IP resolves to an invalid value like `169.254.5.153` because your LAN gateway was not ready.
Login as root in a Terminal session and enter:
```
# systemctl restart tasmota.service
> systemctl status tasmota.service
```
if you recognize the IP of your server on the last status line, it should be OK now... OTA service restored!
If your notice that the OTA mechanism is broken, try at first to download the served file from a browser. If you can't, then check the status of the service as said above. After a power failure, your wlan0 IP may resolve to an invalid value like `169.254.5.153` because your LAN gateway was not ready.
Login as root in a Terminal session and enter:
```
$ sudo su
# systemctl restart tasmota.service
# systemctl status tasmota.service
```
If you recognize the IP address of your server on the last status line, it should be OK now... OTA service restored!
- PlatformIO
Now if you want PlatformIO to be able to upload your compiled binaries to the local server, you'll still have to setup `ssh` with `ssh-keygen` in order to use the Tasmota script `pio/sftp-uploader.py` without password.
If you want PlatformIO to be able to upload your compiled binaries to the local server, you'll still have to setup `ssh` with `ssh-keygen` in order to use the Tasmota script `pio/sftp-uploader.py` without password.