Status update - scripting it away

General HouseBot discussion. Any issues that don't fit into any of the other topics belong here.
dtresinari
Member
Posts: 5
Joined: Wed Aug 19, 2009 11:19 am
Location: Brazil

Status update - scripting it away

Post by dtresinari »

Hello everyone,

Greetings from Brazil. 8)

First I would like to say thank you very much for this software… it´s great, user-friendly and still able to expand. Congrats !

Well now, down to work… :D

I have a generic serial module with 8 sensors. The only ASCII command I can send it is “8INR” and it will respond “8INR @” where @ is a number that varies from 0 to 255. From the number in @ you can determine the sensors that are active. Like below:

# - sensor active
0 – none active
1 – sensor 1 active
2 – sensor 2 active
3 – sensors 1 and 2 active
4 – sensor 3 active
5 – sensors 1 and 3 active
6 – sensors 2 and 4 active
7 – sensors 3 and 4 active
8 – sensor 4 active
9 – sensors 1 and 4 active
16 – sensor 5 active
32 – sensor 6 active
64 – sensor 7 active
128 – sensor 8 active
And so on untill
255 – sensors 1, 2, 3, 4, 5, 6, 7 and 8 active

Basically what I need HB to do is to send this “8INR” command and receive the string and update the sensor status. I have setup the hardware interface and the command, setup the device with command and the 8 ports and now I need to make HB react to the string that comes back from the hardware and update the status… And here is where I need HELP.

I´m not sure how difficult a script like this would be and if any of you would be willing to write it for me. But it´s worth a shot… :wink: :wink: :wink:

Anyone interested in helping ? (here is where I would place quite a few beer smilies...)

Thanks in advance.

Regards,

Daniel
edgar
Member
Posts: 95
Joined: Tue Mar 24, 2009 11:14 pm
Location: Springfield, VA

Re: Status update - scripting it away

Post by edgar »

Hi Daniel,

I have a very simular situation. I communicate with my security alarm panel via a custom serial interface. I can send an overall status command (a bunch of hex code) to it and i get back the entire status of the panel in a series (all separated by carriage return) as numbers as hex code. Inlcudes (keypad status, zones open or closed, etc.).

I send the status command when Housebot startsup by creating a task called Startup (this is executed on startup by HB automatically). Which gets all my devices in housebot 'in sync'........read on...

So, instead of trying to parse out the Hex codes using a script, I looked at my requirement. I only needed to know if a zone is open or closed. I only really needed to check for two states (even 3 would not be bad). Because my panel simply sends numbers, all I needed to know was what the response for the various conditions were "open is 015569 and closed is 015568" for example.

Others have used scripts before to keep track of things like this. If I needed to parse out many pieces of information from the data string I would have gone that route but I did not need that. Scripts also have to be continuously executed by HB when it receives data on the serial port. My alarm panel sends out lots of data. I did not really need to run a script for every time data was received.

What I did:

(1) I defined a null device, for example call it zone 1, with an alpha property that can be open or closed. (you could check more conditions if you needed to as well).

(2) I create a task 'alarm panel' that continuously checks my received serial data:
If serialdatareceived is 015569 then
change zone1 to open
if serialdatareceived is 015568 then
change zone1 to closed

(3) its that simple and works great. when anyone opens a door.....the zone opens changing a green button to red on my security panel on my SWRemote!

I don't know if a script is more efficient but i don't observe any delay whatsoever on my system and have quite a bit of other stuff going on. You can probably guess that coding is not my strong point. So I avoid it when I can.

Hope this helps.

v/r

Kevin

(2) then i create a simple task.......if command received from my alarm panel
dtresinari
Member
Posts: 5
Joined: Wed Aug 19, 2009 11:19 am
Location: Brazil

Re: Status update - scripting it away

Post by dtresinari »

:D :D :D :D

Kevin, thanks a lot ! You made my day. I´m going home early today to test this.

Just two questions:

- Serialdatareceived you mention is the device´s "Received Data" property ?
- Do i need a loop to have it working continuously ?

Regards,

Daniel
Richard Naninck
HouseBot Guru Extraordinaire
Posts: 1121
Joined: Tue Sep 28, 2004 7:49 am
Location: The Netherlands

Re: Status update - scripting it away

Post by Richard Naninck »

You could set up a task that tests the Received Data property for "_". You can be sure that test will never pass, but the task triggers because the Received Data property changes. There is your loop. So now you have set up a task that fires upon change of the Received Data property, you can have the task do the stuff you need it to do.
However, if you want to keep track of all 255 sensors, a script would be in place. With some and's and or's you can filter the sensors which are on and of and go from there using subs in a script.
You could use the same task described above to fire your script any time the Received Data property changes. Scripts just read a lot easier than tasks and are way more flexible for more advanced stuff.
Then you could also create a continuous running script.
The script would look something like this:

Code: Select all

oldData = ""

