Arduino Code/ESP32/ESP8266 测试程序可直接与涂鸦 WiFi 智能设备交互,无需云访问。
该程序使用在 ESP8266(Wemos D1)上运行的 Arduino 代码编写,仅用于测试使用局域网(LAN)访问、读取状态和控制与 Tuya 兼容的 WiFi 智能设备(插头、开关、灯、窗帘等),无需访问云(TuyaCloud API)。
我在 github 上发现了很棒的Tinytuya 库,但我不是 Python 专家,想将一些功能移植到 Arduino Code。我无法通过阅读大量 Tuya 文档找到方法,因此我使用 TinyTuya 以我能理解的方式对消息结构和协议进行逆向工程,并将其中的一部分移植到 Arduino 代码中。
它与 3.4 协议(55AA 消息)和 3.5 协议(6699 消息)兼容。
该程序由几个 .ino 文件组成
EspTuya.ino : 主要代码
Commandes.ino:高级命令功能(Getstatus、On、Off……)
Functions.ino:所有基本命令(解压消息、发送、接收……)
Crypto.ino :v3.4(55AA)和 v3.5(6699)数据包格式的所有加密功能
Find.ino:用于监听和发现设备的 UDP 函数
GetKey.ino :协议协商函数
我向 Arduino 代码专家道歉,我不是其中之一!而且肯定有更有效的方法来实现某些功能。我以一种任何人都能理解的方式编写了它,并故意避免使用任何更有效的捷径。我的目的是证明它是可行的,并且对我来说是有效的。
为了使用它,您需要:
在 Setup() 函数中:根据您的 LAN 设置自定义您的 WIFI.Begin… 访问,并为“loctimestamp”变量提供 Unix 时间戳。以下是示例: https: //randomnerdtutorials.com/epoch-unix-time-esp32-arduino/
获取您需要控制的设备的设备密钥。您可以在https://github.com/jasonacox/tinytuya上找到详细说明 ,请参阅章节:设置向导 - 获取本地密钥(PDF 说明)
复制您的设备信息:密钥、IP 地址和版本,并以特定的结构复制,就像程序开始时那样:
sDev disj1 = { { 0x51, 0x74 … 0x67, 0x2b } , { 192,168, xx, x }, 4 };
sDev lamp1 = { { 0x3c,..x4b, 0x54, 0x73 } , { 192,168, xx, xx }, 5 };
SDev ....
确保包含正确的 Crypto 库:对我来说效果非常好的库: Rhys Weatherley 的Crypto Libraries
在循环部分更新您想要执行的命令,编译并测试......
Wemos D1R1
设备:
2 * Lamp LED Smart Connect (V3.5 Protocol)
1 * Earu Energy monitor (V3.4 Protocol)
1 * Smart Plug (V3.4 Protocol)
1 * Double Energy monitor (V3.5 Protocol)
FindDevice() :扫描局域网上的设备
与特定设备相关
setDevice( disj1 ); Exemple for an energy monitor
getStatus( ); Return all DPS values for the device
turnOff() ;
turnOn() ;
setDevice( lamp1 ); Exemple for a LED Lamp
getStatus( ); Return all DPS values for the device
turnOff() ; (The lamp doesn’t use the standard DPS 1 but 20)
turnOn() ;
很多:使代码更高效,更彻底地检查返回代码和意外行为,使集成新设备更容易,改进_Client TCP 通信,新的 DPS 功能……等等……但我没有时间做这些。我只想让它为我工作并分享它……基本功能在这里,请随意使用和扩展。
"I'm very sorry, we have stopped accessing third-party ESP chips, we suggest you choose other chips or modules to use." just for you to know.. Tuya no more supports esp8266. Only their own chips