3. Wubby VM Tutorial

Wubby VM is installed in the heart of an embedded device, the microcontroller, and provides a runtime environment with built-in Python support so that you can easily program your embedded device as simply as writing a Python script. A Wubby enabled embedded device is the platform where Wubby VM is installed.

3.1. Installing the Wubby VM

In case you have just acquired a new embedded device and you wish to make it Wubby enabled, you will have to install the Wubby VM.

First of all you will have to download the binary Wubby VM (mp_prog) and the corresponding bootloader (mp_bootloader) that suits your platform from here: Broadcom BCM943362WCD4 Wi-Fi module , Murata Type YD Wi-Fi module

To install them on your platform, there are two options:

  1. Using the Wubby IDE (see Installing the Wubby VM for detailed instructions)
  2. Using the WICED SDK and the tools it provides:

Windows

  1. Download the flash.bat script
  2. Open the Windows Command Prompt
  3. Run the script giving as input the directory where the Wubby VM image is stored and the WICED SDK directory
> flash.bat C:\Users\user\Desktop\WICED C:\Users\user\Desktop\mp_prog C:\Users\user\Desktop\mp_bootloader

Linux

  1. Download the flash.sh script
  2. Start a terminal
  3. Run the script giving as input the directory where the Wubby VM image is stored and the WICED SDK directory
$ ./flash.sh /home/user/WICED /home/user/Downloads/mp_prog /home/user/Downloads/mp_bootloader

3.2. Running your first application

In order to run a python application on any Wubby enabled embedded device, you only need to create a main.py file, write your application and upload it to the device.

3.2.1. A “Hello World” application

Below, you can find a vary simple python script that prints the “Hello Wubby World” message every five seconds.

import time

while(True):
   print ("Hello Wubby World")
   time.sleep(5000)

Using the Wubby IDE create a new project under the name MainApp and copy the above lines in the main.py file that is created (see section The Wubby IDE for detailed instructions on how to use the Wubby IDE).

There are two ways to upload an application to a Wubby enabled device: either through the Wubby Cloud or through the Wubby IDE.

The Wubby Cloud can be operated using a Wubby Client. Wubby Client can be either a web page or a smartphone application.

To access the browser version of the Wubby Client please point your browser to http://5.189.168.89:8080/wubby and refer to section An example of the application update process using the Wubby Client (Developer role), for detailed instructions regarding the application upload process through the Wubby Cloud. It is advised to first have a quick look to the complete section 4 so that you can understand the various Wubby Cloud operations and user roles.

For detailed insructions regarding the application upload process through the Wubby IDE please refer to section Cloud Installation.

Note

In the current version of the Wubby VM, the python application keeps being executed repeatedly, so it is strongly recommended to add a “while(True)” loop at the end of it.

3.2.2. A more complex application

Below, you can find a more complex python script that demonstrates how the hardware peripherals of your Wubby enabled device can be handled using python, and also how you can update a feed with values measured in your application.

Note

Depending on your platform, check the related Supported Evaluation Boards section for the pinout assignment.

# main.py -- put your code here!

import time
from hardware import adc, gpio
import feed

## Create a new feed object
testFeed = feed.feed("testFeed","testDatastream")

## Create a new gpio object
led = gpio(11, gpio.GPIO_MODE_OUTPUT_PULLDOWN)

led.write(0)

## Create a new adc object
analog = adc(1)

## Initialize the new object
analog.init(1000)

while True:
    ## Read Value
    val = analog.read()

    print ("value : " + str(val))

    ## Update the "testFeed" feed with the measured value
    testFeed.update(val)

    led.write(1)

    time.sleep(4000)

    led.write(0)

The above application reads a value from the selected ADC peripheral and updates the Live Feed of the Wubby Cloud with the measured value (see the Log File section for more information regarding the Live Feed). It also turns ON and OFF a selected led.

As you can see, the hardware peripherals are represented as classes of the hardware module.

from hardware import adc, gpio

See hardware.gpio, hardware.adc for detailed description.

The first step in our application is to create the necessary objects and initialize them. More specifically, we first create a new led, using the GPIO with pin id 1, which is defined it as output, and we write logical level 0 on it:

## Create a new gpio object
led = gpio(11, gpio.GPIO_MODE_OUTPUT_PULLDOWN)

led.write(0)

Then we create our analog object, using the ADC peripheral, which is initialized with sampling rate 1000:

## Create a new adc object
analog = adc(1)

## Initialize the new object
analog.init(1000)

Inside the while loop we read the value of the adc and perform the rest actions of our application.

You can either update the MainApp project created in the previous paragraph or create a new project. Then you can use either the Wubby IDE or the Wubby Cloud to upload the application to your Wubby enabled device.

3.3. Import custom modules

For a better structure of our application, we can separate the code in several modules which are imported in our main application, as in usual python applications. The files of these modules have to be in the same directory as our main.py file, ie under the MainApp folder.

For example, we can create a LED class that implements the turn on and turn off operations of a led and save it as led.py.

from hardware import gpio

class LED:

        def __init__(self, pinid, onLevel):
                self.onLevel = onLevel
                self.pinid = pinid
                self.gpio = gpio(pinid, gpio.GPIO_MODE_OUTPUT_PULLDOWN)
                self.gpio.write(1 - self.onLevel)

        def ON(self):
                self.gpio.write(self.onLevel)

        def OFF(self):
                self.gpio.write(1 - self.onLevel)

Now we can modify our first example application so that the led is imported and used instead of accessing directly the hardware.gpio.

# main.py -- put your code here!

import time
from hardware import adc
import led
import feed

## Create a new feed object
testFeed = feed.feed("testFeed","testDatastream")

## Create a new led object
led1 = led.LED(11,1)

## Create a new adc object
analog = adc(1)

## Initialize the new object
analog.init(1000)

val = 0

while True:
    ## Read Value
    val = analog.read()

    print ("value is : " + str(val))

    testFeed.update(val)

    led1.ON()

    time.sleep(4000)
    led1.OFF()