We have moved at community.getvera.com

Author Topic: Connected by TCP LED bulbs - code to control them  (Read 27775 times)

Offline ctguess

  • Full Member
  • ***
  • Posts: 145
  • Karma: +4/-2
Connected by TCP LED bulbs - code to control them
« on: January 16, 2014, 02:59:49 pm »
Given that the 'Connected by TCP' LED bulbs ( http://www.tcpi.com/connected-by-tcp ) is one of the least expensive automated bulbs on the market, I've been doing some research to figure out if Vera can control them.  I found a website ( http://misterhouse.10964.n7.nabble.com/Success-controlling-TCP-Connected-lights-td19316.html ) that seems to have cracked the code.  Information from that page is posted below:   You basically need to send an HTTP POST message with your bulb's device ID (DID) and the value you want to conrol.  Further down in the linked page is the perl code needed to connect to the gateway and print a list of all your bulb device ids.   

The misterhouse website/project seems to be an open source HA project written in Perl.  Vera, of course, would need to send these HTTP POST messages in some Lua/Luup code.  Can anyone help and post, perhaps with the fully encoded on/off example at the bottom of my origianal post, the code needed to send the HTTP POST message in lua/luup within a scene?


------------
The decoded commands (with encoded versions < > and / characters
converted to plain ASCII) look like this:

ON/OFF:
> cmd=DeviceSendCommand&data=<gip><version>1</version><token>1234567890</token><did>YOURDID</did><value>VALUE</value></gip>&fmt=xml

where YOURDID is the DID of the light you want to control and VALUE is
either 0 (for off) or 1 (for on)

Brightness:
> cmd=DeviceSendCommand&data=<gip><version>1</version><token>1234567890</token><did>YOURDID</did><value>DIMVALUE</value><type>level</type></gip>&fmt=xml

where YOURDID is the DID of the light you want to control and DIMVALUE
is the brightness in percent (don't use a % sigh on the end).

You can send them to the light with a command like this (properly
encoded and thus a bit harder to read) to turn on a light:

> echo -n "cmd=DeviceSendCommand&data=%3Cgip%3E%3Cversion%3E1%3C%2Fversion%3E%3Ctoken%3E1234567890%3C%2Ftoken%3E%3Cdid%3EYOURDID%3C%2Fdid%3E%3Cvalue%3E1%3C%2Fvalue%3E%3C%2Fgip%3E&fmt=xml" | POST -P "http://YOURGWIP/gwr/gop.php"

Offline ctguess

  • Full Member
  • ***
  • Posts: 145
  • Karma: +4/-2
Re: Connected by TCP LED bulbs - code to control them
« Reply #1 on: January 18, 2014, 04:17:24 pm »
I was able to answer my own question....

Happy to share my findings with the community.  I now have a Connected by TCP bulb controlled by two simple Vera scenes - one to turn on and one to turn off.  The steps to get this working are:

1. The first thing you need to do after installing the kit (TCP gateway and bulb(s)) is to find the device id of the specific bulb you want to control.  To do that, use a Chrome browser (Safari on OS X didn't work) and send in the following request by pasting the following into the address bar and replacing GWURL with your TCP gateway's IP address:

Code: [Select]
http://GWURL/gwr/gop.php?cmd=GWRBatch&data=<gwrcmds><gwrcmd><gcmd>RoomGetCarousel</gcmd><gdata><gip><version>1</version><token>1234567890</token><fields>name,image,imageurl,control,power,product,class,realtype,status</fields></gip></gdata></gwrcmd></gwrcmds>&fmt=xml
Contrary to other hints and findings on the internet that led to my success, the URL does not need to be encoded...pasting that "readable" format in worked fine for me.

2.  Once you have your device id <did> you can use the following Lua code to turn on your TCP bulb

Code: [Select]
local http = require("socket.http")
 
  -- 5 Second timeout
  http.TIMEOUT = 5
 
 
  result, status = http.request("http://GWIP/gwr/gop.php", "cmd=DeviceSendCommand&data=<gip><version>1</version><token>1234567890</token><did>216241969746673524</did><value>1</value></gip>&fmt=xml")

Replacing GWID, again, with your TCP gateway IP address and replacing the <did> with the value you found in step 1.  I left my <did> value in the example above so you can see what it looks like...18 characters long.   A 1 in the <value> tag turns the bulb on, a 0 turns it off. 

I have not implemented the dimming function yet, but it seems to be very straightforward, the code is below (found on the webpage I mentioned in my first post)...

Brightness:
Code: [Select]
cmd=DeviceSendCommand&data=<gip><version>1</version><token>1234567890</token><did>YOURDID</did><value>DIMVALUE</value><type>level</type></gip>&fmt=xml





Offline jimpapa

  • Sr. Member
  • ****
  • Posts: 454
  • Karma: +28/-1
Re: Connected by TCP LED bulbs - code to control them
« Reply #2 on: January 18, 2014, 05:04:07 pm »
Nice work!

Offline pmnb

  • Full Member
  • ***
  • Posts: 103
  • Karma: +2/-0
Re: Connected by TCP LED bulbs - code to control them
« Reply #3 on: January 18, 2014, 07:11:09 pm »
I was able to answer my own question....

1. The first thing you need to do after installing the kit (TCP gateway and bulb(s)) is to find the device id of the specific bulb you want to control.  To do that, use a Chrome browser (Safari on OS X didn't work) and send in the following request by pasting the following into the address bar and replacing GWURL with your TCP gateway's IP address:

Code: [Select]
http://GWURL/gwr/gop.php?cmd=GWRBatch&data=<gwrcmds><gwrcmd><gcmd>RoomGetCarousel</gcmd><gdata><gip><version>1</version><token>1234567890</token><fields>name,image,imageurl,control,power,product,class,realtype,status</fields></gip></gdata></gwrcmd></gwrcmds>&fmt=xml

Great work!

To save a step, you can use the local hostname "lighting" instead of the IP address:

Code: [Select]
http://lighting/gwr/gop.php?cmd=GWRBatch&data=%3Cgwrcmds%3E%3Cgwrcmd%3E%3Cgcmd%3ERoomGetCarousel%3C/gcmd%3E%3Cgdata%3E%3Cgip%3E%3Cversion%3E1%3C/version%3E%3Ctoken%3E1234567890%3C/token%3E%3Cfields%3Ename,image,imageurl,control,power,product,class,realtype,status%3C/fields%3E%3C/gip%3E%3C/gdata%3E%3C/gwrcmd%3E%3C/gwrcmds%3E&fmt=xml

Note that this only seemed to work in the browser and not in LUUP code, where I had to use the IP.
« Last Edit: February 03, 2014, 09:30:42 pm by pmnb »
Author of TCPLighting plugin

Offline pmnb

  • Full Member
  • ***
  • Posts: 103
  • Karma: +2/-0
Re: Connected by TCP LED bulbs - code to control them
« Reply #4 on: January 26, 2014, 06:18:09 pm »
BTW, in addition to scene scripting, I got ambitious and decided to use ctguess's info to write a plugin for a Connected By TCP gateway.  It's pretty well done, but I need to add in polling logic and would like to test some more edge cases.  Since I am off on business for a week, it won't be until the week of the 3rd that I'll have something to share.

The basic approach I followed is to create a new device type for the gateway, which in turn creates standard child devices (Dimming1 or PowerSwitch) for each bulb.
« Last Edit: February 02, 2014, 10:10:08 pm by pmnb »
Author of TCPLighting plugin

Offline pmnb

  • Full Member
  • ***
  • Posts: 103
  • Karma: +2/-0
Re: Connected by TCP LED bulbs - code to control them
« Reply #5 on: February 02, 2014, 09:24:17 pm »
Author of TCPLighting plugin

Offline lininger

  • Newbie
  • *
  • Posts: 18
  • Karma: +1/-0
Re: Connected by TCP LED bulbs - code to control them
« Reply #6 on: February 05, 2014, 08:34:21 pm »
I have been capturing packet to figure out the command set for TCPi as well here are a few I have come up with.

To get your device id's
cmd=GWRBatch&data=<gwrcmds><gwrcmd><gcmd>SceneGetList</gcmd><gdata><gip><version>1</version><token>1234567890</token><fields>bigicon,detail,imageurl</fields><islocal>1</islocal></gip></gdata></gwrcmd></gwrcmds>&fmt=xml

cmd=DeviceSendCommand&data=<gip><version>1</version><token>1234567890</token><did>DEVICE_ID</did><value>50</value><type>level</type></gip>&fmt=xml

cmd=GWRBatch&data=<gwrcmds><gwrcmd><gcmd>SceneGetList</gcmd><gdata><gip><version>1</version><token>1234567890</token><fields>bigicon,detail,imageurl</fields><islocal>1</islocal></gip></gdata></gwrcmd></gwrcmds>&fmt=xml

cmd=GWRBatch&data=<gwrcmds><gwrcmd><gcmd>RoomGetCarousel</gcmd><gdata><gip><version>1</version><token>1234567890</token><fields>name,image,imageurl,control,power,product,class,realtype,status</fields></gip></gdata></gwrcmd><gwrcmd><gcmd>UserGetListDefaultRooms</gcmd><gdata><gip><version>1</version><token>1234567890</token></gip></gdata></gwrcmd><gwrcmd><gcmd>UserGetListDefaultColors</gcmd><gdata><gip><version>1</version><token>1234567890</token></gip></gdata></gwrcmd></gwrcmds>&fmt=xml"
cmd=SceneGetList&data=<gip><version>1</version><token>1234567890</token><did>DEVICE_ID</did><fields>activeonly,bigicon,detail,imageurl</fields><islocal>1</islocal></gip>&fmt=xml


Adjust Dimness
cmd=DeviceSendCommand&data=<gip><version>1</version><token>1234567890</token><did>DEVICE_ID</did><value>29</value><type>level</type></gip>&fmt=xml

Trun On
cmd=DeviceSendCommand&data=<gip><version>1</version><token>1234567890</token><did>DEVICE_ID</did><value>1</value></gip>&fmt=xml

Gateway Info
cmd=GatewayGetInfo&data=<gip><version>1</version><token>1234567890</token><fwnew>1</fwnew></gip>&fmt=xml

When I try to use your Luup files
I get one _dimmable Light in the device tab.  I have two such bulbs that I can get data to and from via the browser direct commands

It looks like the log shows the gateway not found at startup
50   02/05/14 19:37:37.494   luup_log:26: SysMon: Initialising System Monitor Plugin <0x2be45680>
50   02/05/14 19:37:37.495   luup_log:26: SysMon: Startup complete <0x2be45680>
50   02/05/14 19:37:37.499   luup_log:36: TCPLighting: tcplighting_plugin_startup - start <0x2be45680>
50   02/05/14 19:37:37.499   luup_log:36: TCPLighting: tcplighting_plugin_startup - not gateway <0x2be45680>
50   02/05/14 19:37:37.500   luup_log:36: TCPLighting: tcplighting_plugin_startup - end <0x2be45680>

Is there any other data that needs to be added when entering the information in step 4?

08   02/05/14 19:39:34.824   JobHandler_LuaUPnP::HandleActionRequest device: 36 service: urn:upnp-org:serviceId:Dimming1 action: SetLoadLevelTarget <0x2ec45680>
08   02/05/14 19:39:34.825   JobHandler_LuaUPnP::HandleActionRequest argument DeviceNum=36 <0x2ec45680>
08   02/05/14 19:39:34.825   JobHandler_LuaUPnP::HandleActionRequest argument serviceId=urn:upnp-org:serviceId:Dimming1 <0x2ec45680>

Thanks

I think the plug- in will be fantastic

Offline tb001

  • Sr. Member
  • ****
  • Posts: 394
  • Karma: +5/-2
Re: Connected by TCP LED bulbs - code to control them
« Reply #7 on: June 29, 2014, 10:53:08 am »
Thanks for working on this! Do you know if the TCP bulbs will work to extend the mesh of my z-wave network? My guess would be no, but I'm hoping I'm wrong...

Offline garrettwp

  • Master Member
  • *******
  • Posts: 6371
  • Karma: +227/-128
  • Vera 3, Lite, ISY994
Re: Connected by TCP LED bulbs - code to control them
« Reply #8 on: June 29, 2014, 12:12:04 pm »
The TCP bulbs are not z-wave. It will not extend your z-wave network.

- Garrett

Offline tb001

  • Sr. Member
  • ****
  • Posts: 394
  • Karma: +5/-2
Re: Connected by TCP LED bulbs - code to control them
« Reply #9 on: June 29, 2014, 12:17:53 pm »
The TCP bulbs are not z-wave. It will not extend your z-wave network.

- Garrett

Thanks! I would have sworn the TCP rep I spoke with at CES told me they were z-wave, but that was before I was neck deep in this stuff trying to figure it out, so likely remembering wrong. Should have double checked that oh so basic fact before posting!

Offline pmnb

  • Full Member
  • ***
  • Posts: 103
  • Karma: +2/-0
Connected by TCP LED bulbs - code to control them
« Reply #10 on: June 29, 2014, 12:49:56 pm »
The protocol they use is Jennet.
« Last Edit: June 29, 2014, 03:03:12 pm by pmnb »
Author of TCPLighting plugin

Offline Bren1818

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
Re: Connected by TCP LED bulbs - code to control them
« Reply #11 on: August 20, 2016, 11:25:34 am »
Thanks everyone for the info, I have stated a simple php Web application to control the bulbs, create schedules and read scenes. It works with the new and old bridge firmware. I'm trying to essentially make an app clone with a Web interface. Controlling the bulbs is in there.

Check it out here :

https://github.com/bren1818/TCPLightingWebInterface

Hope it helps someone