Serial HTTP Server Example
The Gecko OS HTTP server allows a Gecko OS device to serve web pages from the device flash memory. This is the recommended solution for serving web pages. The HTTP server provided with Gecko OS is full featured and supports GET and POST requests, WebSockets and a REST API to the command API. The Gecko OS 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 Gecko OS device serial port and a TCP server running on the Gecko OS 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
pyserial) python module installed.
Connect the Gecko OS 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.format 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 <- SILABS-WGM160P-4.0.0, Gecko_OS-STANDARD-4.0.12-1198, WGM160P 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 Gecko OS 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 Gecko OS 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.
Supporting Gecko OS Editions and Versions
- Gecko OS 4+