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.

RASPBERRY PI 3 - Model B. 1GB RAM

New Raspberry Pi 3

Raspberry Pi 512MB Model B +

Raspberry Pi Model B+, ARM1176JZF-S Running at 700MHz, 512MB RAM.

Raspberry Pi 3 Ultimate Starter Kit - Wifi, HDMI, Breadboard, SD Card

Everything you need to build basic EasyIoT server on Raspberry Pi platform.

MICRO SD 64GB 32GB 16GB MEMORY CARD

USB STICK SIM Modem 7.2MBPS 3G Wireless USB Dongle

To control EasyIoT server by SMS message.

Acrylic Case Clear Transparent Shell Enclosure Computer Box Kit For Raspberry Pi

Acrylic Case Shell Enclosure for Raspberry Pi B+

Raspberry Pi to NRF24l01+ Shield EasyIoT server compatibile

Raspberry Pi to NRF24l01+ Shield EasyIoT server compatibile, Raspberry Pi, Raspberry PI B, Raspberry Pi 2

Prototyping Shield Module For Raspberry Pi

To build custom sensors and NRF24L01 transmitter/receiver on Raspberry Pi.

NRF24L01+PA+LNA SMA Antenna Wireless Transceiver communication module 2.4G

5V 2A Charger Micro USB

For Raspberry Pi or sensor modules, cheap and easy power solution.

USB Wifi Dongle Adapter 802.11n for for Raspberry Pi Model B+ B

No need for network cables on EasyIoT server. Use WiFI USB dongle.

 

 


Comments   

#4 profile 2018-10-31 15:36
Need cheap hosting? Try webhosting1st, just $10 for an year.

+2 #3 www.pinterest.com 2015-03-28 23:49
Great post. I was checking constantly this blog and I'm impressed!

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.
+2 #2 Oscar Alvarez Valle 2015-03-14 08:22
:-) Agree. I'm ansious for Roller shutter control and to start automation.
Meanwhile I have to follow with OpenHAB that it is not "Easy"
+2 #1 Harry 2015-03-12 16:25
Great tutorial! Just what I needed. Thanks.

You have no rights to post comments

Forum latest

  • No posts to display.