{"id":2437,"date":"2016-12-07T19:49:26","date_gmt":"2016-12-08T03:49:26","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=2437"},"modified":"2016-12-07T19:49:26","modified_gmt":"2016-12-08T03:49:26","slug":"creating-a-basic-6lowpan-http-server-with-nordic-iot-sdk","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arm\/nrf51\/http\/","title":{"rendered":"Creating a basic 6LoWPAN HTTP server with Nordic IoT SDK"},"content":{"rendered":"<p>This tutorial shows how to create a basic HTTP server that will run via the 6LoWPAN (IPv6 over Bluetooth LE) protocol on the Nordic nRF52 device.<\/p>\n<p>Before you begin, follow our <a href=\"http:\/\/visualgdb.com\/tutorials\/arm\/nrf51\/6lowpan\">basic Nordic IoT tutorial<\/a> to connect your nRF52 device to your network and configure the necessary routing.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Embedded Project Wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/01-httpprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2438\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/01-httpprj.png\" alt=\"01-httpprj\" width=\"861\" height=\"512\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/01-httpprj.png 861w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/01-httpprj-300x178.png 300w\" sizes=\"(max-width: 861px) 100vw, 861px\" \/><\/a><\/li>\n<li>Proceed with the default settings on the first page:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/02-msbuild3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2439\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/02-msbuild3.png\" alt=\"02-msbuild\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/02-msbuild3.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/02-msbuild3-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Select the IoT version of your Nordic device so that VisualGDB can use the Nordic IoT SDK:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/03-iot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2440\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/03-iot.png\" alt=\"03-iot\" width=\"738\" height=\"710\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/03-iot.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/03-iot-300x289.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the next page select the IoT TCP Server sample:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/04-tcp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2441\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/04-tcp.png\" alt=\"04-tcp\" width=\"738\" height=\"710\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/04-tcp.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/04-tcp-300x289.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the last page select the debug method. We recommend debugging nRF52 devices with Segger J-Link:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/05-debug4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2442\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/05-debug4.png\" alt=\"05-debug\" width=\"738\" height=\"710\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/05-debug4.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/05-debug4-300x289.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate the project and ensure it builds successfully by pressing Ctrl-Shift-B:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/06-build1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2443\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/06-build1.png\" alt=\"06-build\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/06-build1.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/06-build1-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>The TCP server example will expect special binary packets to be sent to port 9000, so we will modify it to support very basic HTTP requests. First of all, locate the <strong>tcp_recv_data_handler()<\/strong> function and replace its contents with the following code:\n<pre class=\"\">\u00a0\u00a0\u00a0 if (err != ERR_OK)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 asm(\"bkpt 255\");\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 static char s_Request[2048];\r\n\u00a0\u00a0\u00a0 static int s_RequestPos;\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 int todo = MIN(sizeof(s_Request) - s_RequestPos - 1, p_buffer-&gt;len);\r\n\u00a0\u00a0\u00a0 memcpy(s_Request + s_RequestPos, p_buffer-&gt;payload, todo);\r\n\u00a0\u00a0\u00a0 s_RequestPos += todo;\r\n\u00a0\u00a0\u00a0 s_Request[s_RequestPos] = 0;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 if (strstr(s_Request, \"\\r\\n\\r\\n\"))\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 char reply[] = \"HTTP\/1.1 OK\\r\\n\\r\\n&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello, World&lt;\/h1&gt;&lt;p&gt;This page is generated by your nRF52 device&lt;\/p&gt;&lt;\/body&gt;&lt;\/html&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tcp_sent(p_pcb, tcp_write_complete);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 err = tcp_write(p_pcb, reply, sizeof(reply) - 1, 1);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (err != ERR_OK)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 asm(\"bkpt 255\");\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 tcp_recved(p_pcb, p_buffer-&gt;tot_len);\r\n\r\n\u00a0\u00a0\u00a0 return ERR_OK;<\/pre>\n<p>It will read the entire HTTP request and respond with a very basic fixed response:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/07-recv-handler.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2447\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/07-recv-handler.png\" alt=\"07-recv-handler\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/07-recv-handler.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/07-recv-handler-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>Now modify the <strong>tcp_write_complete()<\/strong> function to simply close the connection:\n<pre class=\"\">\u00a0\u00a0\u00a0 tcp_port_close(p_pcb);\r\n\u00a0\u00a0\u00a0 return ERR_OK;<\/pre>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/08-write.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2445\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/08-write.png\" alt=\"08-write\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/08-write.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/08-write-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>Press F5 to build and start debugging your program. Then go to your Raspberry Pi and reconnect to the nRF52 via Bluetooth (see the <a href=\"http:\/\/visualgdb.com\/tutorials\/arm\/nrf51\/6lowpan\">basic Nordic IoT tutorial<\/a> for the explanation on the rolte of Raspberry Pi):<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/09-restart.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2444\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/09-restart.png\" alt=\"09-restart\" width=\"936\" height=\"293\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/09-restart.png 936w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/09-restart-300x94.png 300w\" sizes=\"(max-width: 936px) 100vw, 936px\" \/><\/a><\/li>\n<li>Ensure that the board responds to pings:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/ping.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2449\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/ping.png\" alt=\"ping\" width=\"825\" height=\"273\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/ping.png 825w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/ping-300x99.png 300w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/a><\/li>\n<li>Now you can check the connection to the board by opening <span style=\"text-decoration: underline;\"><strong>http:\/\/[IPv6 address in square brackets]:9000\/<\/strong><\/span> in your browser (e.g. <span style=\"text-decoration: underline;\"><strong>http:\/\/[fd00:a::2f5:3fff:fe54:96b]:9000\/<\/strong><\/span>):<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/10-hello.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2446\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/10-hello.png\" alt=\"10-hello\" width=\"760\" height=\"253\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/10-hello.png 760w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/11\/10-hello-300x100.png 300w\" sizes=\"(max-width: 760px) 100vw, 760px\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create a basic HTTP server that will run via the 6LoWPAN (IPv6 over Bluetooth LE)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[96,56,95],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2437"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=2437"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2437\/revisions"}],"predecessor-version":[{"id":2450,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2437\/revisions\/2450"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=2437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=2437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=2437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}