Basic HTTP Server example
HTTP Server with custom URLs and actions
- SDK directory:
apps/basic/http_server
- Zentri App Store:
ZENTRI-BAHTTPSERVER
API Features Demonstrated
- Event Handling API
- JSON Utility
- Network Interface API
- Protocols/HTTP Server API
- Peripheral/GPIO API
- Settings API
Platforms
This app works on:
- AMW004 - Zentri Mackerel evaluation board
- AMW106 - Zentri Moray evaluation board
- NXP-SCCK - NXP Secure Cloud Connectivity Kit
Requirements and Prerequisites
This app requires a ZentriOS evaluation board with a user LED.
Resources
The initialization file settings.ini is packaged into the .zap
file downloaded to the device.
The script request_issuer.py
(found in the ZentriOS_SDK/apps/http_server
directory) is used for the sample POST request.
Description
The http_server app demonstrates how to create custom URLs.
Usage Instructions
Open a ZentriOS serial terminal to the device. See Getting Started, Opening a ZentriOS Terminal.
This app assumes your ZentriOS device is set up with the credentials to join your local network. If you have not already set up network credentials, on the ZentriOS terminal, run the commands:
network_up -s
save
See the Wi-Fi Command API documentation for network_up and save.
After creating and running the app, connect a PC or mobile device to the same network as the ZentriOS device and open a browser to the URL mDNS name http://http_server.local. The device name is configured in the settings.ini file with mdns.name.
When the HTTP server receives a request for one of the registered custom URLs, the associated callback function executes. Note that HTTP callbacks execute in the HTTP server context, not in the ZentriOS app thread context.
When the app runs, the ZentriOS terminal output is:
> Success
HTTP Server example starting...
HTTP Server running...
Available custom URLs:
0: http://http_server.local/toggle_light
1: http://http_server.local/set_light/%s
2: http://http_server.local/get_params
3: http://http_server.local/json_parser
4: http://http_server.local/json_generator
Network down
Network up
If you are viewing this documentation with a browser connected to the same network as the ZentriOS device, you can click on the links below to verify the behavior of the app:
Link | Request | Behaviour |
---|---|---|
http://http_server.local/toggle_light | GET | Toggle User LED 1 |
http://http_server.local/set_light/1 | GET | Turn User LED 1 on |
http://http_server.local/set_light/0 | GET | Turn User LED 1 off |
http://http_server.local/get_params?msg=HelloZentriOS&light=on&retval=returnToSender | GET | Logs msg on terminalSets User LED 1 to value specified Displays retval and LED val in return HTML |
http://http_server.local/json_parser Try using the 'request_issuer.py' script to send a sample POST request. |
POST | Logs msg parameter to ZentriOS terminal.Returns HTML formatted LED value and Retval |
http://http_server.local/json_generator | GET | Display response with json formatted with GPIO values |
Implementation
The macros:
set up a table of custom URLs assigned to callback functions. Each of the callback functions is defined in a separate .c file. The custom URL function declarations are shared in the file common.h
.
See ZentriOS API documentation, HTTP Server Macros.
zn_app_init
- zn_load_app_settings sets up the required configuration. See settings.ini for details.
- zn_gpio_init initializes the user LED.
- The macro HTTP_SERVER_REGISTER_DYNAMIC_PAGES registers the callback functions associated with the custom URL structure. Each callback function is implemented in a separate file of the same name as the callback function.
- The
zn_app_idle
function returns ZOS_TRUE, so the event loop continues until the app is stopped. - zn_network_register_event_handler sets up an event handler for network connect/disconnect
wlan_network_event_handler
- logs connect/disconnect events to the ZentriOS terminal
get_params_request_processor
(see get_params_request_processor.c)
This handles a standard GET request with URL parameters
- Calls to zn_hs_get_param get the value of the specified parameters
- zn_gpio_set sets the LED GPIO on or off according to the parameter value
- Parameters are logged to the ZentriOS terminal
- With success, parameters are displayed in the web page response
- With failure, errors are displayed in thh web page response
json_generator_request_processor
json_parser_request_processor
set_light_request_processor
toggle_light_request_processor
Source
See:
See also: