Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
3.8 KiB

  1. # Filesystem
  2. ## Introduction
  3. Normally when you flash an ESP8266 you only flash the firmware, like for any other microcontroller. But the ESP8266 has plenty of room and normally it is split into different partitions. One such partition is used to store web files like a normal webserver.
  4. Problem is that, even thou the ESP8266 is a very capable microcontroller it has its limitations and does not handle very well concurrent connections. This is specially true for the *default* webserver that comes with the Arduino Core for ESP8266.
  5. On the other side, to provide a good user experience and work with a comfortable development environment you end up having different files: scripts, stylesheet files, images,... The browser will load the index.html file and quickly request for all those other files and the ESP8266 will easily struggle trying to serve them all.
  6. So the trick here is to squeeze them all into one single compressed file just before uploading it. Luckily, we can do that automatically.
  7. ## Web interface build process
  8. The build process reads the HTML files, looks for the stylesheet and script files linked there, grabs them, minifies them and includes them inline, in the same order they are loaded. The resulting single HTML file is then cleaned, further minified and compressed resulting in a single index.html.gz file. This way the ESP8266 webserver can serve it really fast.
  9. To build this file we are using **[Gulp][1]**, a build system built in [node.js][2]. So you will need node (and [npm][3], its package manager) first. [Read the documentation][4] on how to install them.
  10. Once you have node and npm installed, go to the 'code' folder and install all the dependencies with:
  11. ```
  12. npm install
  13. ```
  14. It will take a minute or two. Then you are ready to build the webserver files with:
  15. ```
  16. gulp
  17. ```
  18. It will create a populate a 'data' folder with all the required files.
  19. ## Images
  20. Along with the HTML, CSS and JS files compressed into the index.html.gz file, the ESPurna firmware uses some images in its web interface. These are the favicon.ico file and a **sprite** for the iPhone style buttons in the interface. Again the idea is to use the minimum possible number of files.
  21. ## Flashing it
  22. ### Using PlatformIO
  23. [PlatformIO][5] allows the developer to define hooks to be executed before or after certain actions. This is really cool since we can plug one such hook to the *uploadfs* target to automatically build the web interface before uploading it to the board.
  24. This is done by specifying the script with the hook definitions in the *extra_script* option in the platformio.ini file. The script, written in python, binds the target file (spiffs.bin) to a method that executes the gulp command we have seen before.
  25. ```
  26. #!/bin/python
  27. from SCons.Script import DefaultEnvironment
  28. env = DefaultEnvironment()
  29. def before_build_spiffs(source, target, env):
  30. env.Execute("gulp buildfs")
  31. env.AddPreAction(".pioenvs/%s/spiffs.bin" % env['PIOENV'], before_build_spiffs)
  32. ```
  33. The included platformio.ini file has all this already configured, so you don't have to worry about it. Just type:
  34. ```
  35. pio run -t uploadfs -e sonoff
  36. ```
  37. (or whatever other enviroment) and you are good to go.
  38. ### Using Arduino IDE
  39. First you will have to manually build the data folder contents using gulp (see [instructions above](#web-interface-build-process)).
  40. Then you will have to have the "[ESP8266 Sketch Data Upload][6]" utility installed. Check the instructions in the previous link. The data folder should be a subfolder of the code folder for the tool to find it. Then just execute it and it will upload the data folder contents to your board.
  41. [1]: http://gulpjs.com/
  42. [2]: https://nodejs.org/en/
  43. [3]: https://www.npmjs.com/
  44. [4]: https://docs.npmjs.com/getting-started/installing-node
  45. [5]: http://www.platformio.org
  46. [6]: https://github.com/esp8266/Arduino/blob/master/doc/filesystem.md#uploading-files-to-file-system