This tutorial will describe helper functions as part of automation in EasyIoT server. With helper functions you can access to EasyIoT server internal structure, trigger events and hook events.
DriverHelper
With driver helper you can send commands to modules. Commands are:
- ControlOn
- ControlOff
- ControlLevel
Definition:
public string ProcessCommad(string domain, string address, string command, string value)
For example, if you want to switch on digital output in MySensors driver address N1S3 use command:
DriverHelper.ProcessCommad(Domains.MYSENSORS, "N1S3", "ControlOn", "");
To switch off the same digital output use:
DriverHelper.ProcessCommad(Domains.MYSENSORS, "N1S3", "ControlOff", "");
If you want to set analog value (for example dimmer) to 50 use:
DriverHelper.ProcessCommad(Domains.MYSENSORS, "N2S0", "ControlLevel", "50");
This command will send value 50 to MySensors driver and addess N2S0.
EventHelper
Use event helper if you want hook event or if we want to trigger event.
Definition:
public EventHelper ModuleChangedHandler(Func<object, Module, DriverPropertyChangedData, bool> handler)
public void SetEvent(string domain, string address, string property)
To hook event use:
public void Setup()
{
EventHelper.ModuleChangedHandler((o, m, p) =>
{
Console.WriteLine(m.Address + " in program id "+ Program.ProgramId.ToString()+ " driver "+ m.Domain + " address "+ m.Address + " property "+ p.Property + " value " + p.Value);
return true;
});
}
This example will write info about all events in EasyIoT server to console output. Always use EventHelper.ModuleChangedHandler in Setup() function and not in Run().
Next example is combination of event helper and driver helper. When MySensor driver on address N5S0 property Sensor.Temperature is changed value is send to ESP8266 module on address N1S4. This example forwards temperature value from one sensor to other.
public void Setup()
{
EventHelper.ModuleChangedHandler((o, m, p) =>
{
// outdoor
if (m.Domain == Domains.MYSENSORS && m.Address == "N5S0" && p.Property == "Sensor.Temperature")
DriverHelper.ProcessCommad(Domains.ESP8266, "N1S4", "ControlLevel", p.Value);
return true;
});
}
To trigger driver event use SetEvent. If event is triggered property value will be changed, logged in database (if logging is enabled) and UI (Web or app. gui) display will be updated. In this case value is not sent to driver, it stays only in EasyIoT server.
Example is:
EventHelper.SetEvent(Domains.MYSENSORS, "N5S0", "Sensor.Temperature");
SmsHelper
Use SMS helper only if SMS interface is enabled.
Definition:
public bool SendSms(string phoneNo, string message)
To send SMS on telephone number XXXXXXX use:
SmsHelper.SendSms("XXXXXXX","EasyIoT server SMS message");
EmailHelper
With email helper you can send emails. You need valid email account.
Definition:
public void SetupSmtp(string username, string password, string host, int port, bool enableSSL)
public bool SendEmail(string from, string to, string subject, string body)
Function SendEmail will return true if value is sent successfully.
IMPORTANT
If you are using Raspberry Pi platform execute following commands before first use of email helper:
sudo mozroots --import --ask-remove --machine
sudo certmgr -ssl smtps://smtp.gmail.com:465
Here is example how to send email from Gmail account:
EmailHelper.SetupSmtp("This email address is being protected from spambots. You need JavaScript enabled to view it.", "password", "smtp.gmail.com", 587, true);
EmailHelper.SendEmail("This email address is being protected from spambots. You need JavaScript enabled to view it.", "This email address is being protected from spambots. You need JavaScript enabled to view it.", "test", "test EasyIoT server email helper");
Change from and to address and password. First line should be called only once and inits email helper. Second line sends email and can be called multiple times.
ModuleHelper
Module helper has two functions to read module property and to set module property. When you set module property value event is not triggered - this means no database logging and no UI update.
Definition:
GetProperty(string domain, string address, string property)
SetProperty(string domain, string address, string property, string value)
Next example will read ESP8266 driver module address N3S0, property Sensor.Humidity and print it's value in console output. Always check if property value is not null.
ModuleParameter p = ModuleHelper.GetProperty(Domains.ESP8266, "N3S0", "Sensor.Humidity");
if (p!= null)
Console.WriteLine(p.Value);
Following example will set property Sensor.Humidity in driver ESP8266 address N3S0 to value 52.2:
ModuleHelper.SetProperty(Domains.ESP8266, "N3S0", "Sensor.Humidity", "52.2");
ProgramHelper
With program helper you can read current automation program id:
Console.WriteLine(Program.ProgramId.ToString());
See also:
EasyIoT server automation - part I
EasyIoT server automation - part II
See more tutorials at http://iot-playground.com/build
Buying guide
To support this site and EasyIoT framework development please buy in our store.
Comments
Very useful information specially the last part :
) I care for such info much. I was seeking this particcular info for a very long time.
Thank you and good luck.
Meanwhile I have to follow with OpenHAB that it is not "Easy"
RSS feed for comments to this post