Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
UbiMakerMaka Hernandez
Published © CC BY-NC-SA

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.

BeginnerProtip2 hours3,596
Design IoT Solutions Using Python And Zerynth

Things used in this project

Software apps and online services

Zerynth Studio
Zerynth Studio
Ubidots
Ubidots

Story

Read more

Code

Code snippet #1

Plain text
################################################################################
# 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)

Code snippet #2

Plain text
################################################################################
# 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)

Code snippet #3

Plain text
################################################################################
# Ubidots GET 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
# ESP32 WiFi driver (Sparkfun Esp32 Thing, Olimex Esp32, ...)
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 = "control" # ubidots variable label 
token = "your_ubidots_TOKEN_here" # ubidots TOKEN

# create a serial port stream with default parameters
streams.serial()

# init the wifi driver
wifi_driver.auto_init()

# set the pin as OUTPUT
pinMode(D3,OUTPUT)

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)

# send the GET HTTP request to Ubidots
def get_value(device_label, variable_label, token):
    # Ubidots API access
    url = "http://things.ubidots.com/api/v1.6/devices/" + device_label + "/" + variable_label + "/lv?token=" + token
    # sends the request
    response = requests.get(url)
    # verify the status of the request
    if response.status != 200:
        return None

    print("---------------------------------")
    print("Http Status:",response.status)
    # return the last value obtained
    return response.content

while True:
    # getting last value from Ubidots
    last_value = get_value(device_label, variable_label, token)
    # verify if the last value received is not 'None'
    if last_value is not None:
        last_value  = float(last_value) * 1.0
        # led control
        if last_value >= 1.0:
            print("LED ON")
            digitalWrite(D3, HIGH)  # turn the LED ON by setting the voltage HIGH
        else:
            print("LED OFF")
            digitalWrite(D3, LOW)   # turn the LED OFF by setting the voltage LOW
    sleep(1500) # minimum time sleep allowed

Code snippet #4

Plain text
################################################################################
# Ubidots GET 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
# ESP32 WiFi driver (Sparkfun Esp32 Thing, Olimex Esp32, ...)
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 = "control" # ubidots variable label 
token = "your_ubidots_TOKEN_here" # ubidots TOKEN

# create a serial port stream with default parameters
streams.serial()

# init the wifi driver
wifi_driver.auto_init()

# set the pin as OUTPUT
pinMode(D3,OUTPUT)

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)

# send the GET HTTP request to Ubidots
def get_value(device_label, variable_label, token):
    # Ubidots API access
    url = "http://things.ubidots.com/api/v1.6/devices/" + device_label + "/" + variable_label + "/lv?token=" + token
    # sends the request
    response = requests.get(url)
    # verify the status of the request
    if response.status != 200:
        return None

    print("---------------------------------")
    print("Http Status:",response.status)
    # return the last value obtained
    return response.content

while True:
    # getting last value from Ubidots
    last_value = get_value(device_label, variable_label, token)
    # verify if the last value received is not 'None'
    if last_value is not None:
        last_value  = float(last_value) * 1.0
        # led control
        if last_value >= 1.0:
            print("LED ON")
            digitalWrite(D3, HIGH)  # turn the LED ON by setting the voltage HIGH
        else:
            print("LED OFF")
            digitalWrite(D3, LOW)   # turn the LED OFF by setting the voltage LOW
    sleep(1500) # minimum time sleep allowed

Credits

UbiMaker

UbiMaker

53 projects • 231 followers
Maker @ ubidots.com
Maka Hernandez

Maka Hernandez

29 projects • 124 followers

Comments