IP controlled relay board

If you have a question or need help, this is the place to be.
Peter M
Posts: 46
Joined: Tue Jun 18, 2019 5:13 am

Re: IP controlled relay board

Post by Peter M » Wed Jul 10, 2019 10:30 pm

No worries. I very much appreciate your help and there's no hurry.

Our time zone difference makes it a slow process !

Cheers,
Peter

User avatar
kgschlosser
Site Admin
Posts: 5504
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: IP controlled relay board

Post by kgschlosser » Thu Jul 11, 2019 4:17 am

i am -6 UTC where abouts are you?
If you like the work I have been doing then feel free to Image

Peter M
Posts: 46
Joined: Tue Jun 18, 2019 5:13 am

Re: IP controlled relay board

Post by Peter M » Thu Jul 11, 2019 10:27 am

I'm in Sydney - UTC+10

Very pleased to report that this is giving me the board status feedback I'm after -

Code: Select all

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.0.56', 17494)) # connect to board
f = 'empty' # initialise feedback
try:
    s.sendall(b'\x24') # request status
    f = s.recv(1024) # get feedback
except:
    pass #ignore all exceptions
finally:
    s.close() #disconnect from board
    print 'feedback = ', repr(f)
I tested with relays 1, 2, 3 and 7 ON and the feedback was 'G' which converts to 01000111 exactly as it should.

Now I have to figure out how to convert the byte to binary and broadcast it out to Demopad.

Cheers,
Peter

Peter M
Posts: 46
Joined: Tue Jun 18, 2019 5:13 am

Re: IP controlled relay board

Post by Peter M » Thu Jul 11, 2019 12:29 pm

Final code -

Code: Select all

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.0.56', 17494)) # connect to board
try:
    s.sendall(b'\x24') # request status
    f = s.recv(1024) # get feedback
except:
    pass # ignore all exceptions
finally:
    s.close() # disconnect from board
    fbin = ' '.join('{0:08b}'.format(ord(x), 'b') for x in f) # convert to binary
    eg.plugins.BroadcastListener.Broadcast('Status '+fbin, u'', 33339) # broadcast to demopad
Parsing the feedback in Demopad works fine so that's this little project finished !

Cheers,
Peter

User avatar
kgschlosser
Site Admin
Posts: 5504
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: IP controlled relay board

Post by kgschlosser » Thu Jul 11, 2019 7:52 pm

well I guess i didn't directly solve your problem. I am hoping the code i provided did help you get to the solution..

Always feels good to get something working especially if you have spent a lot of time trying to get it going.. I may not always have the solution but i usually will say or provide something that helps get the job done. It's hard when dealing with devices I have never dealt with hands on. it's a lot of back and forth testing..

let me know if there is anything else I can do or help with.
If you like the work I have been doing then feel free to Image

Peter M
Posts: 46
Joined: Tue Jun 18, 2019 5:13 am

Re: IP controlled relay board

Post by Peter M » Fri Jul 12, 2019 1:23 am

Yes your code certainly helped. It pointed me to sockets for communication, whereas the help I was getting on the AVS Demopad thread was all about using urllib which doesn't appear to be able to do what I wanted. It's incredible what you can find freely available with a quick Google search. Once I knew about sockets I found a great tutorial on them and then plenty of code examples at Stack Overflow.

Even with my very limited python knowledge I was able to use the code examples with slight modification and it all came together surprisingly easily !

Cheers,
Peter

User avatar
kgschlosser
Site Admin
Posts: 5504
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: IP controlled relay board

Post by kgschlosser » Fri Jul 12, 2019 3:59 am

you did state you have some programming experience.

Python is unbelievably easy.. You are already familiar with what syntax is and and operators are.. if, elif else, +, -, ==, != those kinds of things.

back in the day most languages had markers to separate code blocks. in C code it's ; and {} in python it's indents. indents can be multiples of the tab key, or they can be multiples of even numbered spaces.

python is a high level language so things have been made in a way so that you do not have to concern yourself with memory management, or garbage collection. freeing the memory.. declaring variables.. all of that stuff is done for you. so all you have to concern yourself with is programming your application. Python is also one of the most used languages on the planet.. there is a plethora of tutorials and examples on the internet..

