{"id":968,"date":"2020-07-03T16:48:18","date_gmt":"2020-07-03T14:48:18","guid":{"rendered":"http:\/\/www.embedded-communication.com\/?p=968"},"modified":"2020-07-06T08:14:33","modified_gmt":"2020-07-06T06:14:33","slug":"english-read-lorawan-data-from-ttn-server","status":"publish","type":"post","link":"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/","title":{"rendered":"Read LoRaWAN data from TTN server"},"content":{"rendered":"<p><\/p>\n<h1>TTN &#8211; The Things Network<\/h1>\n<p><a href=\"https:\/\/www.thethingsnetwork.org\/\">https:\/\/www.thethingsnetwork.org\/<\/a><\/p>\n<p>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 &#8216;application id&#8217; and a &#8216;device id&#8217;.<\/p>\n<p><a href=\"https:\/\/console.thethingsnetwork.org\/\">https:\/\/console.thethingsnetwork.org\/<\/a><\/p>\n<p>Now I want to receive the data on my PC. The TTN server allows to use MQTT.<\/p>\n<p><a href=\"https:\/\/www.thethingsnetwork.org\/docs\/applications\/mqtt\/quick-start.html\">https:\/\/www.thethingsnetwork.org\/docs\/applications\/mqtt\/quick-start.html<\/a><\/p>\n<p>Note: Currently only the up-topic is working and get the data in JSON format.<\/p>\n<p><a href=\"https:\/\/status.thethings.network\/\">https:\/\/status.thethings.network\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>mosquitto &#8211; <a href=\"https:\/\/mosquitto.org\/\">https:\/\/mosquitto.org\/<\/a><\/h2>\n<p>Please read the base information on<\/p>\n<p><a href=\"https:\/\/www.thethingsnetwork.org\/docs\/applications\/mqtt\/quick-start.html\">https:\/\/www.thethingsnetwork.org\/docs\/applications\/mqtt\/quick-start.html<\/a><\/p>\n<p>We want to subscribe using TLS. The access schema seems to be a little bit different. In our case only with the option &#8216;-L&#8217; it was possible to switch to TLS communication:<\/p>\n<p>mosquitto_sub.exe -L mqtts:\/\/&lt;AppID&gt;:&lt;AppKey&gt;@&lt;region&gt;.thethings.network:8883\/+\/devices\/+\/up &#8211;cafile mqtt-ca.pem<\/p>\n<p>We receive<\/p>\n<pre>{\r\n  \"app_id\":\"stm32_example_usi_expansion_board\",\r\n  \"dev_id\":\"sensor-ro-20200522\",\r\n  \"hardware_serial\":\"E24F43FFFE44C1EF\",\r\n  \"port\":99,\"counter\":605,\r\n  \"payload_raw\":\"AHMnSwFnARYCaFADAGQEAQAFAdc=\",\r\n  \"payload_fields\":{\r\n    \"barometric_pressure_0\":1005.9,\r\n    \"digital_in_3\":100,\r\n    \"digital_out_4\":0,\r\n    \"digital_out_5\":215,\r\n    \"relative_humidity_2\":40,\r\n    \"temperature_1\":27.8\r\n  },\r\n  \"metadata\":{\r\n    \"time\":\"2020-07-03T14:02:59.272943586Z\",\r\n    \"frequency\":868.1,\r\n    \"modulation\":\"LORA\",\r\n    \"data_rate\":\"SF9BW125\",\r\n    \"airtime\":246784000,\r\n    \"coding_rate\":\"4\/5\",\r\n    \"gateways\":[\r\n      {\r\n        \"gtw_id\":\"eui-3133303735005600\",\r\n        \"timestamp\":920207212,\r\n        \"time\":\"\",\r\n        \"channel\":0,\r\n        \"rssi\":-44,\r\n        \"snr\":11.75,\r\n        \"rf_chain\":0\r\n      }\r\n    ]\r\n  }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2>Python<\/h2>\n<p>Our solution is based on the package paho-mqtt.<\/p>\n<p><a href=\"https:\/\/pypi.org\/project\/paho-mqtt\/\">https:\/\/pypi.org\/project\/paho-mqtt\/<\/a><\/p>\n<p>At the first you have to install it to your python environment. Possible you have to update your paket manager at the first.<\/p>\n<p>example Win10: (of course python should be installed already)<\/p>\n<pre>cd Python37-32\r\npython -m pip install --upgrade pip\r\ncd scripts\r\npip install paho-mqtt<\/pre>\n<p>Now start the python shell &#8211; IDLE (Python 3.7 32-bit)<\/p>\n<p>Load\/create the small script mqtt.py. It print the sensor value temperature.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport paho.mqtt.client as mqtt\r\nimport json\r\n\r\n# The callback for when the client receives a CONNACK response from the server.\r\ndef on_connect(client, userdata, flags, rc):\r\n    print('Connected with result code '+str(rc))\r\n    client.subscribe('+\/devices\/+\/up')\r\n\r\n# The callback for when a PUBLISH message is received from the server.\r\ndef on_message(client, userdata, msg):\r\n    ergebnis = json.loads(msg.payload)\r\n    values = ergebnis&#x5B;'payload_fields']\r\n    print(values&#x5B;'temperature_1'])\r\n\r\nclient = mqtt.Client()\r\nclient.on_connect = on_connect\r\nclient.on_message = on_message\r\nclient.tls_set()\r\nclient.username_pw_set('AppId',password='AppKey')\r\n\r\nclient.connect('eu.thethings.network', 8883, 60)\r\n\r\nclient.loop_forever()\r\n<\/pre>\n<p>Of course you have to configure your AppId and AppKey.<\/p>\n<p>Output:<\/p>\n<pre>Connected with result code 0\r\n27.9\r\n27.9\r\n27.9\r\n27.9\r\n27.8\r\n27.9\r\n27.8\r\n27.8\r\n27.8\r\n27.8\r\n27.8<\/pre>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>TTN &#8211; The Things Network https:\/\/www.thethingsnetwork.org\/ 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 &#8216;application id&#8217; and a &#8216;device id&#8217;. https:\/\/console.thethingsnetwork.org\/ Now I want to receive the data on my PC. The TTN server allows &hellip; <a href=\"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Read LoRaWAN data from TTN server&#8221;<\/span><\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-968","post","type-post","status-publish","format-standard","hentry","category-misc"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Read LoRaWAN data from TTN server - embedded communication<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Read LoRaWAN data from TTN server - embedded communication\" \/>\n<meta property=\"og:description\" content=\"TTN &#8211; The Things Network https:\/\/www.thethingsnetwork.org\/ 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 &#8216;application id&#8217; and a &#8216;device id&#8217;. https:\/\/console.thethingsnetwork.org\/ Now I want to receive the data on my PC. The TTN server allows &hellip; Continue reading &quot;Read LoRaWAN data from TTN server&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/\" \/>\n<meta property=\"og:site_name\" content=\"embedded communication\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-03T14:48:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-07-06T06:14:33+00:00\" \/>\n<meta name=\"author\" content=\"Steffen Rose\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@EmbeddedComm\" \/>\n<meta name=\"twitter:site\" content=\"@EmbeddedComm\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Steffen Rose\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/\"},\"author\":{\"name\":\"Steffen Rose\",\"@id\":\"http:\/\/www.embedded-communication.com\/#\/schema\/person\/24f46183ed01fe51931528c28674b9ee\"},\"headline\":\"Read LoRaWAN data from TTN server\",\"datePublished\":\"2020-07-03T14:48:18+00:00\",\"dateModified\":\"2020-07-06T06:14:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/\"},\"wordCount\":360,\"publisher\":{\"@id\":\"http:\/\/www.embedded-communication.com\/#organization\"},\"articleSection\":[\"Misc\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/\",\"url\":\"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/\",\"name\":\"Read LoRaWAN data from TTN server - embedded communication\",\"isPartOf\":{\"@id\":\"http:\/\/www.embedded-communication.com\/#website\"},\"datePublished\":\"2020-07-03T14:48:18+00:00\",\"dateModified\":\"2020-07-06T06:14:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Startseite\",\"item\":\"http:\/\/www.embedded-communication.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Read LoRaWAN data from TTN server\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.embedded-communication.com\/#website\",\"url\":\"http:\/\/www.embedded-communication.com\/\",\"name\":\"embedded communication\",\"description\":\"A blog about embedded communcation protocols.\",\"publisher\":{\"@id\":\"http:\/\/www.embedded-communication.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.embedded-communication.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/www.embedded-communication.com\/#organization\",\"name\":\"embedded communication\",\"url\":\"http:\/\/www.embedded-communication.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.embedded-communication.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.embedded-communication.com\/wp-content\/uploads\/2016\/08\/embedded-communication.png\",\"contentUrl\":\"https:\/\/www.embedded-communication.com\/wp-content\/uploads\/2016\/08\/embedded-communication.png\",\"width\":240,\"height\":240,\"caption\":\"embedded communication\"},\"image\":{\"@id\":\"http:\/\/www.embedded-communication.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/EmbeddedComm\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.embedded-communication.com\/#\/schema\/person\/24f46183ed01fe51931528c28674b9ee\",\"name\":\"Steffen Rose\",\"url\":\"https:\/\/www.embedded-communication.com\/en\/author\/ro\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Read LoRaWAN data from TTN server - embedded communication","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/","og_locale":"en_US","og_type":"article","og_title":"Read LoRaWAN data from TTN server - embedded communication","og_description":"TTN &#8211; The Things Network https:\/\/www.thethingsnetwork.org\/ 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 &#8216;application id&#8217; and a &#8216;device id&#8217;. https:\/\/console.thethingsnetwork.org\/ Now I want to receive the data on my PC. The TTN server allows &hellip; Continue reading \"Read LoRaWAN data from TTN server\"","og_url":"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/","og_site_name":"embedded communication","article_published_time":"2020-07-03T14:48:18+00:00","article_modified_time":"2020-07-06T06:14:33+00:00","author":"Steffen Rose","twitter_card":"summary_large_image","twitter_creator":"@EmbeddedComm","twitter_site":"@EmbeddedComm","twitter_misc":{"Written by":"Steffen Rose","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/#article","isPartOf":{"@id":"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/"},"author":{"name":"Steffen Rose","@id":"http:\/\/www.embedded-communication.com\/#\/schema\/person\/24f46183ed01fe51931528c28674b9ee"},"headline":"Read LoRaWAN data from TTN server","datePublished":"2020-07-03T14:48:18+00:00","dateModified":"2020-07-06T06:14:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/"},"wordCount":360,"publisher":{"@id":"http:\/\/www.embedded-communication.com\/#organization"},"articleSection":["Misc"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/","url":"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/","name":"Read LoRaWAN data from TTN server - embedded communication","isPartOf":{"@id":"http:\/\/www.embedded-communication.com\/#website"},"datePublished":"2020-07-03T14:48:18+00:00","dateModified":"2020-07-06T06:14:33+00:00","breadcrumb":{"@id":"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.embedded-communication.com\/en\/misc\/english-read-lorawan-data-from-ttn-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Startseite","item":"http:\/\/www.embedded-communication.com\/"},{"@type":"ListItem","position":2,"name":"Read LoRaWAN data from TTN server"}]},{"@type":"WebSite","@id":"http:\/\/www.embedded-communication.com\/#website","url":"http:\/\/www.embedded-communication.com\/","name":"embedded communication","description":"A blog about embedded communcation protocols.","publisher":{"@id":"http:\/\/www.embedded-communication.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.embedded-communication.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/www.embedded-communication.com\/#organization","name":"embedded communication","url":"http:\/\/www.embedded-communication.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.embedded-communication.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.embedded-communication.com\/wp-content\/uploads\/2016\/08\/embedded-communication.png","contentUrl":"https:\/\/www.embedded-communication.com\/wp-content\/uploads\/2016\/08\/embedded-communication.png","width":240,"height":240,"caption":"embedded communication"},"image":{"@id":"http:\/\/www.embedded-communication.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/EmbeddedComm"]},{"@type":"Person","@id":"http:\/\/www.embedded-communication.com\/#\/schema\/person\/24f46183ed01fe51931528c28674b9ee","name":"Steffen Rose","url":"https:\/\/www.embedded-communication.com\/en\/author\/ro\/"}]}},"_links":{"self":[{"href":"https:\/\/www.embedded-communication.com\/en\/wp-json\/wp\/v2\/posts\/968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.embedded-communication.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.embedded-communication.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.embedded-communication.com\/en\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.embedded-communication.com\/en\/wp-json\/wp\/v2\/comments?post=968"}],"version-history":[{"count":6,"href":"https:\/\/www.embedded-communication.com\/en\/wp-json\/wp\/v2\/posts\/968\/revisions"}],"predecessor-version":[{"id":976,"href":"https:\/\/www.embedded-communication.com\/en\/wp-json\/wp\/v2\/posts\/968\/revisions\/976"}],"wp:attachment":[{"href":"https:\/\/www.embedded-communication.com\/en\/wp-json\/wp\/v2\/media?parent=968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.embedded-communication.com\/en\/wp-json\/wp\/v2\/categories?post=968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.embedded-communication.com\/en\/wp-json\/wp\/v2\/tags?post=968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}