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.

292 lines
10 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
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
5 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
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 upload 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 and uploading the firmware
  21. Pull the project and open it in PlatformIO. PlatformIO should download the ESP8266 platform and the project library dependencies automatically.
  22. The project structure is as follows:
  23. Resource | Description
  24. ---- | -----------
  25. [data/](data) | The file system image directory
  26. [interface/](interface) | React based front end
  27. [src/](src) | C++ back end for the ESP8266 device
  28. [platformio.ini](platformio.ini) | PlatformIO project configuration file
  29. #### Building the firmware
  30. Once the platform and libraries are downloaded the back end should be compiling.
  31. > **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)
  32. #### Uploading the firmware
  33. Standard configuration settings, such as build flags, libraries and device configuration can be found in ['platformio.ini'](platformio.ini). The project is configured to upload via serial by default, you can change the upload mechanism to OTA by uncommenting the relevant lines.
  34. See the [PlatformIO docs](http://docs.platformio.org/en/latest/projectconf.html) for full details on what you can do with this.
  35. Click the upload button in PlatformIO, or type the upload command if prefer the command line approach:
  36. ```bash
  37. platformio run -t upload
  38. ```
  39. ![upload](/media/upload.png?raw=true "upload")
  40. ### Building the interface
  41. 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.
  42. 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:
  43. #### Change to interface directory
  44. ```bash
  45. cd interface
  46. ```
  47. #### Download and install the node modules
  48. ```bash
  49. npm install
  50. ```
  51. #### Build the interface
  52. ```bash
  53. npm run build
  54. ```
  55. > **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.
  56. ### Running the interface locally
  57. You can run a local development server during development to preview changes to the front end them without the need to upload a file system image to the device after each change.
  58. ```bash
  59. npm start
  60. ```
  61. > **Note**: To run the interface locally you will need to modify the endpoint root path and enable CORS.
  62. #### Changing the endpoint root
  63. 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:
  64. ```js
  65. REACT_APP_ENDPOINT_ROOT=http://192.168.0.6/rest/
  66. ```
  67. #### Enabling CORS
  68. 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:
  69. ```
  70. -D ENABLE_CORS
  71. -D CORS_ORIGIN=\"http://localhost:3000\"
  72. ```
  73. ## Device Configuration
  74. 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:
  75. File | Description
  76. ---- | -----------
  77. [apSettings.json](data/config/apSettings.json) | Access point settings
  78. [ntpSettings.json](data/config/ntpSettings.json) | NTP synchronization settings
  79. [otaSettings.json](data/config/otaSettings.json) | OTA update configuration
  80. [securitySettings.json](data/config/securitySettings.json) | Security settings and user credentials
  81. [wifiSettings.json](data/config/wifiSettings.json) | WiFi connection settings
  82. The default settings configure the device to bring up an access point on start up which can be used to configure the device:
  83. * SSID: ESP8266-React
  84. * Password: esp-react
  85. ### Building for different devices
  86. 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.
  87. By default, the target device is "esp12e". This is a common ESP8266 variant with 4mb of flash:
  88. ![ESP12E](/media/esp12e.jpg?raw=true "ESP12E")
  89. The settings file ['platformio.ini'](platformio.ini) configures the platform and board:
  90. ```
  91. [env:esp12e]
  92. platform = espressif8266
  93. board = esp12e
  94. ```
  95. 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.
  96. ![ESP32](/media/esp32.jpg?raw=true "ESP32")
  97. Building for the common esp32 "node32s" board for example requires the following configuration:
  98. ```
  99. [env:node32s]
  100. platform = espressif32
  101. board = node32s
  102. ```
  103. ## Customizing and theming
  104. The framework, and MaterialUI allows for a good degree of custoimzation with little effort.
  105. ### Theming the app
  106. 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:
  107. ```js
  108. const theme = createMuiTheme({
  109. palette: {
  110. primary: red,
  111. secondary: deepOrange,
  112. highlight_idle: blueGrey[900],
  113. highlight_warn: orange[500],
  114. highlight_error: red[500],
  115. highlight_success: green[500],
  116. },
  117. });
  118. ```
  119. ### Changing the app icon
  120. 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.
  121. ### Changing the app name
  122. 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)
  123. ```js
  124. REACT_APP_NAME=Funky IoT Project
  125. ```
  126. 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):
  127. ```json
  128. {
  129. "name":"Funky IoT Project",
  130. "icons":[
  131. {
  132. "src":"/app/icon.png",
  133. "sizes":"48x48 72x72 96x96 128x128 256x256"
  134. }
  135. ],
  136. "start_url":"/",
  137. "display":"fullscreen",
  138. "orientation":"any"
  139. }
  140. ```
  141. ## Back End Overview
  142. 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.
  143. 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.
  144. Here is a example of a service with username and password settings:
  145. ```cpp
  146. #include <SettingsService.h>
  147. class ExampleSettingsService : public SettingsService {
  148. public:
  149. ExampleSettingsService(AsyncWebServer* server, FS* fs)
  150. : SettingsService(server, fs, "/exampleSettings", "/config/exampleSettings.json") {}
  151. ~ExampleSettingsService(){}
  152. protected:
  153. void readFromJsonObject(JsonObject& root) {
  154. _username = root["username"] | "";
  155. _password = root["password"] | "";
  156. }
  157. void writeToJsonObject(JsonObject& root) {
  158. root["username"] = _username;
  159. root["password"] = _password;
  160. }
  161. private:
  162. String _username;
  163. String _password;
  164. };
  165. ```
  166. Now this can be constructed, added to the server, and started as such:
  167. ```cpp
  168. ExampleSettingsService exampleSettingsService = ExampleSettingsService(&server, &SPIFFS);
  169. exampleSettingsService.begin();
  170. ```
  171. 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"
  172. 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():
  173. ```cpp
  174. void begin() {
  175. // make sure we call super, so the settings get read!
  176. SettingsService::begin();
  177. reconfigureTheService();
  178. }
  179. void onConfigUpdated() {
  180. reconfigureTheService();
  181. }
  182. void reconfigureTheService() {
  183. // do whatever is required to react to the new settings
  184. }
  185. ```
  186. ## Libraries Used
  187. * [React](https://reactjs.org/)
  188. * [Material-UI](https://material-ui-next.com/)
  189. * [Time](https://github.com/PaulStoffregen/Time)
  190. * [NtpClient](https://github.com/gmag11/NtpClient)
  191. * [ArduinoJson](https://github.com/bblanchon/ArduinoJson)
  192. * [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer)