EnOcean Link  1.14.0.0
Middleware to Connect EnOcean easily to other Projects
tutorial1.cpp
#ifdef WIN32
#include <windows.h>
#define SER_PORT "\\\\.\\COM3" //COM43
#else
#define SER_PORT "/dev/ttyUSB0"
#endif
#define SAVE_CONFIG "./learned.txt"
#define LEARN_TIME_S 1
#include "./eoLink.h"
#include <stdio.h>
int main(int argc, const char* argv[])
{
(void)argc;
(void)argv;
//First a Gateway and a storageManager will be definied
eoGateway myGateway;
printf("Opening Connection to USB300 \n");
if (myGateway.Open(SER_PORT)!=EO_OK)
{
printf("Failed to open USB300\n");
return 0;
}
printf("EnOcean-Link Gateway LearnMode\n");
//adding a dBm Filter as learnFilter
eodBmFilter * learnFilter = new eodBmFilter();
myGateway.learnFilter = learnFilter;
learnFilter->maxdBm=-0;
learnFilter->mindBm=-60;
//the eoIDFilter will allow our Gateway application to filter all the unwanted Telegrams
eoIDFilter * myFilter = new eoIDFilter();
myGateway.filter=myFilter;
//recv stores the return Value of the Gateway
uint16_t recv;
//As RPS Telegrams don't have a teach IN Telegram, we need to set the wished profiled for an automated Teach IN
myGateway.TeachInModule->SetRPS(0x02,0x01);
//Set the learnTime and update the time
uint32_t learnTime=eoTimer::GetTickCount()+LEARN_TIME_S*10000;
uint32_t time=eoTimer::GetTickCount();
//Activate LearnMode
myGateway.LearnModeOn();
while(learnTime>time)
{
//updates the time, using the HAL getTickCount function
//the Gateway::Receive() Functions returns different flags, depending on the Packet we got
recv = myGateway.Receive();
if (recv & RECV_SECTEACHIN)
{
printf("Duplicated Teach IN receveid from %08", myGateway.device->ID);
}
//as we're in LearnMode currently we only want to process Teach_IN Telegrams(as specified in EEP)
else 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 %08X 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));
}
}
}
}
myGateway.LearnModeOff();
//adding our Gateway to the StorageManager allows the storage Manager to save it
myStore.addObject("Gateway",&myGateway);
printf("Storing the Config\n");
myStore.Save(SAVE_CONFIG);
printf("EnOcean-Link Gateway FilterdMode\n");
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 %08X\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) ==EO_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) ==EO_OK)
{
printf("%s %u \n", profile->GetChannel(i)->ToString(NAME),t);
}
}
}
}
return 0;
}