Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-08

ExecuteProcess problem

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Koenraad Lelong

Hi,
I'm extending an old daemon so it writes the data to MQTT, using mosquitto_pub.
To have a correct message (-m ...) I need to include double-quotes. But it seems ExecuteProcess(exec,cmdline) splits the cmdline according some rules if it's a single string.
When I tried to do it in a single string :
-h 127.0.0.1 -t solartopic -u user -P secret -i clientid -m "{\"Pac\": 458.00}"
I got :
Error: Unknown option 'Pac\":'.
So I need to make cmdline an array. How I do that ? I tried :

  MQTTStr : array [0..5] of ansistring;

...

  MQTTStr[0]:='-h '+MQTTBroker;
  MQTTStr[1]:='-t '+MQTTTopic;
  MQTTStr[2]:='-u '+MQTTUser;
  MQTTStr[3]:='-P '+MQTTPassw;
  MQTTStr[4]:='-i '+MQTTClientID;
  MQTTStr[5]:='-m "{\"'+ChanName+'\": '+TextValue+'}"';
  ExecuteProcess(MQTTExec,MQTTStr));

But then :
Error: Unknown option '-h 127.0.0.1'.
Use 'mosquitto_pub --help' to see usage.

Thanks in advance

Koenraad

Reply by email

Karoly Balogh (Charlie/SGR
to Koenraad Lelong

Hi,

Koenraad Lelong wrote 27 lines
I'm extending an old daemon so it writes the data to MQTT, using mosquitto_pub. To have a correct message (-m ...) I need to include double-quotes. But it seems ExecuteProcess(exec,cmdline) splits the cmdline according some rules if it's a single string. When I tried to do it in a single string : -h 127.0.0.1 -t solartopic -u user -P secret -i clientid -m "{\"Pac\": 458.00}" I got : Error: Unknown option 'Pac\":'. So I need to make cmdline an array. How I do that ? I tried : MQTTStr : array [0..5] of ansistring; ... MQTTStr[0]:='-h '+MQTTBroker; MQTTStr[1]:='-t '+MQTTTopic; MQTTStr[2]:='-u '+MQTTUser; MQTTStr[3]:='-P '+MQTTPassw; MQTTStr[4]:='-i '+MQTTClientID; MQTTStr[5]:='-m "{\"'+ChanName+'\": '+TextValue+'}"'; ExecuteProcess(MQTTExec,MQTTStr)); But then : Error: Unknown option '-h 127.0.0.1'. Use 'mosquitto_pub --help' to see usage. Thanks in advance

Just a guess, but I think you need to do:

MQTTStr[0]:='-h';
MQTTStr[1]:=MQTTBroker;
MQTTStr[2]:='-t';
MQTTStr[3]:=MQTTTopic;
... etc.
MQTTStr[n]:='-m';
MQTTStr[n+1]:=MQTTMessage;

Like this. It's maybe a bit counter intuitive, but you have to think from the perspective of the other app, everything which you put into 1 string will go into 1 "slot" of its "ParamStr(x)". And then it's maybe more obvious how you need to split your arguments.

BTW, shameless plug, but this is a very very hacky way to talk to an MQTT broker I think, I've made a raw C header conversion, and a Pascal-style multithreaded OOP wrapper for libmosquitto, so you can properly integrate MQTT communication in your Free Pascal app (tested on Linux, MacOS and Windows):

https://github.com/chainq/mosquitto-p

I'm not sure of the complexity of what you want to do, but for anything remotely more complex, than sending 1 message every minute or so, this might be a better solution than trying to squeeze things through mosquitto_pub's arguments.

Cheers,

Charlie

Reply by email

Koenraad Lelong
to Karoly Balogh (Charlie/SGR: Just a guess, but I think you need to do: MQTTStr[0]:='-h'; MQTTStr[1]:=MQTTBroker;…

Hi,
Thanks for the suggestion I will try that array.

Why I did it like that : I thought it an easy and fast way to enhance my project. I added the same value to rrd, the same way, i.e. a ExecuteProcess with a bunch of parameters. There it works, but no double quotes needed.

LAter I will try your wrapper.

Op 25/08/2020 om 15:52 schreef Karoly Balogh (Charlie/SGR) via fpc-pascal:

Show quoted text 12 lines
Just a guess, but I think you need to do: MQTTStr[0]:='-h'; MQTTStr[1]:=MQTTBroker; MQTTStr[2]:='-t'; MQTTStr[3]:=MQTTTopic; ... etc. MQTTStr[n]:='-m'; MQTTStr[n+1]:=MQTTMessage;

...

Show quoted text 9 lines
BTW, shameless plug, but this is a very very hacky way to talk to an MQTT broker I think, I've made a raw C header conversion, and a Pascal-style multithreaded OOP wrapper for libmosquitto, so you can properly integrate MQTT communication in your Free Pascal app (tested on Linux, MacOS and Windows): https://github.com/chainq/mosquitto-p

Reply by email

Koenraad Lelong
to Luca Olivetti: El 25/8/20 a les 14:31, Koenraad Lelong via fpc-pascal ha escrit: Not what you're asking,…

Hi,

Thanks for the reply, I did know of that project. The thing is, at the moment I don't know how to implement that in my project. I'm still studying this.

Op 25/08/2020 om 15:55 schreef Luca Olivetti via fpc-pascal:

Show quoted text 8 lines
Not what you're asking, but did you try to send the messages yourself instead of using mosquitto_pub? I didn't try it, but there's https://github.com/heX16/mqtt-free-pascal Bye

Reply by email

Koenraad Lelong
to Koenraad Lelong: Thanks for the suggestion I will try that array. Why I did it like that : I thought it an…

FWIW,

That array did the trick. Thanks again.
But I'll study the wrapper in the near future.

Op 25/08/2020 om 18:18 schreef Koenraad Lelong via fpc-pascal:

Show quoted text 36 lines
Hi, Thanks for the suggestion I will try that array. Why I did it like that : I thought it an easy and fast way to enhance my project. I added the same value to rrd, the same way, i.e. a ExecuteProcess with a bunch of parameters. There it works, but no double quotes needed. LAter I will try your wrapper. Op 25/08/2020 om 15:52 schreef Karoly Balogh (Charlie/SGR) via fpc-pascal: > > Just a guess, but I think you need to do: > > MQTTStr[0]:='-h'; > MQTTStr[1]:=MQTTBroker; > MQTTStr[2]:='-t'; > MQTTStr[3]:=MQTTTopic; > > ... etc. > MQTTStr[n]:='-m'; > MQTTStr[n+1]:=MQTTMessage; > ... > > BTW, shameless plug, but this is a very very hacky way to talk to an MQTT > broker I think, I've made a raw C header conversion, and a Pascal-style > multithreaded OOP wrapper for libmosquitto, so you can properly integrate > MQTT communication in your Free Pascal app (tested on Linux, MacOS and > Windows): > > https://github.com/chainq/mosquitto-p > _______________________________________________ fpc-pascal maillist  -  https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email