Python library modules for the micro:bit

Follows the API for the modules linkup.py and mqtt that implement the Linkup interface protocol written for the micro:bit.

API Documentation Module linkup
(Module import: from linkup import *)

connectAP(ssid, password)

connects to the existing access point (hotspot, router) with ssid and password. Returns the received dotted IP address; empty if login fails
createAP(ssid, password) creates an access point with ssid and password. If password is empty, the AP is open, i.e. you can log in without authentication
httpGet(url) executes an HTTP GET request and returns the response. url in the form "http://<server>?key=value&key=value&...". https can also be used instead of http
httpPost(url, content) executes an HTTP POST request and returns the response. url in the form "http://<server>". content in the format "key=value&key=value&...". https can also be used instead of http
httpDelete(url) executes an HTTP DELETE request with the given resource
startHTTPServer(onRequest)

starts an HTTP server (web server on port 80) that listens to HTTP GET requests. For a GET request, the user-defined callback function onRequest(clientIP, filename, params) is called.

clientIP: dotted IP address of the client
filename: filename of the URL with the prefix "/". Missing the filename: "/".
params: dictionary with the GET Request parameters {key: value}.

Example: For the URL http://192.168.0.101/on?a=ok&b=3:

filename = "/on" and params = {"a"" : "ok", "b" : "3"}

Return:

- a single value (string, float, integer): this is returned unchanged to the browser. It can be an HTML web page or a single value. The webpage must not be longer than 250 characters

- a list. The values contained therein are inserted into the % format specifications of the HTML standard file previously saved with saveHTML() and returned to the browser

- none: The HTML text file previously saved with saveHTML() is returned unchanged to the browser

All HTTP replies are provided with a header 200 OK.

The call is blocking. Only the callback function handler is active

saveHTML(text) saves the text on the LinkUp as a standard HTML file. It can contain % format specifications, which are replaced with the return values of the callback function handler. If text is empty, the HTML file is deleted

 

API Documentation Module mqtt
(Module import: import mqtt)

Function Action
mqtt.broker(host, port = 1883,
user = "", password = "", keepalive = 0)
determines the properties of the broker (IP address, IP port and if necessary authentication details). keepalive determines how long the connection remains open without data exchange (in sec) (default depends on the broker). No connection to the broker is established yet

mqtt.connectAP(ssid, password)

connects to the existing access point (hotspot, router) with ssid and password. Returns the received dotted IP address; empty if login fails
mqtt.connect(cleanSession = True) connects to the broker. For cleanSession = True, all previous data will be deleted. Returns True if the connection has been established; otherwise False
mqtt.ping() sends a ping request to the server to keep the connection open. Returns True if successful; otherwise False
mqtt.publish(topic, payload, retain = False, qos = 0) sends a message for the given topic (payload). If retain = True, this message is regarded as the last good/retain message. qos is the Quality of Service level (only 0, 1 supported). Returns True if successful; otherwise False
mqtt.subscribe(topic, qos = 0 subscribes the given topic with the given qos level (only 0, 1 supported). A maximum of 50 receiving message tuples (topic, payload) are stored one after the other in a message buffer (maximum lengths topic: 50, payload: 200 bytes). Returns True if successful; otherwise False
topic, payload = mqtt.receive() retrieves the first element of the message buffer (the "oldest") as a tuple (topic, payload) and removes the element from the buffer. If there is no data in the buffer, (None, None) is returned. The poll period should be at least 1 sec. Returns None, if the connection to the broker is lost
mqtt.disconnect() closes the connection


API Documentation Module ntptime

(Module import: import ntptime)

ntptime.getTimeRaw(server = "pool.ntp.org"

returns the current date_time delivered by the given server. Format: tuple (yyyy, mm, dd, h, m, s, week_day, day_of_year) all ints. Time is GMT
ntptime.getTime(server = "pool.ntp.org") same, but returns a formatted string (Example: "Tu 2019-06-11 13:04:44 GMT")

The source code is part of the LinkUp distribution found here. The module is copied to the micro:bit when the TigerJython flasher is used. The code is optimized to use as little memory as possible (no classes, few functions, short identifiers, no documentation).