AVS Forum banner

DIY Home Automation

22K views 102 replies 15 participants last post by  BassThatHz 
#1 ·
After evaluating the cost of several Home Automation products, I came to the conclusion that I could make my own for a mere fraction of the price.

$60 low-level hardware vs $3000 (or more) big-box stuff.


I was thinking of coding my own software for a DIY Home Automation based off of relays, such as this, for my new home theater.
https://www.sparkfun.com/products/9669


These are 5V relays that are USB controllable.


These would control larger relays up to whatever value... say 30amps.


I'm a professional programmer, so coding interactive webpages and serial-controllers DLL's are no problem for me.


The only challenaging part would be finding a corresponding USB device for digital-varisting capable of 120v support up to say 5 or 15amps.


My Automation system would lack all the fancy RF-wave and video switching stuff, which I don't care about.


I should be able to do remote IR controlling as well, I'm almost 100% sure of that.


Thoughts?
 
See less See more
#2 ·
It depends on what you intend to do with relays. I have built many 120v dimming relays (well, triacs) using DMX and RS-485, which will support much longer runs than USB.


However, all it does is give you dimming. I use them outside for Christmas lights. I wouldn't be keen on installing DIY 120v stuff inside my home.


I suppose you could use the USB relay to power the coil on a 120v ice cube relay, at which point you would at least have a UL listing on the 120v portion. THere are plenty of communities out there for this kind of thing.


Tim
 
#3 ·
Cool idea, and that is a cool little toy!


I'm looking into the Z-wave based options for my upcoming renovation. There look to be a lot of control options for that, some are actually FOSS.


Since this is appliance-level hardware (in other words - it should just work), I'm willing to spend a bit more and get something that I don't have to mess with or code a whole backend for.
 
#4 ·
I'd want security, lighting control and power management all from a single interface on a touchscreen tablet.

For now, it will be limited to my theater. With the possibility of an intercom system between that and the kitchen (which could be a stand-alone system).


For the app interface, I was thinking of a floor-plan overlay design for device selection, and/or a text drilldown tree-list by category.


1) Lighting control: Support for up to 8 light-zones, with at least on/off capability for all 8, but preferably including 4 dimmer zones with support of up to 400watts on each.

2) Power management: Being able to cycle 16 power devices with variable delay and with timer options, all independently and/or in various clusters of user-defined modes. Supporting both 120v and 240v up to 30amps.

3) Security management:

a) Keyless entry, with network lock/unlock. Preferably Biometric or maybe a combination, RFID proximity and/or key code.

b) Support for 3 IP camera HUD. Preferably with tilt/zoom and night-vision.

c) Motion Sensor Alerting


Future Capabilities:

4) Media controller (audio at least)

5) VOIP or Skype/Facetime etc


So I need 24 high-powered relays, 4 fuses and high-powered triacs, one heck of a PC breakout box, and probably a 10 channel gigabit/11g/n router.

All of this for as cheap as possible of course...

(damn, I might as well go with a full Crestron system or something
)


The problem isn't the software for me, it is in finding good controller-hardware, either wired/wireless for cheap. Preferably USB, Eithernet or 11g.

I wonder if something like the X10 breakout controller drivers are proprietary closed-source or open or well documented at least?
 
#5 ·
For $140, I found a 24 channel RS485-over-USB controlled relay system. The cheapest I've found yet...


With my own software this would solve all of 1 & 2, except for the computer-controlled dimming part of the lighting.


I'd still need to connect these low-ish powered relays to high-powered relays as these aren't designed for Lab clones and multi-hundred-watt lights,

which is no big deal. I'd slap that into a DIY box with NEMA inputs and outputs to make it all clean of course (no exposed high-voltage wiring LOL).


The cool thing with this solution is that it isn't wireless, thus it remains absolutely secure; teenaged hackers can't turn my gear off via RF signals from their mom's basement LOL. (Unlike X10 etc...)


I'm not even going to worry about features 3 through 5 until the first two are fully solved. That's why I went down this rabbit hole to start with after all...

