dm_mqtt Module

Module for publishing messages via MQTT, using libcurl. The libcurl library must have been built with the MQTT option enabled.

Limitations of libcurl:

  • Only QoS level 0 is implemented for publish.
  • No way to set retain flag for publish.
  • No TLS (mqtts) support.
  • Naive EAGAIN handling will not handle split messages.

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.


Uses

  • module~~dm_mqtt~~UsesGraph module~dm_mqtt dm_mqtt curl curl module~dm_mqtt->curl iso_c_binding iso_c_binding module~dm_mqtt->iso_c_binding module~dm_error dm_error module~dm_mqtt->module~dm_error module~dm_kind dm_kind module~dm_mqtt->module~dm_kind module~dm_util dm_util module~dm_mqtt->module~dm_util module~dm_error->module~dm_kind module~dm_ascii dm_ascii module~dm_error->module~dm_ascii iso_fortran_env iso_fortran_env module~dm_kind->iso_fortran_env module~dm_util->module~dm_error module~dm_util->module~dm_kind

Used by

  • module~~dm_mqtt~~UsedByGraph module~dm_mqtt dm_mqtt module~dmpack dmpack module~dmpack->module~dm_mqtt

Functions

public function dm_mqtt_init() result(rc)

Initialises MQTT backend.

Arguments

None

Return Value integer

public function dm_mqtt_publish(url, message, timeout, error_message, error_curl) result(rc)

Publishes MQTT message message on topic with address url by calling libcurl.

Arguments

Type IntentOptional 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.

Return Value integer

public function dm_mqtt_url(host, topic, port) result(url)

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 /.

Read more…

Arguments

Type IntentOptional 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).

Return Value character(len=:), allocatable

Created URL.


Subroutines

public subroutine dm_mqtt_destroy()

Cleans-up MQTT backend.

Arguments

None