You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
12
12
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
14
18
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.
19
20
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.
Once the platform and libraries are downloaded the back end should successfully build within PlatformIO.
37
49
38
-
### Installing in PlatformIO
50
+
The firmware may be built by pressing the "Build" button:
39
51
40
-
Pull the project and add it to PlatformIO as a project folder (File > Add Project Folder).
52
+

41
53
42
-
PlatformIO should download the ESP8266 platform and the project library dependencies automatically.
54
+
Alternatively type the run command:
43
55
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:
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
49
81
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
+
```
51
85
52
86
#### Download and install the node modules
53
87
@@ -61,77 +95,163 @@ npm install
61
95
npm run build
62
96
```
63
97
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.
65
99
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:
**NB: To run the interface locally you will need to modify the endpoint root path and enable CORS.**
112
+
### Running the interface locally
73
113
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:
75
115
116
+
```bash
117
+
npm start
76
118
```
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
77
127
REACT_APP_ENDPOINT_ROOT=http://192.168.0.6/rest/
78
128
```
79
129
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
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.
81
168
82
169
## Building for different devices
83
170
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.
85
172
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
+

176
+
177
+
The settings file ['platformio.ini'](platformio.ini) configures the platform and board:
87
178
88
179
```
89
180
[env:esp12e]
90
181
platform = espressif8266
91
182
board = esp12e
92
183
```
93
184
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
+

188
+
189
+
Building for the common esp32 "node32s" board for example requires the following configuration:
95
190
96
191
```
97
192
[env:node32s]
98
193
platform = espressif32
99
194
board = node32s
100
195
```
101
196
102
-
Microcontroller ESP8266
103
-
Frequency 80MHz
104
-
Flash 4MBl
197
+
## Customizing and theming
105
198
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.
107
200
108
-
##Configuration & Deployment
201
+
### Theming the app
109
202
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:
111
204
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
+
consttheme=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
+
```
113
217
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
115
219
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.
122
221
123
-
The default settings configure the device to bring up an access point on start up which can be used to configure the device:
124
222
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)
127
226
128
-
## Software Overview
227
+
```js
228
+
REACT_APP_NAME=Funky IoT Project
229
+
```
129
230
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
131
249
132
250
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.
133
251
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:
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
<!doctype html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><linkrel="stylesheet" href="/css/roboto.css"><linkrel="manifest" href="/app/manifest.json"><title>ESP8266 React</title></head><body><noscript>You need to enable JavaScript to run this app.</noscript><divid="root"></div><scriptsrc="/js/1.b351.js"></script><scriptsrc="/js/2.9881.js"></script><scriptsrc="/js/0.da55.js"></script></body></html>
1
+
<!doctype html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><linkrel="stylesheet" href="/css/roboto.css"><linkrel="manifest" href="/app/manifest.json"><title>ESP8266 React</title></head><body><noscript>You need to enable JavaScript to run this app.</noscript><divid="root"></div><scriptsrc="/js/1.b351.js"></script><scriptsrc="/js/2.8ca9.js"></script><scriptsrc="/js/0.439a.js"></script></body></html>
0 commit comments