Serial HTTP Server Example
The ZentriOS HTTP server allows a ZentriOS device to serve web pages from the device flash memory. This is the recommended solution for serving web pages. The HTTP server provided with ZentriOS is full featured and supports GET and POST requests, WebSockets and a REST API to the command API. The ZentriOS file system can be extended to up to 128MB, allowing for storing a reasonably large collection of web pages. See File System, Internal, Extended and Bulk Flash.
However, in some host-driven applications, it may be preferable to serve web pages from host storage.
This application note demonstrates how to serve web pages from a script running on the host processor, via the ZentriOS device serial port and a TCP server running on the ZentriOS device.
The rudimentary HTTP server in the host script handles only GET requests. It parses a GET request received from a TCP server client, then generates the HTTP response and sends it back to the client. You can create a collection of web pages on the host system and configure the script to point to them and serve them out.
This application demonstrates setting up a TCP server, and using the stream_poll, stream_read and stream_write commands to respond to HTTP requests.
Requirements and Prerequisites
This application requires a host system running Python 2.7, with the
serial python module installed.
Connect the ZentriOS device to a host computer USB port, and note the host COM port associated with the USB connection.
Download the script serial_http_server.py.
Also download the sample web page, index.html.
Place both files in the same directory on your host system.
Modify the script to specify your web server root file. The configuration variables are at the top of the script.
In this demo, the root file is
index.html, so edit the
ROOT_INDEX configuration variable as follows:
ROOT_INDEX = 'index.html'
At a command prompt on your host system, run the
serial_http_server.py script supplying as an argument the host COM port to which the device USB port is connected:
python serial_http_server.py <DEVICE_USB_COM_PORT>
For example, if the host COM port connected to the device is COM9, run the script with:
python serial_http_server.py COM9
The script sets the required variables and reboots the device.
It displays status and instructions similar to the following:
Opening serial port: COM9 -> set system.cmd.mode machine <- R000008 <- Set OK -> set system.print_level all <- R000008 <- Set OK -> set bus.log_bus uart0 <- R000008 <- Set OK -> set network.buffer.size 25000 <- R000008 <- Set OK -> set network.buffer.rxtx_ratio 50 <- R000008 <- Set OK -> set bus.command.rx_bufsize 2048 <- R000008 <- Set OK -> set tcp.keepalive.enabled 0 <- R000008 <- Set OK -> set tcp.keepalive.initial_timeout 7 <- R000008 <- Set OK -> set tcp.keepalive.retry_count 3 <- R000008 <- Set OK -> set tcp.keepalive.retry_timeout 3 <- R000008 <- Set OK -> set tcp.server.auto_start 1 <- R000008 <- Set OK -> set tcp.server.auto_interface softap <- R000008 <- Set OK -> set tcp.server.port 80 <- R000008 <- Set OK -> set tcp.server.idle_timeout 30 <- R000008 <- Set OK -> set tcp.server.max_clients 4 <- R000008 <- Set OK -> set softap.auto_start 1 <- R000008 <- Set OK -> set softap.ssid "HTTP Server Demo" <- R000008 <- Set OK -> set softap.passkey "password" <- R000008 <- Set OK -> set softap.dns_server.url demo.com,www.demo.com <- R000008 <- Set OK -> set stream.auto_close 0 <- R000008 <- Set OK -> save <- R000009 <- Success -> reboot <- R000100 <- ZENTRI-AMW004_ZAP-188.8.131.52-beta, 2016-02-20T01:41:10Z, ZentriOS-WZ-184.108.40.206-beta, Board:AMW004-E03.3 Serial HTTP Server Ready ... Connect your computer to the Wi-Fi network: HTTP Server Demo Wi-Fi password is: password Once connected, open a browser and enter: demo.com This will display your webpages ...
Connect a client device such as a computer or smartphone to the ZentriOS device SoftAP using the ssid
HTTP Server Demo and the password
On the client, open a web browser and navigate to
The TCP script responds with the
index.html web page.
The main section of the script initialises the ZentriOS device by sending a series of set commands. It then goes into a loop, issuing the stream_poll all command to check for connected TCP clients with data available for processing.
It parses the status for each client from the poll results.
If client data is available, it issues the stream_read command to read the data and parses out any HTTP GET requests. If a request is found, it calls the function
sendHttpResponse, which issues the stream_write command to send the response back via the client stream.