Do
  Sleep 1
  Data = GetPropertyValue("device.Received Data")
  If Data <> oldData Then
    oldData = Data
    Call ParseYourStuffHere(Data)
  End If
Loop

Sub ParseYourStuffHere(Data)
'Here is where the rest goes
End Sub
Now, the Sleep 1 is there to keep your CPU usage at 0%. The oldData compare is there because you only want to do stuff when the Received Data changes. Etc

Or you create a script that reads the Received Data property when you want it to. Meaning only after you sent a request to your device by sending it 8INR than you trigger your script to read and parse and do stuff.

There are many examples around in the scripts section. You may want to check some of my examples where the generic serial device is used to get data from AV euquipment and even a video matrix or the ELK M1 alarm system. It should be very readable (I hope).
edgar
Member
Posts: 95
Joined: Tue Mar 24, 2009 11:14 pm
Location: Springfield, VA

Re: Status update - scripting it away

Post by edgar »

Hi all,

Good stuff Richard. I will need to go through it to disect it... Scripting is not a strong point for me...i can figure it out but takes me a while....a must have for processing lots of data.

For Daniel:
Yes, I check the Received Data property. You don't need a loop...HB loops internally, always checking to see when things change. I used to use Event Control System by omnipotence software (ECS) and it worked the same way......I call it an event pass. I am not sure how often HB does this but it works fine and reacts whenever the Received Data Property changes. I can see how this might load up your CPU if you have lots of Tasks doing similar things.

v/r

Kevin
Richard Naninck
HouseBot Guru Extraordinaire
Posts: 1121
Joined: Tue Sep 28, 2004 7:49 am
Location: The Netherlands

Re: Status update - scripting it away

Post by Richard Naninck »

I can say that my setup does a lot, but the cpu usage is always low. So no problems whatsoever on a heavy HouseBot setup. Obviously when done from scripting, don't forget a sleep 1 because otherwise you will see 100% cpu.
dtresinari
Member
Posts: 5
Joined: Wed Aug 19, 2009 11:19 am
Location: Brazil

Re: Status update - scripting it away

Post by dtresinari »

Thank you both, I was very pleased to get this mutch assistance.

I still wasn´t able to put in practice Kevin´s suggestions by sheer lack of time. As for Richard´s I know it would be better but its not my area of expertice, thus I will look for someone to do it for me since time for learing VB is not in the near future.

I will let you know how it turns out.

Regards,

Daniel (very happy)
roussell
Advanced Member
Posts: 268
Joined: Wed Dec 15, 2004 9:07 am
Location: Pelham, AL

Re: Status update - scripting it away

Post by roussell »

It seems like the ASCII number that is returned is the decimal representation of the binary position of the inputs that are active.
For example:

1 – sensor 1 active: decimal 1 = binary 00000001
2 – sensor 2 active: decimal 2 = binary 00000010
3 – sensors 1 and 2 active: decimal 3 = binary 00000011
...

It should be a simple matter to grab the number, convert it to binary and then assign each position to a HB property. Here is a vbscript to get you started. This will read the received data and place it into a set of 8 properties. To use it do the following:

