Difference between revisions of "Swagbadge2021 UpdatingSoftware"

From Open Hardware Miniconf
Jump to: navigation, search
(Saving your private key)
Line 149: Line 149:
 
* Python is very particular about spacing. The rest of the code uses 4 spaces for indents so follow suit.
 
* Python is very particular about spacing. The rest of the code uses 4 spaces for indents so follow suit.
  
= Reboot to shell / Force runaway code to stop =
+
 
 +
= Software development =
 +
 
 +
== Aiko ==
 +
The Aiko Engine is what runs after boot (FreeRTOS, then microPython). Aiko provides a framework for developing applications, by providing higher level abstractions over the basic hardware, events, messaging and other conveniences. Basically, a lot of house keeping that you'd end up writing yourself. After boot, there are two background threads, one looking after Wi-Fi connectivity and the other looking after MQTT connectivity. 
 +
There are also three event handlers: MQTT keep-alive, firmware upgrader and the work-in-progress SwagBadge application handler.
 +
 
 +
<pre>
 +
>>> aiko.event.event_list.print()
 +
<function swagbadge_handler at 0x3ffe6c70> every 5000 next 23099
 +
<function upgrade_handler at 0x3ffedfa0> every 5000 next 23099
 +
<function mqtt_ping_handler at 0x3ffe9820> every 60000 next 73672
 +
</pre>
 +
