Search code examples
linuxiotgateway

Connecting Mainflux IoT Agent to EdgeX Gateway


I'm currently trying to setup a minimal EdgeX - Mainflux setup to monitor specific sensor data at our company.

For prototyping I set up 2 Linux Servers

  • EdgeX Gateway
  • Mainflux Server

The gateway receives readings from hardware devices, and should pass them to the mainflux server for further processing.

For that I was following the examples from the official mainflux docs https://mainflux.readthedocs.io/en/latest/agent/

  1. Created a user
  2. Created a 'gateway-thing'
  3. Created 2 channels (data_channel, control_channel)
  4. Connected gateway-thing to both channels
  5. Created bootstrap configuration
gateway_thing_id="43259265-394c-4cc4-bf50-58aba33432ce"
control_channel_id="18599c67-2699-4654-8eac-016133875932"
data_channel_id="ba67f32e-1912-4038-9515-6d4dd6024413"
bootstrap_configuration='
{
    "external_id": "44:e1:2d:e6:cf:03",
    "thing_id": "43259265-394c-4cc4-bf50-58aba33432ce",
    "external_key": "edged",
    "name": "edged",
    "channels": [
        "ba67f32e-1912-4038-9515-6d4dd6024413",
        "18599c67-2699-4654-8eac-016133875932"
    ],
    "content":"{\"log_level\":\"debug\",\"http_port\":\"9000\",\"mqtt_url\":\"tcp://localhost:1883\",\"edgex_url\":\"http://localhost:48090/api/v1/\"}"
}'

Switched to EdgeX Server

  1. Created environment variables for agent
export MF_AGENT_LOG_LEVEL=debug
export MF_AGENT_BOOTSTRAP_KEY=edged
export MF_AGENT_BOOTSTRAP_ID=44:e1:2d:e6:cf:03
export MF_AGENT_BOOTSTRAP_URL='http://192.168.137.95:8202/things/bootstrap/'
  1. Started agent

Because the instructions required me to install natsd, I spun up an instance via docker. Now everything works as expected. I can publish and subscribe to messages EdgeX <-> Mainflux Server. What the instructions do not cover is how to proceed from there.

As I understand it, the Agent needs to be installed on the EdgeX server because it acts as bridge between Edge and Mainflux. The agent connects to the mqtt server on the EdgeX side to be able to publish and receive control/data commands back and forth.

I don't get how the agent is connected to the gateway and what the NATSD instance is doing. Would anyone would have an idea as to what to do to get data off the edgex server onto the mainflux instance?


Solution

  • It is presumed that you are running both edgex and agent on same gateway/PC.

    To recieve data from edgex server API endpoints you send mqtt message to the agent

    For example:

    mosquitto_pub -u <thing_id> -P <thing_key> -t channels/<channel_id>/messages/req -h localhost -m '[{"bn":"1:", "n":"control", "vs":"edgex-config, edgex-support-notifications, edgex-core-data"}]'
    

    This will make agent to send request to edgex endpoint http://localhost:48082/api/v1/config

    and retrieve config data for edgex-support-notifications, edgex-core-data

    to observe response you have to subscribe on channels/<channel_id>/messages/res

    supported endpoints on edgex are /config, /metrics, /operation, and /ping

    As for the NATS it is not used for communicating with edgex
    NATS is being used for other services that may be subscribed for commands that would receive from Mainflux via agent.

    Also other services can report its liveliness by publishing to heartbeat topic on NATS which is picked up by agent and is available to fetch over mqtt.