http://www.ebay.com/itm/USB-RS485-24-Channel-Relay-Board-controller-/280583706515?pt=LH_DefaultDomain_0&hash=item4154179793
 
#6 ·
I'm doing my apartment/HT automation with a set of networked Arduino Mega2560 and have just received a Raspberry Pi which I'm planning to use as a display interface. Arduinos come with all sorts of shield modules so you can wifi or Xbee somewhere you can't get wiring directly.


Search ebay under 'relay boards' or 'relay modules' and you'll see plenty of them that will handle 10A at 120V. Above that you're going to need to find something else. I use MCP23S17 port expanders driving ULN2803 octal darlington drivers and Omron G4a relays because they meet my specs (20A at 240V, 80A surge) and I got a tray of 50 for under a $1ea new.


I set it up this way because I don't want a PC on all the time. And because it was a fun project and got me back into embedded design after a long hiatus. Much as I loathe C, it makes life a lot easier than assembly, but it still sucks compared to Forth.
 
#7 ·
I am sooo green when it comes to the home automation stuff that is out now as it seems to get more complicated by each passing year. So let me get this straigt, you are using relays, (which are basically just switches that turn some on & off, right?) to control when the lights come on, accompanied by some dimmer switches for the level of light. Then you are also going to be using relays for turning on your equipment, something like 16 different components, right?


I have absolutely no idea what you will be doing for security management, but I am guessing that you will have hi tech cameras that will monitor everything....not sure how you are going to operate those? Also what kind of devise, or software will the security cameras feed the video into, and how do you propose to monitor this video? Also with regards to the security system, are you going to have window breakage detectors and motion sensors, and if so, how do you plan to operate and manage those with regards to how all of this fits together?


Are you writing the software for a DIY home automation controller, similar to a Creston? If so, how and what are you going to use for the hardware? If not, are you going to buy a controller like the Crestons that so many folks use?


What is a high powered Triac and what is its purpose? What is a PC breakout box and what is a 10 channel 11gig router? Sorry for all of the dumb questions but I will be remodeling a house soon and would like to have some sort of home automation like you described, and like I said, I am totally green on this!
 
#8 ·
I'm not sure who you are asking these questions, so I'll assume me in part and reply.
Quote:
Originally Posted by Martycool007  /t/1457157/diy-home-automation/0_100#post_22946179


I am sooo green when it comes to the home automation stuff that is out now as it seems to get more complicated by each passing year. So let me get this straigt, you are using relays, (which are basically just switches that turn some on & off, right?) to control when the lights come on, accompanied by some dimmer switches for the level of light. Then you are also going to be using relays for turning on your equipment, something like 16 different components, right?
I am using relays, both for power selection and control in the HT and some house functions, such as lighting in the theatre room. For example it is programmed so that when you press Play on a selection that uses video, the room lights will dim, and will ramp up when you press Pause or Stop. If I selected TT as source, none of the amps etc that run the surrounds (tri-amped) will be powered up. All power switching is done close to zero cross accounting for relay delay.


The plan is later to add mains outlet control and lighting through the rest of the apartment as I find time and need to do so. I have a friend who has build a similar system in his home, and when he's not there, lights and other devices such as radios turn on/off in a correct sequence to simulate people being there, and I'd like to add this later.
Quote:
Originally Posted by Martycool007  /t/1457157/diy-home-automation/0_100#post_22946179


I have absolutely no idea what you will be doing for security management, but I am guessing that you will have hi tech cameras that will monitor everything....not sure how you are going to operate those? Also what kind of devise, or software will the security cameras feed the video into, and how do you propose to monitor this video? Also with regards to the security system, are you going to have window breakage detectors and motion sensors, and if so, how do you plan to operate and manage those with regards to how all of this fits together?
I'm doing PIR and window break, and am looking at some inexpensive commercial systems to do video. My own stand alone system will call (and later ) email if it detects. Not sure if I'll be able to integrate the two bet they might not be necessary. We'll see.
Quote:
Originally Posted by Martycool007  /t/1457157/diy-home-automation/0_100#post_22946179


