Skip to content
Paul Sokolovsky edited this page Jan 16, 2018 · 8 revisions

Building

"socket" module is now available in upstream/master. Further "stabilized" changes are in pfalcon branch. The working branch is zephyr-wip.

  1. No pending changes to Zephyr, use Zephyr master.
  2. git clone https://github.com/pfalcon/micropython; git checkout zephyr-wip. The branch is based on the pfalcon branch. Use git pull --rebase to update (branch is rebased, on pfalcon branch as mentioned).
  3. Follow https://www.zephyrproject.org/doc/subsystems/networking/qemu_setup.html to bring up QEMU TAP/SLIP based local networking. To access Internet, you will need to setup NAT rules as described there too. Similar set up also applies to a real board set up (filter out QEMU specific parts from instructions).
  4. Setup Zephyr environment per its docs.
  5. cd micropython/ports/zephyr; make
  6. make run. If it doesn't start up, check your step 3.
  7. From another terminal, ping 192.0.2.1. If pings don't come thru, check your step 3.
  8. Switch back to QEMU terminal from step 6, and start playing!

Testing

Sample code, connecting to HTTP server on your local machine (Apache runs by default on many Linux machines, if not, install it):

import socket
s = socket.socket()
s.connect(("192.0.2.2", 80))
s.send(b"GET /foo HTTP/1.0\r\n\r\n")
s.recv(1000)

More advanced testing

For further testing, DNS server/proxy running on the host (192.0.2.2) is required. A typical Ubuntu system runs dnsmasq proxy, but it doesn't pick up newly created network interfaces, so you need to restart it explicitly:

service dnsmasq restart

Also, make sure that you set up masquerading iptable rules to allow access to Internet from Zephyr/QEMU: https://www.zephyrproject.org/doc/subsystems/networking/qemu_setup.html#setting-up-nat-masquerading-to-access-internet

HTTP Client testing

To test large (multi-megabyte) HTTP downloads from a real Internet server, run:

import test_dl

This will download, in loop, large file from http://archive.ubuntu.com, and check hash checksum of each attempt.

HTTP Server testing

To test HTTP server implemented in MicroPython using Zephyr sockets API, run:

import http_server

Then from host, access http://192.0.2.1:8080/ with a browser. The expected outcome is a page with text content:

Hello #0 from MicroPython!

with number incrementing on each page reload (may be not consecutive, as modern browser routinely issue more requests than asked by a user).

For more thorough testing, run Apache Bench:

ab -n1000 http://192.0.2.1:8080/

This will issue 1000 consecutive requests to the sample server. The expected outcome (shortened) is:

Complete requests:      1000
Failed requests:        992
   (Connect: 0, Receive: 0, Length: 992, Exceptions: 0)
Total transferred:      45896 bytes
HTML transferred:       28896 bytes

"Failed requests" are due to the fact that http_server generates dynamically changes content, while ab expected a static page. Make sure that only errors are "Length" ones.

Note: With a larger number of requests, e.g. 10000, there's a growing chance that http_server will fail. The likely cause is some race condition or non-POSIX handling of some edge condition on Zephyr side. It's subject for future investigation.

Clone this wiki locally