Read LoRaWAN data from TTN server

TTN – The Things Network

Our LoRaWAN sensors use the service of The Things Network. So our LoRaWAN gateway sends the sensor data to the TTN server. The sensors are registered with an ‘application id’ and a ‘device id’.

Now I want to receive the data on my PC. The TTN server allows to use MQTT.

Note: Currently only the up-topic is working and get the data in JSON format.


mosquitto –

Please read the base information on

We want to subscribe using TLS. The access schema seems to be a little bit different. In our case only with the option ‘-L’ it was possible to switch to TLS communication:

mosquitto_sub.exe -L mqtts://<AppID>:<AppKey>@<region> –cafile mqtt-ca.pem

We receive




Our solution is based on the package paho-mqtt.

At the first you have to install it to your python environment. Possible you have to update your paket manager at the first.

example Win10: (of course python should be installed already)

cd Python37-32
python -m pip install --upgrade pip
cd scripts
pip install paho-mqtt

Now start the python shell – IDLE (Python 3.7 32-bit)

Load/create the small script It print the sensor value temperature.

import paho.mqtt.client as mqtt
import json

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print('Connected with result code '+str(rc))

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    ergebnis = json.loads(msg.payload)
    values = ergebnis['payload_fields']

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect('', 8883, 60)


Of course you have to configure your AppId and AppKey.


Connected with result code 0


printf() using ST-Link debug interface (SWD, ITM View)


ARM provides the possibility to use a printf() like a serial output, using the SWD interface (ITM port 0). This example describes the usage using a Nucleo-64 board, ST-Link v2.1 and the STM32CubeIDE.

Note: The Cortex-M0 doesn’t have the required hardware in the mcu. So it is not possible on it.

switch printf() to the debug interface

Often you find the describtion to overwrite _write(). That’s correct. In case of the STM32CubeIDE generated code it is also possible to overwrite the more low level __io_putchar().

int __io_putchar(int ch) {
    return ch;

Debug Settings

You have to enable the Serial Wire Viewer (SWV). The most relevant setting is the Core clock. You have to configure the correct clock frequency, that you initialize within your application.

STM32CubeIDE Debug Configuration
STM32CubeIDE Debug Configuration SWV

Prepare the Debug view

  • Open the SWV ITM Console
open SWV ITM Console
open SWV ITM Console

printf() uses the SWO connector and the trace port 0.

  • Add ITM port 0
ITM port 0
ITM port 0
  • Enable ITM port 0
ITM port 0
ITM port 0
  • Start trace to view the printf() output
Start Trace
Start Trace

That’s all.

printf output

Alternative ST-Link Utility

ST-Link_Utility config
ST-Link_Utility config


Using UART3 as virtual COM on Nucleo-64 / STM32L476


By default the UART2 is connected to the virtual COM of the ST-Link. The same UART is possible to connect to the Arduino connector.

So I want to connect the UART3 to the virtual COM connector and use the UART2 for the Arduino connector (CN9 Pin 1 and 2),

The description is for an Nucleo-64 STM32L476, but should be compatible to some more Nucleo-64 boards, too.

Hardware changes

Switch UART2 to the Arduino connector

  1. open solder bridge SB13 and SB14
  2. close solder bridge SB62 and SB63
Nucleo-64 Uart2 to Arduino
Nucleo-64 solder bridges

Nucleo-64 evaluation board

Switch UART3 to the VCOM of the ST-Link

  1.  connect ST-Link connector TX to PC5 (Uart3 RX, CN10 pin 6)
  2. connect ST-Link connector RX to PC4 (Uart3 TX, CN10 pin 34)
Nucleo-64 UART3 to ST-Link
Wires to connect the UART3 to the ST-Link

Software changes

  • initialize UART3  (e.g. 115200 Baud, 8N1) with PC4 and PC5
  • in case you want to use printf
int __io_putchar(int ch) {
    HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 0xFFFF);
    return ch;
  • alternatively overwrite _write()