{"id":9698,"date":"2022-11-06T14:03:37","date_gmt":"2022-11-06T14:03:37","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9698"},"modified":"2022-11-06T14:03:37","modified_gmt":"2022-11-06T14:03:37","slug":"wifi-using-nanoframework-on-the-m5core2","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/wifi-using-nanoframework-on-the-m5core2\/","title":{"rendered":"Wifi using nanoFramework on the M5Core2"},"content":{"rendered":"<p>When we&#8217;re creating an IoT device, the likelihood is we&#8217;ll want to connect to Wifi and the internet (well it&#8217;s sort of in the IoT name isn&#8217;t it).<\/p>\n<p><em>Please note, this post is heavily based upon the nanoFramework samples, so do go and view them for a whole host of excellent sample code.<\/em><\/p>\n<p>The nanoFramework comes with a NuGet package named <em>nanoFramework.System.Device.Wifi<\/em> which you&#8217;ll need to add to your project. <\/p>\n<p>Now we can use the <em>WifiNetworkHelper<\/em> class to connect to your DHCP server (your router generally) using the <em>ConnectDhcp<\/em> method. Now this will also save the configuration of the network ssid and password. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nconst string ssid = &quot;YOUR_SSID&quot;;\r\nconst string password = &quot;YOUR_WIFI_PASSWORD&quot;;\r\n\r\nvar cs = new CancellationTokenSource(60000);\r\nvar success = WifiNetworkHelper.ConnectDhcp(ssid, password, requiresDateTime: true, token: cs.Token);\r\nif (!success)\r\n{\r\n    Debug.WriteLine($&quot;Cannot connect to the WiFi, error: {WifiNetworkHelper.Status}&quot;);\r\n    if (WifiNetworkHelper.HelperException != null)\r\n    {\r\n        Debug.WriteLine($&quot;ex: {WifiNetworkHelper.HelperException}&quot;);\r\n    }\r\n}\r\nelse\r\n{\r\n   Debug.WriteLine(&quot;Connected successfully&quot;);\r\n}\r\n<\/pre>\n<p>As mentioned, the <em>ConnectDhcp<\/em> method saves our configuration, so once that&#8217;s happened, we can switch to using the <em>Reconnect<\/em> method instead, i.e.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar cs = new CancellationTokenSource(60000);\r\nvar success = WifiNetworkHelper.Reconnect(requiresDateTime: true, token: cs.Token);\r\nif (!success)\r\n{\r\n    Debug.WriteLine($&quot;Cannot connect to the WiFi, error: {WifiNetworkHelper.Status}&quot;);\r\n    if (WifiNetworkHelper.HelperException != null)\r\n    {\r\n        Debug.WriteLine($&quot;ex: {WifiNetworkHelper.HelperException}&quot;);\r\n    }\r\n}\r\nelse\r\n{\r\n   Debug.WriteLine(&quot;Connected successfully&quot;);\r\n}\r\n<\/pre>\n<p>The ssid and password are stored using the <em>Wireless80211Configuration<\/em> on the device. So we can check if our configuration has been stored &#8211; this ofcourse also means we can get access to the configuration &#8211; beware if security is an issue that these are then available as plain text<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar configuration = Wireless80211Configuration.GetAllWireless80211Configurations();\r\nforeach (var config in configuration)\r\n{\r\n   Debug.WriteLine($&quot;SSID: {config.Ssid}, Password: {config.Password}&quot;);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When we&#8217;re creating an IoT device, the likelihood is we&#8217;ll want to connect to Wifi and the internet (well it&#8217;s sort of in the IoT name isn&#8217;t it). Please note, this post is heavily based upon the nanoFramework samples, so do go and view them for a whole host of excellent sample code. The nanoFramework [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[89,345,341],"tags":[],"class_list":["post-9698","post","type-post","status-publish","format-standard","hentry","category-electronics","category-m5core2","category-nanoframework"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9698"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9698\/revisions"}],"predecessor-version":[{"id":9703,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9698\/revisions\/9703"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}