Module for publishing messages via MQTT, using libcurl. The libcurl library must have been built with the MQTT option enabled.
Limitations of libcurl:
An MQTT server must be running, such as Mosquitto. On FreeBSD install Mosquitto with:
# pkg install net/mosquitto
Start the service locally:
# service mosquitto onestart
Subscribe topic /fortran
:
# mosquitto_sub -h 127.0.0.1 -t /fortran
In Fortran, we then create the URL of the topic /fortran
on host
127.0.0.1
, and publish the message:
character(len=:), allocatable :: url
integer :: rc
rc = dm_mqtt_init()
url = dm_mqtt_url(host='127.0.0.1', topic='/fortran', port=1883)
rc = dm_mqtt_publish(url, 'Hello, from Fortran!')
call dm_mqtt_destroy()
Any client subscribing topic /fortran
will receive the message.
The procedures dm_mqtt_init()
and dm_mqtt_destroy()
have to be called
once per process, and only if neither the RPC nor the mail backend is
initialised already.
Initialises MQTT backend.
Publishes MQTT message message
on topic with address url
by
calling libcurl.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | url |
URL to MQTT server/topic. |
||
character(len=*), | intent(in), | target | :: | message |
Message to publish. |
|
integer, | intent(in), | optional | :: | timeout |
Connection timeout. |
|
character(len=:), | intent(out), | optional, | allocatable | :: | error_message |
cURL error message. |
integer, | intent(out), | optional | :: | error_curl |
cURL error code. |
Returns allocatable string of URL to MQTT server. Uses the URL API
of libcurl to create the URL. If port
is 0
, the default port
will be used. The topic must start with character /
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | host |
IP or FQDN of MQTT server. |
||
character(len=*), | intent(in) | :: | topic |
MQTT topic. |
||
integer, | intent(in), | optional | :: | port |
MQTT server port (1883 by default). |
Created URL.
Cleans-up MQTT backend.