• 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
From sensor to the Cloud in just 15 lines of Python

From sensor to the Cloud in just 15 lines of Python

Oct 31, 2018 | Posted by Giacomo Baldi | Zerynth Academy |

The world of IoT is all about moving data to the cloud and then back to the devices. That process may sound simple, but once you start doing it, you realize it has its complications.

Let’s be honest, all the technicalities behind correctly programming a device, in order to connect it to the internet and send data in a secure way, are still a daunting task.

So, after we were done with all the technical issues and the winding road of tests, and demonstrations, we wanted to make sure that everyone else had a clear “how-to” guide to sending sensor data to the cloud, in an easy,  secure, cost-effective and robust way.

Isn’t that every embedded developer’s dream?

For everyone that’s new to Zerynth tools, here’s a brief summary. Zerynth software tools simplify IoT development, providing an easy and efficient way to program the most popular 32-bit microcontrollers in Python and connect them to the top Cloud infrastructures like Microsoft Azure, Amazon Web Services, IBM Bluemix, and Google Cloud IoT.

Zerynth has been listed as a Trusted IoT Platform Partner by Microchip in their partner program designed to recognize and recommend companies that are deemed as a genuine security expert on Microchip technologies. Zerynth has also formed numerous partnerships with companies like Espressif Systems, NXP, Amazon Web Services, Google Cloud Platform, RS Components, Mouser Electronics, XinaBox, Eseye and more.

Reusable Python code, and a vast collection of libraries

We worked to make sure other IoT developers can focus on the actual value in the project – the data. That is why we have created the Zerynth Toolchain. More precisely, our tools make all those daunting tasks become an easy routine.

How did we accomplish this?

On one hand, we made reading the sensors a simple task, with the numerous Python libraries that take care of all the complexities, leaving you with what really matters, the data.

On the other hand, a robust TCP/IP stack, a wifi driver and a state of the art TLS library allow connecting any supported 32-bit microcontroller to AWS MQTT endpoints (or to the other supported Cloud services ) and stream data to your database.  And if this wasn’t enough, the resulting Python code is 99% reusable on a different hardware, in case you decide to use a different board later.

Let’s take a look at the code:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# import wireless and iot modules
from wireless import wifi
from espressif.esp32net import esp32wifi as wifi_driver
from aws.iot import iot
# import the sensor module
from bosch.bme280 import bme280
# import some utilities
import streams
import json
import helpers
 
# load device credentials
new_resource('private.key')
new_resource('certificate')
new_resource('config')
 
streams.serial()
 
# connect to wifi
wifi_driver.auto_init()
wifi.link("SSID",wifi.WIFI_WPA2,"PASSWORD")
 
# configure the AWS device
pkey, cert = helpers.load_key_cert('private.key', 'certificate')
thing_conf = helpers.load_thing_conf()
 
thing = iot.Thing(thing_conf['endpoint'], thing_conf['mqttid'], cert, pkey, thingname=thing_conf['thingname'])
 
# start the MQTT client
thing.mqtt.connect()
thing.mqtt.loop()
 
# send data!
sensor = bme280.BME280(I2C0)
while True:
    thing.mqtt.publish("data/sink", {'temp':sensor.get_temp()})
    sleep(1000)

We start by importing the wireless and IoT modules. Once configured with the appropriate credentials, the device can connect first to the local wifi and then authenticate itself to the AWS IoT Core.

The last lines perform the actual work: initializing the sensor and keep reading and streaming data.

As you can see from the example, every single line except line 3 is hardware independent. By changing the wireless driver from ESP32 by Espressif Systems  (if you’re using a XinaBox CW02 ) to let’s say WINC1500 Module by Microchip, the code works the same thanks to the power of the Zerynth Virtual Machine.

The same occurs also if you want or need to change the Cloud: the resulting Python code is almost completely reusable. Take a look at these two examples for a comparison:

  • “Controlled publish period” example of Zerynth AWS IoT library
  • “Controlled publish period” example of Zerynth Google Cloud IoT library

Boosting secure IoT connections using Python

With connecting to the WiFi, and Cloud authentication covered, we can move on to the next section of this tutorial – how to make your projects more secure in just a few lines of code.

As cool as it can be, the previous example is not that “professional”. For example (and you probably already know this), it’s a bad practice to store the credentials of the device (private key and certificate) in the firmware itself, or in any part of the device that can be easily accessed and tampered with. This method of work is insecure and forces the generation of different firmware for each device.

For these reasons, we have released the support for Microchip crypto elements, tiny integrated circuits that can generate and securely store all the credentials of a device.

Now take a look at the modified code:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# import wireless and iot modules
from wireless import wifi
from espressif.esp32net import esp32wifi as wifi_driver
from aws.iot import iot
from bosch.bme280 import bme280
# import some utilities
import streams
import json
import helpers
# import microchip ateccx08a module for its hw crypto element interface
from microchip.ateccx08a import ateccx08a
 
# load device credentials
new_resource('certificate')
new_resource('config')
 
streams.serial()
 
# connect to wifi
wifi_driver.auto_init()
wifi.link("SSID",wifi.WIFI_WPA2,"PASSWORD")
 
# start hardware crypto interface
ateccx08a.hwcrypto_init(I2C0, 0)
 
# configure the AWS device
cert = helpers.load_cert('certificate')
thing_conf = helpers.load_thing_conf()
 
thing = iot.Thing(thing_conf['endpoint'], thing_conf['mqttid'], cert, '', thingname=thing_conf['thingname'])
 
# start the MQTT client
thing.mqtt.connect()
thing.mqtt.loop()
 
# send data!
sensor = bme280.BME280(I2C0)
while True:
    thing.mqtt.publish("sensors", {'temp':sensor.get_temp()})
    sleep(1000)

And that’s it! Only a few lines changed, and now the private key is generated by the crypto element and the secure connection library directly interacts with the element to perform hardware accelerated cryptography!

The solution can be made even more secure by following all the guidelines that come with the crypto element. For more info, take a look at this tutorial: “Zero Touch Secure Provisioning for AWS IoT using Python, our Python version of the popular Microchip’s demo. Thanks to this porting, Zerynth has recently been listed as a “Trusted IoT Platform Partner” by Microchip. More info here.

Send data to the cloud in 15 lines of Python

Still, the code can be simplified even further. With the last update, it’s now possible to send data to the cloud in just 15 lines of Python.

Take a look at the code:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from wireless import wifi
from espressif.esp32net import esp32wifi as wifi_driver
from bosch.bme280 import bme280
from aws.iot import iot, default_credentials
wifi_driver.auto_init()
wifi.link("SSID",wifi.WIFI_WPA2,"PSW")
endpoint, mqttid, clicert, pkey = default_credentials.load()
thing = iot.Thing(endpoint, mqttid, clicert, pkey)
thing.mqtt.connect()
thing.mqtt.loop()
sensor = bme280.BME280(I2C0)
while True:
    thing.mqtt.publish("sensors", {'temp':sensor.get_temp()})
    sleep(1000)

We cut the amount of code in half, and now it’s possible to send temperature data to the Cloud in minutes. All that in a secure way with the generated private key.

Get started with Zerynth on XinaBox xChips

The XK12 IoT Starter Kit is a Zerynth programmable toolset intended for fast evaluating and prototyping. Since the Zerynth license is already onboard this IoT kit is a great combination of XinaBox’s modular electronics and our easy-to-use software tools.

Now it’s your turn. Get your XinaBox kit “powered by Zerynth” and pick these xChips:

  • 1 x SW01 – Advanced Weather Sensor (BME280)
  • 1 x CW02 – Wi-Fi & Bluetooth Core (based on ESP-WROOM-32 by our partner Espressif Systems)
  • 1 x AH01 – SHA-256 Hardware Encryption (ATECC508A)
  • 1 x IP01 – USB Programming Interface (FT232R)

Follow the getting started, and you’ll be finished in no time. Just to recap, you have to:

  1. Download Zerynth Studio
  2. Connect, Register and Virtualize the board
  3. Clone a ready-made example. You can start with the “Hello Zerynth” and then go with “Cloud15Lines” (in this case, of course, you need also to set up your accounts and things on Cloud platforms)
  4. Uplink the script
  5. Enjoy!

We hope that you found this tutorial helpful and that you are already making a mental list of sensors you would like to use. Our main goal was to show you how easy it is to send data to the Cloud with Zerynth Studio, and why it shouldn’t be thought of as difficult or complex.

Related

Tags: AWSCloudcryptoCryptoAuthenticationCW02EspressifEspressif SystemsIoT SecurityMicrochipPythonXinaBox
Share

Newsletter

Follow us!

My Tweets

Latest Posts

  • Industry 4.0, Industrial Internet of Things and their software challenge
  • Python and C hybrid programming on a microcontroller with Zerynth
  • Python and LoRaWAN – The Things Conference workshop that had everyone interested
  • Embedded World 2019 – the Zerynth team at the RS Components stand
  • Build a voltmeter in Python with the Zerynth App

Categories

  • News
  • Releases and Updates
  • Zerynth Academy

TAGS CLOUD

4zerobox 4ZeroPlatform Amazon Web Services Arduino AWS Blockchain Cloud Conference crypto DesignSpark Embedded World ESP32 ESP8266 Espressif Espressif Systems Flip and Click FOTA Google Cloud Platform Google IoT Core Hackster Hexiwear Industry 4.0 IoT LoRa LoraWAN Microchip MikroElektronika partnership Python RS Components SAMD21 The Things Conference The Things Network tutorial Ubidots Wolkabout Workshop XinaBox Zerynth 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

© 2019 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