Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MicroPython #421

Open
xp2110 opened this issue Nov 10, 2023 · 22 comments
Open

MicroPython #421

xp2110 opened this issue Nov 10, 2023 · 22 comments
Labels
out-of-scope Question or issue not related to tinytuya question Further information is requested

Comments

@xp2110
Copy link

xp2110 commented Nov 10, 2023

Is there any way to run it in MicroPython

@uzlonewolf
Copy link
Collaborator

Unfortunately I don't think so due to the dependencies tinytuya requires; PyCryptodome is the first one that comes to mind, and there may be others.

@jasonacox
Copy link
Owner

I had a TODO to look at the library ucryptolib but haven't had time yet. I don't know if it would work and/or how difficult it would be to support.

@uzlonewolf
Copy link
Collaborator

From a quick glance it seems to have the same issue as pyaes: no GCM support. As I have yet to see a v3.5 device in the wild I don't think this would be a problem for most people, but it's something to keep in mind.

My bigger question is: run what, exactly? Are people wanting to run the wizard and scanner under MicroPython too? That would be interesting.

@xp2110
Copy link
Author

xp2110 commented Nov 14, 2023

From a quick glance it seems to have the same issue as pyaes: no GCM support. As I have yet to see a v3.5 device in the wild I don't think this would be a problem for most people, but it's something to keep in mind.

My bigger question is: run what, exactly? Are people wanting to run the wizard and scanner under MicroPython too? That would be interesting.

In a distributed network environment with multiple “Tuya” smart devices placed across various locations, centralized control through a private server is essential. This necessitates the employment of an intermediary hardware module, such as an Arduino, which interfaces with the private server and manages the operation of all smart devices within its local network domain.

Moreover, it is feasible to execute Micropython on Arduino platforms, enabling the programming of Arduino hardware with Python syntax and libraries. This integration allows for the streamlined development of control scripts and the flexibility of Python's programming paradigms in hardware interactions.

@jasonacox
Copy link
Owner

Interesting use case @xp2110 - Would an alternative be to place low cost readonly Raspberry Pi Zero W in these various locations?

@xp2110
Copy link
Author

xp2110 commented Nov 14, 2023

Raspberry Pi Zero W cost 10 times the a Arduino

@xp2110
Copy link
Author

xp2110 commented Nov 14, 2023

I made Arduino Code Template for Tuya Communication

#include <WiFi.h>
#include <WiFiClient.h>

// WiFi network details
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// Tuya device details
const char* deviceID = "YOUR_DEVICE_ID";
const char* localKey = "YOUR_LOCAL_KEY";
const char* deviceIP = "DEVICE_IP_ADDRESS";
float protocolVersion = 3.3;  // Tuya protocol version

void setup() {
    Serial.begin(115200);

    // Connect to WiFi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");

    // Additional setup code...
}

void loop() {
    // Get device status
    String status = getDeviceStatus();
    Serial.println("Device Status: " + status);

    // Add more loop code as needed...
    delay(10000); // Delay between status checks
}

String getDeviceStatus() {
    // Generate payload based on Tuya protocol version
    String payload = generatePayload();

    // Send payload to Tuya device and receive response
    String response = sendReceivePayload(payload);

    // Process and return the response
    return processResponse(response);
}

String generatePayload() {
    // Implement payload generation based on protocol version
    // This will involve constructing a correctly formatted and encrypted message
    String payload;
    // Payload construction logic...
    return payload;
}

String sendReceivePayload(String payload) {
    // Implement the logic to send the payload to the Tuya device and receive its response
    // This will involve establishing a TCP connection and handling the response
    String response;
    // Communication logic...
    return response;
}

String processResponse(String response) {
    // Implement response processing logic
    // This might involve decrypting and parsing the response
    String processedResponse;
    // Response processing logic...
    return processedResponse;
}

@jasonacox
Copy link
Owner

Raspberry Pi Zero W cost 10 times the a Arduino

Unless you factor in the time required to code the cyprto libraries for the Arduino? ;)

As @uzlonewolf mentioned, there may be a way to get it to work on MicroPython if you remove the Tuya 3.5 device support.

I made Arduino Code Template for Tuya Communication

Building a C++ library for interfacing with local Tuya devices is an interesting idea. I did a quick search and didn't find one (but there may be one so I would suggest checking first). It's a non-trivial task to be sure.

@uzlonewolf
Copy link
Collaborator

While I do find it interesting and think we should support it if it just requires a crypto library addition, it feels like we're getting into "can" vs. "should" territory and I suspect the end result is going to be disappointing. Using an IoT device just to talk to another IoT device makes me ask "why??" Just have the server talk to the Tuya device directly, or use something like a Raspberry Pi as a hub/gateway for the entire network.

The changes in #423 should make it easy to add ucryptolib if someone wants, or pyaes should work already since it's pure Python (with the performance to match).

