Things used in this project

Software apps and online services:
Zpm toolbar 1
Zerynth Studio
Ubidots
Ubidots

Code

Code snippet #1Plain 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 #2Plain 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 #3Plain 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 #4Plain 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

Generic icon taoacaqyns
UbiMaker
49 projects • 142 followers
Maker @ ubidots.com
Contact
Whatsapp image 2017 09 07 at 11 24 21 pm n3htmsm5yh
Maria Carlina Hernandez
12 projects • 31 followers
Hardware Developer Engineer & Content Specialist at Ubidots Cloud
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog