{"id":152,"date":"2019-01-28T20:52:45","date_gmt":"2019-01-28T11:22:45","guid":{"rendered":"https:\/\/sighmon.com\/says\/?p=152"},"modified":"2019-01-28T20:53:35","modified_gmt":"2019-01-28T11:23:35","slug":"debugging-lora-connectivity","status":"publish","type":"post","link":"https:\/\/sighmon.com\/says\/debugging-lora-connectivity\/","title":{"rendered":"Debugging LoRa connectivity"},"content":{"rendered":"\n<p>After successfully <a href=\"https:\/\/sighmon.com\/says\/upgrading-the-things-uno-rn2903-firmware\/\">upgrading the firmware of my Uno&#8217;s RN2903 LoRa chip<\/a>, I was still having trouble connecting to The Things Network and seeing a lot of these lines in the serial monitor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sending: mac join otaa \nJoin not accepted: denied\nCheck your coverage, keys and backend status.<\/code><\/pre>\n\n\n\n<p>I&#8217;d tried five different locations across two states, so was getting pretty paranoid that it was either something I&#8217;d done in my Arduino water quality sensor reading code (I was using Software Serial).<\/p>\n\n\n\n<p>So I asked Leo from TTN Adelaide if it might be possible for him to send me a known working Uno so that I could try it here using stock software. His setup was identical to the one I had been trying, but at least we could rule out hardware failure, and it&#8217;d tell me a lot more about trusting my code.<\/p>\n\n\n\n<p>As soon as it arrived I plugged it into my USB power brick so that my laptop couldn&#8217;t do anything weird to it (super paranoid), but it didn&#8217;t connect successfully. So after a couple of minutes I plugged it into my laptop so I could read the serial monitor output. Same output as mine.<\/p>\n\n\n\n<p>So at this point I could at least feel a little better about my code and hardware, but it was still a bit of a mystery why all of the locations I&#8217;d tried didn&#8217;t work.<\/p>\n\n\n\n<p>I wandered around the Melbourne CBD with it switched on, monitoring <a href=\"https:\/\/console.thethingsnetwork.org\">TTN console<\/a> on my phone for uploads&#8230; still nothing.<\/p>\n\n\n\n<p>In a last ditch effort to find some signal, I got myself onto a 38th floor rooftop, switched it on, and almost instantly the join confirmation and payloads started streaming in.<\/p>\n\n\n\n<p>So it was just blackspots all over the Melbourne CBD, East St Kilda, Southbank, Adelaide CBD and Port Willunga.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Test the payload<\/h3>\n\n\n\n<p>Next thing to do was to test the payload to see if I was sending a readable byte array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- SENSOR READING\nEC:78.23\nTDS:42\nSAL:0.00\nGRAV:1.000\n\n\n-- BYTE ARRAY TO SEND\n37 38 2E 32 33 2C 34 32 2C 30 2E 30 30 2C 31 2E 30 30 30 00 \n\nSending: mac tx uncnf 1 37382E32332C34322C302E30302C312E30303000<\/code><\/pre>\n\n\n\n<p>Over on the Things Network console I could see that exact byte array appear, which converts to an ASCII string: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>78.23,42,0.00,1.000<\/code><\/pre>\n\n\n\n<p>So that&#8217;s great! It works.<\/p>\n\n\n\n<p>The Things Network console also has a really nice feature where you can decode your byte array into something more human readable, it&#8217;s the Payload Formats tab.<\/p>\n\n\n\n<p>There&#8217;s some sample code to extend, so here&#8217;s what I ended up with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Decoder(bytes, port) {\n  \/\/ Decode an uplink message from a buffer\n  \/\/ (array) of bytes to an object of fields.\n  var decoded = {};\n\n  if (port === 1) {\n    var stringFromBytes = String.fromCharCode.apply(String, bytes);\n    var stringArray = stringFromBytes.split(',');\n    decoded.electrical_conductivity = parseFloat(stringArray[0]);\n    decoded.total_dissolved_solids = parseFloat(stringArray[1]);\n    decoded.salinity = parseFloat(stringArray[2]);\n    decoded.specific_gravity = parseFloat(stringArray[3]);\n  }\n\n  return decoded;\n}<\/code><\/pre>\n\n\n\n<p>It&#8217;s very simple, with no error handling or validation (though there is a validation tab when I get some more time).<\/p>\n\n\n\n<p>There&#8217;s also a neat Payload Test field, where you can test your decoder as you write it. After I&#8217;d ironed out the bugs I switched back over to the Data tab and saw it all working!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"809\" src=\"https:\/\/sighmon.com\/says\/wp-content\/uploads\/2019\/01\/sensor_payload_decoded-1024x809.png\" alt=\"\" class=\"wp-image-153\" srcset=\"https:\/\/sighmon.com\/says\/wp-content\/uploads\/2019\/01\/sensor_payload_decoded-1024x809.png 1024w, https:\/\/sighmon.com\/says\/wp-content\/uploads\/2019\/01\/sensor_payload_decoded-300x237.png 300w, https:\/\/sighmon.com\/says\/wp-content\/uploads\/2019\/01\/sensor_payload_decoded-768x606.png 768w, https:\/\/sighmon.com\/says\/wp-content\/uploads\/2019\/01\/sensor_payload_decoded-1568x1238.png 1568w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Success! The water quality sensor payload is decoded and displayed.<\/figcaption><\/figure>\n\n\n\n<p>Next step: writing some kind of integrator, so that it can send the data onto a database for visualisation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After successfully upgrading the firmware of my Uno&#8217;s RN2903 LoRa chip, I was still having trouble connecting to The Things Network and seeing a lot of these lines in the serial monitor: I&#8217;d tried five different locations across two states, so was getting pretty paranoid that it was either something I&#8217;d done in my Arduino &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/sighmon.com\/says\/debugging-lora-connectivity\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Debugging LoRa connectivity&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,9],"tags":[11,30,29,25,18],"class_list":["post-152","post","type-post","status-publish","format-standard","hentry","category-arduino","category-sensors","tag-arduino","tag-debugging","tag-lora","tag-the-things-uno","tag-ttn","entry"],"_links":{"self":[{"href":"https:\/\/sighmon.com\/says\/wp-json\/wp\/v2\/posts\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sighmon.com\/says\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sighmon.com\/says\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sighmon.com\/says\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sighmon.com\/says\/wp-json\/wp\/v2\/comments?post=152"}],"version-history":[{"count":1,"href":"https:\/\/sighmon.com\/says\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"predecessor-version":[{"id":154,"href":"https:\/\/sighmon.com\/says\/wp-json\/wp\/v2\/posts\/152\/revisions\/154"}],"wp:attachment":[{"href":"https:\/\/sighmon.com\/says\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sighmon.com\/says\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sighmon.com\/says\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}