What is Resource
Resources are some static files behind your application. These files could have any content, but most resource file types are:
- CSS, JS, HTML, JPG
- MP3
- LCD Images & Icons
- etc.
Most IoT devices have an internal web page for setup or usage. Storing these files in code is very hard. On the other hand, if you want to make a tiny change to an icon, you have to recompile the entire source code. Compiling codes is always risky. A tiny change in the MCU framework or a third-party library may make your device a lousy failure.
That's why we should store these files outside the main application. In ESP (ESP32 or ESP8266) microcontrollers, you can have a user file area to store these kinds of files. The SPIFFS and LittleFS libraries allow you to easily manage files on the main NAND flash memory.
Resource Service
The OTAdrive lets you create unlimited resources in your product's [Resources]
section. Each resource can have thousands of files and directories. You can assign none or one resource for each group. Then, all devices in the group will download and save the files in the resource.
When a device asks the server for the resources, it sends its serial number to the OTAdrive. The OTAdrive will return the list of files that exist in the resource of the group you've assigned. So, you can change the resources of all devices in a group with just a single click.
The device will check the list with local storage files. The list contains three columns: file name, MD5 checksum, and file path. If the file doesn't exist on the local storage (NAND flash in ESP) or has some differences (MD5 checksum discovers this) then it will download and save/replace to the specified path in the list.
Create Resource
The following window will show when you click on the [+]
button in the [Resources]
section of the product. You should enter a name for the resource and pick a directory for it. Selecting files could be single or a folder select (mass choose).
If you click on the directory, the folder pick dialog will shown, and some permission is required to complete the choose operation.
After choosing a file/files, the following window will show, and you can check whether all selected files are correct. You can either remove some files here.
Base Path
This parameter sets the resource files root path in the devices. For example if you set Base Path
to \staticfiles\web\
all file and folder will save into this path in the device.
e.g.
- Index.html →
\staticfiles\web\index.html
- favicon.ico →
\staticfiles\web\favicon.ico
- css\app.css →
\staticfiles\web\css\app.css
- js\main.js →
\staticfiles\web\js\app.css
Gzip
Gzip is a file format used for file compression and decompression. More info in Wikipedia.
If your resource file is for use in web browsers such as HTML, CSS, JS,... it's better to compress it to GZIP format. GZIP makes these files up to 10 times smaller. If you check the GZIP in the upload form, the file will compress, and (.gzip) will be added to the end of the file.
e.g.
- Index.html →
\staticfiles\web\index.html.gzip
- favicon.ico →
\staticfiles\web\favicon.ico.gzip
- css\app.css →
\staticfiles\web\css\app.css.gzip
- js\main.js →
\staticfiles\web\js\app.css.gzip
Assign Resource
Assign resources to a group just like you do it for firmware. You can open group assignments (Firmware,Config,Resource) from multiple forms. There are two common way.
- Go to
[Groups]
section and click one of the assignments badges.
- Go to
[Groups]
then open your group, click one of the assignments badges orChange
button.
Coding
In Arduino IDE, you can sync all local resources files with the server by simply calling the OTADRIVE.syncResources()
method. The following code shows you how to sync the resources in your program.
#include <otadrive_esp.h>
void doUpdate()
{
// sync SPIFFS files with the OTAdrive server
OTADRIVE.syncResources();
// get new firmware if available
OTADRIVE.updateFirmware();
}