Sending TCP packets to a device

Please post your exact code as the crystal ball is not working this week.

There’s no such thing as unprintable characters.
You need to let the underlying Linux console send the data to the ip:port with the net command.
It expects a hex bytesequence like a0/f3/e9
You’re trying to send hex data with a json query directly from HA. That doesn’t work.

Here is a command state switch…

- platform: command_line
  scan_interval: 10
  switches:
    light_led:
      command_on: echo -n 11 | nc 192.168.1.100 6722
      command_off: echo -n 21 | nc 192.168.1.100 6722
      command_state: echo -n 00 | nc -q 1 <ip_addr> <port>
      value_template: "{{ value[0:1] == '1' }}"
      friendly_name: LED
1 Like

I am also attempting to accomplish something quite similar to what is being posted on this thread. I am trying to send a simple TCP/IP command to my TV via SSH.

I am able to use a putty session and make a raw connection to my TV to control it. This is a Sharp Aquos TV which supports TCP/IP control and or RS232 Serial control. I have been unable to get the Aquos component operational. I am able to load it but it only offers a power button. Further the power button when i turn off the TV only turns the TV into some form of standby and remains in a somewhat back lit state on the screen. The protocol Sharp makes available supports about any command I can think of except for 1: it will not power on the TV when TV is powered off.

I have tested the operation of all the commands I wish to use and they work wonderfully using putty from a mac using a RAW connection. It fails any other method attempted which includes: Telnet, Rlogin, SSH and Serial. I am using the assigned ip address of the Sharp TV and the port of 10002 which is default and I have kept it that in the TV settings. I put all of this information in homes I avoid the suggestions to use the Sharp Aquos component as this hasn’t worked for me.

My goal is to mainly allow the switching of inputs from HDMI 1 to HDMI 2 to HDMI 3 as needed. This is mainly the only functionality I am seeking as I use a soundbar on one TV and a yamaha receiver on the other. I am able to force the TV on using the Yamaha receiver through HDMI CEC on one TV and the same method with an Apple TV on the other TV. So I have a sure fire way of automating the power on feature of the TV. So once it is on I want to use command_line to send the TCP/IP protocol to switch inputs or other settings I may wish to use.

The IP protocol for the Sharp for setting input is 8 characters. "IAVD# ".
The # would represent the number of the corresponding input and it follows with 3 spaces.

I wish to send: "IAVD3 " via ssh. I have attempted:

echo -e “\x49\x41\x56\x44\x33\x20\x20\x20” | nc 192.xxx.x.xxx 10002
This continually got me a .connection closed which i have gotten through
a direct connection before using an invalid connection method.

I did some reading and came across the idea to use the -u flag:
echo -e “\x49\x41\x56\x44\x33\x20\x20\x20” | nc -u 192.xxx.x.xxx10002
this had no effect but it appeared like the command worked from my SSH
connection into my hass.io port 22. It was ready for me to send my next command. However there was no change to the TV.

Now the good news I can report is it appears it is connected successfully to the TV. I attempted to open a new putty session to connect direct to the TV. It is showing Service Busy. Connection closed. This usually occurs when something is already using the port as only one connection is allowed.

As shown above I am using hex for the command inside quotes. I have also tried raw text such as "IAVD " instead of hex. Of course this works with a direct raw connection from putty to TV with just normal typing. Lastly, I just wanted to verify that enter command is automatic or if this may be a missing piece to the puzzle. Do I need to somehow account for that? I wasn’t sure if I should add a hex for carriage return or something.

Can anyone offer guidance on how I may send the command from another SSH instance?

FYI: I am attempting this directly typing into Putty which is connected to my Hass.io port 22. I do have private/public keys setup for my putty connections for both port 22 and 22222 though I am currently testing this through 22 connection. The TV currently has no username or password set. I have had the defaults set in the past as admin, password. When using a RAW connection when set, It prompted me for username and then password which I had to enter/login before sending commands. Without a username/password set, I am able to just send my first command such as "IAVD1 " and it works great.

