Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
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.

287 lines
10 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. # ESP8266 React
  2. A simple, extensible framework for IoT projects built on ESP8266/ESP32 platforms with responsive React front-end.
  3. Designed to work with the PlatformIO IDE with limited setup, please read below for build and flash instructions.
  4. ![Screenshots](/media/screenshots.png?raw=true "Screenshots")
  5. ## Features
  6. Provides many of the features required for IoT projects:
  7. * Configurable WiFi - Network scanner and WiFi connection screen
  8. * Configurable Access Point - Can be continuous or automatically enabled when WiFi connection fails
  9. * Network Time - Synchronization with NTP
  10. * Remote Firmware Updates - Enable secured OTA updates
  11. * Security - Protected RESTful endpoints and a secured user interface
  12. The back end is provided by a set of RESTful endpoints and the React based front end is responsive and scales well to various screen sizes.
  13. The front end has the prerequisite manifest file and icon, so it can be added to the home screen of a mobile device if required.
  14. ## Getting Started
  15. ### Prerequisites
  16. You will need the following before you can get started.
  17. * [PlatformIO](https://platformio.org/) - IDE for development
  18. * [Node.js](https://nodejs.org) - For building the interface with npm
  19. * Bash shell, or [Git Bash](https://gitforwindows.org/) if you are under windows
  20. ### Building in PlatformIO
  21. Pull the project and open it in PlatformIO. PlatformIO should download the ESP8266 platform and the project library dependencies automatically.
  22. The project directory structure is as follows:
  23. Directory | Description
  24. ---- | -----------
  25. /data/ | The file system image directory
  26. /interface/ | React based front end
  27. /src/ | C++ back end for the ESP8266 device
  28. /platformio.ini | PlatformIO project configuration file
  29. Once the platform and libraries are downloaded the back end should be compiling.
  30. > **WINDOWS BUILDS**: If building under Windows you need to delete .piolibdeps/Time/Time.h - due to a [file system case insensitivity issue](https://github.com/me-no-dev/ESPAsyncWebServer/issues/96)
  31. ### Building the interface
  32. The interface has been configured with create-react-app and react-app-rewired so the build can customized for the target device. The large artefacts are gzipped and source maps and service worker are excluded from the production build.
  33. Change to this directory with your bash shell (or Git Bash) and use the standard commands you would with any react app built with create-react-app:
  34. #### Change to interface directory
  35. ```bash
  36. cd interface
  37. ```
  38. #### Download and install the node modules
  39. ```bash
  40. npm install
  41. ```
  42. #### Build the interface
  43. ```bash
  44. npm run build
  45. ```
  46. > **Note**: The build command will also delete the previously built interface (the ['data/www'](data/www) directory) and replace it with the freshly built one, ready for upload to the device.
  47. #### Running the interface locally
  48. You can run a local development server during development to preview changes to the front end them without uploading a file system image to the device.
  49. ```bash
  50. npm start
  51. ```
  52. > **Note**: To run the interface locally you will need to modify the endpoint root path and enable CORS.
  53. The endpoint root path can be found in .env.development, defined as the environment variable 'REACT_APP_ENDPOINT_ROOT'. This needs to be the root URL of the device running the back end, for example:
  54. ```js
  55. REACT_APP_ENDPOINT_ROOT=http://192.168.0.6/rest/
  56. ```
  57. CORS can be enabled on the back end by uncommenting the -D ENABLE_CORS build flag in platformio.ini and re-deploying.
  58. ## Customizing and theming
  59. The framework, and MaterialUI allows for a good degree of custoimzation with little effort.
  60. ### Theming the app
  61. The app can be easily themed by editing the [MaterialUI theme](https://material-ui.com/customization/themes/). Edit the theme in ./interface/src/App.js as you desire:
  62. ```js
  63. const theme = createMuiTheme({
  64. palette: {
  65. primary: red,
  66. secondary: deepOrange,
  67. highlight_idle: blueGrey[900],
  68. highlight_warn: orange[500],
  69. highlight_error: red[500],
  70. highlight_success: green[500],
  71. },
  72. });
  73. ```
  74. ### Changing the app icon
  75. You can replace the app icon is located at [./interface/public/app/icon.png](interface/public/app/icon.png) with one of your preference. A 256 x 256 PNG is recommended for best compatibility.
  76. ### Changing the app name
  77. The app name displayed on the login page and on the menu bar can be modified by editing the REACT_APP_NAME property in [./interface/.env](interface/.env)
  78. ```js
  79. REACT_APP_NAME=Funky IoT Project
  80. ```
  81. There is also a manifest file which contains the app name to use when adding the app to a mobile device, so you may wish to also edit [./interface/public/app/manifest.json](interface/public/app/manifest.json):
  82. ```json
  83. {
  84. "name":"Funky IoT Project",
  85. "icons":[
  86. {
  87. "src":"/app/icon.png",
  88. "sizes":"48x48 72x72 96x96 128x128 256x256"
  89. }
  90. ],
  91. "start_url":"/",
  92. "display":"fullscreen",
  93. "orientation":"any"
  94. }
  95. ```
  96. ## Building for different devices
  97. This project supports ESP8266 and ESP32 platforms. To support OTA programming, enough free space to upload the new sketch and file system image will be required. It is recommended that a board with at least 2mb of flash is used.
  98. By default, the target device is "esp12e". This is a common ESP8266 variant with 4mb of flash:
  99. ![ESP12E](/media/esp12e.jpg?raw=true "ESP12E")
  100. The settings file platformio.ini configures the platform and board:
  101. ```
  102. [env:esp12e]
  103. platform = espressif8266
  104. board = esp12e
  105. ```
  106. If you want to build for an ESP32 device, all you need to do is re-configure playformio.ini with your devices settings.
  107. ![ESP32](/media/esp32.jpg?raw=true "ESP32")
  108. Building for the common esp32 "node32s" board for example requires the following configuration:
  109. ```
  110. [env:node32s]
  111. platform = espressif32
  112. board = node32s
  113. ```
  114. ## Configuration & Deployment
  115. Standard configuration settings, such as build flags, libraries and device configuration can be found in platformio.ini. See the [PlatformIO docs](http://docs.platformio.org/en/latest/projectconf.html) for full details on what you can do with this.
  116. ***** TODO ******
  117. The project is configured to upload via serial by default, you can change the upload mechanism to OTA by uncommenting the relevant lines.
  118. As well as containing the interface, the SPIFFS image (in the ./data folder) contains a JSON settings file for each of the configurable features. The config files can be found in the ./data/config directory:
  119. File | Description
  120. ---- | -----------
  121. apSettings.json | Access point settings
  122. ntpSettings.json | NTP synchronization settings
  123. otaSettings.json | OTA update configuration
  124. securitySettings.json | Security settings and user credentials
  125. wifiSettings.json | WiFi connection settings
  126. The default settings configure the device to bring up an access point on start up which can be used to configure the device:
  127. * SSID: ESP8266-React
  128. * Password: esp-react
  129. ## Software Overview
  130. ### Back End
  131. The back end is a set of REST endpoints hosted by a [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer) instance. The source is split up by feature, for example [WiFiScanner.h](src/WiFiScanner.h) implements the end points for scanning for available networks.
  132. There is an abstract class [SettingsService.h](src/SettingsService.h) that provides an easy means of adding configurable services/features to the device. It takes care of writing the settings as JSON to SPIFFS. All you need to do is extend the class with your required configuration and implement the functions which serialize the settings to/from JSON. JSON serialization utilizes the excellent [ArduinoJson](https://github.com/bblanchon/ArduinoJson) library. Here is a example of a service with username and password settings:
  133. ```cpp
  134. #include <SettingsService.h>
  135. class ExampleSettingsService : public SettingsService {
  136. public:
  137. ExampleSettingsService(AsyncWebServer* server, FS* fs)
  138. : SettingsService(server, fs, "/exampleSettings", "/config/exampleSettings.json") {}
  139. ~ExampleSettingsService(){}
  140. protected:
  141. void readFromJsonObject(JsonObject& root) {
  142. _username = root["username"] | "";
  143. _password = root["password"] | "";
  144. }
  145. void writeToJsonObject(JsonObject& root) {
  146. root["username"] = _username;
  147. root["password"] = _password;
  148. }
  149. private:
  150. String _username;
  151. String _password;
  152. };
  153. ```
  154. Now this can be constructed, added to the server, and started as such:
  155. ```cpp
  156. ExampleSettingsService exampleSettingsService = ExampleSettingsService(&server, &SPIFFS);
  157. exampleSettingsService.begin();
  158. ```
  159. There will now be a REST service exposed on "/exampleSettings" for reading and writing (GET/POST) the settings. Any modifications will be persisted in SPIFFS, in this case to "/config/exampleSettings.json"
  160. Sometimes you need to perform an action when the settings are updated, you can achieve this by overriding the onConfigUpdated() function which gets called every time the settings are updated. You can also perform an action when the service starts by overriding the begin() function, being sure to call SettingsService::begin():
  161. ```cpp
  162. void begin() {
  163. // make sure we call super, so the settings get read!
  164. SettingsService::begin();
  165. reconfigureTheService();
  166. }
  167. void onConfigUpdated() {
  168. reconfigureTheService();
  169. }
  170. void reconfigureTheService() {
  171. // do whatever is required to react to the new settings
  172. }
  173. ```
  174. ### Front End
  175. The front end is a bit of a work in progress (as are my react skills), but it has been designed to be a "mobile first" interface and as such should feel very much like an App.
  176. I've tried to keep the use of libraries to a minimum to reduce the artefact size (it's about 150k gzipped ATM).
  177. ## Future Improvements
  178. - [x] Reduce boilerplate in interface
  179. - [ ] Provide an emergency config reset feature, via a pin held low for a few seconds
  180. - [x] Access point should provide captive portal
  181. - [ ] Perhaps have more configuration options for Access point: IP address, Subnet, etc
  182. - [ ] Enable configurable mDNS
  183. - [x] Introduce authentication to secure the device
  184. ## Libraries Used
  185. * [React](https://reactjs.org/)
  186. * [Material-UI](https://material-ui-next.com/)
  187. * [Time](https://github.com/PaulStoffregen/Time)
  188. * [NtpClient](https://github.com/gmag11/NtpClient)
  189. * [ArduinoJson](https://github.com/bblanchon/ArduinoJson)
  190. * [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer)