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
Right... I think I had inverted the test value in the if-statement when I had that problem,
Thank you for your patience!
No, it won't go recursive because of
if (p.Value == "1")
clause. Your program is simply different then this example program.
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.
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.
http://iot-playground.com/forum/bug-reports/197-sms-automation-enable-disable#1331
iot-playground.com/forum/automation/191-automation-sequencing
iot-playground.com/forum/automation/191-automation-sequencing
iot-playground.com/forum/automation/191-automation-sequencing
RSS feed for comments to this post