Skip to main content

Programming Micro:bit remotely

The Micro:bit track of our local Coderdojo went virtual during a COVID-19 peak. This created a challenge: not all kids have Micro:bits at home, and the makecode Micro:bit simulator doesn't simulate all extensions and functions. We provided an e-mail gateway to remotely program Micro:bits, with a live webcam view of the running programs. Our solution runs on a Linux machine, which could be a regular PC or even a Raspberry Pi. Please make sure your Linux box can power the Micro:bits and the extensions you connect, or otherwise use a powered USB hub to connect them.

Create a mailbox 

We created a dedicated mailbox that is accessible via IMAP or POP3. In our case, we used a local mailserver, but an account at an external e-mail provider could be used as well. 

Install fetchmail and procmail 

Fetchmail can poll an IMAP or POP3 mailbox and save the fetched mails locally. Local delivery is done via procmail. Our fetchmail config is

#### .fetchmailrc


 set daemon 60

 set logfile fetchmail.log


 poll mailserver-FQDN proto POP3

  user "mail-username" pass "mail-password" is "pi" postconnect "/home/pi/microbit/munpack-microbit.sh"

  ssl

  fetchall

  no keep

  no rewrite

  mda "/usr/bin/procmail -f %F -d %T";


Our local user is "pi", and we poll our mailbox every 60 seconds. The postconnect script will unpack the attachments and write them to the Micro:bit (see below). We need the mail messages containing the Micro:bit programs as separate files, so we configure procmail to save them in Maildir format. Our .procmailrc

#.procmailrc.

#

SHELL=/bin/bash

LINEBUF=4096

PATH=/bin:/usr/bin:/usr/local/bin:/opt/local/bin

VERBOSE=off

MAILDIR=/home/pi/maildir

DEFAULT=$MAILDIR/inbox/ # See the slash!

LOGFILE=$HOME/procmaillog

FORMAIL=/usr/bin/formail

SENDMAIL=/usr/sbin/sendmail


## save everything in Maildir format in ~/maildir

#

:0

$MAILDIR/

This means the fetched mails will be saved as individual files in ~pi/maildir/new/

Install mpack 

The mpack package provides the munpack command, which saves attachments from mail files. We integrate unpack into a shell script that copies the attached .hex files to the Micro:bit USB mass storage device: munpack-microbit.sh

#!/bin/bash


ls ~pi/maildir/new/???* 2> /dev/null > /dev/null || exit 0


for mailfile in ~pi/maildir/new/*; do 

        munpack -C ~pi/microbit/ $mailfile ; 

        mv $mailfile ~pi/maildir/cur/ ;

done


disklist=""

for disk in $(echo "sda sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl sdm sdn sdo"); do

        [[ -L /sys/block/$disk ]] && [[ "$(cat /sys/block/$disk/size)" -le "131200" ]] && disklist="$disklist $disk"

done


for disk in $(echo $disklist); do

        if [[ -L /sys/block/$disk ]]; then

                #[[ "x$(cat /sys/block/$disk/size)" -eq "x16512" ]] || continue #flash size old Micro:bit firmware

                #[[ "x$(cat /sys/block/$disk/size)" -eq "x131200" ]] || continue #flash size new Micro:bit firmware

                [[ "$(cat /sys/block/$disk/size)" -le "131200" ]] || continue # only continue on small disks

                [[ -d /tmp/$disk ]] || mkdir /tmp/$disk

                [[ -e /tmp/$disk/MICROBIT.HTM ]] || sudo mount -t vfat /dev/$disk /tmp/$disk

        fi

done

ls ~pi/microbit/*.hex 2> /dev/null > /dev/null || exit 0


for hexfile in ~pi/microbit/*.hex ; do

        #echo $hexfile | grep -iq scroll && disklist="sdh sdi sdj" # use fixed disklist for scroll:bit

        #echo $hexfile | grep -iq halo && disklist="sdg" # use fixed disklist for Kitronik ZIP Halo

        [[ -d ~pi/microbit/used ]] || mkdir ~pi/microbit/used

        for disk in $(echo $disklist); do

                [[ -e /tmp/$disk/MICROBIT.HTM ]] && sudo cp $hexfile /tmp/$disk/

                sudo sync

        done

        mv $hexfile ~pi/microbit/used/

done

exit 0


Note that some actions like mount require root privileges, so the user running this (pi, in our case) needs passwordless sudo privileges. 

Run it

Now start fetchmail: fetchmail -f ~pi/.fetchmailrc . You can monitor the fetchmail log file (see fetchmailrc) using tail -f. Fetchmail will run as a daemon until you stop it using fetchmail -q. 


Comments