Please login or register.

Login with username, password and session length

Author Topic: Basic program to turn on a Light  (Read 2243 times)

petera

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 27
  • Posts: 1750
Basic program to turn on a Light
« on: January 22, 2019, 01:14:34 PM »

Here's an example of a very basic program to turn your light on. Its written in CSharp and is the equivalent of Hello World in the Home Automation world. This is just to get you started using Helper Classes and can be expanded to cover as many lights as you like on or off

You need to rename this 1004-Houselights.hgx before you import it into HG.

To install in HG click Configure-Programs-Lights and move to the bottom of the screen click Actions-Import and click on Browse to wherever you save the program, select that program and click Import. You will see your new program in the list.

To edit the program click on Houselights. The click on Program Code tab. You will now see a simple 5 lines of code.

In the code on Line 4 and Line 5 just change the addresses to one's that suit you. When finished select Compile in the Compile tab.When finished compiling move down to the bottom left Actions tab and click Save. Then click Actions tab and select Run. All going well you should have just turned your light on.

just let me know if it works for you.
Logged

bkenobi

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 24
  • Posts: 2081
Re: Basic program to turn on a Light
« Reply #1 on: January 22, 2019, 05:23:16 PM »

If you want to be really fancy, you can even have the program notify the user who on the GUI that the light has been turned on by adding:

Code: [Select]
Program.Notify( "Activity Alert", "Module X10 B5 turned ON" );
Or, you could also have it send an email notification with:

Code: [Select]
Net.SendMessage( "email@address.com", "HG Activity Alert", "HG set Module X10 B5 to ON" );
All kinds of possibilities if there is interest in learning!

petera

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 27
  • Posts: 1750
Re: Basic program to turn on a Light
« Reply #2 on: January 22, 2019, 06:03:10 PM »

If you want to be really fancy, you can even have the program notify the user who on the GUI that the light has been turned on by adding:

Code: [Select]
Program.Notify( "Activity Alert", "Module X10 B5 turned ON" );
Or, you could also have it send an email notification with:

Code: [Select]
Net.SendMessage( "email@address.com", "HG Activity Alert", "HG set Module X10 B5 to ON" );
All kinds of possibilities if there is interest in learning!

Great minds think alike but you were one step ahead  :)%

I was aiming to gently introduce those concepts. The five lines of code didn't look too scary I reckoned but this is what we need to be doing to get HG off the launch pad and into orbit.

It's one thing installing HG and another getting productive with it.

Logged

Tuicemen

  • Administrator
  • Hero Member
  • ****
  • Helpful Post Rating: 282
  • Posts: 10497
  • I don't work for X10, I use it successfuly!
Re: Basic program to turn on a Light
« Reply #3 on: February 05, 2019, 08:30:58 AM »

Stating that it is 5 lines of code is a bit misleading as the first 3 are comments stating what the code is and do nothing. They could have been combined into one line or even removed and put into the program description on the Summary page
In reality there are only 2 lines of actual code.

This is similar to doing a macro record in HG and clicking on b5 then save. Except this is only triggerable from the API or testable from inside the program itself unless expanded upon with a trigger which should have been included in the sample.
Adding bkenobi's additional email snippet makes it a bit more interesting however I suspect those that downloaded the sample were a bit disappointed having no trigger and that's the reason for poor feed back.
Logged
Please Read Topic:
General Forum Etiquette
Before you post!

Tuicemen

  • Administrator
  • Hero Member
  • ****
  • Helpful Post Rating: 282
  • Posts: 10497
  • I don't work for X10, I use it successfuly!
Re: Basic program to turn on a Light
« Reply #4 on: February 05, 2019, 09:07:16 AM »

I also noticed the commented out sections state it is for Modules.WithName("Light 1").On() and it should read // Modules.WithAddress("B5").On(); but that's being overly picky. ;) The program works how you stated petera.
 >!
