The Try Wren page allows you to execute Wren scripts directly in your browser, reducing the barrier to try language features and syntax, to zero. To emulate this simplicity in the IoT environment, the Constrained Application Protocol (CoAP) was leveraged to provide a lightweight RESTful protocol to a target board, the Adafruit Feather nRF52840 Express.

From the CoAP website, “The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained networks in the Internet of Things. The protocol is designed for machine-to-machine (M2M) applications such as smart energy and building automation.” The model is opposite from what might be expected, where the IoT device acts as the server and the client is often a powerful desktop machine. Again from the website, “Servers make resources available under a URL, and clients access these resources using methods such as GET, PUT, POST, and DELETE.”

In our example, the server exposes two endpoints:

coap://127.0.0.1/wren/about

coap://127.0.0.1/wren/try

The first endpoint is accessed with a GET and reports the WREN_VERSION_STRING.
The second endpoint emulates the “enter code below” entry field of the wren.io/try page via a POST.

To exercise these endpoints, the coap-client command line tool available with libcoap is utilized, much as wget can be used to interact with a web server. An example invocation is:

coap-client -e "var e = [1, 3]\ne.insert(1, 4)\nSystem.print(e)\n" -m post coap://127.0.0.1/wren/try

and the expected answer is returned:

[1, 4, 3]

It is possible to run the fractal example:

coap-client -e "for (yPixel in 0…13) {\n var y = yPixel / 12 - 1\n for
(xPixel in 0…30) {\n var x = xPixel / 30 - 2\n var x0 = x\n var y0 = y\n var
iter = 0\n while (iter < 11 && x0 * x0 + y0 * y0 <= 4) {\n var x1 = (x0 * x0)
- (y0 * y0) + x\n var y1 = 2 * x0 * y0 + y\n x0 = x1\n y0 = y1\n iter = iter
+ 1\n }\n System.write(\" .-:;+=xX$& \"[iter])\n }\n System.print(\"\")\n}\n"
-m post coap://127.0.0.1/wren/try

yielding:

       .......--------------
     ......-----------------
    ....--------------------
   ...----------------------
  ..----------------------::
  ..--------------------::::;
..----------------::::;;;;;;
.-----------:::::;;= x++++++
.-----:::::::::;;;++=$  &$X$
---:::::::::;;;;;;+==X$
-::::::::;;;;;;+=xxX$
::;;:::++++++==xX&

Of note 1) each statement is terminated with the newline character within the script string, including the last statement 2) the ranges in the fractal example are significantly shorter than in the web sample, to fit within the restricted buffer size.

Before attempting to host the server on an nRF52840 target, a toy server was configured under Ubuntu, using an implementation microcoap (https://github.com/1248/microcoap), Designed for microcontrollers, microcoap includes a simple endpoint model to add the necessary request handlers and felt appropriate for a proof of concept. microcoap explored the idea of using CoAP to interact with the peripherals of a target boards, using an Arduino sketch.

As the target Zephyr development model utilizes monolithic firmware images representing the application and operating system, the facility to dynamically load, manage and execute scripts over the air (OTA) on an onboard Wren VM would be useful.

The server code is available at uCoAPWren, and although built and debugged in JetBrains CLion, can be built at the command line as a regular CMake project.

In subsequent posts, we will explore how this code might run on a nRF52840 target and improvements available using a server based on libcoap rather than microcoap.