Are you writing the software for a DIY home automation controller, similar to a Creston? If so, how and what are you going to use for the hardware? If not, are you going to buy a controller like the Crestons that so many folks use?
Can't afford a Crestron, so I'm basically making a DIY on that does what I want, some of which is highly specific to my my particular set up and equipment, some of which is modified to suit my needs. Total cost for everything should be under $500, plus a lot of coding and soldering, but I enjoy that anyway.
Quote:
Originally Posted by Martycool007  /t/1457157/diy-home-automation/0_100#post_22946179


What is a high powered Triac and what is its purpose?
A triac is a solid state switch which can handle alternating currents, making it suitable for controlling the mains on many devices. They are also sued in some dimmers for lighting. High powered would mean one that can handle mains voltage as well as the current that the load you wish to control will draw, say 5 or 10A. Some can handle considerably more current.
Quote:
Originally Posted by Martycool007  /t/1457157/diy-home-automation/0_100#post_22946179


What is a PC breakout box and what is a 10 channel 11gig router?
PC aren't natively suitable for controlling ancillary devices such as relays, so that need some sort of interface to connect to the PC via USB or RS232. These usually then let you control say 24 relays or other devices. These are used in many industrial applications worldwide.


I've never heard of a '10 channel 11gig router'. 10 channel I assume means 10 RJ45 ports but this is an uncommon configuration. Routes/switchers etc are usually in a multiple of 8 ports, 8/16/24/48 and small home/SOHO versions have 5. 11gig might refer to one of the common networking protocols 802.11g . There are faster variants such as 802.11n. Go with the fastest you can afford in a new installation to allow for longest life.
Quote:
Originally Posted by Martycool007  /t/1457157/diy-home-automation/0_100#post_22946179


Sorry for all of the dumb questions but I will be remodeling a house soon and would like to have some sort of home automation like you described, and like I said, I am totally green on this!
If I was with you in person and had the floorplans and could ask questions about what you want to control and how, whether you want to use a single server and route all TV/video through the network etc it would be easier to make suggestions. FWIW I'm licensed to do this sort of work in my country.


My basic plan is to add at least one Cat6 cable and port to each room, and depending upon the room and it's use and possible uses later, I'd likely add more. You could always bring say 4 cables from each room to a central point with the switch(es) placed there and connect up the outlets you are using now. Rearrange the living room in 5 years might mean a point on the opposite wall is more convenient (no exposed cables), so all you'd need to do then is go to the 'network cupboard' and change to the correct wire. Trust me, you will regret not putting them in now when the cost will be minimal as it's easy and cheap to do before the drywall/gyprock goes up, and much more expensive later. In some cases due to the construction of the house it may be very difficult and expensive or impossible to add later without something you may not like, such as an external visible conduit.
 
#9 ·

Quote:
Originally Posted by Martycool007  /t/1457157/diy-home-automation#post_22946179


I am sooo green when it comes to the home automation stuff that is out now as it seems to get more complicated by each passing year. So let me get this straigt, you are using relays, (which are basically just switches that turn some on & off, right?) to control when the lights come on, accompanied by some dimmer switches for the level of light. Then you are also going to be using relays for turning on your equipment, something like 16 different components, right?

Correct.
Quote:
Originally Posted by Martycool007  /t/1457157/diy-home-automation#post_22946179


I have absolutely no idea what you will be doing for security management, but I am guessing that you will have hi tech cameras that will monitor everything....not sure how you are going to operate those? Also what kind of devise, or software will the security cameras feed the video into, and how do you propose to monitor this video? Also with regards to the security system, are you going to have window breakage detectors and motion sensors, and if so, how do you plan to operate and manage those with regards to how all of this fits together?

I don't have any glass in my theater, only doors. Motion activated sensors for video recording and alerts, should be doable, not sure about a single integrated DIY solution for that though (depends on the camera vendor).

My UPS probably has email alerts for power loss, which may also be handy (not that there is anything I could do about it though).
Quote:
Originally Posted by Martycool007  /t/1457157/diy-home-automation#post_22946179


