• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

front/web-demo/11-Mar-2024-418384

main/11-Mar-2024-431348

CMakeLists.txtD11-Mar-2024304 85

MakefileD11-Mar-2024489 1612

README.mdD11-Mar-20247.4 KiB13888

partitions_example.csvD11-Mar-2024306 76

sdkconfig.defaultsD11-Mar-2024303 98

README.md

1# HTTP Restful API Server Example
2
3(See the README.md file in the upper level 'examples' directory for more information about examples.)
4
5## Overview
6
7This example mainly introduces how to implement a RESTful API server and HTTP server on ESP32, with a frontend browser UI.
8
9This example designs several APIs to fetch resources as follows:
10
11| API                        | Method | Resource Example                                      | Description                                                                              | Page URL |
12| -------------------------- | ------ | ----------------------------------------------------- | ---------------------------------------------------------------------------------------- | -------- |
13| `/api/v1/system/info`      | `GET`  | {<br />version:"v4.0-dev",<br />cores:2<br />}        | Used for clients to get system information like IDF version, ESP32 cores, etc            | `/`      |
14| `/api/v1/temp/raw`         | `GET`  | {<br />raw:22<br />}                                  | Used for clients to get raw temperature data read from sensor                            | `/chart` |
15| `/api/v1/light/brightness` | `POST` | { <br />red:160,<br />green:160,<br />blue:160<br />} | Used for clients to upload control values to ESP32 in order to control LED’s brightness  | `/light` |
16
17**Page URL** is the URL of the webpage which will send a request to the API.
18
19### About mDNS
20
21The IP address of an IoT device may vary from time to time, so it’s impracticable to hard code the IP address in the webpage. In this example, we use the `mDNS` to parse the domain name `esp-home.local`, so that we can alway get access to the web server by this URL no matter what the real IP address behind it. See [here](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/protocols/mdns.html) for more information about mDNS.
22
23**Notes: mDNS is installed by default on most operating systems or is available as separate package.**
24
25### About deploy mode
26
27In development mode, it would be awful to flash the whole webpages every time we update the html, js or css files. So it is highly recommended to deploy the webpage to host PC via `semihost` technology. Whenever the browser fetch the webpage, ESP32 can forward the required files located on host PC. By this mean, it will save a lot of time when designing new pages.
28
29After developing, the pages should be deployed to one of the following destinations:
30
31* SPI Flash - which is recommended when the website after built is small (e.g. less than 2MB).
32* SD Card - which would be an option when the website after built is very large that the SPI Flash have not enough space to hold (e.g. larger than 2MB).
33
34### About frontend framework
35
36Many famous frontend frameworks (e.g. Vue, React, Angular) can be used in this example. Here we just take [Vue](https://vuejs.org/) as example and adopt the [vuetify](https://vuetifyjs.com/) as the UI library.
37
38## How to use example
39
40### Hardware Required
41
42To run this example, you need an ESP32 dev board (e.g. ESP32-WROVER Kit, ESP32-Ethernet-Kit) or ESP32 core board (e.g. ESP32-DevKitC). An extra JTAG adapter might also needed if you choose to deploy the website by semihosting. For more information about supported JTAG adapter, please refer to [select JTAG adapter](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/jtag-debugging/index.html#jtag-debugging-selecting-jtag-adapter). Or if you choose to deploy the website to SD card, an extra SD slot board is needed.
43
44#### Pin Assignment:
45
46Only if you deploy the website to SD card, then the following pin connection is used in this example.
47
48| ESP32  | SD Card |
49| ------ | ------- |
50| GPIO2  | D0      |
51| GPIO4  | D1      |
52| GPIO12 | D2      |
53| GPIO13 | D3      |
54| GPIO14 | CLK     |
55| GPIO15 | CMD     |
56
57
58### Configure the project
59
60Open the project configuration menu (`idf.py menuconfig`).
61
62In the `Example Connection Configuration` menu:
63
64* Choose the network interface in `Connect using`  option based on your board. Currently we support both Wi-Fi and Ethernet.
65* If you select the Wi-Fi interface, you also have to set:
66  * Wi-Fi SSID and Wi-Fi password that your esp32 will connect to.
67* If you select the Ethernet interface, you also have to set:
68  * PHY model in `Ethernet PHY` option, e.g. IP101.
69  * PHY address in `PHY Address` option, which should be determined by your board schematic.
70  * EMAC Clock mode, GPIO used by SMI.
71
72In the `Example Configuration` menu:
73
74* Set the domain name in `mDNS Host Name` option.
75* Choose the deploy mode in `Website deploy mode`, currently we support deploy website to host PC, SD card and SPI Nor flash.
76  * If we choose to `Deploy website to host (JTAG is needed)`, then we also need to specify the full path of the website in `Host path to mount (e.g. absolute path to web dist directory)`.
77* Set the mount point of the website in `Website mount point in VFS` option, the default value is `/www`.
78
79### Build and Flash
80
81After the webpage design work has been finished, you should compile them by running following commands:
82
83```bash
84cd path_to_this_example/front/web-demo
85npm install
86npm run build
87```
88
89After a while, you will see a `dist` directory which contains all the website files (e.g. html, js, css, images).
90
91Run `idf.py -p PORT flash monitor` to build and flash the project..
92
93(To exit the serial monitor, type ``Ctrl-]``.)
94
95See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
96
97### Extra steps to do for deploying website by semihost
98
99We need to run the latest version of OpenOCD which should support semihost feature when we test this deploy mode:
100
101```bash
102openocd-esp32/bin/openocd -s openocd-esp32/share/openocd/scripts -f board/esp32-wrover-kit-3.3v.cfg
103```
104
105## Example Output
106
107### Render webpage in browser
108
109In your browser, enter the URL where the website located (e.g. `http://esp-home.local`). You can also enter the IP address that ESP32 obtained if your operating system currently don't have support for mDNS service.
110
111Besides that, this example also enables the NetBIOS feature with the domain name `esp-home`. If your OS supports NetBIOS and has enabled it (e.g. Windows has native support for NetBIOS), then the URL `http://esp-home` should also work.
112
113![esp_home_local](https://dl.espressif.com/dl/esp-idf/docs/_static/esp_home_local.gif)
114
115### ESP monitor output
116
117In the *Light* page, after we set up the light color and click on the check button, the browser will send a post request to ESP32, and in the console, we just print the color value.
118
119```bash
120I (6115) example_connect: Connected to Ethernet
121I (6115) example_connect: IPv4 address: 192.168.2.151
122I (6325) esp-home: Partition size: total: 1920401, used: 1587575
123I (6325) esp-rest: Starting HTTP Server
124I (128305) esp-rest: File sending complete
125I (128565) esp-rest: File sending complete
126I (128855) esp-rest: File sending complete
127I (129525) esp-rest: File sending complete
128I (129855) esp-rest: File sending complete
129I (137485) esp-rest: Light control: red = 50, green = 85, blue = 28
130```
131
132## Troubleshooting
133
1341. Error occurred when building example: `...front/web-demo/dist doesn't exit. Please run 'npm run build' in ...front/web-demo`.
135   * When you choose to deploy website to SPI flash, make sure the `dist` directory has been generated before you building this example.
136
137(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.)
138