In this tutorial we will show how to execute sequence of commands in EasyIoT server (scene control). In program example we will set all devices to off by single switch in UI interfce.

Sometimes we want to execute set of commands when we press button. We will show how to do it in EasyIoT server Virtua module and automation script. In our example we will Add button to switch off more devices for example lights.

It is recommended that you read Automation tutorials before start programming in EasyIoT server Automation:

EasyIoT server automation - part I

EasyIoT server automation - part II

EasyIoT server automation - part III

 

First step is to add Virtual Digital DO module in EasyIoT server Virtual driver. We will name it "All off" Remember virtual module address.

Then go in automation section and add new automation program. In this case there is no need for CRON parameter. Read Automation tutorial how to add new program.

 

We will use EventHelper to catch event when we press button - this part will be in setup function. Following example program will be triggered when switch on is pressed in Virtual switch. Virtual switch will be automatically switched off after 3 sec. Change module address to catch your Virtual module. Also change part where commands are send to modules to suits your configuration.

/* 
 This code is running one time when program is enabled 
*/ 
public void Setup() 
{ 
  EventHelper.ModuleChangedHandler((o, m, p) => 
  {  
    // filter digital switch - change module address 
    if (m.Domain == Domains.VIRTUAL && m.Address == "N14S0" && p.Property == "Sensor.DigitalValue"){ 
      // filter when switch on event  
      if (p.Value == "1") 
      { 
        //Console.WriteLine(m.Address + " in program id "+ Program.ProgramId.ToString()+ " driver "+ m.Domain + " address "+ m.Address + " property "+ p.Property + " value " + p.Value); 

        //here are commands to execute in sequence - change commands to suit your configuration   
        DriverHelper.ProcessCommad(Domains.MYSENSORS, "N1S3", "ControlOff", ""); 
        DriverHelper.ProcessCommad(Domains.ESP8266, "N1S2", "ControlOff", ""); 
        DriverHelper.ProcessCommad(Domains.ESP8266, "N2S4", "ControlOff", ""); 
        DriverHelper.ProcessCommad(Domains.MYSENSORS, "N3S3", "ControlOff", ""); 

        // switch off module - change module address 
        System.Threading.Thread.Sleep(3000);  
        DriverHelper.ProcessCommad(Domains.VIRTUAL, "N14S0", "ControlOff", "");       
      } 
    } 
    return true;  
  });  
} 

/* 
  This code is running periodicaly when program is enabled.   
  Cron job detirmine running period.  
*/ 
public void Run() 
{ 
} 

 

 

See more tutorials at ​http://iot-playground.com/build​

 


Comments   

+1 #9 Egil Opsahl 2015-05-07 17:33
Quoting Super User:

No, it won't go recursive because of
if (p.Value == "1")
clause. Your program is simply different then this example program.

Right... I think I had inverted the test value in the if-statement when I had that problem,
Thank you for your patience! :-)
+1 #8 Super User 2015-05-07 09:49
Quoting Egil Opsahl:
Quoting Super User:
[quote name="Egil Opsahl"]
What is the purpose of this delay?

Read tutorial and you will know.

The tutorial says "Virtual switch will be automatically switched off after 3 sec."
I had to remove the following line, as it seemed it made the server go recursive (or some other eternal loop) at this point. I had to restart the EasyIoT server to get out of it.


No, it won't go recursive because of
if (p.Value == "1")
clause. Your program is simply different then this example program.
+1 #7 Egil Opsahl 2015-05-07 01:03
Quoting Super User:
[quote name="Egil Opsahl"]
What is the purpose of this delay?

Read tutorial and you will know.

The tutorial says "Virtual switch will be automatically switched off after 3 sec."
I had to remove the following line, as it seemed it made the server go recursive (or some other eternal loop) at this point. I had to restart the EasyIoT server to get out of it.
#6 Egil Opsahl 2015-05-07 00:58
Quoting Super User:

I just tested with 3 virtual switches and it works for me. Be careful to use Automation patch. Read this post

http://iot-playground.com/forum/bug-reports/197-sms-automation-enable-disable#1331

1) My nodes are dimmers, not switches, I think that is what makes a difference. I have to insert delays that are longer than the duration of the dimming to make it work.

2) I am not able to see any relevant information in your link.
#5 Super User 2015-05-06 23:06
Quoting Egil Opsahl:
Finally, I am not able to make the sequencing work. These lines should switch off all three color channels of an RGB LED strip:
Quote:
//here are commands to execute in sequence - change commands to suit your configuration DriverHelper.ProcessCommad(Domains.VIRTUAL, "N10S0", "ControlOff", ""); DriverHelper.ProcessCommad(Domains.VIRTUAL, "N11S0", "ControlOff", ""); DriverHelper.ProcessCommad(Domains.VIRTUAL, "N12S0", "ControlOff", "");

For some reason, only one of the channels gets switched off. I have tried to reorder the lines, but I do not see any pattern to which line gets executed.
I just tested with 3 virtual switches and it works for me. Be careful to use Automation patch. Read this post

http://iot-playground.com/forum/bug-reports/197-sms-automation-enable-disable#1331
-1 #4 Super User 2015-05-06 23:04
Quoting Egil Opsahl:
The preceding line:
Quote:
System.Threading.Thread.Sleep(3000);

is a 3 second delay.
What is the purpose of this delay?
Read tutorial and you will know.
#3 Egil Opsahl 2015-05-06 22:55
Deleted. Please see feedback in the forum:
iot-playground.com/forum/automation/191-automation-sequencing
#2 Egil Opsahl 2015-05-06 22:51
Deleted. Please see feedback in the forum:
iot-playground.com/forum/automation/191-automation-sequencing
#1 Egil Opsahl 2015-05-06 22:42
Deleted. Please see feedback in the forum:
iot-playground.com/forum/automation/191-automation-sequencing

You have no rights to post comments