Are you writing the software for a DIY home automation controller, similar to a Creston? If so, how and what are you going to use for the hardware?
Yes. My HTPC, which is on 24/7 already.


I was thinking of getting a RAID-5 NAS in time for my HTPC as well.
 
#10 ·

Quote:
Originally Posted by A9X-308  /t/1457157/diy-home-automation#post_22947951


It is programmed so that when you press Play on a selection that uses video, the room lights will dim, and will ramp up when you press Pause or Stop. If I selected TT as source, none of the amps etc that run the surrounds (tri-amped) will be powered up. All power switching is done close to zero cross accounting for relay delay.

Dude that is insane, how did you detect zero-crossing. Diagram?

I've never really dipped into the hardware-aspect of automation control before.

You must have used the Arduino chip for the relay delay, calculating the next half-wave 8ms delay or 16ms full-cycle (substracting the relay activation time of course).


Also, what media player are you using for those control hooks?
 
#11 ·

Quote:
Originally Posted by BassThatHz  /t/1457157/diy-home-automation/0_100#post_22956778


Dude that is insane, how did you detect zero-crossing. Diagram?
I'm not at home at the moment, but I'll post it when I get a chance.
Quote:
Originally Posted by BassThatHz  /t/1457157/diy-home-automation/0_100#post_22956778


I've never really dipped into the hardware-aspect of automation control before.
My background is electronics, so I approach design of a system as 'how is this easiest (for me) to do' and use a lot of electronic sub systems to augment processors. For me, that is implement anything in software that I can't do easily in hardware.
Quote:
Originally Posted by BassThatHz  /t/1457157/diy-home-automation/0_100#post_22956778


You must have used the Arduino chip for the relay delay, calculating the next half-wave 8ms delay or 16ms full-cycle (substracting the relay activation time of course).
That's pretty much it. The zero cross detector goes to a spare input pin on the Mega, so when it's about to open/close a relay, it constantly loops looking for that pin to change state and adds a delay to get relay operating delay plus software delay to equal 20mS (50Hz here). I tested all the relays I used for many operational cycles and averaged the delay to use for that type of relay.
Quote:
Originally Posted by BassThatHz  /t/1457157/diy-home-automation/0_100#post_22956778


Also, what media player are you using for those control hooks?
I'm not using a media player to implement this - it operates with no PC running.

I have XBMC but I find it easier most of the time to simply access a file and play it rather than use media players. When I wish to play something that's on a PC, I select that as a 'Source' via IR remote, which brings the PC out of sleep mode (and sets up the rest of the system) and I use a keyboard/trackball combo to control the PC.
 
#12 ·

Quote:
Originally Posted by A9X-308  /t/1457157/diy-home-automation#post_22958162


I'm not using a media player to implement this - it operates with no PC running.

I find it easier most of the time to simply access a file and play it rather than use media players. When I wish to play something that's on a PC, I select that as a 'Source' via IR remote, which brings the PC out of sleep mode (and sets up the rest of the system) and I use a keyboard/trackball combo to control the PC.

A9X you don't use a computer? How then are you accessing the file-system on the drive? (AVR with network file streaming or ?)

How do you pass the h264 into an HDMI AV connection?

How does your Arduino know that the movie is playing or paused/stopped?
 
#13 ·

Quote:
Originally Posted by BassThatHz  /t/1457157/diy-home-automation/0_100#post_22964774


A9X you don't use a computer? How then are you accessing the file-system on the drive? (AVR with network file streaming or ?)

How do you pass the h264 into an HDMI AV connection?

How does your Arduino know that the movie is playing or paused/stopped?
There is some misunderstanding. The 'control' system of Arduinos, does not need a PC to be running. I can put a disc in the BRP and select 'Play CD' via IR RC and control the system from the RC (stop, play, change sources etc). The Arduino receives the IR codes and acts according to what it receives.


The Arduinos do not need to know whether a source is playing or not. They set up the conditions to allow it to happen and control some of the functions of the devices. If I was watching a movie from the BRP, after initial system set up, and pressing play, they are idle until the next control is pressed.


If I wish to play a file stored on computer selecting 'PC Music' / 'PC Movie' sets up the system by turning on/off power amps, projector, AVR etc and by applying the settings for that source if needed, and then I control the movie/music being played from the PC via a combo keyboard and trackball.


On the PC where I have a lot of movies and music stored, I have XBMC but it is sort of annoying, and I have no problem just opening MPC and selecting a DVD file to play for instance. I might get J River later, when I build a dedicated HTPC. Movie files from the PC are played out to the AVR via HDMI. The PC is connected to a network that runs throughout the apartment (not the garage yet though) so I can access and play files from any computer connected to the network.
 
#15 ·
Here is the KMTronic based Relay Controller code I wrote yesterday.


It's an Alpha-version since I don't have the hardware yet to prove that it is totally bug free.


[Alpha version delete, contained errors in the relay code. Beta 0.9 is now available as of Nov 23, 2013 which is error-free.]


I wrote it in C# .Net 4.5 (Visual Studio 2012)


It runs as a Window Service.

The Windows Service hosts my own custom-coded WebHost controller (aka webserver) that I wrote.

The web server hosts the content of the web page that I intend to use from my tablet via wi-fi to control the USB attached relay circuits (as I explained in previous posts),

the KMTronic relay controller logic is embedded into the webserver's server-side code interacting with the webpage (of course).


I coded my own WebHost because I didn't want to have to install Microsoft IIS and the baggage that comes with that. (and just for the sake of the programming-challenge I suppose
).



The code is a bit ugly ATM because I threw it together very quickly, but it all works up to the KMTronic stuff; this I know.

It's a good starting foundation, at least...
 
#99 ·
#16 ·
Here's an explanation of each part of the app.


It is composed of 6 parts.


1) The TestConsole project: is a command-line interface to host the WebHost. (I use this for fast debugging).

2) The WebContent project: has the webpage stuff in it and bridges the gap between the webhost layer and the RelayConfigurator. (aka code-behind page).

3) The WebHost project: that hosts the WebContent stuff. (This emulates IIS, or Apache for the Java folks)

4) The RelayLib project: that talks with the KMTronic hardware. (Untested)

5) The RelayConfig project: this stores the Relay Configuration stuff, and right now it bridges the gap between the WebContent and the RelayLib. (aka business-logic layer)

