Cutipy Web Browser
This page outlines a basic guide to getting starting serving web pages from a Cutipy.
Contents
General Information
Running a web browser on the Cutipy is fairly simple, and can be accomplished by using the PicoWebSrv project. This project is primarily aimed at the EMAC CutiPy board with the RS9116 module onboard. PicoWebSrv allows serving of standard webpages (html, css, javascript), as well as hosting a simple REST API implementation.
With only one required file, it is very easy to integrate with new or existing projects.
- picoWebSrv.py - The Actual Web server
- picoMimetypes.py - (Optional) Full list of mimetypes for the Content-Type: header line
Currently the server only has support for GET and POST requests.
Developing with the Cutipy Web Server
PicoWebSrv has just one external MicroPython library requirement:
- logging.py - Note: This will be included in all standard EMAC MicroPython builds, starting with the next major release.
Basic Web Server
To run a basic web server from the Cutipy, use the following instructions. These instructions assume that you are storing your webpage files on an SD cart attached to the Cutipy.
-
Remove the SD card from the Cutipy, if already inserted.
-
Connect the SD card to the host PC.
-
Place web page files on the SD card in the www directory
-
Connect the Cutipy to the host PC via a USB cable. See The getting started page.
-
Create an empty file called SKIPSD in the base of the flash memory.
-
Edit boot.py in the flash memory and add the following lines.
import uos try: uos.mount(pyb.SDCard(), '/sd') except Exception: pass
-
Place the PicoWebSrv files in the base of the flash memory as well.
-
Disconnect the Cutipy, insert the SD card, then reconnect the Cutipy.
-
Run the following commands to start the server.
import network import picoWebSrv nic = network.RS9116() nic.init() nic.connect('SSID', 'PASSWORD') host_ip = nic.ifconfig()[0] pws = picoWebSrv.PicoWebSrv(host_ip, 80, file_location='/sd/www/') pws.start()
The web pages placed on the SD card will now be served at the IP of the Cutipy. The IP will be displayed on the Cutipy LCD if populated, it will also be printed on the REPL.
REST Server
To start a server with some REST API calls, preform the same steps as with the Basic Web Server, but stop before the last step. You will need to register some functions and endpoints for the API calls before starting the server. This process is illustrated below.
import network
import picoWebServer
import pyb
import ujson
def get_switch(var_list):
if var_list:
switch_num = var_list[0] # (e.g. '/get_switch/1')
else:
switch_num = 1
sw = pyb.Switch(switch_num)
if sw():
response_data = ujson.dumps({'status': 'pressed'})
else:
response_data = ujson.dumps({'status': 'not_pressed'})
return response_data.encode('utf-8')
def led_control(json_dict):
try:
led_num = json_dict['number']
except KeyError:
led_num = 1
led = pyb.LED(led_num)
try:
led_action = json_dict['action']
except KeyError:
led.toggle()
else:
if led_action == 'on':
led.on()
elif led_action == 'off':
led.off()
elif led_action == 'toggle':
led.toggle()
else:
raise ValueError
if led.intensity() == 255: # CutiPy reports 255 as off. LED pins sink current, instead of source.
response_data = ujson.dumps({'status': 'off'})
else:
response_data = ujson.dumps({'status': 'on'})
return response_data.encode('utf-8')
nic = network.RS9116()
nic.init()
nic.connect('SSID', 'PASSWORD')
host_ip = nic.ifconfig()[0]
pws = picoWebSrv(host_ip, 80, file_location='/sd/www/')
pws.add_rest_function('GET', '/get_sw', get_switch)
pws.add_rest_function('POST', 'led_ctrl', led_control)
pws.start()
This will start a server with an API call listening for GET at /get_sw and a call listening for POST at led_ctrl. For more information about these calls, see the library documentation here.
Pages with Related Content
CutiPy Micropython Documentation