Skip to content

Commit 5a94ad9

Browse files
authored
Merge pull request #29 from rjwats/ft_user_security
Ft user security
2 parents 49eb17b + 1bcb51e commit 5a94ad9

File tree

94 files changed

+3060
-990
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+3060
-990
lines changed

README.md

Lines changed: 174 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
# ESP8266 React
22

3-
A simple, extensible framework for getting up and running with the ESP8266/ESP32 microchip and a react front end.
3+
A simple, secure and extensible framework for IoT projects built on ESP8266/ESP32 platforms with responsive React front-end.
44

5-
Designed to work with the PlatformIO IDE with limited setup.
5+
Designed to work with the PlatformIO IDE with [limited setup](#getting-started). Please read below for setup, build and upload instructions.
66

7-
This project supports ESP8266 and ESP32 devices, see build instruction below for more details.
7+
![Screenshots](/media/screenshots.png?raw=true "Screenshots")
88

9-
## Why I made this project
9+
## Features
1010

11-
I found I was repeating a lot of work when starting new IoT projects with the ESP8266 chip.
11+
Provides many of the features required for IoT projects:
1212

13-
Most of my IoT projects have required:
13+
* Configurable WiFi - Network scanner and WiFi configuration screen
14+
* Configurable Access Point - Can be continuous or automatically enabled when WiFi connection fails
15+
* Network Time - Synchronization with NTP
16+
* Remote Firmware Updates - Enable secured OTA updates
17+
* Security - Protected RESTful endpoints and a secured user interface
1418

15-
* Configurable WiFi
16-
* Configurable access point
17-
* Synchronization with NTP
18-
* The ability to perform OTA updates
19+
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.
1920

20-
I also wanted to adopt a decent client side framework so the back end could be simplified to a set of REST endpoints.
21-
22-
All of the above features are included in this framework, which I plan to use as a basis for my IoT projects.
23-
24-
The interface is responsive and should work well on mobile devices. It also has the prerequisite manifest/icon file, so it can be added to the home screen if desired.
25-
26-
![Screenshots](/screenshots/screenshots.png?raw=true "Screenshots")
21+
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.
2722

2823
## Getting Started
2924

@@ -32,22 +27,61 @@ The interface is responsive and should work well on mobile devices. It also has
3227
You will need the following before you can get started.
3328

3429
* [PlatformIO](https://platformio.org/) - IDE for development
35-
* [NPM](https://www.npmjs.com/) - For building the interface
36-
* Bash shell, or Git Bash if you are under windows
30+
* [Node.js](https://nodejs.org) - For building the interface with npm
31+
* Bash shell, or [Git Bash](https://gitforwindows.org/) if you are under windows
32+
33+
### Building and uploading the firmware
34+
35+
Pull the project and open it in PlatformIO. PlatformIO should download the ESP8266 platform and the project library dependencies automatically.
36+
37+
The project structure is as follows:
38+
39+
Resource | Description
40+
---- | -----------
41+
[data/](data) | The file system image directory
42+
[interface/](interface) | React based front end
43+
[src/](src) | C++ back end for the ESP8266 device
44+
[platformio.ini](platformio.ini) | PlatformIO project configuration file
45+
46+
### Building the firmware
47+
48+
Once the platform and libraries are downloaded the back end should successfully build within PlatformIO.
3749

38-
### Installing in PlatformIO
50+
The firmware may be built by pressing the "Build" button:
3951

40-
Pull the project and add it to PlatformIO as a project folder (File > Add Project Folder).
52+
![build](/media/build.png?raw=true "build")
4153

42-
PlatformIO should download the ESP8266 platform and the project library dependencies automatically.
54+
Alternatively type the run command:
4355

44-
Once the platform and libraries are downloaded the back end should be compiling.
56+
```bash
57+
platformio run
58+
```
59+
60+
#### Uploading the firmware
61+
62+
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).
63+
64+
The firmware may be uploaded to the device by pressing the "Upload" button:
65+
66+
![uploadfw](/media/uploadfw.png?raw=true "uploadfw")
67+
68+
Alternatively run the 'upload' target:
69+
70+
```bash
71+
platformio run -t upload
72+
```
4573

4674
### Building the interface
4775

48-
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.
76+
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.
77+
78+
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:
79+
80+
#### Change to interface directory
4981

50-
You will find the interface code in the ./interface directory. 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:
82+
```bash
83+
cd interface
84+
```
5185

5286
#### Download and install the node modules
5387

@@ -61,77 +95,163 @@ npm install
6195
npm run build
6296
```
6397

64-
**NB: The build command will also delete the previously built interface (the ./data/www directory) and replace it with the freshly built one, ready for upload to the device.**
98+
> **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.
6599
66-
#### Running the interface locally
100+
#### Uploading the file system image
101+
102+
The compiled user interface may be uploaded to the device by pressing the "Upload File System image" button:
103+
104+
![uploadfs](/media/uploadfs.png?raw=true "uploadfs")
105+
106+
Alternatively run the 'uploadfs' target:
67107

68108
```bash
69-
npm start
109+
platformio run -t uploadfs
70110
```
71111

72-
**NB: To run the interface locally you will need to modify the endpoint root path and enable CORS.**
112+
### Running the interface locally
73113

74-
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:
114+
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:
75115

116+
```bash
117+
npm start
76118
```
119+
120+
> **Note**: To run the interface locally you will need to modify the endpoint root path and enable CORS.
121+
122+
#### Changing the endpoint root
123+
124+
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:
125+
126+
```js
77127
REACT_APP_ENDPOINT_ROOT=http://192.168.0.6/rest/
78128
```
79129

80-
CORS can be enabled on the back end by uncommenting the -D ENABLE_CORS build flag in platformio.ini and re-deploying.
130+
#### Enabling CORS
131+
132+
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:
133+
134+
```
135+
-D ENABLE_CORS
136+
-D CORS_ORIGIN=\"http://localhost:3000\"
137+
```
138+
139+
## Device Configuration
140+
141+
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:
142+
143+
File | Description
144+
---- | -----------
145+
[apSettings.json](data/config/apSettings.json) | Access point settings
146+
[ntpSettings.json](data/config/ntpSettings.json) | NTP synchronization settings
147+
[otaSettings.json](data/config/otaSettings.json) | OTA update configuration
148+
[securitySettings.json](data/config/securitySettings.json) | Security settings and user credentials
149+
[wifiSettings.json](data/config/wifiSettings.json) | WiFi connection settings
150+
151+
### Access point settings
152+
153+
The default settings configure the device to bring up an access point on start up which can be used to configure the device:
154+
155+
* SSID: ESP8266-React
156+
* Password: esp-react
157+
158+
### Security settings and user credentials
159+
160+
The security settings and user credentials provide the following users by default:
161+
162+
Username | Password
163+
-------- | --------
164+
admin | admin
165+
guest | guest
166+
167+
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.
81168

82169
## Building for different devices
83170

84-
This project supports ESP8266 and ESP32 platforms however your target device will need at least a 1MB flash chip to support OTA programming.
171+
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.
85172

86-
By default this project is configured to build for the esp12e device. This is an esp8266 device with 4MB of flash. The following config in platformio.ini configures the build:
173+
By default, the target device is "esp12e". This is a common ESP8266 variant with 4mb of flash:
174+
175+
![ESP12E](/media/esp12e.jpg?raw=true "ESP12E")
176+
177+
The settings file ['platformio.ini'](platformio.ini) configures the platform and board:
87178

88179
```
89180
[env:esp12e]
90181
platform = espressif8266
91182
board = esp12e
92183
```
93184

94-
If you want to build for an ESP32 device, all you need to do is re-configure playformio.ini with your devices settings:
185+
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.
186+
187+
![ESP32](/media/esp32.jpg?raw=true "ESP32")
188+
189+
Building for the common esp32 "node32s" board for example requires the following configuration:
95190

96191
```
97192
[env:node32s]
98193
platform = espressif32
99194
board = node32s
100195
```
101196

102-
Microcontroller ESP8266
103-
Frequency 80MHz
104-
Flash 4MBl
197+
## Customizing and theming
105198

106-
**NB: If building under Windows you need to delete .piolibdeps/Time/Time.h - due [filesystem case insensitivity](https://github.com/me-no-dev/ESPAsyncWebServer/issues/96)*
199+
The framework, and MaterialUI allows for a good degree of custoimzation with little effort.
107200

108-
## Configuration & Deployment
201+
### Theming the app
109202

110-
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.
203+
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:
111204

112-
By default, the target device is "esp12e". This is a common ESP8266 variant with 4mb of flash though any device with at least 2mb of flash should be fine. The settings configure the interface to upload via serial by default, you can change the upload mechanism to OTA by uncommenting the relevant lines.
205+
```js
206+
const theme = createMuiTheme({
207+
palette: {
208+
primary: red,
209+
secondary: deepOrange,
210+
highlight_idle: blueGrey[900],
211+
highlight_warn: orange[500],
212+
highlight_error: red[500],
213+
highlight_success: green[500],
214+
},
215+
});
216+
```
113217

114-
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:
218+
### Changing the app icon
115219

116-
File | Description
117-
---- | -----------
118-
apSettings.json | Access point settings
119-
ntpSettings.json | NTP synchronization settings
120-
otaSettings.json | OTA Update configuration
121-
wifiSettings.json | WiFi connection settings
220+
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.
122221

123-
The default settings configure the device to bring up an access point on start up which can be used to configure the device:
124222

125-
* SSID: ESP8266-React
126-
* Password: esp-react
223+
### Changing the app name
224+
225+
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)
127226

128-
## Software Overview
227+
```js
228+
REACT_APP_NAME=Funky IoT Project
229+
```
129230

130-
### Back End
231+
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):
232+
233+
```json
234+
{
235+
"name":"Funky IoT Project",
236+
"icons":[
237+
{
238+
"src":"/app/icon.png",
239+
"sizes":"48x48 72x72 96x96 128x128 256x256"
240+
}
241+
],
242+
"start_url":"/",
243+
"display":"fullscreen",
244+
"orientation":"any"
245+
}
246+
```
247+
248+
## Back End Overview
131249

132250
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.
133251

134-
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:
252+
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.
253+
254+
Here is a example of a service with username and password settings:
135255

136256
```cpp
137257
#include <SettingsService.h>
@@ -195,21 +315,6 @@ void reconfigureTheService() {
195315

196316
```
197317

198-
### Front End
199-
200-
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.
201-
202-
I've tried to keep the use of libraries to a minimum to reduce the artefact size (it's about 150k gzipped ATM).
203-
204-
## Future Improvements
205-
206-
- [x] Reduce boilerplate in interface
207-
- [ ] Provide an emergency config reset feature, via a pin held low for a few seconds
208-
- [x] Access point should provide captive portal
209-
- [ ] Perhaps have more configuration options for Access point: IP address, Subnet, etc
210-
- [ ] Enable configurable mDNS
211-
- [ ] Introduce authentication to secure the device
212-
213318
## Libraries Used
214319

215320
* [React](https://reactjs.org/)

data/config/ntpSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"server":"pool.ntp.org",
3-
"interval":60
3+
"interval":3600
44
}

data/config/securitySettings.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"jwt_secret":"esp8266-react",
3+
"users": [
4+
{
5+
"username": "admin",
6+
"password": "admin",
7+
"admin": true
8+
},
9+
{
10+
"username": "guest",
11+
"password": "guest",
12+
"admin": false
13+
}
14+
]
15+
}

data/www/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><link rel="stylesheet" href="/css/roboto.css"><link rel="manifest" href="/app/manifest.json"><title>ESP8266 React</title></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script src="/js/1.b351.js"></script><script src="/js/2.9881.js"></script><script src="/js/0.da55.js"></script></body></html>
1+
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><link rel="stylesheet" href="/css/roboto.css"><link rel="manifest" href="/app/manifest.json"><title>ESP8266 React</title></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script src="/js/1.b351.js"></script><script src="/js/2.8ca9.js"></script><script src="/js/0.439a.js"></script></body></html>

data/www/js/0.439a.js.gz

10.9 KB
Binary file not shown.

data/www/js/0.da55.js.gz

-7.45 KB
Binary file not shown.

data/www/js/2.8ca9.js.gz

154 KB
Binary file not shown.

data/www/js/2.9881.js.gz

-134 KB
Binary file not shown.

interface/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
REACT_APP_NAME=ESP8266 React

interface/.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
REACT_APP_ENDPOINT_ROOT=http://192.168.0.4/rest/
1+
REACT_APP_ENDPOINT_ROOT=http://192.168.0.11/rest/

0 commit comments

Comments
 (0)