Tuya.Net
|
.NET Standard 2.0 library to interface with Tuya WiFi smart devices over LAN, without using the cloud.
Full documentation: https://clusterm.github.io/tuyanet/
This library controls and monitors Tuya compatible WiFi Smart Devices (Plugs, Switches, Lights, Window Covers, etc.) using the local area network (LAN). Tuya devices are designed to communicate with the Tuya Cloud but most also expose a local area network API, allowing us to directly control the devices without using the cloud.
You can use NuGet:
Every Tuya device broadcasts to local network UDP packets with short info about itself. This data is encrypted with AES but the encryption key is the same for every device and it can be easily decrypted. This packet is sent every 5 seconds and contains data with a unique device ID, device type ID and protocol version.
Also, every device can accept TCP connection and proceed requests. Every request contains command code and JSON string. JSON string is encrypted with AES using local key. This key is assigned by the Tuya Cloud server and is unique for each device. So you need to create a Tuya developer account and request this key from the server.
Most requests must contain base JSON values:
Where DEVICE_ID
is a device ID and CURRENT_TIME
is a Unix timestamp as a string.
Every response contains command code, JSON and return code (0 = success).
Most devices are controlled via data points (DPs). Every data point has a number and a value. Usually you can send DP_QUERY
(0x0A) command and receive ra esponse like
dps
is a dictionary with data points and current values. This is the response from the "2 in 1" smart switch. 1
is a state of switch #1, 2
is a state of switch #2, 9
and 10
are timer values. Please note that DPs numbers are strings. DPs values can be of any type.
Usually you can change DPs values using CONTROL
(0x07) command and JSON like:
This request will turn off switch #1.
Don't worry, this library will help you to create requests automatically.
Click on Cloud
Click on the Create Cloud Project
button
Enter any name for your project, select "Smart Home" for industry and development method. You can select any data center but you must remember which one you chose.
Skip Configuration Wizard.
Copy and save your Access ID and Access Secret.
Select Devices
.
Select Link Tuya App Account
.
Click on Add App Account
and it will display a QR code.
Scan the QR code using your mobile phone and Smart Life app by going to the "Me" tab and clicking on the QR code button [..] in the upper right hand corner of the app. Your account should appear on the list.
Now open the Devices
tab.
You should see list of your devices. Copy and save at least one device ID.
Click on the Service API
tab.
Click on Go to Authorize
Add IoT Core
API (subscribe to it first).
TuyaApi
class: region
- the region of the data center that you have selected on iot.tuya.com accessID
and apiSecret
- Access ID
and Access Secret
from iot.tuya.com anyDeviceId
- ID of any of your smart devices (to fetch user ID).
You can use TuyaScanner
class to catch and decode broadcast UDP packets from devices:
You can use it to retrieve the device's IP address, ID, and protocol version. Remember that your computer must be on the same network as your devices to receive broadcasts.
You should now have:
TuyaApi
There is TuyaDevice
class, you need create instance of it:
It uses protocol version 3.3 by default but you can specify version 3.1 as well:
Now you can encode requests:
Send it:
And decode response:
How to set DPs:
Too complicated, isn't it? There is more simple way. You can use FillJson()
method to fill standard fields automatically:
Also, there is SendAsync()
overload that accepts command ID with JSON, encodes it, and returns decoded data:
Finally, there are GetDps()
and SetDps()
methods:
Some devices may have dynamically changed values, such as timers. They need to be updated before reading:
Or just:
Since version 1.0.3 you can control infrared remote controls. There is TuyaIRControl class for it. You can learn button from real remote control using GetButtonCodeAsync
method (it returns Base64-encoded pulses sequences) and simulate button press using SendButtonCodeAsync
method. Example:
You can create TuyaDevice
object without local key but with API key/secret like this:
Then obtain local key:
You can use official Tuya Cloud API.
Example of device specifications request:
devspec
will contain JSON with all device data points with descriptions and possible values.