jasonacox / tinytuya

Python API for Tuya WiFi smart devices using a direct local area network (LAN) connection or the cloud (TuyaCloud API).
MIT License
956 stars 172 forks source link

IR blaster not responding after being idle for some time #375

Closed avsha114 closed 1 year ago

avsha114 commented 1 year ago

I wrote a simple code to control my IR devices with this IR blaster: https://www.aliexpress.com/item/4000825628583.html?spm=a2g0o.order_list.order_list_main.71.2df61802vf46gy

I used the tutorial in the official docs and everything is working great. This is my code (without the irrelevant parts):

d = tinytuya.OutletDevice(id, ip_address, key)

d.set_version(3.3)

command = {
        "control": "send_ir",
        "head": "Fan",
        "key1": action,
        "type": 0,
        "delay": 300,
    }

payload = d.generate_payload(tinytuya.CONTROL, {"201": json.dumps(command)})
d.send(payload)
data = d.receive()

The problem is that after some times, maybe 24 hours, the program doesn't work anymore. I get no response. I also noted that I can't ping the device.

I am using static IP address that I reserved via my router. The weird thing is that it still works through Google Assistant, but even after I use voice commands I still can't control the device with my python code. If I pull it out and in the power plug it's working again, but only until the next time.

Should I use some kind of different configuration?

uzlonewolf commented 1 year ago

Every 24 hours really sounds like a DHCP issue. The next time it happens run a scan (python3 -m tinytuya scan) to see if it got a different IP.

avsha114 commented 1 year ago

Every 24 hours really sounds like a DHCP issue. The next time it happens run a scan (python3 -m tinytuya scan) to see if it got a different IP.

It happened a lot of time, but the IP hasn't changed. Anyway, I will wait for it to happen again and let you know. Thanks.

avsha114 commented 1 year ago

Every 24 hours really sounds like a DHCP issue. The next time it happens run a scan (python3 -m tinytuya scan) to see if it got a different IP.

OK so it happened again. The IP is not changed according to my router. It is not make sense that it changed because I used reservation of the IP address with the devices MAC address. But on the other hand I can't ping the device. I tried to ping its hostname also but no luck. I tried to run "arp -a" command but the device didn't show up on the list of devices in my network.

It still works fine from the app or from google assistant.

My thoughts are that maybe it went to some kind of a power saving mode or maybe it disconnected from the network and I can control it using other protocol (but I turned bluetooth off on my phone and it still works through the app so...)

uzlonewolf commented 1 year ago

And what did python3 -m tinytuya scan return?

avsha114 commented 1 year ago

And what did python3 -m tinytuya scan return?

Here are the results. I deleted the IDs and put **** instead.

Unknown v3.3 Device   Product ID = *******  [Valid Broadcast]:
    Address = 192.168.31.5   Device ID = ****** (len:22)  Local Key =   Version = 3.3  Type = default, MAC =
    No Stats for 192.168.31.5: DEVICE KEY required to poll for status
Scan completed in 18.0082 seconds

Scan Complete!  Found 1 devices.
Broadcasted: 1
Versions: 3.3: 1
Unknown Devices: 1
jasonacox commented 1 year ago

Not sure if this applies to IR blaster or not, but I have seen this on other devices.

I tried to ping its hostname also but no luck. ... My thoughts are that maybe it went to some kind of a power saving mode or maybe it disconnected from the network and I can control it using other protocol (but I turned bluetooth off on my phone and it still works through the app so...)

If you are controlling it via app or google assistant, it is using the cloud. That would mean that the device is still connecting (outbound) to the Tuya Cloud (which is why you can control it) but it stops listening to LAN traffic. You could test that by disabling internet connection (cable/DSL modem) and seeing if you can still control it on your LAN.

I have an old 3.1 device that behaves this way. It stops responding to ARP after a few days. I have to power cycle it to fix it but all the while I can control it via the cloud. I suspect it is just some buggy code in some of these devices.

avsha114 commented 1 year ago

I have an old 3.1 device that behaves this way. It stops responding to ARP after a few days. I have to power cycle it to fix it but all the while I can control it via the cloud. I suspect it is just some buggy code in some of these devices.

So in other words, there's nothing to do but power cycle?

jasonacox commented 1 year ago

For me, it wasn't worth more investigation as I could replace the old device with a newer one. However, I have wondered if the issue is just arp broadcasts and if the device was still on the network (MAC address). If so, you could set up a static ARP mapping if you have the device MAC address, eg.:

# set the IP to MAC mapping
sudo arp -s 192.168.31.5  0:1e:58:b1:64:40

# test it
ping 192.168.31.5
avsha114 commented 1 year ago

For me, it wasn't worth more investigation as I could replace the old device with a newer one. However, I have wondered if the issue is just arp broadcasts and if the device was still on the network (MAC address). If so, you could set up a static ARP mapping if you have the device MAC address, eg.:

# set the IP to MAC mapping
sudo arp -s 192.168.31.5  0:1e:58:b1:64:40

# test it
ping 192.168.31.5

Didn't work :( Probably the device is not responding to any LAN traffic like you said before. Is there a better device that you would recommend?

avsha114 commented 1 year ago

