Project tutorial

Using Python and Arduino MKR1000 for Secure IoT © GPL3+

Getting started with Python and Arduino MKR1000 for secure IoT projects.

  • 4,756 views
  • 1 comment
  • 20 respects

Components and supplies

Apps and online services

About this project

Arduino MKR1000 meets Python

With the last release, Zerynth officially supports Arduino MKR1000, a device specifically designed for secure IoT projects.

The MKR1000 is described as a powerful board that combines the functionality of an Arduino Zero (already supported by Zerynth) and the connectivity of a Wi-Fi Shield, with a Cryptochip for secure communication. The design also includes a Li-Po charging circuit that allows the Arduino MKR1000 to run on battery power or external 5V, charging the Li-Po battery while running on external power.

Now, programming the board in Python thanks to Zerynth Studio makes Arduino MKR1000 one of the best choices for the development of secure IoT battery-powered projects.

MKR1000 specifications

The Arduino MKR1000 is based on the Microchip ATSAMW25 SoC, that is part of the SmartConnect family of Microchip Wireless devices. In particular, the ATSAMW25 is composed of three main blocks:

  • SAMD21 Cortex-M0+ 32bit low power ARM MCU
  • WINC1500 low power 2.4GHz IEEE® 802.11 b/g/n Wi-Fi
  • ECC508 Crypto Authentication

The main features of the board are:

  • Board Power Supply (USB/VIN): 5V
  • Supported Battery: Li-Po single cell, 3.7V, 700mAh minimum
  • Operating Voltage: 3.3V
  • Digital I/O Pins (DIO): 14
  • Analog Input Pins (ADC): 7
  • UARTs: 1
  • SPIs: 1
  • I2Cs: 1
  • Flash Memory: 256 KB
  • SRAM: 32 KB
  • Clock Speed: 48 MHz
  • Size (LxW mm): 61.5 x 25.0

Go to the dedicated page of the Zerynth Docs to get more info about this board.

Get started with Arduino MKR1000 and Python

Download and Install Zerynth Studio

First of all, of course, you have to download and install Zerynth Studio, our cross-platform IDE for embedded and IoT development. 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 recognize the device, Windows machines require drivers that can be downloaded from the official Arduino MKR100 page, while OSX and Linux machines will recognize the board automatically.

Once connected to a USB port, if drivers have been correctly installed, the Arduino MKR1000 board is recognized by the Zerynth Studio and listed in the Device Management Widget.

To register and virtualize the board, Arduino MKR1000 MUST be put in virtualization mode by double clicking the RST button.

Then select “Arduino MKR1000 Virtualizable” on the Device Management Widget and register the device by clicking the “Z” button.

Put again the board in virtualization mode by double clicking the RST button and create a Zerynth Virtual Machine for the device by clicking the “Z” button for the second time. Now you can virtualize the device by clicking the “Z” button for the third time. You can find more info about the virtualization process in the relative doc.

Now you can start to program your Arduino MKR1000 in Python! Zerynth Studio includes a lot of examples that you can clone with just a few clicks.

Let’s start with the “Blink” example. You can find it in the “Examples Browser” panel or you can use the “Quick Search” feature, searching for “Blink”. Let’s take a look at the code in the “main.py” file. The Python script is very easy:

pinMode(LED0, OUTPUT)
 
# loop forever
while True:
    digitalWrite(LED0, HIGH)  # turn the LED ON by setting the voltage HIGH
    sleep(1000)               # wait for a second
    digitalWrite(LED0, LOW)   # turn the LED OFF by setting the voltage LOW
    sleep(1000)               # wait for a second

Clicking the third button of the toolbar you can get the pin map of the device. As you can see from the figure, the onboard LED is connected to the pin D6. However, Zerynth abstracts the board layout allowing to use LED0, LED1, etc as led names. In this example, LED0 is used.

Once you have cloned the example, uplink the code to your board and reset the device by pressing the RST button when asked.

Done! Now you can see the LED of your MKR1000 turn ON and OFF thanks to a Python script running on the board!

Enabling IoT Security using Arduino MKR1000 and Zerynth

A plus of this board is its ability to access to a WiFi network through a wifi chip with SSL/TLS hardware support.

Zerynth helps you exploit this functionality in an easy and effective way: you just need to import the lib.microchip.winc1500 library in the Zerynth script!

(Of course, also root certificates must be loaded on the chip. See more here: Certificate Uploading.)

An example to simplify your job and let you get started with a few clicks is already available: just clone the “Secure HTTP” example as done for the “Blink” example and you’re almost done!

Below you can see the code.  You have just to change “Network-Name” and “Wifi-Password” to match your actual network configuration. Easy, isn’t it?

################################################################################
# Zerynth Secure Sockets
################################################################################
 
 
# import streams & socket
import streams
import socket
 
# import json parser, will be needed later
import json
 
# import the wifi interface
from wireless import wifi
 
# import the http module
import requests
 
# the wifi module needs a networking driver to be loaded
# in order to control the board hardware.
# FOR THIS EXAMPLE TO WORK, A NETWORK DRIVER MUST BE SELECTED BELOW
 
# uncomment the following line to use the CC3000 driver (Particle Core or CC3000 Wifi shields)
# from texas.cc3000 import cc3000 as wifi_driver
 
# uncomment the following line to use the BCM43362 driver (Particle Photon)
# from broadcom.bcm43362 import bcm43362 as wifi_driver
 
# uncomment the following line to use the WINC1500 driver (MKR1000)
from microchip.winc1500 import winc1500 as wifi_driver
 
 
streams.serial()
 
