Programming Hexiwear (wearables + IoT) in Python using Zerynth

By on June 12, 2017

Web site:

https://www.zerynth.com/blog/programming-hexiwear-wearable-iot-in-python-using-zerynth/

Project Summary:

Getting started with Python and Hexiwear, one of the most loved wearable IoT development platforms!

Full Project:

Wearables and IoT

The wearables market is booming with predictions of more than a $10B market size by 2020. Despite the encouraging numbers, this is not an easy market to access. Designing smartwatches or fitness bracelets is a tough challenge as consumers expect lots of functionality alongside with a compact form-factor, lightweight, and long battery life.

From a technological point of view, wearables could be one of the greatest applications of the Internet of Things and Zerynth’s contribution to enable this world is reality.

Hexiwear meets Python thanks to Zerynth

As reported in this thread in the Zerynth Community Forum, Zerynth officially supports Hexiwear, one of the most loved wearable IoT development platforms!

Hexiwear has been designed by MikroElektronika in collaboration with NXP to better support projects and products by accelerating their time to market, reducing development costs and securing their migration from prototype to production. A vision that matches perfectly with Zerynth, with its platform for developing embedded devices in Python using paradigms and features typical of personal computer (PC) and mobile programming.

Hexiwear specifications

This open-source reference design brings together everything developers need to design their next wearable IoT application.

The main element is its power-efficient Kinetis K64F MCU, featuring:

  • ARM® Cortex®-M4 core;
  • max frequency up to 120MHz;
  • 1024KB Flash, 256KB RAM;
  • many peripherals (16-bit ADCs, DAC, Timers);
  • interfaces (USB Device Crystal-less, UART, SPI, I2C, I2S, SD-card).

Integration is also a key feature of Hexiwear, with a Bluetooth Low Energy (BLE) SoC (Kinetis KW40z) and 8 Sensors onboard:

  • 6-axis Accelerometer and Magnetometer combo (FXOS8700CQ);
  • 3-axis Gyroscope (FXAS21002CQ);
  • Pressure sensor accurate up to Altitude sensing (MPL3115A2);
  • Temperature and humidity combo (HTU21D);
  • Ambient light sensor (TSL2561);
  • Optical Heart rate sensor (Maxim MAX30101).

It also includes a 1.1” OLED color display and a 190mAh 2C Li-Po battery with a charger (MC34671).

Along with the Hexiwear device, you also need the Hexiwear Docking Station, that allows developers to easily program their code and expand the board features with up to three click boards™ among a portfolio of more than 200 references.

Get started with Hexiwear and Python

Download and Install Zerynth Studio

First of all, of course, you have to download and install Zerynth Studio, our professional IDE that provides a platform for developing your Python (or hybrid C/Python) code and managing your boards. It includes a compiler, debugger and an editor, alongside tutorials and example projects for an easy learning experience.

Zerynth Studio is cross-platform. If you didn’t do it yet, download it here for free! Once you’ve installed it, open Zerynth Studio and create a Zerynth user account.

Connect and Virtualize the board

To start using Hexiwear with Zerynth Studio, first you have to connect the device to your PC through the Hexiwear Docking Station. In particular, it has an onboard DAP Link circuitry that exposes a serial port over USB. You can find it below the Power LED, as shown in the following figure.

DAP Link should be supported natively by all platforms. So you don’t need to install any driver!

Once connected to your PC, Hexiwear device can be seen as a Virtual Serial port and it is automatically recognized by Zerynth Studio. The next steps are:

  • Select Hexiwear on the Device Management Toolbar. To complete this step a “Disambiguate” operation may be required because the docking station shares the same hardware of other Zerynth supported boards. Just click on the field above “Ambiguous Devices” to “disambiguate” it and then choose “Hexiwear (Docking Station)”.
  • Register the device by clicking the “Z” button from the Zerynth Studio. If you notice it’s taking too long, press the reset button on the Docking Station.
  • Create a Virtual Machine for the device by clicking the “Z” button for the second time;
  • Virtualize the device by clicking the “Z” button for the third time. Flash the Zerynth Virtual Machineinto the device.

Once you have installed the Virtual Machine you are ready to program the board.

Clone the example and uplink it to the Hexiwear

Zerynth Studio includes a specific example for the Hexiwear, that you can clone with just a few clicks. You can find the example in the “Examples Browser” panel or you can use the “Quick Search” feature, searching for “Hexiwear”.

 

Once you have cloned the example, uplink the code to your board.

Let’s take a look at the original code in the “main.py” file: here is where you develop the logic of your Python script.

################################################################################
# Basic example of use for Hexiwear Library
################################################################################

import streams
from nxp.hexiwear import hexiwear
import threading
import zLogo

streams.serial()

def pressed_up():
    print("Up Button Pressed")
    hexi.vibration(100)
    hexi.enable_bt_upd_sensors()

def pressed_down():
    print("Down Button Pressed")
    hexi.vibration(100)
    hexi.disable_bt_upd_sensors()

def toggle_ble():
    try:
        print("Left Button Pressed")
        hexi.vibration(100)
        hexi.bt_driver.toggle_adv_mode()
    except Exception as e:
        print("error on left_pressed", e)

def toggle_touch():
    try:
        print("Right Button Pressed")
        hexi.vibration(100)
        hexi.bt_driver.toggle_tsi_group()
    except Exception as e:
        print("error on right_pressed", e)
    
def print_paircode():
    # print the pair code in the serial monitor
    print("Your Pair Code:",hexi.bt_driver.passkey)

# used to check the bluetooth status
pinMode(LED2, OUTPUT)

try:
    print("init")
    hexi = hexiwear.HEXIWEAR()
    print("start")
    hexi.fill_screen(0xFFFF,False)
    # attach toggle_ble function to left button (enabled/disabled ble)
    hexi.attach_button_left(toggle_ble)
    # attach toggle_touch function to right button (toggle active button - left/right pair)
    hexi.attach_button_right(toggle_touch)
    # attach pressed_up function to up button - enabled ble update sensor value thread
    hexi.attach_button_up(pressed_up)
    # attach pressed_up function to down button - disabled ble update sensor value thread
    hexi.attach_button_down(pressed_down)
    # attach print_paircode function to bluetooth pairing request
    hexi.attach_passkey(print_paircode)
    print("Ready!")
    print("------------------------------------------------------------------------------")
except Exception as e:
    print(e)
    
    
def read_bt_status():
    while True:
        bt_on, bt_touch, bt_link = hexi.bluetooth_info()
        digitalWrite(LED2, 0 if bt_on==1 else 1)
        sleep(1000)

thread(read_bt_status)

hexi.draw_image(zLogo.zz, 38, 10, 20, 20)
hexi.draw_text("Start!", 0, 60, 96, 20, align=3, color=0xFFFF, background=0x0000, encode=False)

while True:
    try:
        bl, chg = hexi.get_battery_level(chg_state=True)
        print("Battery Level:", bl, "% - Charging:", chg)
        al = hexi.get_ambient_light()
        print("Ambient Light:", al)
        acc = hexi.get_accelerometer_data()
        print("Accelerometer Data [xyz]", acc, "m/s^2")
        magn = hexi.get_magnetometer_data()
        print("Magnetometer Data [xyz]", magn, "uT")
        magn = hexi.get_gyroscope_data()
        print("Gyroscope Data [xyz]", magn, "dps")
        temp = hexi.get_temperature()
        print("Temperature", temp, "*C")
        humid = hexi.get_humidity()
        print("Humidity", humid, "RH%")
        press = hexi.get_pressure()
        print("Pressure", press, "Pa")
        hr = hexi.get_heart_rate()
        print("Heart Rate", hr, "bpm")
        print("------------------------------------------------------------------------------")
        sleep(3000)
    except Exception as e:
        print(e)
        sleep(3000)

 

You can start from the original code and then you can edit the script to develop your specific project.

Check out the official doc of the Hexiwear library to get more info on how to use it. This module contains the driver for enabling and handling all Hexiwear onboard sensors and features. The HEXIWEAR class permits an easier access to the internal peripherals and exposes all functionalities in simple function calls.

More documentation and tutorials?

Tutorials and guides are available under the section Zerynth Academy.

Have questions and feedback?

Post them on Zerynth Community Forum. And don’t forget to share your projects on Hackster!

Power saving is the key for wearables success

One of the most important challenges of the wearable technology is how to maximize the battery life. To meet this specification, Zerynth has included the “Power Saving” feature within the Zerynth Studio PRO version (available from June 28 and in pre-order with up to 50% discount), that will also include features like:

  • Selectable RTOS
  • Over the Air update development
  • Hardware-driven Secured Firmware burned on the device at industrial volumes
  • …and many more

 







About Luigi F. Cerfeda

Yet another Tony Stark wannabe