LG TV controller



  • Hi!
    I just got my TV controller working and would like to share this to you.

    Picture of the controller

    Behind my LG TV there is a RS232 port. I have always been curious what that port does. Then I read the manual (after 5 years of curiosity) and apparently you can control almost everything in the TV with simple serial commands and it´s well documented in the manual.

    The commands are built in three segments: “[COMMAND] [TV ID] [VALUE]”. For example, if I would like to turn on my TV, the command is Serial.println(“ka 01 01”), where “ka” is the command for power. To turn it off again I use Serial.println(“ka 01 00”). You can also check if the TV is on or off via the command Serial.println(“ka 01 FF”) and it will return “a 01 OK01” (on) or “a 01 OK00” (off).

    After testing the commands from my computer via null modem cable I ordered a RS232 to TTL converter module. A simple way to use software serial to control RS232. The hardware is simple to set up. I use a 5v Arduino Mini Pro, the RS232 to TTL, mini usb BOB (only for power source) and of course the NRF24L01 radio with a socket adapter.

    I bought this one: RS232 To TTL Converter Module

    • Connect the radio like always.
    • Connect the VCC on RS232 to 5v, ground to ground, RX to D6 and TX to D7

    Circuit

    With this code I can controll the power and volume on the TV. I can also see if the TV is on or off.

    #include <MySensor.h>
    #include <SPI.h>
    #include <SoftwareSerial.h>
    
    MySensor gw;
    MyMessage msg(1, V_LIGHT);
    MyMessage msg2(2, V_PERCENTAGE);
    
    //Software Serial for RS232 to TTL board, define output pins
    SoftwareSerial mySerial(6, 7); // RX, TX
    
    String tvPower = "00"; //Power 00=off 01=on
    int tvVolume = 0; //Volume
    const String tvid = "01"; //The ID you set in your TV
    unsigned long previousMillis = 0; // last time update
    int messageToSend = 0;
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("LG TV RS232", "1.1");
      gw.present(1, S_LIGHT);
      gw.present(2, S_DIMMER);
    
      //Software Serial for RS232 to TTL board, begin
      mySerial.begin(9600);
      
    }
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
      if (mySerial.available() > 0) {
        String incommingString;
          // read the incoming byte:
          //incomingByte = mySerial.read();
          // say what you got:
          incommingString = mySerial.readStringUntil('\n');
          Serial.print("I received: ");
          Serial.println(incommingString);
          parseSerialString(incommingString);
      }
    
    
      //Check if TV is on
      unsigned long currentMillis = millis();
      if(currentMillis - previousMillis > 30000) {
         previousMillis = currentMillis;
         if(messageToSend>0) messageToSend=0;
         if(messageToSend==0){
          sendMessage("ka", "FF"); //Set status to FF if you want to see the current power status.
          messageToSend++;
         }else if(messageToSend==1){
          sendMessage("kf", "FF"); //Set status to FF if you want to see the current volume.
          messageToSend++;    
         }
      }
    }
    
    void incomingMessage(const MyMessage &message) {
      if (message.sensor==1) {
         if(message.getBool()){
          setPower("01");
         }else{
          setPower("00"); 
         }
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
      else if (message.sensor == 2) {
        Serial.println( "V_PERCENTAGE command received..." );  
        int dimvalue= atoi( message.data );
        if ((dimvalue<0)||(dimvalue>100)) {
          Serial.println( "V_DIMMER data invalid (should be 0..100)" );
          return;
        }else{
          Serial.print("V_PERCENTAGE value: ");
          Serial.println(dimvalue);
        }
        gw.saveState(message.sensor, dimvalue);
        dimvalue = round(dimvalue*2/10);
        Serial.print("New value: ");
        Serial.println(dimvalue);
        setVolume(dimvalue);
      }
    }
    
    
    //Send message to tv
    void sendMessage(String tvcommand, String tvstatus){
      mySerial.println(tvcommand + " " + tvid + " " + tvstatus);
    }
    
    //Turn TV on or off
    void setPower(String tvstatus){
      sendMessage("ka", tvstatus);
    }
    //Set TV volume
    void setVolume(int volume){
      String strVolume;
      if(volume<0) volume=0;
      if(volume>64) volume=64;
      strVolume = String(volume, HEX);
      if(strVolume.length()==1){
        strVolume = "0" + strVolume;
      }
      sendMessage("kf", strVolume);
    }
    
    //Parse incomming serial string
    void parseSerialString(String serialString){
      String tvcommand;
      String tvstatus;
      tvcommand = serialString.substring(0,1);
      tvstatus = serialString.substring(7,9);
      if(tvcommand=="a"){
        tvPower = tvstatus;
        gw.send(msg.set(tvPower.toInt()));
        Serial.println("Power is: " + tvPower);
      }
      if(tvcommand=="f"){
        tvVolume = tvstatus.toInt();
        gw.send(msg.set(tvVolume));
        Serial.println("Volume is: " + tvstatus);
      }
    }
    

    Any feedback on the code is welcome! This is my first project for MySensors and I have a lot to learn!


  • Mod

    Very cool, thanks for sharing!


  • Admin

    Nice!

    Make sure to add your project to OpenHardware.io and tag it with "MySensors" and "Contest2016" 🙂



  • @hek said:

    Nice!

    Make sure to add your project to OpenHardware.io and tag it with "MySensors" and "Contest2016" 🙂

    Thanks, I will do that!


  • Hardware Contributor

    Nice! I will check my Samsung if I can do that!
    Great job.



  • @sundberg84 said:

    Nice! I will check my Samsung if I can do that!
    Great job.

    Thanks!

    In my "research" I found out that some Samsung TV's and other manufactors have a serial port that can be controlled like this.


  • Hardware Contributor

    very cool idea 👍


  • Admin

    @Smurphen

    Now, that's cool.. I have thought about doing something similar, to detect if my tv (also an LG) is turned on. And use that to control some lightning in the living room. But as so many other things, it's only on the idea stage at the moment..



  • @tbowmo said:

    @Smurphen

    Now, that's cool.. I have thought about doing something similar, to detect if my tv (also an LG) is turned on. And use that to control some lightning in the living room. But as so many other things, it's only on the idea stage at the moment..

    Thanks!

    I'm currently doing that. When my TV turns on, the light behind my TV also turns on. But because I don't want to spam my TV with requests, there can be up to 30 seconds delay before Domoticz knows that the TV is on or not. But there is also a USB port behind my TV. I have noticed that there is only power in the USB when the TV is on. So I thinking of connecting the USB port to my Arduino to check if the power is on or not.


  • Hardware Contributor

    Sooo... after some waiting i recieved my RS232 to TTL... but soon found out that my Samsung does not use the RS232... but a 3.5mm stereo as a service port... seems like its the same protocoll so it should be able to do everything from changing channel to volyme, source and other cool stuff...

    Build Plans:
    0_1457642388647_1.jpg

    A great weekend project... 🙂 I will be back!



  • @sundberg84 Sounds like it would work! Looking forward too see the result! 😃


  • Hardware Contributor

    Im not having any luck with this project. I have made my Arduno->RS232 to TTL -> 3.5 plug and it seems to work when im testing. Connecting it to my Samsung does nothing. TV does not seem like its able to send any status... What i have read its possible to send command in hex to do different thing but at this point im pausing this project... I need a new TV as well so i might solve it with bying a LG 😉


  • Hero Member

    @sundberg84 I'm not sure if any of the current LG TV's have an RS232 port anymore. Most new ones (at least the "smart" ones) are now running WebOS and can in theory be controlled through it over IP. I have two LG TV's, one is a 2010 and it has a serial port, but my 2012 Smart TV does not.

    Cheers
    Al



  • @sundberg84 That´s too bad! I thought it would work with the 3.5 mm plug.
    It would be funny to see the salespersons reaction when you are in the store and asks after a TV with a RS232 port!



  • @Smurphen said:

    I'm currently doing that. When my TV turns on, the light behind my TV also turns on. But because I don't want to spam my TV with requests, there can be up to 30 seconds delay before Domoticz knows that the TV is on or not. But there is also a USB port behind my TV. I have noticed that there is only power in the USB when the TV is on. So I thinking of connecting the USB port to my Arduino to check if the power is on or not.

    Why not have the Arduino keep polling the TV to see if it's on, and then only when it is on, then send some signal to your controller (Domotics) that the TV just came on. Then Domotics could do whatever (turn on/off other lights, etc...)? Power (battery) is not a concern on this node, as it's powered by USB.



  • @TRS-80 For some reason, if i'm sending request too frequently the response from the TV get mixed up and unreadible. Sometimes it takes few seconds for the responses too arrive.

    If I'm also changing volume or maybe channel via arduino and that is happening at the same time as arduino is checking power status, it will get som weird response. But this is only a problem when the TV is on, so you have point. I should be able to change the frequens when the power is off to maybe 5 seconds and keep 30 seconds when the power is on.



  • @Smurphen said:

    @TRS-80 For some reason, if i'm sending request too frequently the response from the TV get mixed up and unreadible. Sometimes it takes few seconds for the responses too arrive.

    If I'm also changing volume or maybe channel via arduino and that is happening at the same time as arduino is checking power status, it will get som weird response. But this is only a problem when the TV is on, so you have point. I should be able to change the frequens when the power is off to maybe 5 seconds and keep 30 seconds when the power is on.

    Perhaps further debugging and/or careful programming and error trapping between Arduino and TV could alleviate some of those problems? And/or, once TV is set to on, have Arduino check less frequently?

    I still would prefer the former solution over the latter, to make it more nice, faster response, and higher WAF as well as "show off to friends" factor. I mean, let's get our priorities straight, that is at least partly why we do this, right? 🙂





  • I am going to have to try this. I have a 60 inch Sharp Aquos TV that has a 9 pin serial port. I have ordered one of these: RS232 TTL converter It is a china version, but I couldn't beat it at $0.73 with free shipping. The other nice thing is that in the manual for my TV on page 51 it shows the complete serial protocol and command set. If I can in some way combine this with the IR sender and receiver project to control my cable box too, this would be a well rounded solution.



  • So I received my serial converter the other day and did some testing. I connected the converter to one of my FTDI adapters and to the TV. I found out how to send the commands to the TV. The TV will respond with OK if the command was successful or ERR if it wasn't. The one thing I can't get from the TV is feedback such as power on and other commands. It would be nice to be able to tell when it is turned on and off manually, but I will work with what I have.



  • @dbemowsk Nice to see that you got it working! Behinde my tv there is a USB port that is powered only when the TV is on. Maybe its the same on your tv and that way you could check if the TV is on it not.



  • @Smurphen, that is a good point.I could wire the 5 volts from that to an input on the nano and check for a state change on that. I have seen USB cables that are basically a short usb extension with another pigtail coming off to pull power for a device. Can't remember what it was from though. If I could find one of those, I could keep the functional use of the USB port (if needed) while still pulling power for this.

    My next step is to write the sketch needed to do the control from MySensors. Part of my problem in this is that I am currently running Domoticz which I am seeing doesn't work well with this. Some of the functions I may have a hard time implementing. I am looking at possibly getting a Vera controller soon though which may be more flexible in this regard.


  • Admin

    @dbemowsk

    Just out of curiosity, which parts is hard to implement in domoticz?



  • @tbowmo said:

    Just out of curiosity, which parts is hard to implement in domoticz?

    I guess I was just thinking about things like setting volume level and channel number. With my Sharp Aquos TV I have to send 2 parts, the command and the parameter. An example would be for volume, on the node side I need to send the command "VOLM" and a number from 0 to 60. I would need 2 parts in Domoticz. Somthing to tell it to use the volume command, and something else to send it the level (the 0 to 60).

    I was thinking of somehow controlling it with my Amazon Echo which I was reading up on and seems a bit tedious to integrate. I would like to be able to say things like, "Alexa, change the channel to 43", or "Alexa, set volume level to 20".

    Some of what I am planning with this node is not only control of my TV, but also IR control of my cable box. This is going to make my node more complex. I don't think it is going to be that hard to do some of the basic stuff in Domoticz, like power on/off and channel up/down, but like for volume, there is no volume up/down command. And for changing the channel, I do that on the cable box. I don't use the internal tuner on the TV. Some of this I would program the node to handle, like when a channel change is sent, do that with the IR to the cable box, whereas a power command would control both the TV and cable box.

    These are some of the things that I think are going to be a bit difficult with Domoticz.


  • Admin

    @dbemowsk

    Just an idea, use a dimmer in domoticz for the volume, when you receive a value from the controller, then send VOLM and the value.. There is no need for the controller to know how to set the volume (the VOLM command)

    For IR commands, that's probably another story, Haven't looked that much into it (yet).. Right now I have a logitech harmony elite, integrated with domoticz, so that I (from domoticz) can turn on different scenes.. But I can not send an individual command to the tv, or the radio..


Log in to reply
 

Suggested Topics

  • 8
  • 2
  • 1
  • 90
  • 3
  • 29

23
Online

11.2k
Users

11.1k
Topics

112.5k
Posts