# init the wifi driver!
# The driver automatically registers itself to the wifi interface
# with the correct configuration for the selected board
wifi_driver.auto_init()
 
 
# use the wifi interface to link to the Access Point
# change network name, security and password as needed
print("Establishing Link...")
try:
    # FOR THIS EXAMPLE TO WORK, "Network-Name" AND "Wifi-Password" MUST BE SET
    # TO MATCH YOUR ACTUAL NETWORK CONFIGURATION
    wifi.link("Network-Name",wifi.WIFI_WPA2,"Wifi-Password")
except Exception as e:
    print("ooops, something wrong while linking :(", e)
    while True:
        sleep(1000)
 
# let's try to connect to https://www.howsmyssl.com/a/check to get some info
# on the SSL/TLS connection
 
        
for i in range(3):
    try:
        print("Trying to connect...")
        url="https://www.howsmyssl.com/a/check"
        # url resolution and http protocol handling are hidden inside the requests module
        user_agent = {"User-Agent": "curl/7.53.1", "Accept": "*/*" }
        response = requests.get(url,headers=user_agent)
        # if we get here, there has been no exception, exit the loop
        break
    except Exception as e:
        print(e)
 
 
try:
    # check status and print the result
    if response.status==200:
        print("Success!!")
        print("-------------")
        # it's time to parse the json response
        js = json.loads(response.content)
        # super easy!
        for k,v in js.items():
            print(k,v)
        print("-------------")
except Exception as e:
    print("ooops, something very wrong! :(",e)

This simple script connects to https://www.howsmyssl.com/a/check and displays info about the SSL/TLS connection on the serial monitor, as you can see in the figure above. Take a look at the ssl module in the standard library to spot how to expand this example.

Enabling Power Saving for battery-powered IoT devices

One of the most important challenges of battery-powered IoT devices 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), 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 much more

You still have few days to pre-order Zerynth Studio PRO for 199$/year for life instead of 390$/year!

Discover Zerynth Studio PRO and Pre-Order

Via Zerynth Academy

Schematics

arduino_mkr1000_python_-_pinmap_64bsfgwRAj.png
Arduino mkr1000 python   pinmap 64bsfgwraj

Code

Code snippet #1Python
pinMode(LED0, OUTPUT)
 
# loop forever
while True:
    digitalWrite(LED0, HIGH)  # turn the LED ON by setting the voltage HIGH
    sleep(1000)               # wait for a second
    digitalWrite(LED0, LOW)   # turn the LED OFF by setting the voltage LOW
    sleep(1000)               # wait for a second
Code snippet #2Python
################################################################################
# Zerynth Secure Sockets
################################################################################
 
 
# import streams & socket
import streams
import socket
 
# import json parser, will be needed later
import json
 
# import the wifi interface
from wireless import wifi
 
# import the http module
import requests
 
# the wifi module needs a networking driver to be loaded
# in order to control the board hardware.
# FOR THIS EXAMPLE TO WORK, A NETWORK DRIVER MUST BE SELECTED BELOW
 
# uncomment the following line to use the CC3000 driver (Particle Core or CC3000 Wifi shields)
# from texas.cc3000 import cc3000 as wifi_driver
 
# uncomment the following line to use the BCM43362 driver (Particle Photon)
# from broadcom.bcm43362 import bcm43362 as wifi_driver
 
# uncomment the following line to use the WINC1500 driver (MKR1000)
from microchip.winc1500 import winc1500 as wifi_driver
 
 
streams.serial()
 
# init the wifi driver!
# The driver automatically registers itself to the wifi interface
# with the correct configuration for the selected board
wifi_driver.auto_init()
 
 
# use the wifi interface to link to the Access Point
# change network name, security and password as needed
print("Establishing Link...")
try:
    # FOR THIS EXAMPLE TO WORK, "Network-Name" AND "Wifi-Password" MUST BE SET
    # TO MATCH YOUR ACTUAL NETWORK CONFIGURATION
    wifi.link("Network-Name",wifi.WIFI_WPA2,"Wifi-Password")
except Exception as e:
    print("ooops, something wrong while linking :(", e)
    while True:
        sleep(1000)
 
# let's try to connect to https://www.howsmyssl.com/a/check to get some info
# on the SSL/TLS connection
 
        
for i in range(3):
    try:
        print("Trying to connect...")
        url="https://www.howsmyssl.com/a/check"
        # url resolution and http protocol handling are hidden inside the requests module
        user_agent = {"User-Agent": "curl/7.53.1", "Accept": "*/*" }
        response = requests.get(url,headers=user_agent)
        # if we get here, there has been no exception, exit the loop
        break
    except Exception as e:
        print(e)
 
 
try:
    # check status and print the result
    if response.status==200:
        print("Success!!")
        print("-------------")
        # it's time to parse the json response
        js = json.loads(response.content)
        # super easy!
        for k,v in js.items():
            print(k,v)
        print("-------------")
except Exception as e:
    print("ooops, something very wrong! :(",e)

Comments

Similar projects you might like

Quadrature Encoder Demo

Project tutorial by Shawn hymel

  • 97 views
  • 0 comments
  • 3 respects

Arduino Amiga Floppy Disk Reader

Project in progress by Team RobSmithDev

  • 3,985 views
  • 6 comments
  • 7 respects

Physical Computing - Scratch for Arduino

Project tutorial by MJRoBot

  • 122 views
  • 0 comments
  • 2 respects

Fidget Spinner RPM Counter

by Andriy Baranov

  • 8,180 views
  • 2 comments
  • 36 respects

Arduino Controlled USB Trackpad

Project tutorial by Dante Roumega

  • 2,904 views
  • 2 comments
  • 4 respects
Add projectSign up / Login