micro:bit and a Pi with a touch of Python

By now you should be aware of the Mu micropython editor for the micro:bit. Its a self-contained version of the same editor that can be found on the microbit.org

However, for use, it needs to be installed onto your system. But how do you use it when your in an environment where there is no internet or you would like to share it across a few systems?

A solution would be to have a local version of the online editor available ready for use on a webserver. This guide will show you how to do it.

Whats needed?

  • 1 x Raspberry Pi , unused computer or laptop to be configured as a webserver

    Any will do...my build used a Pi Zero.

  • 1 x WiFi hotspot

    Again any should be fine as it will handle the connection between the Raspberry Pi. The one that was used was an old random Wifi access point that was lying around.

So lets do this...

Sorting out the network

Power up the access point and login in to the admin console (you will need to refer to the manual for the chosen access point) and ensure that it is operating with no encryption. Make a note of the SSID or name it to something relevant. We'll come back to the access point later on.

Pi server?

To start with the Pi will need to be connected to a normal internet connection so that the required packages may be downloaded and installed.
The act as a webserver the nginx package will need to be installed via the terminal as follows:

sudo apt-get install nginx

Once installed, if you start the browser and browse to localhost you should be presented with the default nginx page. This will prove that the server is running.

Getting the editor file....

Next, we need to get the necessary files to allow a local microbit python editor to be used. To do this we will clone the repository that holds the files with git. This can be installed as follows. So, once more via the terminal type:

sudo apt-get install git

Next we need to clone the repository at https://github.com/bbcmicrobit/PythonEditor:

git clone git@github.com:bbcmicrobit/PythonEditor.git

This will clone a copy to your local filesystem. To fully enable the blockly interface the files will need to be downloaded. To do this automatically:

cd <path of directory git cloned PythonEditor to>

and then:

git submodule update --init --recursive

Git will then automatically clone the required modules into place.

Putting it all together

To put it all together have a look in the default configuration for nginx. We are looking for the webroot from where nginx will serve content. It's usually /var/www/html but to confirm this a quick peep of the configuration file is required.

Using your preferred text editor open the following file:

/etc/nginx/sites-available/default

and look for the following line which will be similar to:

root /var/www/html;

and make a note of the path mentioned after root.

Now we need to copy the contents of the PythonEditor directory into this path:

cp -R <path of directory git cloned PythonEditor to> /var/www/html

Browsing to localhost/editor.html in the browser on the Raspberry Pi should display the editor. This can be used the same as the version hosted on microbit.org

Once this is confirmed working we can connect the Pi to the access point. This should be a simple case of using the WiFi applet to connect to the access point configured earlier.

Once the Pi is connected to the access point the IP address of the Pi will need to be found. This can found a variety of ways:

  • In the WiFi applet
  • Using the ifconfig command in the terminal
  • In the access points configuration. (Note down the HWaddr in the output of ifconfig and search for it)
  • Using the nmap tool to search the network from your computer

After you have found it then you should be able to connect to it from another device connected to the same access point. To do this just enter the IP address of the Pi, for example 192.168.0.4/editor.html, into the browsers address bar and create some awesome code

Further ideas

  • Encrypt the WiFi connection
  • Read the nginx documentation to figure out how to autoload the editor.html file on connection
  • Read the html file and learn how it all works
  • Make the Pi into a hotspot and skip the Access Point