Basic HTTP Server example

HTTP Server with custom URLs and actions

API Features Demonstrated

Platforms

This app works on:

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 RequestBehaviour
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 terminal
Sets 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

wlan_network_event_handler

get_params_request_processor (see get_params_request_processor.c)

This handles a standard GET request with URL parameters

json_generator_request_processor

json_parser_request_processor

set_light_request_processor

toggle_light_request_processor

Source

See:

See also: