Close
0%
0%

Design IoT Solutions Using Python and Zerynth

Develop IoT applications in Python with Zerynth on any 32 bit microcontroller and control your insights with Ubidots

Similar projects worth following
Do you prefer to code in python? If so, Zerynth is a suite of professional development tools that supports Python or hybrid C/Python firmware development for 32-bit micro-controllers and the most common prototyping boards: Arduino DUE, ST Nucleo, Particle Photon and Electron, Flip&Click, ESP32, ESP8266, and more. In this guide we will explore Python code development within the Zerynth Studio and how to visualize your results using the simplicity of Ubidots data visualization.

  • Design IoT solutions using Python and Zerynth

    Maria Carlina Hernandez01/10/2018 at 15:05 0 comments

    Do you prefer to code in python? If so, Zerynth is a suite of professional development tools that supports Python or hybrid C/Python firmware development for 32-bit micro-controllers and the most common prototyping boards: Arduino DUE, ST Nucleo, Particle Photon and Electron, Flip&Click, ESP32, ESP8266, and more. In this guide we will explore Python code development within the Zerynth Studio and how to visualize your results using the simplicity of Ubidots data visualization.

    Zerynth is not just another programming library for Arduino associated sensor kits. Zerynth is a set of professional development tools developed from scratch to allow you access to the embedded world in just a few clicks. Zerynth reaches beyond professional embedded developers to also offer web programmers, product designers, and IoT system integrators a complete set of high-quality development tools to program microcontrollers and develop IoT solutions.

    Zerynth is multi-board compatible and allows rapid integration with sensors, actuators and cloud services, reducing product development time and efforts.

    In the following guide we will introduce you to the Zerynth platform using a ESP32 DevKitC connected to the Ubidots cloud. When finished with this guide you will be able to visualize your board's sensor data in Ubidots and control a LED using a sample code provided.


View project log

  • 2
    Zerynth Setup

    1. Download and install the Zerynth Studio using the installation guide for additional assistance if required.

    2.  Next, continue with the the Getting Started guide paying close attention to the "Connect, Register and Virtualize your Device" section. Once your device is properly virtualized in the Zerynth you're ready to continue with this guide. 

    FAQs and Troubleshooting: For Windows and Mac user, if you receive errors creating the Virtual Machine verify the latest drivers are installed. For Linux users, allow access to serial ports adding the user to the group and giving the required read/write access:

    • Ubuntu distribution –> dialout group
    • Arch Linux distribution –> uucp group
  • 3
    Posting values to Ubidots

    1. Create a new Zerynth Project. To do this, press Browser Project > New Project then assign the project title and the project folder. To finish, press "Create".  

    2. Next, your new Zerynth Project will automatically open to the "main.py" file. This main.py is where the principal Zerynth code will be written in Python or hybrid C/Python. Here is where we're going to develop the logic of any script.


    3.  Once the project is properly created, copy and paste the sample code provided below into the "main.py" file. After pasting the code, assign your Ubidots TOKEN, and WiFi credentials where is indicated in the code. 

    If you don't how to find your Ubidots TOKEN, please reference Ubidots Help Center

    DEPLOYMENT NOTE: In this sample code the device label is set as "ESP32" and the variable label as "temperature". This means that your request will be sent with these parameters to Ubidots. If you wish to alter this sample code, please do so as needed.

    ################################################################################
    # Ubidots POST ESP32DevKitC
    #
    # Created at 2017-10-09 22:15:06.348664
    # Authors: M. Hernandez
    ################################################################################
    
    import streams  # import streams
    import json     # import json
    import adc      # import the adc module
    import requests # import the http module
    from wireless import wifi # import the wifi interface
    from espressif.esp32net import esp32wifi as wifi_driver # networking driver for the wifi module
    
    # assign Ubidots parameters
    device_label = "esp32" # Ubidots device label
    variable_label = "temperature" # Ubidots variable label token = "Ubidots_TOKEN_here" # ubidots TOKEN
    
    # create a serial port stream with default parameters
    streams.serial()
    
    # define the analog pin (A0) for reading the value inputAnalog = A0
    
    # set the pin as input with INPUT_ANALOG,
    pinMode(inputAnalog,INPUT_ANALOG)
    
    # init the wifi driver
    wifi_driver.auto_init()
    
    print("Establishing connection...")
    try:
        # change network name "ssid-name", security and password "ssid-pass" as needed
        wifi.link("ssid-name",wifi.WIFI_WPA2,"ssid-pass")
        print("Connected!")
    except Exception as e:
        print("Something wrong while connection. Verify your WiFi credentials", e)
        while True:
            sleep(1000)
    
    # build the JSON directory to be sent
    def build_json(variable, value):
        try:
            data = {variable: {"value": value}}
            return data
        except:
            return None
    
    # send the POST HTTP request to Ubidots
    # reference to the Ubidots REST API reference for more information (https://ubidots.com/docs/api/)
    def post_var(device_label, variable_label, value, token):
        # Ubidots API access
        url = "http://things.ubidots.com/api/v1.6/devices/" + device_label + "/?token=" + token
        # data to be sent
        data = build_json(variable_label, value)
        # sends the request
        response = requests.post(url, json=data)
        # prints the status and the content of the request
        print("Http Status:",response.status)
        print("Http Content:",response.content)
        print("---------------------------------")
        return response
        while True:
        try:
            # read the input on analog pin 0
            sensorValue = adc.read(inputAnalog)
            # send the POST HTTP request to Ubidots
            print("Posting variables to Ubidots")
            post_var(device_label, variable_label, sensorValue, token)
                except Exception as e:
            print(e)
        sleep(2000)

    This sample script makes a POST HTTP request to the Ubidots cloud. Posting one variable that contains the reading of the analog pin(A0) of the ESP32 DevKitC. To send more values using your device reference Ubidots REST API to learn how to build your own HTTP Requests. 

    4. With the code pasted and updated with your credentials you must next verify. To verify, press the "check-mark" icon which can found below:

    5. Once the script is properly verified, it's time to Uplink the script into the ESP32DevKitC. To uplink the script, press the "up arrow" icon found below:


    Wait a couple seconds until the the script is uplinked into the board. Once the uplink finishes, you will receive the message "Uplink done" in the terminal log of the Zerynth Studio. 

    6. Open the console to debug the status of the request; press the "terminal" icon found below to open the console:

    At first, your console will report the connection status. Then you are going to start receiving the status of the HTTP request to Ubidots.

    FAQs and Troubleshooting: If you open the console and you're not able to visualize the debug messages, please press the reset button from the ESP32 DevKitC to start receiving messages. 

    8. Go to the Device section of your Ubidots for Business account to verify the message is received:

View all 5 instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates