EnOcean Link  1.14.0.0
Middleware to Connect EnOcean easily to other Projects
Tutorial 1

The Source Code of this Tutorial can be found here: tutorial1.cpp

This Tutorial will help you to understand the base concept of an eoGateway,eoStorageManager and the concept of Filter eoIDFilter,eodBmFilter.

After starting up the example, it will a be definied Time in learnMode for near field Devices. These learned in Devices and their configuration will be stored using the eoStorageManager. To start developing an eoLink application you've either to include the SourceCode or a preCompiled library to your linker, and include the "eoLink.h" file.

#include "./eoLink.h"

In our example we use some defines, to change the LearnTime,the config FileName and the device to use.

#define SER_PORT "/dev/ttyUSB1"// the Serial Port Device
#define SAVE_CONFIG "./learned.txt" // where to store the gateway Configuration
#define LEARN_TIME_S 10 // allows you to set the time the gateway should stay in learnmode
Note
The following code parts can be either all in the main function, or an own function which returns an integer.

To receive Telegrams we need an eoGateway and connect it via eoGateway::Open to an EnOcean Gateway Device like the USB300.

eoGateway = myGateway;
printf("Opening Connection to USB300 \n");
if (myGateway.Open(SER_PORT)!=OK)
{
printf("Failed to open USB300\n");
return 0;
}

If the connection is stable, we could now start to receive Telegrams, but we want only to receive Telegrams which are in the near Field range. To archieve this we add to our Gateway Class an eodBmFilter.

eodBmFilter * learnFilter = new eodBmFilter();
myGateway.learnFilter = learnFilter;
learnFilter->maxdBm=-0;
learnFilter->mindBm=-60;

As we want to see in the Normal Mode only Telegrams from learned IN devices, we've to add an eoIDFilter.

eoIDFilter * myFilter = new eoIDFilter();
myGateway.filter=myFilter;

Now we're nearly ready to receive and handle the Packets but to do so we need an uint16_t variable to handle the ReturnFlags of the eoGateway::Receive Function.

uint16_t recv;

And as we want to stay only a short time, after the startup, in learnMode we've to add some variables which check the passed Time.We're gonna use Time::getTickCount(), which increases every 10ms the Tick amount.

uint32_t learnTime=Timer::getTickCount()+LEARN_TIME_S*100;
uint32_t time=Timer::getTickCount();

In this case we say we want to TeachIN devices which either send a 4BS TeachIN telegram, or if we get a RPS telegram, that we TeachIN a 4Button Rocker.

As the RPS Profiles don't have a TeachIN message we've to set the profile manually.

myGateway.TeachInModule->SetRPS(0x02,0x01);

Now we only have to change from the normal gateway mode to the learnMode.

Note
to activate learnMode just change the eoGateway::LearnMode to true, to deactivate it again change it to false
//Activate LearnMode
myGateway.LearnMode=true;
Now we've prepared everything we need to get to Receive Messages and handle TeachIN Telegrams.
while(learnTime>time)
{
//updates the time, using the HAL getTickCount function
time=eoTimer::getTickCount();
//the Gateway::Receive() Functions returns different flags, depending on the Packet we got
recv = myGateway.Receive();
//as we're in LearnMode currently we only want to process Teach_IN Telegrams(as specified in EEP)
if (recv & RECV_TEACHIN)
{
//add the Source ID to the Normale Mode Filter
myFilter->Add(myGateway.telegram.sourceID);
//Print out the Message to stdout
//If the TeachIN process was successfull and we got a Profile print out the Message!
eoProfile *profile = myGateway.device->GetProfile();
if(profile!=NULL)
{
printf("Device %08lX Learned-In EEP: %02X-%02X-%02X\n", myGateway.device->ID, profile->rorg, profile->func, profile->type );
for (int i = 0; i<profile->GetChannelCount(); i++)
{
printf("%s %.2f ... %.2f %s\n", profile->GetChannel(i)->ToString(NAME), profile->GetChannel(i)->min, profile->GetChannel(i)->max, profile->GetChannel(i)->ToString(UNIT));
}
}
}
}
When we leave this function, we want to switch to normal mode and store the current configuration of the Gateway and all learned in devices!

To store an Object, which has the ISerialize interface implemented, you use the eoStorageManager and add Objects to handle to the Manager.

myGateway.LearnMode=false;
myStore.addObject("Gateway",&myGateway);//the name has to be unique!
myStore.Save(SAVE_CONFIG);

Now you've a text file, containing all the learned-in devices, which you can use for other applications.

This example application now just shows the incoming data of TeachIN devices, and if the profile is Supported, the Values are printed in clear Text.

while (1)
{
recv = myGateway.Receive();
if (recv & RECV_MESSAGE)
{
//Resend received telegrams
myGateway.Send(myGateway.message);
}
//If we got a valid Profile Telegram which is not a Learn IN we print the received Values
if (recv & RECV_PROFILE)
{
printf("Device %08lX\n", myGateway.device->ID);
eoProfile *profile = myGateway.device->GetProfile();
float f;
uint8_t t;
for (int i = 0; i<profile->GetChannelCount(); i++)
{
//get the channel value if it is a float
if (profile->GetValue( profile->GetChannel(i)->type, f) ==OK)
printf("%s %.2f %s\n", profile->GetChannel(i)->ToString(NAME),f,profile->GetChannel(i)->ToString(UNIT));
//get the channel value if it is an uint
if (profile->GetValue( profile->GetChannel(i)->type, t) ==OK)
printf("%s %u \n", profile->GetChannel(i)->ToString(NAME),t);
}
}
}
return 0;