Sending data to controller

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
cherowley
Normal user
Posts: 125
Joined: 14 Jan 2016, 09:39

Sending data to controller

#1 Post by cherowley » 09 Oct 2016, 09:55

I'm writing a plugin where I need to send values to several different devices on the controller (in my case domoticz).

I can't see how to send info except using the event struct which uses the uservars.

I want to use my own variables and set device type etc...

Currently i've fudged it by taking the code in the domoticz controller file and copying it into my own plugin and changing as required.

I would however prefer to do it the "proper" way so as to keep it compatible with multiple controllers etc..

Any pointers gratefully received :)

cherowley
Normal user
Posts: 125
Joined: 14 Jan 2016, 09:39

Re: Sending data to controller

#2 Post by cherowley » 10 Oct 2016, 16:30

Still struggling with this :(

Any help out from you C coding gurus? :)

User avatar
moelski
Normal user
Posts: 161
Joined: 31 Aug 2016, 06:33
Location: Germany - NRW
Contact:

Re: Sending data to controller

#3 Post by moelski » 10 Oct 2016, 18:25

Hi !

Maybe some code would be helpful ;)

And for me it´s not clear what you are doing ...
Are you writing a new controller plugin or a normal Input/Output plugin?
regards
Dominik

cherowley
Normal user
Posts: 125
Joined: 14 Jan 2016, 09:39

Re: Sending data to controller

#4 Post by cherowley » 10 Oct 2016, 20:41

Thanks moelski :)

I've written my own plugin (thermostat+timer) which has 3 user vars I wish to see in domoticz:
Set point
Heating On
Flame Lit

These 3 values need to go to (in my case) domoticz, BUT 3 different IDX (3 different virtual devices in domoticz:

Virtual Thermostat
Virtual Switch
Virtual Switch.


How would I use the standard espeasy procedures? I woudl call it 3 times each time passing my own data (switch type, idx, value etc)

Thanks :)

At the moment I pinched the code from the standard domoticz controller and adjusted it as below. But obviously this is bad for duplication and also means it will only work for domoticz..


void Plugin_121_Manual_Controller_Update(int IDX, int DeviceType, int DeviceValue, float DeviceValueFloat)
{
String authHeader = "";
if ((SecuritySettings.ControllerUser[0] != 0) && (SecuritySettings.ControllerPassword[0] != 0))
{
base64 encoder;
String auth = SecuritySettings.ControllerUser;
auth += ":";
auth += SecuritySettings.ControllerPassword;
authHeader = "Authorization: Basic " + encoder.encode(auth) + " \r\n";
}

char log[80];
boolean success = false;
char host[20];
sprintf_P(host, PSTR("%u.%u.%u.%u"), Settings.Controller_IP[0], Settings.Controller_IP[1], Settings.Controller_IP[2], Settings.Controller_IP[3]);

sprintf_P(log, PSTR("%s%s using port %u"), "HTTP : connecting to ", host, Settings.ControllerPort);
addLog(LOG_LEVEL_DEBUG, log);

// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, Settings.ControllerPort))
{
connectionFailures++;
strcpy_P(log, PSTR("HTTP : connection failed"));
addLog(LOG_LEVEL_ERROR, log);
return;
}

if (connectionFailures)
connectionFailures--;

// We now create a URI for the request
String url = F("/json.htm?type=command&param=");

switch (DeviceType)
{
case 1: // switch
url += F("switchlight&idx=");
url += String(IDX);
if (DeviceValue == 1) url += "&switchcmd=On";
if (DeviceValue == 0) url += "&switchcmd=Off";

break;
case 2: // Set Point
url += F("udevice&idx=");
url += String(IDX);
url += F("&nvalue=0&svalue=");
url += toString(DeviceValueFloat, 1);
break;
}

url.toCharArray(log, 80);
addLog(LOG_LEVEL_DEBUG_MORE, log);

// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + authHeader + "Connection: close\r\n\r\n");

unsigned long timer = millis() + 200;
while (!client.available() && millis() < timer)
delay(1);

// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\n');
line.toCharArray(log, 80);
addLog(LOG_LEVEL_DEBUG_MORE, log);
if (line.substring(0, 15) == "HTTP/1.1 200 OK")
{
strcpy_P(log, PSTR("HTTP : Success"));
addLog(LOG_LEVEL_DEBUG, log);
success = true;
}
delay(1);
}
strcpy_P(log, PSTR("HTTP : closing connection"));
addLog(LOG_LEVEL_DEBUG, log);

client.flush();
client.stop();
}

User avatar
moelski
Normal user
Posts: 161
Joined: 31 Aug 2016, 06:33
Location: Germany - NRW
Contact:

Re: Sending data to controller

#5 Post by moelski » 11 Oct 2016, 07:11

Hi !

I think you have done something wrong here 8-)

Your thermostat/timer should update the uservar. Take a look in the _P005_DHT.ino for example ...
You will find a section like this:

Code: Select all

          UserVar[event->BaseVarIndex] = NAN;
          UserVar[event->BaseVarIndex + 1] = NAN;
There you have to place your variables.

And of cource you have to configure the plugin correctly ... (SensorType SENSOR_TYPE_TRIPLE for example)

And the transport to domoticz would be done automatically if you choose the controller plugin (the build in - not yours).

Hope this helps a little ...
regards
Dominik

cherowley
Normal user
Posts: 125
Joined: 14 Jan 2016, 09:39

Re: Sending data to controller

#6 Post by cherowley » 11 Oct 2016, 09:57

Hello :)

Ah yes but I have 3 values to send to 3 different devices - NOT just one device you see.. All from ONE Task...

That is why I am using 3 devices in domoticz:

A switch to turn heating on/off.
Another switch just to keep track of when boiler is actually lit. (read only in domoticz)
A thermostat SET point.

As standard ESPeasy does NOT allow sending to different device id's in the controller from ONE TASK....

The actual temperature as read from a dht22 or dallas etc is sent as normal as it has its own task in espeasy.

You're quite right, espeasy could send the data from uservars to the controller automatically BUT it can only send it to ONE device as standard...

Hope this clears up the confusion :)

cherowley
Normal user
Posts: 125
Joined: 14 Jan 2016, 09:39

Re: Sending data to controller

#7 Post by cherowley » 12 Oct 2016, 12:41

Bump :)

User avatar
moelski
Normal user
Posts: 161
Joined: 31 Aug 2016, 06:33
Location: Germany - NRW
Contact:

Re: Sending data to controller

#8 Post by moelski » 12 Oct 2016, 19:49

Hi !
BUT it can only send it to ONE device as standard...
If you mean by device the Protocol like "MQTT" or "domoticz" then you are right. 8-)

Write your own controller plugin whuich can send to 3 receivers at a time.
There is no way for doing this in ESPEasy at the moment - at least I don´t know one.
regards
Dominik

cherowley
Normal user
Posts: 125
Joined: 14 Jan 2016, 09:39

Re: Sending data to controller

#9 Post by cherowley » 13 Oct 2016, 09:37

Yep think I'll have to duplicate code unfortunately, thanks anyways :)

cherowley
Normal user
Posts: 125
Joined: 14 Jan 2016, 09:39

Re: Sending data to controller

#10 Post by cherowley » 13 Oct 2016, 16:46

This code seems to work, hopefully it'll be reliable, I simply call it 3 times :)

void Plugin_121_Controller_Update(byte TaskIndex, byte BaseVarIndex, int IDX, byte SensorType)
{
if (Settings.Protocol && IDX > 0)
{
LoadTaskSettings(TaskIndex);
byte ProtocolIndex = getProtocolIndex(Settings.Protocol);
struct EventStruct TempEvent;
TempEvent.TaskIndex = TaskIndex;

TempEvent.BaseVarIndex = BaseVarIndex;
TempEvent.idx = IDX;
TempEvent.sensorType = SensorType;
CPlugin_ptr[ProtocolIndex](CPLUGIN_PROTOCOL_SEND, &TempEvent, dummyString);
}
}

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 35 guests