27
loading...
This website collects cookies to deliver better user experience
Data Point (DP)
, Identifier
,Data Transfer Type
, Data Type
and Value
tuyalinksdk
are PID
, AUTHKEY
and UUID
. Once you create a Product in the platform, you automatically obtain the PID (aka, Product ID).tuyalinksdk
to setup a link connection between the circuit and platform. tuyalinksdk
is a Python library which makes the connection between the circuit and the Tuya IoT platform.# install the package
$ pip install tuyalinksdk
$ git clone https://github.com/tuya/tuyaos-link-sdk-python.git
$ python3 -m pip install ./tuyaos-link-sdk-python
from tuyalinksdk.client import TuyaClient
client = TuyaClient(productid='PID', uuid='UUID', authkey='AUTHKEY')
def on_connected():
print('Connected.')
def on_dps(dps):
print('DataPoints:', dps)
client.push_dps(dps)
client.on_connected = on_connected
client.on_dps = on_dps
client.connect()
client.loop_start()
on_connected
is the method which checks if the connection is well setup. If everything is okay the print statement will run.on_dps
is a method which receives the datapoints (values of the sensor) and upload them to the platform so that we can monitor the process from our devices (phones and PC), ie IoT itself. Data are uploaded through client.push_dps()
method.#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import coloredlogs
from tuyalinksdk.client import TuyaClient
from tuyalinksdk.console_qrcode import qrcode_generate
from time import sleep
GPIO.setmode(GPIO.BCM)
coloredlogs.install(level='DEBUG')
client = TuyaClient(
productid='44apXXXXXXXXXXX',
uuid='tuya19dXXXXXXXXXXX',
authkey='uFjw3VqrrfXXXXXXXXXXXXXX')
TRIG = 2
ECHO = 3
i=0
GPIO.setup(TRIG ,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.setup(4 ,GPIO.OUT)
GPIO.output(TRIG, False)
print("Starting.....")
sleep(2)
while True:
GPIO.output(TRIG, True)
sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_stop = time.time()
pulse_time = pulse_stop - pulse_start
water_level = pulse_time * 17150
print(round(water_level, 2))
time.sleep(1)
if water_level < 4:
print("Water will overflow")
GPIO.output(4, True)
time.sleep(0.5)
GPIO.output(4, False)
time.sleep(0.5)
GPIO.output(4, True)
time.sleep(0.5)
GPIO.output(4, False)
time.sleep(0.5)
else:
GPIO.output(4, False)
def on_connected():
print('Connected.')
def on_qrcode(url):
qrcode_generate(url)
def on_reset(data):
print('Reset:', data)
def on_dps(dps):
print('DataPoints:', dps)
dps = {'101':True}
dps['101'] = data
client.push_dps(dps)
client.on_connected = on_connected
client.on_qrcode = on_qrcode
client.on_reset = on_reset
client.on_dps = on_dps
client.connect()
client.loop_start()
while True:
data = water_level
time.sleep(1)
$ git clone https://github.com/tuya/tuyaos-link-sdk-python.git
$ cd examples
# view the starter codes here
$ cat outlet.py
outlets.py
.