If you have an android device I would recommend downloading an application called "Learn Python" It is an almost kindergartenish app... But It will teach you a whole lot.. It has lessons and then tests at the end of the lesson It is done in a way that makes you remember.. You should use that somewhat..
I know you are new to EG and if you dive into the python scripting you will then begin to see the power of what EG can do. the true potential of EG is in it's real time scripting.

I have 4 TV's and 1 projector, 5 HTPC's, 5 remotes, 80 zwave devices, 2 fireplace controllers (self made), 3 furnaces, security system including cameras, Whole home audio, sump pump controllers (self made), filter reminders for the furnaces (self made), door locks, geofencing.... and I am sure I am forgetting some things in that list.. But all of it is controlled by a single running copy of EG
If you like the work I have been doing then feel free to Image

Peter M
Posts: 46
Joined: Tue Jun 18, 2019 5:13 am

Re: IP controlled relay board

Post by Peter M » Wed Apr 08, 2020 4:36 am

kgschlosser wrote:
Sun Jul 07, 2019 9:39 pm
Now.. the demo pad has sparked my interest. It is hard do determine what it exactly is. It appears to be a control4 type of a system with a controller and then the remotes. And the remotes talk to the controller and tells it what to do. But then on another place it appears as tho it is software based. So I am a little fuzzy on its functionality. But it appears as tho you can set the thing up to make network connections to devices and pass data to the device. I do not know the extent of it's ability to get state fr a device or if it has a poll loop that asks for the state or if the connection remains open and it will update state when data is set to the demopad.

One thing I can see being really useful with the demopad would be htpc control. Because the demopad is touchscreen based you could use the demopad as a touchpad to control mouse movements on an htpc. This would be really useful.
Hope you don't mind me resurrecting this old thread, but I thought I'd remind you of our earlier discussion about Demopad, rather than start a new thread.

I've just started on HTPC control from Demopad, and so far I have a keyboard working really well. Next up is the mouse. If I send X,Y coords from Demopad to EG, how do I use them to control the mouse pointer ?

Cheers,
Peter

User avatar
kgschlosser
Site Admin
Posts: 5504
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: IP controlled relay board

Post by kgschlosser » Wed Apr 08, 2020 7:22 pm

when you move your finger across the demopad does it spew out a stream of x, y coords? if it does then we can use mouse absolute movement to move the mouse.