Sorry for all of the information but wanted to ensure I paint a complete picture so we can get to solutions if one exists. Thanks for reading this far if you have.

I will have a look at my code later, will post it…
But I also sometimes need netcat to get a value, not to send it … that doesn’t work for me from hassio… so instead I created a small .py file… And I cust call the python script file from my switch…

So maybe you can make your commands in a python file, and just call that file?

See here what I did to receive from netcat, you can use it as a send command

Why? you said ssh doesn’t work?

That is not ssh, it is a raw tcp connection, which I think you do want. However I think you are very confused about what ssh is.

Nickrout,

I have a very basic understanding. When I say I want to send it via SSH I do realize that seems silly. When later I am indicating I am trying to send RAW TCP. I am trying to accomplish this through HASSIO installation and send RAW TCP command from the HASSIO SSH. Or at least that’s what I was thinking I wanted.

is command_line not using the SSH built into the SSH? Or is it just using the basic command_line of the HASSOS?

I am not a programmer but I am willing to try try and try some more. Which is why I finally began asking questions since nothing I was coming across was applicable. I guess I was asking if SSH can send raw commands through a setting.

I will test some other things to test my theory. Thanks for helping me look at it in a different light.

Nickrout,

Can i run a command such as below direct from config.yaml?

- platform: command_line
  scan_interval: 10
  switches:
    light_led:
      command_HDMI3: echo -e “\x49\x41\x56\x44\x33\x20\x20\x20” | nc 192.xxx.x.xxx 10002
      friendly_name: LED

Obviously I would still need to tweak this for my configuration and something it could be called from?

try

echo "IAVD3   " | nc 192.x.x.x 10002

Do not be confused by the fact that you use ssh to connect to your hassio instance. Once you have ssh’d in, it is the same as if you are sitting in front of the hassio machine looking at a monitor.

There. Is no scan interval on a command line, you need to send an on or off command, see below… If you want to fire just a one time command, use a script instead…

Well great news! I got it to work. I will share the code tomorrow but it is simple command line commands. This is promising and proof of concept for anyone struggling to control a Sharp Aquos TV through IP via Homeassistant.

I made a card and it’s a command line switch card. I’m not happy yet with the design but I will clean that up. On the card are 4 switches total for: HDMI 1, HDMI 2, HDMI 3 & HDMI 4. Each of those switches has an on and an off. Both on and off perform the same function of sending the command to switch to that input. I did consider having 1 switch control two different HDMI inputs one with on and another with off. Went with one switch equals one input for both on and off. I mainly wanted to get it functional. I will research other methods of creating a card with just a simple icon or button press to executed the command line.

Sharp uses an 8 character command structure followed by pressing enter. This is sufficient when you are typing into a terminal and pressing enter. However with my command line we are echoing what we want sent to the Sharp TV IP address. When echoing we need to echo the carriage return. For hex the manual kept referencing 0DH. This is referencing hex carriage return which is written as x0D. This needs to be the 9th character we are echoing for it to work. The echo command works directly through an SSH terminal as well as via the command line. It didn’t help I was using the wrong characters initially. I was attempting to use IVAD instead of IAVD.

A sample command for HDMI 1:

echo -e “\x49\x41\x56\x44\x31\x20\x20\x20\x0D” | nc !secret aquostv_IP 1002

image

I will mention I did this with a tv setup with no username or password. I’m sure you could make it work with a user name and password. You would just need to send a command to provide both in two separate commands likely. It worked using secrets file to store IP as well.

Sharp makes available a plethora of IP commands for this would work for nearly 100% of functionality. 2 of my Sharp TVs won’t allow me to power it on using this method. Another I have will allow me to power it on.

I will follow up with maybe learning a cleaner way of doing this with python. I’m not a coder so I have limited experience. But I am quite familiar with some languages which help me such as my in depth experience with VBA.