6) The BassThatHzRelay project: that hosts the Window Service stuff (when we are running it for real, and aren't debugging stuff via the command line hoster), this hosts the WebHost.



The two important pieces are as follows....




#4 The RelayLib:


Pretty straight forward, based on the examples that KMTronic has posted on their website. This is where the rubber meets the road.

Since the web host is multithreaded I dropped down into single-threading mode so as not to have issues with the controller.


I see a code bug there with the State flags, they are being ignored, no big deal I'll fix it when I get the hardware.
Code:
Code:
[CODE]public class Relay
    {
        public byte ID;
        public bool State;
        public string Name;
    }

     public class RelayController
    {
        private static Mutex mut = new Mutex();

        public void ToggleRelays(List RelaysToStart, List RelaysToStop)
        {
            ToggleListOfRelayBoxes(true, RelaysToStart);
            ToggleListOfRelayBoxes(false, RelaysToStop);
        }

        private void ToggleListOfRelayBoxes(bool State, List Relays)
        {
            if (Relays != null)
            {
                foreach (var relay in Relays)
                {
                    relay.State = State;
                    ToggleRelay(relay.ID, relay.State);
                }
            }
        }

        private void ToggleRelay(byte ChannelID, bool State)
        {
            byte StateByte = 0x00;
            if (State)
                StateByte = 0xFF;

            mut.WaitOne();          
            var coms = System.IO.Ports.SerialPort.GetPortNames();
            var serialPort1 = new System.IO.Ports.SerialPort(coms[0]);
            serialPort1.Open();
            serialPort1.Write(new byte[] { 0xFF, ChannelID, StateByte }, 0, 3);
            serialPort1.Close();
            mut.ReleaseMutex();
        }
    }
[/CODE]



#3 The WebHost:

This is my favorite part, I always wanted to write my own HTTP handler.


This code is gonna need more work if I want things to look they weren't built in 1995 (resource locator/handler)



I have it hardcoded to localhost domain name resolution and port 8080 with no security layer yet. (good enough for now....)


I process the HTML requests Asynchronously, assuming EndGetContext is multi-threaded and thread-safe.



I put in some default HTML return code just to help with debugging stuff, just in-case I mess something up (unlikely).
Code:
Code:
[CODE]public class WebHost
    {
        private HttpListener listener = new HttpListener();

        private Func _HttpResponseHandler;
        Thread ListenerThread;
        bool IsRunning = true;

        public void Start(string uri, Func httpResponseHandler)
        {
            //example: "http://localhost:8080/"
            listener.Prefixes.Add(uri);
            listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;

            _HttpResponseHandler = httpResponseHandler;
            
            Stop();
            listener.Start();
            IsRunning = true;

            ListenerThread = new Thread(new ThreadStart(ProcessRequestsAsync));
            ListenerThread.Start();
       }

        private void ProcessRequestsAsync()
        {
            while (IsRunning && listener.IsListening)
            {
                IAsyncResult result = listener.BeginGetContext(new AsyncCallback(HttpRequestHandler), listener);
                result.AsyncWaitHandle.WaitOne();
            }
        }

        public void Stop()
        {
            IsRunning = false;
            if (ListenerThread != null)
            {
                try
                {
                    ListenerThread.Abort();
                }
                catch { }
            }

            if (listener.IsListening)
            {
                listener.Stop();
            }
        }

        private void HttpRequestHandler(IAsyncResult result)
        {
            HttpListenerContext Context = listener.EndGetContext(result);

            HttpListenerRequest Request = Context.Request;
            HttpListenerResponse Response = Context.Response;

            if (Request.RawUrl != "/" && Request.RawUrl != "/Default.html")
            {
                return;
            }

            string ResponseString  = String.Empty;

            Response.StatusCode = 200;
            Response.StatusDescription = "OK";
            Response.ContentType = "text/html";

            if (_HttpResponseHandler != null)
            {
                ResponseString = _HttpResponseHandler.Invoke(Request);
            }

            if (String.IsNullOrEmpty(ResponseString))
            {
                ResponseString = "Default Content";
            }

            byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(ResponseString);
            Response.ContentLength64 = Buffer.Length;
            System.IO.Stream OutputStream = Response.OutputStream;
            OutputStream.Write(Buffer, 0, Buffer.Length);
            OutputStream.Close();
            Response.Close();
            
        }
    }
[/CODE]


Here is the actual webpage that gets send to the bowser, kept it pretty simple (for now...)
Code:
Code:
[CODE]
[/CODE]



Should run lightning-fast, and all of this fits within just 30kb of harddrive space and RAM. (winning batman, WINNING!
)

Much better than the jigobytes of RAM, diskspace, and zillions of CPU cores needed to run the more traditional bloatware of today (Norton AV and Vista both come to mind
).
 
#17 ·
Just purchased the hardware. 16 channels. It's coming from Bulgaria so it's gonna take a while to get here. I paid the extra for a tracking number though.



It will be cool to see my software in action.


Now, I wonder if I even have a free USB port left? I might have to get a usb hub too!
 
#18 ·
Hardware will be here on the 22nd according to the tracking number.

It's on the east coast right now and it has to go to the west coast yet, so 1900 more miles to go.




It's traveled 5200miles so far, hopefully it's still in one piece.
 
#19 ·
The hardware has arrived.


I'll be cannibalizing this LiteOn power supply as a power source for it.




Now, time to see if this thing works... or if it catches on fire. haha



Looks like I have some work to do...
 
#21 ·
I installed the 64bit driver and plugged it in to my $3000 HTPC, and flames didn't come out. So good so far...






Now I'll have to load my code and see if I can make it all work together!
 
#23 ·
Here's Beta-Version: 0.9 of my app.


I've corrected some minor errors in the alpha version of my code that prevented the relay from operating correctly, as I speculated.


This code works:

BassThatHzRelayBeta0.9.zip 24k .zip file




You can see the changes I had to make to fix it compared to the previous blurb.


I also now keep the controller connection open until all devices have finished switching states, it makes them switch faster (more on this below...)
Code:
Code:
[CODE]public class RelayController
    {
        private static Mutex mut = new Mutex();
        private System.IO.Ports.SerialPort serialPort1;

        public RelayController()
        {
            mut.WaitOne();
            var coms = System.IO.Ports.SerialPort.GetPortNames();
            serialPort1 = new System.IO.Ports.SerialPort(coms[1]);
            mut.ReleaseMutex();
        }

        public void ToggleRelays(List RelaysToStart, List RelaysToStop)
        {
            mut.WaitOne();
            serialPort1.Open();

            ToggleListOfRelayBoxes(true, RelaysToStart);
            ToggleListOfRelayBoxes(false, RelaysToStop);

            serialPort1.Close();
            mut.ReleaseMutex();
        }

        private void ToggleListOfRelayBoxes(bool State, List Relays)
        {
            if (Relays != null)
            {
                foreach (var relay in Relays)
                {
                    ToggleRelay(relay.ID, State);
                }
            }
        }

        private void ToggleRelay(byte ChannelID, bool State)
        {
            byte StateByte = 0x00;
            if (State)
                StateByte = 0x01;
        
            serialPort1.Write(new byte[] { 0xFF, ChannelID, StateByte }, 0, 3);
        }
    }
[/CODE]


The non-beta version will include a relay delayer and have configurable relay settings / com channel.

Basically better control over the activities going on, right now everything is still hard-coded to my theater's specifications, which you are welcome to customize yourself.
 
#25 ·
I've coded the delay-er logic and made the com channel configurable.


I have at least three more things I want to do to it yet:

1) Browser image hosting (to make the browser page look fancier).

