Archive for the ‘Flash’ Category

Just thought I’d post this little tidbit.  When using Flash/action script to call the infamous ‘Loadvariables’ functions I have found that some browsers like IE like to grab cached results, for some reason it worked fine in Firefox but if you try to embedded IE in your programs you will run into the same problems.

In my case I call the same function and the server side takes care of the action.  And if your like me you may be calling the same page more than once quickly.  This issue got me for days, and worked on some computers and other didn’t.

So when calling your pages just add a fictional variable not used by your pages, I called my cache_buster and add ‘date’ and ‘time’  to get a 8-15 character number that should be unique, tricking the browser you are looking for a unique page and not grab from cache

loadVariables("http://192.168.0.2:85/finddata.aspx?show_all_configs=1&cache_buster="+new Date().getTime(), this);

One of the questions ive been asked is how to I put my flash script to grab data from the SQL database and provide updates if the device is on or off etc.

Well the awnser is, I dont. Like mentioned before to have your flash talk directly to a sql database you will need additional components which I dont want to install $$$, such as cold fusion or some advanced Flash stuff.  Again I want speed and functionality, especially if its only for a few users.

So what I did was use a internal web server which is installed with VB 2008, to do all the talking for me, more like a middle man.  for example my flash program talks to this web page using the ‘load’ function which sends data using the POST commands (example http://192.168.0.101/finddata.aspx&IN=14) and the code runs and returns the values needed in a specific variable in this case ‘VALUE’. you can also use the LoadVariables command which explain that in this article.

Flash:Loading Variables from files into Flash

So here is a high level of what is being done in the background.

All all times the Insteon Server is updating the SQL Server with the status of the devices as they are turned on or off, so we dont need to connect there. Also note that the Local .net, SQL database and insteon server are all on the same box!

For example lets say I want to pull the status of device #14. (Fan in master bedroom)  This is what I would do from the Flash Program

Example in FLASH CLIENT;

Prepare: In Flash you will need to create a empty movie clip called ‘fan_master’.  Also have images already in your webpage in the location mentioned below. I call it right of the same page you can use your local library if you like.

Here are my images I use in my Flash program,  you may use them if you like (Just right click on them and use SAVE AS)

Light_bulb_off.png

Light_bulb_on.png

Off Button (small_button_on.png)

On Button (small_button_off.png)

In my actionScript, I would put on one of the frames;

//--------------------- SETUP A NEW VARIABLE TO GRAB MY DATA THIS FUNCTION WILL RUN WHEN THE BELOW SITE IS OPENED
var mfan:LoadVars = new LoadVars();
    mfan.onLoad = function() {

//------ Here I check the return value that is returned back as 'value' if its is ON then replace the fan_master 'movie' to the bulb on image
          if (mfan.value=="ON"){
            loadMovie("http://192.168.0.101/images/small_button_on.png", fan_master);
        } else {
            loadMovie("http://192.168.0.101/images/small_button_off.png", fan_master);
        }
    };
//---------------- do the actual opening of the webpage and pass the variable IN and the device im asking for
mfan.load("http://192.168.0.101/finddata.aspx?in=14");
stop();

BLUE BOX (SERVER .NET SIDE)

A description of my Devices Database is found here

‘————————— VB 2008 / WEBPAGE filename finddata.aspx

On my Server side I would grab the value on “IN” using the

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      '------- Device will hold the value of in 'IN' which is #14
      device = Context.Request.Params("IN")
      value = Lg_Get_Device_Status(device)
      Response.Write("value=" + value)
 End Sub

This will return to my program the following text
“value=ON” or “value=OFF”

‘——– Function to grab the device data by the device #

    Public Function Lg_Get_Device_Status(ByVal Marker As String) As String

        Dim sqlConnection10 As New System.Data.SqlClient.SqlConnection(ConnectionSTring) ' Set your connectionstring accordingly.
        Dim Cmd10 As New System.Data.SqlClient.SqlCommand

        '-------- We'll use the SqlDataReader to get the values of the table 
        Dim Reader As SqlClient.SqlDataReader

        Cmd10.CommandText = "SELECT * FROM Devices where Marker= '" + Marker + "'"
        Cmd10.Connection = sqlConnection10
        sqlConnection10.Open()
        Reader = Cmd10.ExecuteReader()

        ' check for data, if not return 'FALSE'
        If Reader.HasRows = False Then
            Lg_Get_Device_Status = False
            Exit Function
        End If
        Reader.Read()
        '-------- RETURN THE VALUE FROM THE DATABASE SUCH AS ON OR OFF OF %
        Lg_Get_Device_Status = Reader.Item("status")
        sqlConnection10.Close()

    End Function

And there you have it, I do use the same file (finddata.aspx) for other stuff and you can easily query what is being passed and eventually act on it like this,

For example if I pass(showall=1) it would run another function, but that is another article.

        '------------------- SHOW ALL DEVICES
        If Context.Request.Params("showall") <> "" Then Show_insteon_devices() : Exit Sub

        '------------------- SHOW ALL DEVICES by MAC address
        If Context.Request.Params("showmacs") <> "" Then Show_insteon_devices_mac() : Exit Sub

        '------------------- SHOW ALL CONFIGS
        If Context.Request.Params("show_all_configs") <> "" Then Show_all_configs() : Exit Sub

Instead of re-inventing the wheel this is the document I used to get data for my flash variables

http://www.smartwebby.com/flash/external_data.asp

Basically using the “loadvariables” command you can read any file containing properly formated text, and just match up the variable with the variable in the text file, for example

loadVariables("http://192.168.0.101/data.txt",this);

The File ‘data.txt’ contains

name=1

If using more than one variable use the & (Ampersign) for example

name=1&address=somewhere&city=Kissimmee&state=FL

Presto…

Since my main database is SQL, I would have liked to read that data directly but unless you use Coldfussion and other goodies you not able to get data directly, but what you can do is have a backend page such as PHP,ASP or something else which does the query for you.

One thing I always wanted to do was be able to setup a automatic coffee machine and turn it on at will.  Yes I know there are many out there already that do this, but we want to automate this right??

So after checking with my die hard Insteon users’ regarding wattage and amps.  I decided to give a try putting a  Icon’s Appliance Link, and see how it works. Below is my candidate #27.  You can get this off of Ebay like for $25 each.

The Applicance link Is rated 400 watts and 10amps, after playing with the Kill a watt device (Which by the way are worth every penny, they can tell you how much watts, amps and other goodies any device you plug into them.) It comes in at 825 watts but only 8amps.  So it should work. 🙂 And work it did.

Here is a picture of #27,Icon Appliance Link next to a Insteon dimmer which has all the features of the decora style , and a regular toggle switch

So after creating a simple Flash screen it will relay the information back to the server.

Here is my screen, basically allows you to turn on the device at a certain time and leave it on after that.

The button are tradition button which increment the time, below the current time is updated each second.  I show this here

Flash:Showing the ‘time’ in flash continuously.

The action script is very simple,  create the fields one for the hour and put the var field called ‘chours’, the minutes is called ‘minutes’ the AM or PM setting is called ‘ampm’

Here is what you will put as the actionscript for each button.

The hours UP button (If the value is 13 scroll back to 1)

on (release) {
    chours=int(chours)+1;
    if (chours==13) chours="1";       
}

The hours DOWN button (If the value is 0 then set as 12)

on (release) {
    chours=int(chours)-1;
    if (chours==0) chours="12";       
}

Minutes Down Button (If minutes hits 0 then set as 59, also put a 0 in front of any value under ten)

on (release) {
	minutes=int(minutes)-1;
	if (minutes<10) {
		minutes="0"+minutes;
		}
	if (minutes=="0-1")
	{
	minutes=59;
		chours=int(chours)-1
	}
}

Minutes Down Button (If minutes hits 60 then set as 00, also put a 0 in front of any value under ten)

on (release) {
    minutes=int(minutes)+1;
    if (minutes<10) {
      minutes="0"+minutes;
        }
   if (minutes==60)
    {
    minutes="00";
        chours=int(chours)+1;
    }   
}

The button which changes the AM / PM Setting

on (release) {
	if (ampm=="AM") {
		ampm="PM";
		} else {
		ampm="AM";
}
}

The Confirm button actually uses the internal SDM webbrowser which is picked up by my server, in the same string I send the variables.
I prepare the variable called ‘string’ and send it and presto!  We will use actionscript’s ‘loadvariable’ command actually to send the data.. funny…


on (release) {
string=”http://192.168.0.101:9020/abc.txt?DCCM”+chours+&#8221;:”+minutes+”:”+ampm;
loadVariablesNum(string, 0);
}

Note: If you need to read previos setting into these fields you can check out this entry which explains how up update fields from files.
Flash:Loading Variables from files into Flash

Enjoy!

Last week I was working on automating the house’s coffee machine.  And a real neat feature I saw around was the ability to display updated content in flash.
In bare bones, simple copy and paste, below is the the code I used to update the clock each second.

Basically open a scene of one frame and insert the code below on it.

Here is a sample of my coffee machine with the time below.   The images were done in Photo Impact and the using Flash CS3 and ActionScript 2.0

In your flash script insert in to frame 0 the following code by right clicking on the frame and selecting ‘action’

In my example above just create a dynamic text with the var setting as ‘txtTime’
Please remove the ‘Comments’ before copy and pasting.’ they are for showing what is being done and will give you errors if not removed 🙂

'-------------------- Setup Variables
var timeint;
'---------------- Set the timer to run the function called 'timer' each second or 1000 milliseconds
timeint=setInterval(timer,1000);

'------------------------ the function itself
function timer() {

'--------------------- grab the date, and break down by hours, minutes and AM or PM
  oDate = new Date();
  nHours= oDate.getHours();
  nMinutes = oDate.getMinutes();
'------------------------------------ If minutes is less the 10 then add a 0 in front so it looks like 01,02,03 etc.
  if (nMinutes<10) nMinutes="0"+nMinutes;
  nSeconds = oDate.getSeconds();

'------------------------------------ If seconds is less the 10 then add a 0 in front so it looks like 01,02,03 etc.
  if (nSeconds<10) nSeconds="0"+nSeconds;

'---------------------------------------- If hours is over 24 then its PM.
  sAmPm = (nHours < 12) ? " AM" : " PM";
'--------------------------------- if hours >12 then subtract -12

  nHours = (nHours%12 == 0) ? 12 : nHours%12;

'--------------- Update Dynamic variabel to .txtTime on the _root layer. 
  _root.txtTime = nHours + ":" + nMinutes + ":" + nSeconds + sAmPm
}

stop();   '---------- Remember to stop the movie if not stopped somewhere else.