Do you have button assignments for the mouse buttons?? or do you want to use say a double tap in a location to move the mouse. Also we are going to need to convert the x, y from the demopad to actual screen resolution. so we need to know the resolution of the demopad (or where it sees 0, 0 as being typically the upper left hand corner and we will need to know the x, y for the opposite corner of the demopad screen. with that we will be able perform a rescale of the incoming x, y to the computers screen resolution.

also does the demopad support multi touch?
If you like the work I have been doing then feel free to Image

Peter M
Posts: 46
Joined: Tue Jun 18, 2019 5:13 am

Re: IP controlled relay board

Post by Peter M » Thu Apr 09, 2020 3:34 am

Thanks for your ongoing help with this - much appreciated !!

So far in Demopad I've set up buttons for left and right mouse click, and up and down scroll wheel, and they are all working fine.

For the mouse pointer movement I've set up a large button and assigned two number variables "X coord" and "Y coord" to it. These number variables can be assigned any range of integer values, and for testing purposes I've got "X coord" being 0 to 500 and "Y coord" being 0 to 300. Upper left corner is (0,0) and bottom right is (500,300). As I move my finger the values of "X coord" and "Y coord" are constantly, and very quickly changing.

Demopad can issue a command whenever a number variable changes, so it's quite easy for me to blast all these numbers at EG.

Cheers,
Peter

Peter M
Posts: 46
Joined: Tue Jun 18, 2019 5:13 am

Re: IP controlled relay board

Post by Peter M » Thu Apr 09, 2020 3:39 am

I should have added that Demopad doesn't support multi touch.

Initially I'd just like to get discrete button click and pointer movement working, and maybe later I can add click and drag somehow. Maybe a separate "click + drag" button, so I push "click + drag" --> move the pointer --> push "click and drag" again.

Cheers,
Peter

User avatar
kgschlosser
Site Admin
Posts: 5504
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: IP controlled relay board

Post by kgschlosser » Thu Apr 09, 2020 7:24 am

OK you have some choices here. we can set everything up so it will work almost identical to a touch pad if you want. Here is what I would be able to do.

we should be able to do a click and drag and also a double click with just the use of your finger. So a single tap would be a single left click. a double tap would be a double left click. a single tap and hold would be right click. It becomes difficult to do a drag because there is no real way to start a drag with only a single touch screen. not unless we use a "hot spot" like one of the corners to tell EG that you want to hold the left click down then you would be able to drag the item.

we can also set up the right side of the screen (right handed) or the left side (left handed) to be an "invisible" scroll bar. the center right would be stop scroll or the lifting of your finger off the screen. if you press the center right side that enables the scroll bar when you drag your finger up the right side from the center this would be scroll up the farther you move your finger from the center the faster the scroll becomes. the same thing when you move your finger down from the center. that would be scroll down. if there is a need for horizontal scrolling the same thing can be done at the bottom of the screen.

I do not own a demopad so I am not able to write this without a lot of help from you. are you able to get the x, y coords into EG as an event? If so I need to know if it is able to track your finger movements with decent speed. also if you tap the screen in the same spot how much of a variation occurs in the x, y coords occurs. I will need you to run many samples of that recording the results. I want to have a basic idea of what the variation is so it can be coded in as a start off point.

I can write the script so it will interpret what you want to do. I can also set up some variables that will allow you to adjust double tap speed. and tap and hold duration. and also have a % tolerance for tap and double tap. that same % tolerance is what would be used to activate the scroll bars and also the activation point for the click and drag.



Or you can leave it with physical buttons if you want. which ever is easier for you to use.

I have another question. are you able to change the image being displayed on the demopad dynamically? as in send an image from the PC and have it change what is being displayed on the demopad? I have a really cool idea. We can set EG up to do screen captures every 5 seconds or so or any time you click on something so the demopad will be displaying what is on your PC screen. with only a 500, 300 resolution the image would not be all that fantastic. But at least it would give you a general idea of what is happening on the PC screen if the pc screen is not directly visible.

To get you started off and on your way, since you already have the house buttons mapped to hard buttons on the demopad we can get the mouse moving for ya.

create a macro and add the Python Script action to it. Copy and paste the code below into that action.
then add the event for the mouse movement from the demopad.

I do not know what the event looks like that is coming form the demopad, so I took a guess that the x, y is a payload that is attached to the event.
You would need to copy the event from your log and paste it into a forum post for me to be able to set that portion up correctly.

READ the comments. It explains what the code is doing.
This code also supports having multiple displays (monitors) attached to your computer. and it will properly align the mouse to where it is supposed to be across the multiple displays.

I have tested this on my 3 monitor setup entering random x, y coords that would be within the demopads resolution. It works without any issues that I can find.

Code: Select all

# make sure you read the comments to know what is going on

from eg.WinApi.Utils import GetMonitorDimensions
import wx

# I do not know how the coordinates come in to EG.
# I am assuming it is a payload of [x, y]. if it is something 
# different then I will need you to make a copy of the event 
# and paste it into the forum so I can see what it is.
x, y = eg.event.payload

DEMOPAD_WIDTH = 500
DEMOPAD_HEIGHT = 300

# I do not know if you have multiple monitors or not.
# if you do do it is possible to end up with negative 
# x and y start positions. So we need to account for that
# properly. we also need to get the total width and total 
# height of all of the monitors. GetMonitorDimensions
# does some magic for us. It returns the monitors that 
# are attached to the system and tells us where the 0, 0 
# position of that monitor actually is. and also the width 
# and height of the monitor.
rect = wx.Rect()
for monitor in GetMonitorDimensions():
    # because of some back end magic we are able to "join" the 
    # monitors to get a single monitor. This is only for the 
    # purposes of math and it does nothing to your actual display.
    rect.Union(monitor)


# now that we have a single "monitor" that represents all of the 
# displays that are attached. we are able to get the starting point 
# and also the ending point of the combined displays. 
SCREEN_START_X = rect.GetX()
SCREEN_START_Y = rect.GetY()

# in order to know where the end point is we add the start point to the width/height
SCREEN_END_X = SCREEN_START_X + rect.GetWidth()
SCREEN_END_Y = SCREEN_START_Y + rect.GetHeight()

# this function takes a value that is within a given range and "re maps" 
# it to a new range of numbers.
# this is how we turn the x, y for a screen size of 500, 300 into 
# the x, y of your combined displays
def remap(value, old_min, old_max, new_min, new_max):
    old_range = old_max - old_min
    new_range = new_max - new_min
    return (((value - old_min) * new_range) / old_range) + new_min

# the conversion.
x = remap(x, 0, DEMOPAD_WIDTH, SCREEN_START_X, SCREEN_END_X)
y = remap(y, 0, DEMOPAD_HEIGHT, SCREEN_START_Y, SCREEN_END_Y)

# we need to use a wx.Point object to pass to a monitor to see 
# if the x, y is within that monitor
point = wx.Point(x, y)

for monitor_num, monitor in enumerate(GetMonitorDimensions()):
    # checking to see if the remapped x, y are in a given monitor.
    if monitor.Contains(point):
        #now this is the tricky part. the Mouse action does not deal 
        # in the x, y of a combined display. each display always starts at 
        # 0, 0 and ends at width, height. So now we need to convert the x, y
        # into the x, y needed for this display.
        x_min = monitor.GetX()
        x_max = x_min + monitor.GetWidth()
        y_min = monitor.GetY()
        y_max = y_min + monitor.GetHeight()

        # here is the conversion. so the old min and max are the "real" start and end positions
        # for the display. the new range is 0 to the width/heigh. This is going to give us the x, y
        #  that the mouse action needs.
        x = remap(x, x_min, x_max, 0, monitor.GetWidth())
        y = remap(y, y_min, y_max, 0, monitor.GetHeight())

        # and here we call the action and move the mouse.
        eg.plugins.Mouse.MoveAbsolute(x, y, monitor_num, False, False)
        
        # if we found the monitor there is no point in checking any other ones.
        # so we break out of checking the rest of them
        break
else:
    # This code gets run if no monitor is found. This would be a catastrophic 
    # failure of the above code. and tells us that something is horrifically wrong.
    # If you ever see this printed in your log you have to let me know and I will 
    # have to locate the error.
    print 'No monitor found'
If you like the work I have been doing then feel free to Image

Peter M
Posts: 46
Joined: Tue Jun 18, 2019 5:13 am

Re: IP controlled relay board

Post by Peter M » Fri Apr 10, 2020 6:21 am

Wow ... I was planning to start out very basic and see how it might develop later. One thing I can confirm is that it'll only ever be used on a single monitor as it's a projector in a theatre room. I know I'm aiming pretty low but I'm trying to swim ... not sink !

Using my normal method of sending a command from Demopad to EG, and inserting X, Y into the command, gives this as the event in EG -

udp.(460, 236)

I have no idea if this is what you mean by payload.

Cheers,
Peter

Peter M
Posts: 46
Joined: Tue Jun 18, 2019 5:13 am

Re: IP controlled relay board

Post by Peter M » Fri Apr 10, 2020 6:29 am

These are the events from tapping the same spot four times -

udp.(152, 136)
udp.(152, 136)
udp.(152, 136)
udp.(152, 136)
udp.(152, 136)
udp.(152, 136)
udp.(153, 136)
udp.(153, 136)
udp.(153, 136)
udp.(153, 136)
udp.(153, 136)
udp.(153, 136)
udp.(150, 138)
udp.(150, 138)
udp.(150, 138)
udp.(150, 138)
udp.(150, 138)
udp.(150, 138)
udp.(154, 142)
udp.(154, 142)
udp.(154, 142)
udp.(154, 142)
udp.(154, 142)
udp.(154, 142)

Cheers,
Peter

Peter M
Posts: 46
Joined: Tue Jun 18, 2019 5:13 am

Re: IP controlled relay board

Post by Peter M » Fri Apr 10, 2020 6:33 am

Moving my finger from top left to bottom right in about 2 secs, generated approx 150 events.

Post Reply