2) Configuration-driven relays (to make it not specific to just my theater, and not needing recompiles).

3) SSL and Password Authentication (to have security control).
Code:
Code:
[CODE]namespace RelayLib
{
    public class Relay
    {
        public byte ID = 0x01;
        public string Name = string.Empty;
        public TimeSpan? StartDelay = null;
    }

    public class RelayController
    {
        private static Mutex mut = new Mutex();
        private System.IO.Ports.SerialPort serialPort1;

        public RelayController()
        {
            mut.WaitOne();

            string com_channel = ConfigurationManager.AppSettings.Get("com_channel");
            if (String.IsNullOrEmpty(com_channel))
            {
                var coms = System.IO.Ports.SerialPort.GetPortNames();
                com_channel = coms[0];
            }
            serialPort1 = new System.IO.Ports.SerialPort(com_channel);

            mut.ReleaseMutex();
        }

        public void ToggleRelays(List RelaysToStart, List RelaysToStop)
        {
            mut.WaitOne();
            serialPort1.Open();

            ToggleListOfRelayBoxes(true, RelaysToStart);
            ToggleListOfRelayBoxes(false, RelaysToStop);

            serialPort1.Close();
            mut.ReleaseMutex();
        }

        private void ToggleListOfRelayBoxes(bool State, List Relays)
        {
            if (Relays != null)
            {
                foreach (var relay in Relays)
                {
                    if (relay.StartDelay.HasValue)
                    {
                        System.Threading.Thread.Sleep((int)relay.StartDelay.Value.TotalMilliseconds);
                    }

                    ToggleRelay(relay.ID, State);
                }
            }
        }

        private void ToggleRelay(byte ChannelID, bool State)
        {
            byte StateByte = 0x00;
            if (State)
                StateByte = 0x01;

            serialPort1.Write(new byte[] { 0xFF, ChannelID, StateByte }, 0, 3);
        }
    }
}
[/CODE]
 
#26 ·

Quote:
Originally Posted by BassThatHz  /t/1457157/diy-home-automation/0_50#post_23988112


my $3000 HTPC
Three grand? I just built a 3770/7790/8G/120B SSD/1TB HDD HTPC and had change from a grand at Australian prices and can run full SVP/madVR.
 
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top