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.

327 lines
11 KiB

6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. # ESP8266 React
  2. [![Build Status](https://travis-ci.org/rjwats/esp8266-react.svg?branch=master)](https://travis-ci.org/rjwats/esp8266-react)
  3. A simple, secure and extensible framework for IoT projects built on ESP8266/ESP32 platforms with responsive React front-end.
  4. Designed to work with the PlatformIO IDE with [limited setup](#getting-started). Please read below for setup, build and upload instructions.
  5. ![Screenshots](/media/screenshots.png?raw=true "Screenshots")
  6. ## Features
  7. Provides many of the features required for IoT projects:
  8. * Configurable WiFi - Network scanner and WiFi configuration screen
  9. * Configurable Access Point - Can be continuous or automatically enabled when WiFi connection fails
  10. * Network Time - Synchronization with NTP
  11. * Remote Firmware Updates - Enable secured OTA updates
  12. * Security - Protected RESTful endpoints and a secured user interface
  13. 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.
  14. 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.
  15. ## Getting Started
  16. ### Prerequisites
  17. You will need the following before you can get started.
  18. * [PlatformIO](https://platformio.org/) - IDE for development
  19. * [Node.js](https://nodejs.org) - For building the interface with npm
  20. * Bash shell, or [Git Bash](https://gitforwindows.org/) if you are under windows
  21. ### Building and uploading the firmware
  22. Pull the project and open it in PlatformIO. PlatformIO should download the ESP8266 platform and the project library dependencies automatically.
  23. The project structure is as follows:
  24. Resource | Description
  25. ---- | -----------
  26. [data/](data) | The file system image directory
  27. [interface/](interface) | React based front end
  28. [src/](src) | C++ back end for the ESP8266 device
  29. [platformio.ini](platformio.ini) | PlatformIO project configuration file
  30. ### Building the firmware
  31. Once the platform and libraries are downloaded the back end should successfully build within PlatformIO.
  32. The firmware may be built by pressing the "Build" button:
  33. ![build](/media/build.png?raw=true "build")
  34. Alternatively type the run command:
  35. ```bash
  36. platformio run
  37. ```
  38. #### Uploading the firmware
  39. The project is configured to upload over a serial connection by default. You can change this to use OTA updates by uncommenting the relevant lines in ['platformio.ini'](platformio.ini).
  40. The firmware may be uploaded to the device by pressing the "Upload" button:
  41. ![uploadfw](/media/uploadfw.png?raw=true "uploadfw")
  42. Alternatively run the 'upload' target:
  43. ```bash
  44. platformio run -t upload
  45. ```
  46. ### Building the interface
  47. 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. This reduces the production build to around ~200k, which easily fits on the device.
  48. Change to the ['interface'](interface) directory with your bash shell (or Git Bash) and use the standard commands you would with any react app built with create-react-app:
  49. #### Change to interface directory
  50. ```bash
  51. cd interface
  52. ```
  53. #### Download and install the node modules
  54. ```bash
  55. npm install
  56. ```
  57. #### Build the interface
  58. ```bash
  59. npm run build
  60. ```
  61. > **Note**: The build command will also delete the previously built interface, in the ['data/www'](data/www) directory, replacing it with the freshly built one ready to upload to the device.
  62. #### Uploading the file system image
  63. The compiled user interface may be uploaded to the device by pressing the "Upload File System image" button:
  64. ![uploadfs](/media/uploadfs.png?raw=true "uploadfs")
  65. Alternatively run the 'uploadfs' target:
  66. ```bash
  67. platformio run -t uploadfs
  68. ```
  69. ### Running the interface locally
  70. You can run a local development server to allow you preview changes to the front end without the need to upload a file system image to the device after each change. Change to the interface directory and run the following command:
  71. ```bash
  72. npm start
  73. ```
  74. > **Note**: To run the interface locally you will need to modify the endpoint root path and enable CORS.
  75. #### Changing the endpoint root
  76. The endpoint root path can be found in ['interface/.env.development'](interface/.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:
  77. ```js
  78. REACT_APP_ENDPOINT_ROOT=http://192.168.0.6/rest/
  79. ```
  80. #### Enabling CORS
  81. You can enable CORS on the back end by uncommenting the -D ENABLE_CORS build flag in ['platformio.ini'](platformio.ini) then re-building and uploading the firmware to the device. The default settings assume you will be accessing the development server on the default port on [http://localhost:3000](http://localhost:3000) this can also be changed if required:
  82. ```
  83. -D ENABLE_CORS
  84. -D CORS_ORIGIN=\"http://localhost:3000\"
  85. ```
  86. ## Device Configuration
  87. As well as containing the interface, the SPIFFS image (in the ['data'](data) folder) contains a JSON settings file for each of the configurable features. The config files can be found in the ['data/config'](data/config) directory:
  88. File | Description
  89. ---- | -----------
  90. [apSettings.json](data/config/apSettings.json) | Access point settings
  91. [ntpSettings.json](data/config/ntpSettings.json) | NTP synchronization settings
  92. [otaSettings.json](data/config/otaSettings.json) | OTA update configuration
  93. [securitySettings.json](data/config/securitySettings.json) | Security settings and user credentials
  94. [wifiSettings.json](data/config/wifiSettings.json) | WiFi connection settings
  95. ### Access point settings
  96. The default settings configure the device to bring up an access point on start up which can be used to configure the device:
  97. * SSID: ESP8266-React
  98. * Password: esp-react
  99. ### Security settings and user credentials
  100. The security settings and user credentials provide the following users by default:
  101. Username | Password
  102. -------- | --------
  103. admin | admin
  104. guest | guest
  105. It is recommended that you change the JWT secret and user credentials from their defaults protect your device. You can do this in the user interface, or by modifying [securitySettings.json](data/config/securitySettings.json) before uploading the file system image.
  106. ## Building for different devices
  107. 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.
  108. By default, the target device is "esp12e". This is a common ESP8266 variant with 4mb of flash:
  109. ![ESP12E](/media/esp12e.jpg?raw=true "ESP12E")
  110. The settings file ['platformio.ini'](platformio.ini) configures the platform and board:
  111. ```
  112. [env:esp12e]
  113. platform = espressif8266
  114. board = esp12e
  115. ```
  116. If you want to build for an ESP32 device, all you need to do is re-configure ['platformio.ini'](platformio.ini) with your devices settings.
  117. ![ESP32](/media/esp32.jpg?raw=true "ESP32")
  118. Building for the common esp32 "node32s" board for example requires the following configuration:
  119. ```
  120. [env:node32s]
  121. platform = espressif32
  122. board = node32s
  123. ```
  124. ## Customizing and theming
  125. The framework, and MaterialUI allows for a reasonable degree of customization with little effort.
  126. ### Theming the app
  127. 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'](interface/src/App.js) as you desire:
  128. ```js
  129. const theme = createMuiTheme({
  130. palette: {
  131. primary: red,
  132. secondary: deepOrange,
  133. highlight_idle: blueGrey[900],
  134. highlight_warn: orange[500],
  135. highlight_error: red[500],
  136. highlight_success: green[500],
  137. },
  138. });
  139. ```
  140. ### Changing the app icon
  141. 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.
  142. ### Changing the app name
  143. 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)
  144. ```js
  145. REACT_APP_NAME=Funky IoT Project
  146. ```
  147. 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):
  148. ```json
  149. {
  150. "name":"Funky IoT Project",
  151. "icons":[
  152. {
  153. "src":"/app/icon.png",
  154. "sizes":"48x48 72x72 96x96 128x128 256x256"
  155. }
  156. ],
  157. "start_url":"/",
  158. "display":"fullscreen",
  159. "orientation":"any"
  160. }
  161. ```
  162. ## Back End Overview
  163. 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.
  164. 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.
  165. Here is a example of a service with username and password settings:
  166. ```cpp
  167. #include <SettingsService.h>
  168. class ExampleSettingsService : public SettingsService {
  169. public:
  170. ExampleSettingsService(AsyncWebServer* server, FS* fs)
  171. : SettingsService(server, fs, "/exampleSettings", "/config/exampleSettings.json") {}
  172. ~ExampleSettingsService(){}
  173. protected:
  174. void readFromJsonObject(JsonObject& root) {
  175. _username = root["username"] | "";
  176. _password = root["password"] | "";
  177. }
  178. void writeToJsonObject(JsonObject& root) {
  179. root["username"] = _username;
  180. root["password"] = _password;
  181. }
  182. private:
  183. String _username;
  184. String _password;
  185. };
  186. ```
  187. Now this can be constructed, added to the server, and started as such:
  188. ```cpp
  189. ExampleSettingsService exampleSettingsService = ExampleSettingsService(&server, &SPIFFS);
  190. exampleSettingsService.begin();
  191. ```
  192. 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"
  193. 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():
  194. ```cpp
  195. void begin() {
  196. // make sure we call super, so the settings get read!
  197. SettingsService::begin();
  198. reconfigureTheService();
  199. }
  200. void onConfigUpdated() {
  201. reconfigureTheService();
  202. }
  203. void reconfigureTheService() {
  204. // do whatever is required to react to the new settings
  205. }
  206. ```
  207. ## Libraries Used
  208. * [React](https://reactjs.org/)
  209. * [Material-UI](https://material-ui-next.com/)
  210. * [Time](https://github.com/PaulStoffregen/Time)
  211. * [NtpClient](https://github.com/gmag11/NtpClient)
  212. * [ArduinoJson](https://github.com/bblanchon/ArduinoJson)
  213. * [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer)