• Get Started
    • Overview
      • What is Zerynth
      • How Zerynth Works
      • Licensing
    • Tools
      • Zerynth Studio
      • Zerynth Virtual Machine
      • Zerynth App
    • Integrations
      • Microcontrollers / Boards
      • Cloud / IoT Dashboards
      • Sensors / Actuators
    • Learn
      • Documentation
      • Video Tutorials
      • Zerynth Academy
      • Knowledge Base
  • Community
    • Forum
    • Projects
    • Blog
  • Company
    • About Zerynth
    • Partners
    • Careers
    • Services
    • Contact us
Zerynth - Python for Microcontrollers, IoT and Embedded Solutions Zerynth - Python for Microcontrollers, IoT and Embedded Solutions
  • Get Started
    • Overview
      • What is Zerynth
      • How Zerynth Works
      • Licensing
    • Tools
      • Zerynth Studio
      • Zerynth Virtual Machine
      • Zerynth App
    • Integrations
      • Microcontrollers / Boards
      • Cloud / IoT Dashboards
      • Sensors / Actuators
    • Learn
      • Documentation
      • Video Tutorials
      • Zerynth Academy
      • Knowledge Base
  • Community
    • Forum
    • Projects
    • Blog
  • Company
    • About Zerynth
    • Partners
    • Careers
    • Services
    • Contact us
Programming RedBear BLE Nano2 and Blend2 in Python with Zerynth

Programming RedBear BLE Nano2 and Blend2 in Python with Zerynth

Nov 2, 2016 | Posted by Luigi F. Cerfeda | News, Zerynth Academy |

Announced in June 2016, the new Bluetooth 5 specification promises significantly increased speed, and broadcast messaging capacity over current Bluetooth 4.2.

However most existing BLE development boards are not upgradable to support these.

RedBear has launched a Kickstarter campaign to enable the new generation of Bluetooth Low Energy products for makers and professional developers. The campaign includes three boards based on the Nordic nRF52832 BLE chipset, including a BLE module suitable for mass production and secure Over-the-Air Updates.

Why programming RedBear boards with Zerynth

As discussed in this post, there are more and more reasons to use Python for microcontrollers and embedded development.

But why use Zerynth?

Zerynth provides a platform for developing embedded devices in Python using paradigms and features typical of personal computer (PC) and mobile programming. It lets you make your object ‘connected’ and ‘smart’ with just a few lines of Python code and almost no wiring.

The advantages of programming RedBear Nano2/Blend2 with Zerynth include:

  • Coding in Python or Hybrid C/Python with a multithreaded real-time OS that requires a footprint of just a 60k-80k of flash and 3-5k RAM;
  • Development of flexible, scalable and customizable IoT solutions with reduced development time and guaranteed high performances and reliability;
  • Native mobile app and cloud connector for mobile control and cloud connection with the architectures that best fit with your needs.

 

All this is possible thanks to the Zerynth modular set of software tools that form the Zerynth Stack.

 

Zerynth Stack for RedBearLab

The main components of Zerynth are:

  • ZERYNTH VIRTUAL MACHINE: A multithreaded real-time OS that provides real hardware independence allowing code reuse on a wide set of 32bit chips. The VM supports most high-level features of Python like modules, classes, multi-threading, callback, timers and exceptions. In addition to these, it allows to use custom hardware-related features like interrupts, PWM and digital I/O. RTOS threads written in C can live along the VM allowing for a mixed C/Python realtime environment.
  • ZERYNTH STUDIO: an open-source browser based 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 ADVANCED DEVICE MANAGER: A device manager compatible with many cloud providers (like Azure, Kinetis, Bluemix, Amazon), exposing a simple API based interface for data gathering, actuation and mobile integration.
  • ZERYNTH APP: a general purpose interface for all network- or Bluetooth-powered Zerynth objects, that turns any mobile into the controller and display for smart objects and IoT systems. You do not need to write any separate code for Android or iOS.

 

In order to guarantee a reliable, maintainable and scalable architecture of the Zerynth stack, the Zerynth Virtual Machine license is based on a closed source policy with distribution of binaries packags only and custom VM compilation as an online service. On the other hand, the Zerynth Studio IDE and most of Zerynth libraries and drivers are opensource. With the free version of Zerynth Studio, you will be able to program for free up to 5 Blend2 and up to 5 Nano2. Discounted/free extension packages for education are available on request.

We provide below some examples to explain 2 features of Zerynth through the LED blinking example: multithreading and code reuse.

Blinking one LED

Following the code related to the blink of one LED using Zerynth.

Zerynth Blink
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Initialize the digital pins where the LEDs are connected as output
led_1 = D2
pinMode(led_1, OUTPUT)
 
# Define the 'blink' function to be used by the threads
def blink(pin,timeON=100,timeOFF=100): # timeON and timeOFF are optional parameters, used as default
   while True:
       digitalWrite(pin,HIGH)   # turn the LED ON by making the voltage HIGH
       sleep(timeON)            # wait for timeON
       digitalWrite(pin,LOW)    # turn the LED OFF by making the voltage LOW
       sleep(timeOFF)           # wait for timeOFF
 
# Create threads that execute instances of the 'blink' function.
thread(blink,led_1)               # led_1 is ON for 100 ms and OFF for 100 ms, the default values of timeON an timeOFF