At a minimum I believe I will tabulate all of the commands Sharp is capable of. I’m sure there may be a better place to place those for others to learn or use for development. I will test them as well to ensure each code works as expected.

I’m just not sure I have the experience to slap an add on together worthy of sharing. At least I have proof of concept for the codes and method for it to work. Now to cleaning it up!

Tomorrow I will post the snippet for the completed card I created for others to see in case it’s found useful. I’m on my cell and about to crash. Can’t copy and paste from my file at moment.

Good work :wink:

Do you also capture the state? Or just on and off ?

This doesn’t capture the state in my particular instance. However the protocol Sharp provides it does send back a response. By default when It receives a valid command a response of ok is received. If an invalid command is received it responds with err. You can send a command to get it to respond with what input is active. I’ll have to double check what it is. I believe it is IAVD???; the present setting value responds which would be 1-5. So if there is a method for listening for the response this state could be recorded.

I believe there is a way for the commands be sent to listen for a response. Obviously when I’m using my terminal I can view the response. This is more advanced than what I will need. I’m happy to automate and set the state I want by powering on via my Apple TV and then set the input I want via a command line command. I will play with it and experiment.

try this

the -q should capture the state, but if you are running hassos, like in my case, i dont have an -Q , i have another netcat version i am afraid :frowning:

command_state: echo -n 00 | nc -q 1 <ip_addr> <port>

I’m trying to create some switches for my HDMI matrix. Anyone see what I’m doing wrong?

Here are the IP commands - http://nohassleav.com/8x8_hdmi_hdr/after_dec_15th/New_simplified_RS232_IP_commands.txt

switch:
  - platform: command_line
    switches:
      hdmi_matrix:
        command_on: echo -e “@T 04 00 !” | nc 192.168.0.210 5000
        friendly_name: HDMI Matrix

whats your issue? do you have a config error? or ?
is your net command working if you do i from a terminal?

Alex_Yeoman,

I looked at the data in the link. Is that just text characters? It doesn’t appear to be specific ASCII codes to corresponding characters. I use this as a handy chart: http://www.asciitable.com

Is it possible you need to convert those characters to hex? In your link it also mentions: "epending on the control system that you’re using, you might need a carriage return at the end of each of these commands. Check with your control system manufacturer to see what symbol you should use for a carriage return. It could be any of these symbols: \r, /r, <CR>, \0XD" The last one mentioned " \OXD" appears to be hex which would be entered as "\x0D".

I converted your string into hex below:

@T 0F 01 ! Without a carriage return:
echo - e "\x40\x54\x20\x30\x46\x20\x30\x31\x20\x21"

When I pasted into my terminal and pressed enter it shows it echoes: @T 0F 01 ! which is what your device is expecting to execute the power on command. However according to your documentation which you linked to your device may need a carriage return added. If so try the below option. You likely won’t know if that is needed when testing it from the actual terminal interface directly. This may need to be tested directly within your command_line script.

@T 0F 01 ! With a carriage return:
echo - e "\x40\x54\x20\x30\x46\x20\x30\x31\x20\x21\x0D"

Doing the above steps is what my device required. Yours may behave similarly. Give it a try to see if that makes a difference.

Yours may look like this:
command_on: echo -e "\x40\x54\x20\x30\x46\x20\x30\x31\x20\x21"| nc 192.168.0.210 5000
 
or this:
command_on: echo -e "\x40\x54\x20\x30\x46\x20\x30\x31\x20\x21\x0D "| nc 192.168.0.210 5000

pergola.fabio,

I will give it a try to test with capturing the state. I would need to figure out how that would be stored on my card or if it needs anything else in addition to the -q . I fear it wouldn’t matter as just like you, I am operating on HassOS as well. It is likely I am in the same boat as yourself.

yes, then indeed its not working
i am going to step away from hassos in the future, then i try it again :slight_smile: