I connected an Arduino MKR WAN 1310 to The Things Network. Then tested if the Arduino uplinks are published on TTN MQTT service. And if I can publish a downlink message on that service too. This post is my log of the activity.
Read this post first: Arduino MKR WAN 1310 on The Things Network (TTN) . This posts starts at the point where the Arduino is working with The Things Network (TTN). |
The Example setup
An Arduino MKR WAN 1310 is registered and tested on TTN ( Arduino MKR WAN 1310 on The Things Network (TTN) ).
TTN MQTT service integration is enabled.
Data that the Arduino uplinks via LoRaWan, arrives on a TTN MQTT topic.
Data that's published to a topic on that TTN MQTT service, arrives on the Arduino as a downlink.
Earlier, I showed how to integrate with the AWS IoT MQTT service. That allows you to use your data with all AWS services.
This is simpler. It's a direct integration within the TTN network. You can use an MQTT client (or Node-RED, ...) to exchange messages.
Assumptions:
- you have your Arduino MKR WAN 1310 integrated with TTN
- you have (or download an MQTT client. I use MQTTX. It has to support MQTT 3.1.1.
Steps:
- enable integration of your TTN application with the MQTT service
- configure an MQTT client
- test uplink to MQTT and
- test downlink from MQTT
We don't have to change the Arduino code or modify existing TTN setup. The integration is (same as with AWS before) an extension, not a modification.
Enable TTN to MQTT integration
Enable the integration and set up a password
The integration is actually enabled by default. You just need to get an access key for your client. From the TTN console, open your application, and navigate to Integrations -> MQTT. The console shows all settings you need, except for the password. Generate that.
Take care to copy the key and store it in a safe place. It 'll be your MQTT password.
Test the integration with TTN MQTT
TTN has written a good manual for that too: TTN MQTT Server. I followed the exact steps and it worked. I downloaded MQTTX as test client. In a real application, you could use Node-RED flows...
Take over the settings from your console. The password is the one you saved a few steps ago when generating the new API key. Set the MQTT version to 3.1.1. Else the client connection will fail.
Connect.
uplink from Arduino
To see incoming messages, Subscribe to topic v3/*****-01@ttn/devices/****7/up. (pattern: v3/{application id}/devices/{device id}/up).As soon as your Arduino sends a message over LoRaWan, it will appear in MQTT log . Here's a capture of an incoming payload:
That works. The payload is JSON, because we provided an uplink formatter in our TTN device setup.
downlink to Arduino
We can test that with MQTTX too. Publish a message to the v3/********-01@ttn/devices/*************7/down/push topic. (pattern: v3/{application id}/devices/{device id}/down/push)
Payload:
{ "downlinks": [{ "f_port": 1, "decoded_payload": { "ledState": "on" }, "priority": "NORMAL" }] }
We can also use JSON here, because we have a downlink formatter in TTN.
The message will immediately appear in the live data log in the TTN console
But will not yet arrive on the Arduino. That's because we'll only get downlink data when we start a communication (design choice in my sketch). Once we receive an uplink from the Arduino, the downlink is sent back:
And the LED will light up
One more exercise: Arduino MKR WAN 1310 integration with Node-RED via The Things Network .
Enjoy.