Blinking More LEDs

Blinking one LED is known as the “Hello World of embedded devices”… but with Zerynth it can be funnier. You can blink many LEDs at different frequencies using many separated threads in just a few lines of code.

It’s the magic of multithreading!

No more creepy single loop logic code, typical of imperative programming. Zerynth allows pure thread driven implementation!

This can save your mind. Take a look here to spot the differences with Multiblink in C/C++.

Zerynth Multiblink
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Initialize the digital pins where the LEDs are connected as output
led_1 = D2
led_2 = D3
led_3 = D4
pinMode(led_1, OUTPUT)
pinMode(led_2, OUTPUT)
pinMode(led_3, OUTPUT)
 
# Define the 'blink' function to be used by the threads
def blink(pin,timeON=100,timeOFF=100): # timeON and timeOFF are optional parameters, used as default
   while True:
       digitalWrite(pin,HIGH)   # turn the LED ON by making the voltage HIGH
       sleep(timeON)            # wait for timeON
       digitalWrite(pin,LOW)    # turn the LED OFF by making the voltage LOW
       sleep(timeOFF)           # wait for timeOFF
 
# Create three threads that execute instances of the 'blink' function.
thread(blink,led_1)             # led_1 is ON for 100 ms and OFF for 100 ms, the default values of timeON an timeOFF
thread(blink,led_2,400)         # led_2 is ON for 400 ms and OFF for 100 ms, the default value of timeOFF
thread(blink,led_3,1000,1000)   # led_3 is ON for 1000 ms and OFF for 1000 ms

This example shows how to use Zerynth threads for driving three LEDs with asymmetric and different blinking rates.

Once the script is run, three threads are created (plus the main thread, that is always available), each running a specific instance of the blink function, with different parameters.

Each thread in Zerynth is a sort of separated and parallel process that runs autonomously on your board. A thread requires a function to be executed as input for the definition. The same function can be instanced by various thread giving you the possibility to write very concise and readable code. With threads, you can design your algorithm architecture assuming parallelism that is typical of high level. More info about Zerynth Virtual Machine here.

Moreover, thanks to Python argument passing, default values can be defined for function inputs. In this way you can launch threads without specifying all the inputs required by the function, default values will fill the holes. In this case, all the parameters following ‘blink’ are passed to the functions as arguments.

Write Once, Run Everywhere

You can easily switch the boards (for example from BLEND 2 to Nano 2) maintaining the same configuration.

Thanks to the Zerynth Hardware Abstraction Layer, the code remains the same!

 

Support RedBear boards on Kickstarter!

 

 

Related

Tags: BLEBlend 2BluetoothKickstarterMultiblinkNano 2NordicnRF52832RedBearZerynth Advanced Device ManagerZerynth AppZerynth StackZerynth Studio
Share
Loading Facebook Comments ...

Newsletter

Follow us!

My Tweets

Latest Posts

  • Day one – Impressions from the IoT Tech Expo in London
    Day one – Impressions from the IoT Tech Expo in London
  • Zerynth r2.1.1 is out with support for JTAG and customizable VMs for your own hardware solutions!
    Zerynth r2.1.1 is out with support for JTAG and customizable VMs for your own hardware solutions!
  • Connected Industrial Toolkit for IoT – Zerynth and Eseye partnership announced
    Connected Industrial Toolkit for IoT – Zerynth and Eseye partnership announced
  • Meet Zerynth at IoT Tech Expo and learn about IoT, Microcontrollers and Blockchain
    Meet Zerynth at IoT Tech Expo and learn about IoT, Microcontrollers and Blockchain
  • Zerynth and WolkAbout Announce Technology Partnership
    Zerynth and WolkAbout Announce Technology Partnership

Categories

  • News
  • Releases and Updates
  • Zerynth Academy

TAGS CLOUD

4zerobox Adafruit Amazon Web Services Arduino AWS Blockchain Click Boards Cloud Cloud IoT Embedded World ESP32 ESP8266 Espressif Espressif Systems Flip and Click FOTA Google Cloud Platform Google IoT Core Hexiwear HUZZAH Industry 4.0 IoT Tech Expo Kickstarter LoRa LoraWAN Maker Faire Maker Faire NYC Maker Faire Rome Microchip mikroBUS MikroElektronika Multiblink Particle Photon SAMD21 ST Nucleo The Things Network Ubidots Wolkabout Workshop Zerynth Advanced Device Manager Zerynth App Zerynth Package Manager Zerynth Stack Zerynth Studio Zerynth Virtual Machine

Zerynth Tools

• Zerynth Studio
• Zerynth Virtual Machine
• Zerynth App

Zerynth Integrations

• Microcontrollers / Boards
• Cloud / IoT Dashboards
• Sensors / Actuators

Get started

• What is Zerynth
• How Zerynth Works
• Licensing

Learn

• Documentation
• Video Tutorials
• Zerynth Academy
• Knowledge Base

Community

• Forum
• Projects
• Blog

Company

• About zerynth
• Partners
• Careers
• Services
• Contact us

Zerynth Newsletter

Zerynth Social

© 2018 by Kinzica Ventures LLC, New York, USA | Copyright | Terms of service | License | Privacy policy

  • License
  • Terms of Service
  • Privacy
  • Copyright
Prev Next
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Read More