@jasonacox I noticed you can control the device through the cloud with tinytuya. I used the getproperties function on the IR blaster and didn't get anything important. then I ran it on the actual device that I wanted to send IR command to, which is a simple fan and I got these functions: 'functions': [{'code': 'NegativeIon', 'desc': 'NegativeIon', 'name': 'NegativeIon', 'type': 'STRING', 'values': 'NegativeIon'}, {'code': 'PowerOff', 'desc': 'PowerOff', 'name': 'PowerOff', 'type': 'STRING', 'values': 'PowerOff'}, {'code': 'PowerOn', 'desc': 'PowerOn', 'name': 'PowerOn', 'type': 'STRING', 'values': 'PowerOn'}, {'code': 'Speed', 'desc': 'Speed', 'name': 'Speed', 'type': 'STRING', 'values': 'Speed'}, {'code': 'Swing', 'desc': 'Swing', 'name': 'Swing', 'type': 'STRING', 'values': 'Swing'}, {'code': 'Timing', 'desc': 'Timing', 'name': 'Timing', 'type': 'STRING', 'values': 'Timing'}]}

Then I used this code to send a command:

commands = {
        "commands": [
            {"code": "PowerOn", "value": "PowerOn"}
        ]
    }
    print("Sending command...")
    result = c.sendcommand(id,commands)
    print("Results\n:", result)

but nothing happened. Although I got this result:

Sending command...
Results
: {'result': True, 'success': True, 't': 1688299753394, 'tid': '423ce2ba18d111ee94e0a2e0adfc0b14'}

Seems like it should work but it doesn't. What am I missing?

jasonacox commented 1 year ago

Good question. I would defer to @uzlonewolf as an expert in the IR space, but I wonder if your command payload is right. I think it should be more like:

command = {
    "control": "send_ir",
    "head": "",
    "key1": "[[TO_BE_REPLACED]]",
    "type": 0,
    "delay": 300,
}

Quick search on https://developer.tuya.com/en/docs/cloud/d59591e9f6?id=Kb3oe4hspk8bi and I see:

{
  "category_id": 2,
  "key": "power",
  "key_id": 1
}
uzlonewolf commented 1 year ago

@avsha114 See https://github.com/jasonacox/tinytuya/blob/master/examples/cloud_ir.py lines 29-49 for raw codes or lines 60-79 for predefined keys.

avsha114 commented 1 year ago

Good question. I would defer to @uzlonewolf as an expert in the IR space, but I wonder if your command payload is right. I think it should be more like:

command = {
    "control": "send_ir",
    "head": "",
    "key1": "[[TO_BE_REPLACED]]",
    "type": 0,
    "delay": 300,
}

Quick search on https://developer.tuya.com/en/docs/cloud/d59591e9f6?id=Kb3oe4hspk8bi and I see:

{
  "category_id": 2,
  "key": "power",
  "key_id": 1
}

@avsha114 See https://github.com/jasonacox/tinytuya/blob/master/examples/cloud_ir.py lines 29-49 for raw codes or lines 60-79 for predefined keys.

Thank you both so much for your help. The commands I'm trying to send are self learned since my device don't exist in the predefined devices list. @uzlonewolf I did everything like in the link you sent. I took the key from the debug section in the Tuya portal. I already had it from the LAN control I used but I still double checked it, it's the correct key code. This is my code:

result = c.getproperties(id)
print("Properties of device:\n", result)

ir_cmd = {
        "control": "send_ir",
        "head": "Fan",
        "key1": "15gTUAeYEnwGfAeYE5gSfAeYE1AGfAeYEnwHmBJ8B5gSfAeYEnwHmBJ8B5gTmBPka5gTUAeYEnwGfAeYE5gSfAeYE1AGfAeYEnwHmBJ8B5gSfAeYEnwHmBJ8B5gTmBPka5gSfAeYEnwGfAeYE5gSfAeYEnwGfAeYEnwHmBNQB5gSfAeYEnwHmBJ8B5gTmBPka5gSfAeYE1AGfAeYE5gSfAeYEnwGfAeYEnwHmBJ8B5gSfAeYEnwHmBJ8B5gTmBPka5gSfAeYEnwGfAeYE5gSfAeYEnwGfAeYEnwHmBJ8B5gSfAeYEnwHmBJ8B5gTmBDB1",
        "type": 0,
        "delay": 300,
}

    cloud_cmd = {
    "commands": [
        {
            "code": "ir_send",
            "value": json.dumps(ir_cmd)
        },
      ]
   }
    print("Sending command...")
    result = c.sendcommand(id,cloud_cmd)
    print("Results\n:", json.dumps(result, indent=2))

I sent it to the ID of the IR blaster of course like in the link you sent. Here is the result:

Properties of device:
 {'result': {'category': 'wnykq'}, 'success': True, 't': ********, 'tid': '********'}
Sending command...
Results
: {
  "code": 2008,
  "msg": "command or value not support",
  "success": false,
  "t": *********,
  "tid": "*********"
}

I feel like I'm one step away from success but I still can't find the last piece of the puzzle.

uzlonewolf commented 1 year ago

Hmmm, this might be a Control Instruction Mode issue. If you follow https://github.com/jasonacox/tinytuya/blob/master/DP_Mapping.md through Step 4, does "ir_send" show up in the list of available instructions?

avsha114 commented 1 year ago

Hmmm, this might be a Control Instruction Mode issue. If you follow https://github.com/jasonacox/tinytuya/blob/master/DP_Mapping.md through Step 4, does "ir_send" show up in the list of available instructions?

This is the what I see: image

Do I need to change it?

uzlonewolf commented 1 year ago

Yes, you need to change it.

I recently discovered you can speed up the "Step 6" change propagation by going back to Step 4 and clicking the "Eye" icon and then the "Refresh Configuration" button.

avsha114 commented 1 year ago

Yes, you need to change it.

I recently discovered you can speed up the "Step 6" change propagation by going back to Step 4 and clicking the "Eye" icon and then the "Refresh Configuration" button.

YOU ARE MY HERO! It works! It's nice to know such a helpful project with active collaborators. @jasonacox @uzlonewolf Thank you both :)