1. Copy the following code to a file and place it in your scripts folder.
2. Create a script device and point it to the newly created file.
3. In the script device create an Alpha property named 'Raw Binary' (without quotes)
3. In the script device create Alpha properties named Input 1, Input 2, Input 3 all the Way to Input 8. Pay attention to case and the space between Input and the Number.
4. Edit the script to reflect your Device name (Replace the uppercase _DEVICE in the script with the name of your serial and script device where appropriate.
5. Change the script state to "Running"

Note that the script assumes and input of '8INR nnn' where there are 5 characters (including a space) followed by the decimal data. If this is not the case then you will need to change line 8 to correctly reflect the number of characters before the data. The '5' in this line - WriteToHB(DecToBin(cint(Mid(Data,5)))) - is the number you would change.

Code: Select all

oldData = ""

Do
  Sleep 1
 '****Change DEVICE below to suit your environment****
 Data = GetPropertyValue("SERIAL_DEVICE.Received Data")
  If Data <> oldData Then
    oldData = Data
    WriteToHB(DecToBin(cint(Mid(Data,5))))
  End If
Loop


Function DecToBin(intDec)
  dim strResult
  dim intValue
  dim intExp

  strResult = ""

  intValue = intDEC
  intExp = 256
  while intExp >= 1
    if intValue >= intExp then
      intValue = intValue - intExp
      strResult = strResult & "1"
    else
      strResult = strResult & "0"
    end if
    intExp = intExp / 2
  wend

  DecToBin = strResult
End Function


Function WriteToHB(binData)
  dim binCount
  binCount = 1
  while binCount <> 9

   'remove the msgbox line below for production use
   'msgbox "Position: " & bincount & ", Data: " & Mid(binData, binCount+1, 1)
   
   'uncomment and change the line below to suit your devices and properties
   
 '****Change DEVICE below to suit your environment****
   SetPropertyValue "SCRIPT_DEVICE.Raw Binary", binData
   SetPropertyValue "SCRIPT_DEVICE.Input " & binCount, Left(Right(binData,binCount),1)  

  binCount = binCount+1
   wend

End Function
Hope this helps.

Terry
Richard Naninck
HouseBot Guru Extraordinaire
Posts: 1121
Joined: Tue Sep 28, 2004 7:49 am
Location: The Netherlands

Re: Status update - scripting it away

Post by Richard Naninck »

And yet another way to get you going all based on the same stuff off course. Just a little bit more into binary like I said in my first post using And's and Or's (except for the Or's :wink:)

If you do want to go ahead and use a script setup like Roussell or mine, just remember one thing. If you keep on aksing for a status update while the received data doesn't change regarding the previous request, your Sub won't be called. In other words, if you ever end up wanting to get a ofrced update even if the result remains the same, than just ask since there are tricks to get over that as well.

Have fun. Plenty of stuff to go by and always nice to get somebody going!

Code: Select all

oldData = ""

Do
	Sleep 1
	Data = GetPropertyValue("SERIAL_DEVICE.Received Data")

	If Data <> oldData Then
		oldData = Data
		Call SetResults(CInt(Mid(Data,5)))
	End If
Loop


Sub SetResults(Data)
Dim n
Dim strResult
	
	For n = 0 To 8
		strResult = "Off"
		
		If (Data And (2^n)) >= 1 Then
			strResult = "On"
		End If
		
		SetPropertyValue "SCRIPT_DEVICE.Input " & n, strResult
	Next
End Function
roussell
Advanced Member
Posts: 268
Joined: Wed Dec 15, 2004 9:07 am
Location: Pelham, AL

Re: Status update - scripting it away

Post by roussell »

Richard Naninck wrote:

Code: Select all

If (Data And (2^n)) >= 1 Then
			strResult = "On"
End If
VERY nice Richard. Your Kung Fu is strong. That's a great way to convert to binary, I didn't know that little trick.

Dtresinari, use my instructions, but Richard's code. They both do the same thing, but he only uses one line to convert decimal to binary - whereas I wrote a whole function...

Terry
Last edited by roussell on Thu Aug 20, 2009 10:04 pm, edited 1 time in total.
Richard Naninck
HouseBot Guru Extraordinaire
Posts: 1121
Joined: Tue Sep 28, 2004 7:49 am
Location: The Netherlands

Re: Status update - scripting it away

Post by Richard Naninck »

roussell wrote: VERY nice Richard. Your Kung Fu is strong. That's a great way to convert to binary, I didn't know that little trick.
Terry
:D

Still working with assembler programming micro controllers. And's, Or's, EOR's and rotating bits left and right are basically the only way to go when working on a bit level. Use a mask to test a bit in a byte. Always works.

Very funny way of putting it though. That Kung Fu line put a smile on :wink:
Richard Naninck
HouseBot Guru Extraordinaire
Posts: 1121
Joined: Tue Sep 28, 2004 7:49 am
Location: The Netherlands

Re: Status update - scripting it away

Post by Richard Naninck »

Just for the record, I think this thread should be moved to the general section since it is not a feature request.
dtresinari
Member
Posts: 5
Joined: Wed Aug 19, 2009 11:19 am
Location: Brazil

Re: Status update - scripting it away

Post by dtresinari »

Hi everyone !!

It´s been ages since I don´t play around with HB... work is killing me... :oops:

Thank you all for the assistance... I must say that I finally purchased HB, very happy with it too !

I tried using the script... however I keep getting the following error everytime I try to execute it:

Source: 'Compilation error from Microsoft VBScript'
File:'c:\....\HouseBot\Config\Scripts\8entradas.txt' Line:27 Char:4 Error:0 "Sub' expected'

On HB Error Log says:
Unable to compile the script.
Unable to load script file [C:\Arquivos de programas\HouseBot\Config\Scripts\8entradas.txt]. Error = [System cannot find the specified file. ]

Sorry for any translation mistakes.

Any ideas what I´m doing wrong ?

Cheers !
Richard Naninck
HouseBot Guru Extraordinaire
Posts: 1121
Joined: Tue Sep 28, 2004 7:49 am
Location: The Netherlands

Re: Status update - scripting it away

Post by Richard Naninck »

I take it you created a script device which points to the script file.
First try to rename your scripts name so it doesn't end with .txt but with .vbs
Call it something like test.vbs and point your script device to it. Then try running it again...
dtresinari
Member
Posts: 5
Joined: Wed Aug 19, 2009 11:19 am
Location: Brazil

Re: Status update - scripting it away

Post by dtresinari »

Hi Richard,

Thanks for the reply. Yes i did create the device.

Renamed the file to .vbs, edited in the device but still the same...
Post Reply