PDA

View Full Version : iPhone Lighting Sliders - Dimming to integer value & Poll check in httpd



raypp2
October 16th, 2009, 02:20 PM
I am attempting to make a custom iPod Touch web app for Girder 4. Preliminary tests are going well with iUI for the front-end and basic SendEvents AJAX:
OnClick='return SendEvent("Command to Run", 18);'

So now I am trying to take it to the next level. First order of business is variable sliders for lighting control. I'm planning to use the "range" input control for safari and AJAX to send values. Here are my questions:

1) Has anyone tried this before and can share some wisdom?
2) How can I poll lighting dim levels and return them via AJAX?
3) How should I send an integer value to Girder with the SendEvent function? I assume I should use a different device code but I'm unclear on what this should be.
4) Question 3 will allow me to trigger a script but how can I then pass that value to a script?

Thank you in advance.

Ron
October 16th, 2009, 02:26 PM
Well there is the Girder 5 implementation of what you are trying to do :)

raypp2
October 16th, 2009, 03:30 PM
Hi Ron,

Thanks for your reply. I know that Girder 5 does have an iPhone implementation which I have tested on a trial. Unfortunately, I need a more customized approach for my needs. For example, Girder 5 did not have a continuous slider in my tests. It seemed to stop at predetermined positions. I also understand that there is added functionality for passing Insteon commands over AJAX but I also would not like to upgrade if at all possible. Please let me know if I can accomplish this in version 4. Thanks.

Ron
October 16th, 2009, 03:35 PM
You can probably do this on Girder 4 yes, I am not sure how well the web-server will hold up but it's worth a try. I suggest using the JQuery libraries, they have sliders as well.

raypp2
October 16th, 2009, 04:09 PM
Hi Ron,

Thanks for the suggestion. I understand that there are some issues with the iPhone and iPod and any drag-drop components because it uses the same motions for navigation around the page. This may have been corrected in more recent versions. For the moment, I am just testing the "range" input type. Thus far, it has been a very simple matter to pass commands to Girder
<form><input type="range" value="75" OnInput='return SendEvent(this.value, 18);'></input></form>
My concern is the other questions in my post. Please let me know if you can shed any light. Thanks.

raypp2
October 19th, 2009, 01:51 PM
So after a bit of work this weekend, here is an update:

The iPhone / iPod Touch will not allow you to use a traditional slider in the safari mobile browser. It will work with jQueryui slides if you click anywhere on the bar, but you can not slide it with a touch and drag. I am going to attempt to program something with the SDK but if that doesn't work or becomes too time consuming, I'll just stick with the non-sliding option. In order to transmit the commands:

1) Created a device in the device manager for "Lighting Contol" and gave it an ID of 4001

2) Created a new "ajax_lighting.lhtml" file based on ajax_sendevent.lhtml with added variables for pld1 and pld2 in order to carry the lighting device and dim setting


<%
webserver:SetGZIP(false)
webserver:SetHeader("Cache-Control: no-cache, must-revalidate")
webserver:SetHeader("Expires: Fri, 30 Oct 1998 14:19:41 GMT")
local table = webserver:GetCGI()
if ( gir ) then
gir.TriggerEvent( table['event'], table['device'], table['pld1'], table['pld2'], 0)
end
%>

3) Added a function to the girder.js file to accommodate the new variables


function SendLighting( event, device , pld1 , pld2 )
{
var formRequester = null;

try
{
formRequester = new XMLHttpRequest();
}
catch (error)
{

try
{
formRequester = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (err)
{
try
{
formRequester = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (err2)
{
formRequester = null;
return false;
}
}
}

formRequester.open("GET", "ajax_lighting.lhtml?event="+event+"&device="+device+"&pld1="+pld1+"&pld2="+pld2);

formRequester.onreadystatechange = function()
{
if (formRequester.readyState == 4)
{
return true;
}
}

formRequester.send(null);
return false;
}

4) Created a script action triggered by a call to the "Lighting Control" Device


--[[
From AJAX call:

EventDevice: Should be Lighting Control Device
EventString: Should be "Dim"
pld1: Target Insteon Controller
pld2: Dim setting

DEBUG
print(EventDevice)
print(EventString)
print (pld1)
print (pld2)
--]]

local deviceTable = {
OfficeHTPC = "00.00.00",
OfficeBrick = "00.00.00",
LivingRoomProjection = "00.00.00",
LivingRoomBrick = "00.00.00",
Kitchen = "00.00.00",
Hallway = "00.00.00",
DiningRoom = "00.00.00"
}

DeviceID=deviceTable[pld1]
DeviceLevel = (((pld2)/3.125)* 7.96875)

local device = Insteon.DeviceManager:GetDevice(DeviceID)
assert(device,'Device not found')
Insteon.DeviceCommands.SetLevel(DeviceID,DeviceLev el)

print ("Changed " .. pld1 .. " (" .. DeviceID .. ") to " .. pld2 .. "% (" .. DeviceLevel .. ")")

5) Called the function with the lighting slider

<form><input id="office_htpc" type="range" OnInput='return SendLighting("Dim", 4001, "OfficeHTPC", this.value);'></input></form>

Still todo:

Create a function to poll the lighting levels and load those values into the sliders when the page loads
Write an iPhone native application to send these commands so I have more interface flexibility

raypp2
October 21st, 2009, 10:56 AM
When I tested the commands I posted, I realized that the Insteon system did not perform so well with quickly updating dim commands therefore killing the possibility of real-time sliders. I adjusted so just the stop point of the slide would be sent. This system limitation probably negates the need to create an iPhone native app since I can accomplish what I need without draggable sliders. I used Jquery UI sliders with steps of 10 and just click along the bar.
http://www.yoray.com/home_control_project/Lighting_Page_Screenshot_v01.png
I also created the query system to update the sliders which I will include in a later post.

The individual light changes work almost flawlessly. The master switch is another story. I wrote the following code to deal with the dim commands:


--[[
From AJAX call:

EventDevice: Should be Lighting Control Device
EventString: Should be "Dim"
pld1: Target Insteon Controller
pld2: Dim setting

DEBUG
print(EventDevice)
print(EventString)
print (pld1)
print (pld2)
--]]

local deviceTable = {
OfficeHTPC = "00.00.00",
OfficeBrick = "00.00.00",
TheaterFront = "00.00.00",
TheaterRear = "00.00.00",
Kitchen = "00.00.00",
Hall = "00.00.00",
DiningRoom = "00.00.00"
}

--Translate the DeviceLevel into an Insteon compatable value
DeviceLevel = (((pld2)/3.125)* 7.96875)

if pld1=="Master" then
actionTable=deviceTable
else
--Clear the table and only set the first value
actionTable = nil
actionTable = {onlyONE = deviceTable[pld1]}
end

for k in pairs(actionTable) do
DeviceID=actionTable[k]

local device = Insteon.DeviceManager:GetDevice(DeviceID)
assert(device,'Device not found')
Insteon.DeviceCommands.SetLevel(DeviceID,DeviceLev el)

print ("Changed " .. pld1 .. " (" .. DeviceID .. ") to " .. pld2 .. "% (" .. DeviceLevel .. ")")

end

The issue I am having now is that the system fails to execute dim commands on average 30% of the time when using the master. I believe it has something to do with the rapid succession of Insteon commands. There is no pattern to the failures. I suspect that I may have to create a delay between the commands. I can't imagine I'm the only person to have experienced this problem. Any thoughts?

Also, if would be useful to know the Lua commands for All On and All Off. Thanks.

Mike C
October 21st, 2009, 03:28 PM
hi

are you using the PLC with the SDM and Girder 4?

raypp2
October 21st, 2009, 04:35 PM
hi

are you using the PLC with the SDM and Girder 4?
Hi Mike,

Yes, I am using the PLC with SDM:

Version: 1.01.122.278
Plugin Version: 1.4.3
Core App Version: 12

Mike C
October 21st, 2009, 11:10 PM
i think that is going to give you a fair amount of grief :(

the sdm does the command pairing ie it skims off extra or similar commands but it doesn't do it well.

and the plc is unreliable.

i know you don't want to spend the $ but the PLM with G5 is that much better. i wrote the component for the PLM in G5. the command pairing is much more sophisticated and works allowing the use of a slider with resonable results (upto 4 dim level changes per second). and the code is free.

raypp2
October 23rd, 2009, 03:56 PM
Thanks Mike. Ok, well I'll probably do another trial on Girder 5 and see how it works comparitively but I'm hoping you can answer 2 questions for me:
1) Based on the code I have posted, will I have to modify it, particularly with the Insteon commands?
2) If I did stick with Girder 4, would I need to put a delay between commands? If so, what would you recommend?

Thanks.

Mike C
October 25th, 2009, 05:57 PM
hi,

i do not use the webserver apart from as implemented for the G5 Device Manager - so i cannot comment on your code.

you might get better results putting a delay between sending commands, ie 500 ms or so.

after spending hundreds of hours on the sdm/plc combo i cannot emphasive how much better the plm is.