That last handler, which every 5 seconds is updating the title bar "LCA2021" <--> "SwagBadge" (see https://github.com/geekscape/aiko_engine_mp/blob/master/applications/swagbadge.py ).
 +
 
 +
You can stop a handler, like this (this will stop the top bar status updater):
 +
<pre>
 +
>>> import applications.swagbadge
 +
>>> aiko.event.remove_timer_handler(application.swagbadge_handler)
 +
</pre>
 +
 
 +
== Replacing the swagbadge application with your own ==
 +
 
 +
Edit configuration/main.py and set "application": "application/yourcode"
 +
 
 +
 
 +
== Pushing new code ==
 +
Occasionally you might find that the updated source code that you just used the mpfshell "put" command to transfer isn't properly stored on the ESP32 microPython flash filesystem. After rebooting, you might see unexpected errors. A simple way to check is to use the mpfshell "cat" command to check that the file was completely transferred. 
 +
 
 +
The safest way to update code is to perform a reboot after transferring all the files. Doing a soft-reboot using Control-D doesn't take long, i.e it is faster and easier than doing a hard-reboot with the reset button. The good thing about a reboot (microPython interpreter restart) is that there is no confusion about the complete state of your system, i.e what references have been held onto from your older source code that might still be running in a thread or as an event handler (via the Aiko Engine framework).
 +
 
 +
You can also delete all running modules:
 +
<pre>
 +
import sys
 +
sys.modules.clear()
 +
</pre>
 +
 
 +
More details in https://spectrum.chat/lca2021-swagbadge/software/safe-software-updates~ba541e3c-d8a2-41a8-ac47-71d87696de2d
 +
 
 +
== Reboot to shell / Force runaway code to stop ==
  
 
It's possible to write code that will leave your badge unresponsive. Rebooting just reruns the same bad code. What to do?
 
It's possible to write code that will leave your badge unresponsive. Rebooting just reruns the same bad code. What to do?
Line 157: Line 196:
  
 
The badge will restart but not autorun any code, just drop you back at the repl prompt. From there you can <code>put</code> bugfixed code back onto the device.
 
The badge will restart but not autorun any code, just drop you back at the repl prompt. From there you can <code>put</code> bugfixed code back onto the device.
 +
 +
See https://github.com/geekscape/aiko_engine_mp/blob/a5b5593d37509df64a3dcb4cdc3d13a411152d82/main.py#L20

Revision as of 22:07, 16 January 2021

Updating the software framework

Swagbadge owners might want to update the software framework (Aiko). Dagbadge owners will need to put the framework onto their badge before they can use it.

Here's how to do that!

Requirements

  • #swagbadge or #dagbadge or any ESP32 with an OLED screen
  • Micro-USB cable
  • Linux, Mac OS or Linux system running Python 3
  • Command line tools: git

Setting up your Windows 10 system for connecting to badge hardware

How to find your serial COM port on Windows:

  • Use the Device Manager.
  • Under the "Ports" category, you'll see one or more entries. Hopefully one of which is your badge! It will tell you what Port it is on.
  • If you can't see your badge, you might need to go up to the View menu and use "Show hidden devices" and see if that helps.

Windows 10 may automatically discover your #swagbadge USB hardware and automatically install the correct USB serial hardware driver, or if nothing still shows up, you might need to update your USB drivers for supporting the USB hardware CP210x on your #swagbadge, please follow these instructions:

By this point, you have know the serial COM port that your #swagbadge is connected to, e.g COM4

Setting up your Mac OS system for connecting to #swagbadge hardware

How to find your serial COM port on OS X:

   $ ls /dev/tty.usb*

Unless you have lot of dev boards (or maybe a phone) plugged in you should just see a single filename there. Use this (without the `/dev/` prefix) when using the `open` command in `mpfshell`

To get a working `mpfshell` install a recent version of Python (Python3.8 is known to work) using a installed DMG. Then create a *venv* and install the necessary tools using pip. For example:

   $ mkdir swag
   $ cd swag
   $ python3.8 -m venv env
   $ ./env/bin/pip install esptool mpfshell

You can then run `mpfshell` to connect:

   $ ./env/bin/mpfshell
   mpfs [/]> open <your device node as found previously>

Setting up your Linux system for connecting to #swagbadge hardware

[Link other instructions here]

Setting up your Windows 10 system for installing #swagbadge firmware

  • Install Miniconda3 to create a specific Python development environment. (Note we are using Python3 not Python2)
  • Create specific Python 3 environment for playing around with your #swagbadge
    • Start an Anaconda Powershell Prompt (aka Windows Terminal)
    • conda create --yes -n swagbadge python=3
    • conda activate swagbadge
  • Your command prompt should now look like: (swagbadge) C:\Users\[Username]>
  • Install some python tools to let us prepare and communicate with the badge
    • pip install esptool mpfshell
  • To test that the required tools are installed, trying running the following commands
    • esptool # Should show you help on running the esptool
    • mpfshell # Should show three lines of output
      • exit # To exit mpfshell

Download #swagbadge (Aiko Engine) firmware

Within your Anaconda Prompt session, change directory to where you’d like the #swagbadge software to be downloaded, for example:

  • cd $HOME/software

Download #swagbadge firmware:

Install MicroPython

  • Create a directory to hold the MicroPython firmware, such as aiko_engine_mp/firmware/
  • mkdir firmware
  • Using your web browser, download: esp32-idf4-20200902-v1.13.bin (mouse right button click → [Save As …] saving the MicroPython firmware in the firmware directory you just made.
    • The MicroPython download list has a link to a bunch of firmware drivers, we are using the one for the esp32 device, with the latest 1.4 generic

Erase #swagbadge LoLin-Lite ESP32 flash memory

  • esptool --chip esp32 --port COM3 erase_flash # adjust the port to suit.
  • You’ll know it’s worked, if the output finishes with “Hard resetting via RTS pin…”

Install microPython on the badge

  • esptool --chip esp32 --port COM3 --baud 460800 write_flash -z 0x1000 firmware\esp32-idf4-20200902-v1.13.bin #adjust port and firmware location if necessary
  • You’ll know it’s worked, if the output finishes with “Hard resetting via RTS pin…”

Test that the software is installed correctly by using mpfshell

  • mpfshell -o COM3 # Adjust port to suit
  • If it has made a connection to your badge, you will see it say
Connected to esp32
** Micropython File Shell v0.9.1, sw@kaltpost.de **
-- Running on Python 3.8 using PySerial 3.4 --
mpfs [/]> 

Using mpfshell

mpfshell has two functions: it lets you put files on/off the device, and it can give you a python shell to execute code on the device.

  • Getting files on/off the device is a bit like commandline ftp
    • ls - looks at the files on your micropython device
    • lls - looks at the files on your local computer in the current directory
    • put - puts a file to the device
    • cat - shows you the contents of the file
    • help() - gives you more information on the commands
    • repl - opens up the python shell on the badge

Note: To get out of repl, use Ctrl-Q (on Windows) which drops you back into mpfshell

A little program to say hello world!

  • >>> print("hello world")
  • Output: hello world

A little program to turn the blue light on the board on and off (this is on the underside of the Swagbadge, visible through a circular cutout)

>>> import machine
>>> pin22=machine.Pin(22, machine.Pin.OUT, machine.Pin.PULL_UP)
>>> pin22.value(0) 
>>> pin22.value(1)

Saving your private key

Use mpfshell to get the file: `configuration/keys.db`.

Putting the Aiko framework onto the device, using mpfshell

  • mpfshell COM5 -s scripts\aiko.mpf #run from within the aiko_framework_mp directory

Windows only: Patch mpfshell

Note! mpfshell needs a patch applied to let it run a script under Windows.

  • Inside C:/users/USERNAME/Miniconda2/envs/swagbadge/Lib/site-packages/mp
  • edit mpfshell.py
  • Search for elif args.script is not None:, around line 796
  • between that line and the next, insert two lines. It should look like:
    elif args.script is not None:

        if platform.system() == "Windows":  #INSERT THIS LINE
            mpfs.use_rawinput = True        #INSERT THIS LINE

        f = open(args.script, "r")

  • Python is very particular about spacing. The rest of the code uses 4 spaces for indents so follow suit.


Software development

Aiko

The Aiko Engine is what runs after boot (FreeRTOS, then microPython). Aiko provides a framework for developing applications, by providing higher level abstractions over the basic hardware, events, messaging and other conveniences. Basically, a lot of house keeping that you'd end up writing yourself. After boot, there are two background threads, one looking after Wi-Fi connectivity and the other looking after MQTT connectivity. There are also three event handlers: MQTT keep-alive, firmware upgrader and the work-in-progress SwagBadge application handler.

>>> aiko.event.event_list.print()
<function swagbadge_handler at 0x3ffe6c70> every 5000 next 23099
<function upgrade_handler at 0x3ffedfa0> every 5000 next 23099
<function mqtt_ping_handler at 0x3ffe9820> every 60000 next 73672

That last handler, which every 5 seconds is updating the title bar "LCA2021" <--> "SwagBadge" (see https://github.com/geekscape/aiko_engine_mp/blob/master/applications/swagbadge.py ).

You can stop a handler, like this (this will stop the top bar status updater):

>>> import applications.swagbadge
>>> aiko.event.remove_timer_handler(application.swagbadge_handler)

Replacing the swagbadge application with your own

Edit configuration/main.py and set "application": "application/yourcode"


Pushing new code

Occasionally you might find that the updated source code that you just used the mpfshell "put" command to transfer isn't properly stored on the ESP32 microPython flash filesystem. After rebooting, you might see unexpected errors. A simple way to check is to use the mpfshell "cat" command to check that the file was completely transferred.

The safest way to update code is to perform a reboot after transferring all the files. Doing a soft-reboot using Control-D doesn't take long, i.e it is faster and easier than doing a hard-reboot with the reset button. The good thing about a reboot (microPython interpreter restart) is that there is no confusion about the complete state of your system, i.e what references have been held onto from your older source code that might still be running in a thread or as an event handler (via the Aiko Engine framework).

You can also delete all running modules:

import sys
sys.modules.clear() 

More details in https://spectrum.chat/lca2021-swagbadge/software/safe-software-updates~ba541e3c-d8a2-41a8-ac47-71d87696de2d

Reboot to shell / Force runaway code to stop

It's possible to write code that will leave your badge unresponsive. Rebooting just reruns the same bad code. What to do?

  • Place a finger on each of the bottom slider pads, either side of the microprocessor.
  • Trigger a restart by using the button on the back of the badge, or Ctrl-D in repl (if you can get into repl)

The badge will restart but not autorun any code, just drop you back at the repl prompt. From there you can put bugfixed code back onto the device.

See https://github.com/geekscape/aiko_engine_mp/blob/a5b5593d37509df64a3dcb4cdc3d13a411152d82/main.py#L20