i have edited and tried csharp program for 2 led control using 2 switch modules: program run successfully. But only green led could controlled. red led didnt show the op. Can you pls check the code and correct it.
var portname = "COM1"; /* Serial port on PC or on Raspberry Pi */
/* Virtual module added in Setup(Trigger) section (other tab) */
/* In this case we added one module with the address "A0" in the domain "HomeAutomation.MCU"
domain of type "Switch" and using the "generic switch" widget for displaying it in the control
panel. There are other widgets that can be used (sensor, door, window, light, dimmer etc..)
Btw, a program can also create more that just one module by using AddVirtualModules. */
//Program.AddVirtualModule("HomeAutomation.MCU", "A0", "Switch", "homegenie/generic/switch");
/* We will use this module */
/* Now, to get a reference to your virtual module: */
var module = Modules.InDomain("HomeAutomation.MCU").WithAddress("A0").Get();
var mod1 = Modules.InDomain("HomeAutomation.X10").WithAddress("3").Get();
/* Serial Reception */
/* HandleMessageReceived(): ASCII
* HandleStringReceived(): UTF-8 */
Action<string>
HandleStringReceived = (string message) =>
{
/* This will be called every time a message is received from serial port */
Program.Notify("SerialPort String", message);
/* our MCU Protocol is very simple, 1 Byte RXed, if that is '0' turn on module, otherwise turn off */
switch(message)
{
case "A0":
if (message[0] != '0')
{
module.On();
}
else
{
module.Off();
}
break;
case "3":
if (message[0] != '0')
{
mod1.On();
}
else
{
mod1.Off();
}
break;
}
/* turn the hue light 1 on (method 2, calling api directly)
module.Command("Control.On").Execute(); */
};
Action<bool>
HandleStatusChanged = (bool connected) =>
{
/* this will be called every time the connection status changes */
Program.Notify("SerialPort Status", connected ? "CONNECTED!" : "DISCONNECTED!");
};
/* As a switch, the module should also handle "Control.On" and "Control.Off" commands issued by the user
from the Web user interface, or by scripting. (define Web service) */
When.WebServiceCallReceived("HomeAutomation.MCU", ( args ) =>
{
string[] reqs = ((string)args).Split('/');
var res = "{ 'ResponseValue' : 'ERROR' }";
try
{
string command = reqs[2];
string moduleid = reqs[1];/* we wont use this since we only have one module */
switch(command)
{
case "Control.On":
/* eg. Control URL:
http://hg_address/api/HomeAutomation.MCU/A0/Control.On */
/* eg. here you send a message to the serial port for switch ON the device */
Program.RaiseEvent(module, "Status.Level", "1", "Indicator");
res = "{ 'ResponseValue' : 'OK' }";
SerialPort.SendMessage("1");
break;
case "Control.Off":
/* eg.
http://hg_address/api/HomeAutomation.MCU/A0/Control.Off */
/* here you send a message to the serial port for switch OFF the device */
Program.RaiseEvent(module, "Status.Level", "0", "Indicator");
res = "{ 'ResponseValue' : 'OK' }";
SerialPort.SendMessage("0");
break;
}
}
catch (Exception ex)
{
res = ex.Message + " " + ex.StackTrace;
}
return res; /* unable to process request */
});
When.WebServiceCallReceived("HomeAutomation.X10", ( args ) =>
{
string[] reqs = ((string)args).Split('/');
var res = "{ 'ResponseValue' : 'ERROR' }";
try
{
string command = reqs[2];
string moduleid = reqs[1]; /* we wont use this since we only have one module */
switch(command)
{
case "Control.On":
/* eg. Control URL:
http://hg_address/api/HomeAutomation.MCU/A0/Control.On */
/* eg. here you send a message to the serial port for switch ON the device */
Program.RaiseEvent(mod1, "Status.Level", "1", "Indicator");
res = "{ 'ResponseValue' : 'OK' }";
SerialPort.SendMessage("1");
break;
case "Control.Off":
/* eg.
http://hg_address/api/HomeAutomation.MCU/A0/Control.Off */
/* here you send a message to the serial port for switch OFF the device */
Program.RaiseEvent(mod1, "Status.Level", "0", "Indicator");
res = "{ 'ResponseValue' : 'OK' }";
SerialPort.SendMessage("0");
break;
}
}
catch (Exception ex)
{
res = ex.Message + " " + ex.StackTrace;
}
return res; /* unable to process request */
});
/* open the serial port channel and register handlers */
SerialPort.WithName( portname )
.OnStatusChanged( HandleStatusChanged )
/* .OnMessageReceived( HandleMessageReceived ) */ /* For UTF-8 */
.OnStringReceived( HandleStringReceived )
.Connect( 9600 ); /* change baud rate if needed */