Logged
Please Read Topic:
General Forum Etiquette
Before you post!

bkenobi

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 24
  • Posts: 2081
Re: Basic program to turn on a Light
« Reply #5 on: February 05, 2019, 10:36:19 AM »

If the name of the light is "Light 1" then the commented out version also works.

Tuicemen

  • Administrator
  • Hero Member
  • ****
  • Helpful Post Rating: 282
  • Posts: 10497
  • I don't work for X10, I use it successfuly!
Re: Basic program to turn on a Light
« Reply #6 on: February 05, 2019, 11:07:50 AM »

If the name of the light is "Light 1" then the commented out version also works.
Yes, if the first actual code line  looks like this
Code: [Select]
var x10B5 = Modules.InDomain("HomeAutomation.x10").WithName("Light 1"); At least on my HG  ;)
Logged
Please Read Topic:
General Forum Etiquette
Before you post!

bkenobi

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 24
  • Posts: 2081
Re: Basic program to turn on a Light
« Reply #7 on: February 05, 2019, 12:57:30 PM »

There are lots of ways to write code.  Many people don't include documentation of any kind which is technically acceptable though harder to maintain.  Heck, some people intentionally make it hard to read by obfuscating the source so even experts would be delayed.  When I build a project, I always try to keep the documentation in the form of comments and debug capability (even if it's been disabled).  If I were to release a snippet for people to see as an example, I would probably leave those comments in and refer to the total length of code rather than just the executable lines since that's what my IDE usually would display at the bottom.  Whether I said it was 1 line or 5 lines is really irrelevant.  The point is that I provided a snippet to help rather than how I accounted for the length.

Another point worth making is that HG allows for the user to script in a variety of languages.  Each of those results in a different length of code that one must type ranging from nothing (Wizard script via recording) to many (pretty much any other option).  If I wrote the code myself, I could make a single complicated line or break it out into 2-5 parts to make it more readable for myself (or new users) to follow.  None of that is wrong as it accomplishes the same goal.  I've seen many examples of a single line loop including logic, print statements, etc. that was near unreadable.

In the end, a snippet that shows a new user an example is a good thing...   >!

bkenobi

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 24
  • Posts: 2081
Re: Basic program to turn on a Light
« Reply #8 on: February 05, 2019, 01:06:34 PM »

Oh, just to extend my point on the length of code being kinda meaningless:

C#:
Code: [Select]
// Hello World! program
namespace HelloWorld
{
    class Hello {         
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");
        }
    }
}

C# 1 line semi-obfuscated:
Code: [Select]
using System;struct a{static int Main(){object[]c={"\u0048e\x6c\x6co "+(C\u0068ar)(86+1)+"or\x6c\x64"};typeof(Conso\u006ce).GetMet\u0068o\u0064s()[101].Invoke(c,c);return 0;}}

Tuicemen

  • Administrator
  • Hero Member
  • ****
  • Helpful Post Rating: 282
  • Posts: 10497
  • I don't work for X10, I use it successfuly!
Re: Basic program to turn on a Light
« Reply #9 on: February 05, 2019, 01:37:23 PM »

In the end, a snippet that shows a new user an example is a good thing...   >!
Exactly, no disagreement there!
Many new to C sharp wouldn't know // at the beginning of a line was just a comment and not required.
Newbies may download it, look at it and maybe even run it but I suspect that is all.
I stated the fact of only 2 lines of actual code to maybe have more look at it, not to attack any ones coding skills.
Logged
Please Read Topic:
General Forum Etiquette
Before you post!

racerfern

  • Hero Member
  • *****
  • Helpful Post Rating: 6
  • Posts: 275
Re: Basic program to turn on a Light
« Reply #10 on: February 05, 2019, 04:35:47 PM »

I can't see the forest!

KISS!

Logged
 

X10.com | About X10 | X10 Security Systems | Cameras| Package Deals
© Copyright 2014-2016 X10.com All rights reserved.