@jasonacox
Copy link
Owner

I agree. I'm in favor of the PR based on the flexibility it gives the library to accommodate other crypto libraries. I have other comments I'll drop in the PR.

@xp2110
Copy link
Author

xp2110 commented Nov 15, 2023

I mostly agree, but I want to point out something about using an IoT device instead of a server. For example, if I have several branches, each with many Tuya devices like temperature and humidity sensors, power meters, and light switches, it would be expensive to have a server in each branch. However, if one ESP chip can do a similar job to a regular server ( scanning local devices ), it would be cheaper. This would also ensure data security and allow a central server to gather and process all the data, no matter where it is. I hope this example makes my point clear.

@jasonacox
Copy link
Owner

@xp2110 - Feel free to try the latest release (v1.13.0) as mentioned by @uzlonewolf .

@uzlonewolf
Copy link
Collaborator

uzlonewolf commented Nov 15, 2023

If you're going to try it, doing import tinytuya.core as tinytuya (instead of just import tinytuya) will help reduce the dependencies as it avoids loading all the cloud stuff.

@xp2110
Copy link
Author

xp2110 commented Nov 22, 2023

I tried running it, but it didn't work. I think it might be necessary to look into implementing the same idea in C++. If it's possible to shorten the Python code so that it only serves to determine the device's status, I will then convert it to C++ to make it work on Arduino.

I know that the project is not small, but this will be useful for everyone.

@jasonacox
Copy link
Owner

I think it might be necessary to look into implementing the same idea in C++

Thanks @xp2110 . That would be outside the scope of this project (python library). However, I encourage anyone interested in that effort to join you in helping.

If I have time I'll see if I can get the micropython version to work on my Arduinos.

@uzlonewolf
Copy link
Collaborator

I started getting it to work on the Unix port of MicroPython, but stopped at the point it needed a crypto library. Basically I just had to comment out the colorama stuff and "mip install" some dependencies. If I can find time I'll see about getting the crypto library patched in.

@uzlonewolf
Copy link
Collaborator

If it's possible to shorten the Python code so that it only serves to determine the device's status

Basically 90% of the complexity is handling the different protocol versions. If you only wanted to handle 1 (say, 3.3) it would greatly simplify everything.

@xp2110
Copy link
Author

xp2110 commented Nov 22, 2023

Yes, I specifically want to deal with version 3.3 of the protocol. Focusing on this single version will significantly streamline the process, reducing the complexity involved in managing multiple versions and enhancing the stability and efficiency of my application.

@uzlonewolf uzlonewolf added question Further information is requested out-of-scope Question or issue not related to tinytuya labels Nov 27, 2023
@botaneta
Copy link

botaneta commented Dec 27, 2023

I made Arduino Code Template for Tuya Communication

#include <WiFi.h>
#include <WiFiClient.h>

// WiFi network details
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// Tuya device details
const char* deviceID = "YOUR_DEVICE_ID";
const char* localKey = "YOUR_LOCAL_KEY";
const char* deviceIP = "DEVICE_IP_ADDRESS";
float protocolVersion = 3.3;  // Tuya protocol version

void setup() {
    Serial.begin(115200);

    // Connect to WiFi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");

    // Additional setup code...
}

void loop() {
    // Get device status
    String status = getDeviceStatus();
    Serial.println("Device Status: " + status);

    // Add more loop code as needed...
    delay(10000); // Delay between status checks
}

String getDeviceStatus() {
    // Generate payload based on Tuya protocol version
    String payload = generatePayload();

    // Send payload to Tuya device and receive response
    String response = sendReceivePayload(payload);

    // Process and return the response
    return processResponse(response);
}

String generatePayload() {
    // Implement payload generation based on protocol version
    // This will involve constructing a correctly formatted and encrypted message
    String payload;
    // Payload construction logic...
    return payload;
}

String sendReceivePayload(String payload) {
    // Implement the logic to send the payload to the Tuya device and receive its response
    // This will involve establishing a TCP connection and handling the response
    String response;
    // Communication logic...
    return response;
}

String processResponse(String response) {
    // Implement response processing logic
    // This might involve decrypting and parsing the response
    String processedResponse;
    // Response processing logic...
    return processedResponse;
}

Hello. I'm looking for a library to use on an esp32-arduino. I want to do a direct communication exercise between an esp32 and a tuya device on the local network. My programming skills are basic. This template you created is just what I need. Have you been able to communicate with a device tuya?

@FrBerger83
Copy link

If it's of any interst, I created a small Arduino Code Program to test direct access to Tuya devices running on a ESP8266..
https://github.com/FrBerger83/EspTuya

@jasonacox
Copy link
Owner

Thanks @FrBerger83 ! Adding to my "try this" list. :)

@botaneta
Copy link

botaneta commented Mar 9, 2024

Thanks @FrBerger83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
out-of-scope Question or issue not related to tinytuya question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants