{"id":9716,"date":"2022-11-07T23:23:47","date_gmt":"2022-11-07T23:23:47","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9716"},"modified":"2022-11-07T23:23:47","modified_gmt":"2022-11-07T23:23:47","slug":"turning-your-m5core2-into-a-nanoframework-based-web-server","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/turning-your-m5core2-into-a-nanoframework-based-web-server\/","title":{"rendered":"Turning your M5Core2 into a nanoFramework based web server"},"content":{"rendered":"<p>Like most of my posts regarding nanoFramework and the M5Core2, I owe a debt to those who created this stuff and I&#8217;m really just going through some of the samples etc. Trying them out and documenting my findings. This post is no different, it&#8217;s based on the <a href=\"https:\/\/github.com\/nanoframework\/nanoFramework.WebServer\" rel=\"noopener\" target=\"_blank\">Welcome to the .NET nanoFramework WebServer repository<\/a><\/p>\n<p>Add the NuGet package <em>nanoFramework.WebServer<\/em> to your nanoFramework project.<\/p>\n<p>You&#8217;ll need to also include code to connect to your WiFi, so checkout my post on that subject &#8211; <a href=\"http:\/\/putridparrot.com\/blog\/wifi-using-nanoframework-on-the-m5core2\/\" rel=\"noopener\" target=\"_blank\">Wifi using nanoFramework on the M5Core2<\/a>.<\/p>\n<p>Assuming you&#8217;ve connected to your WiFi, we can set up a <em>WebServer<\/em> like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing var server = new WebServer(80, HttpProtocol.Http, new&#x5B;] { typeof(PowerController) });\r\nserver.Start();\r\n\r\nThread.Sleep(Timeout.Infinite);\r\n<\/pre>\n<p>The first line supplies an array of controllers, so you can have multiple controllers for your different endpoints. In this case we&#8217;ve just got the single controller <em>PowerController<\/em>. This is a simple class that includes <em>RouteAtrribute<\/em> and <em>MethodAttribute<\/em> adorned methods to acts as routes\/endpoints.<\/p>\n<p>Let&#8217;s look at the PowerController, which just returns some <em>M5Core2.Power<\/em> values when accessed via <em>http:\/\/m5core2_ip_address\/power<\/em>. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class PowerController\r\n{\r\n   &#x5B;Route(&quot;power&quot;)]\r\n   &#x5B;Method(&quot;GET&quot;)]\r\n   public void PowerRoute(WebServerEventArgs e)\r\n   {\r\n      var power = M5Core2.Power;\r\n           \r\n      var sb = new StringBuilder();\r\n      sb.AppendLine(&quot;Power:&quot;);\r\n      sb.AppendLine($&quot;  Adc Frequency: {power.AdcFrequency}&quot;);\r\n      sb.AppendLine($&quot;  Adc Pin Current: {power.AdcPinCurrent}&quot;);\r\n      sb.AppendLine($&quot;  Adc Pin Current Setting: {power.AdcPinCurrentSetting}&quot;);\r\n      sb.AppendLine($&quot;  Adc Pin Enabled: {power.AdcPinEnabled}&quot;);\r\n      sb.AppendLine($&quot;  Batt. Temp. Monitor: {power.BatteryTemperatureMonitoring}&quot;);\r\n      sb.AppendLine($&quot;  Charging Current: {power.ChargingCurrent}&quot;);\r\n      sb.AppendLine($&quot;  Charging Stop Threshold: {power.ChargingStopThreshold}&quot;);\r\n      sb.AppendLine($&quot;  Charging Voltage: {power.ChargingVoltage}&quot;);\r\n      sb.AppendLine($&quot;  Dc Dc1 Voltage: {power.DcDc1Voltage.Millivolts} mV&quot;);\r\n      sb.AppendLine($&quot;  Dc Dc2 Voltage: {power.DcDc2Voltage.Millivolts} mV&quot;);\r\n      sb.AppendLine($&quot;  Dc Dc3 Voltage: {power.DcDc3Voltage.Millivolts} mV&quot;);\r\n      sb.AppendLine($&quot;  EXTEN Enable: {power.EXTENEnable}&quot;);\r\n      sb.AppendLine($&quot;  VOff Voltage: {power.VoffVoltage}&quot;);\r\n      sb.AppendLine($&quot;  Gpio0 Behavior: {power.Gpio0Behavior}&quot;);\r\n      sb.AppendLine($&quot;  Gpio0 Value: {power.Gpio0Value}&quot;);\r\n\r\n      e.Context.Response.ContentType = &quot;text\/plain&quot;;\r\n      WebServer.OutPutStream(e.Context.Response, sb.ToString());\r\n}\r\n<\/pre>\n<p>As you can see from the last line of code, we send the response back with our payload, the string of power information.<\/p>\n<p>We can also return HTTP codes using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nWebServer.OutputHttpCode(e.Context.Response, HttpStatusCode.OK);\r\n<\/pre>\n<p>This is great, but what&#8217;s the IP address of our IoT device, so I can access the web server? <\/p>\n<p>Well, ofcourse you could check your router or DHCP server, but better still, let&#8217;s output the IP address to the M5Core2 screen using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nConsole.WriteLine(IPGlobalProperties.GetIPAddress().ToString());\r\n<\/pre>\n<p>We can support multiple routes per method, such as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ublic class PowerController\r\n{\r\n&#x5B;Route(&quot;power&quot;)]\r\n&#x5B;Route(&quot;iotpower&quot;)]\r\n&#x5B;Method(&quot;GET&quot;)]\r\npublic void PowerRoute(WebServerEventArgs e)\r\n{\r\n\/\/ code removed\r\n}\r\n<\/pre>\n<p><em>Note: Routes are usually case insensitive, unless you add the <em>CaseSensitiveAttribute<\/em> to your method.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like most of my posts regarding nanoFramework and the M5Core2, I owe a debt to those who created this stuff and I&#8217;m really just going through some of the samples etc. Trying them out and documenting my findings. This post is no different, it&#8217;s based on the Welcome to the .NET nanoFramework WebServer repository Add [&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-9716","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\/9716","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=9716"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9716\/revisions"}],"predecessor-version":[{"id":9719,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9716\/revisions\/9719"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}