WebServices written in C # to run on Raspberry Pi Raspkate - based lightweight Web .NET can run on a server Raspberry Pi

Raspkate - based lightweight Web .NET can run on a server Raspberry Pi

 

Raspberry Pi recent play in their spare time, the beginning of time to write some of the Raspberry Pi control program based on the C language library wiringPi Raspberry Pi GPIO pins in order to control flashing LED light-emitting diodes, but then it was you can not use HTML5 + jQuery and other popular front-end technology to make a simple Web site, so the site equipped with raspberry pie, raspberry pie is controlled through a mobile phone or tablet. After some research, consider the following:

  • Using Apache or Nginx set up a Web server
    • Pros: relatively mature server implementation
    • Disadvantages: too much weight, but also need to interact with the Raspberry Pi relates Service calls to achieve certain learning RESTful service costs for Apache or Nginx require additional components to support, to pay
  • Use Python + Flesk
    • Advantages: technology is relatively mature, there are many cases of successful application
    • Cons: For me, the need for additional language learning Python

Based on this premise, given the current Mono outstanding performance under Linux, or think about their own use .NET to develop a lightweight Web server, right, the first version of Let It supports both static files to access the service, and also supports RESTful API calls, in RESTful API can be exposed raspberry Pi GPIO pin interface to access for HTML5 + jQuery single page application call, and then also to achieve the purpose and on any device to control raspberry Pi through a browser.

Raspkate project

Name Raspkate project, I think it is two aspects: Rasp, its English name comes from the Raspberry Pi Raspberry Pi, indicating meaning "small, lightweight", while Kate is able to give full play to the moral imagination of developers, for its extended and customized. Raspkate is such a basis of .NET Framework, the C # development of lightweight Web server, it is a core part of HttpListener object model through multi-threaded asynchronous HTTP requests delegated to HttpListener execution. When a HTTP request is accepted, Raspkate based format or the type of request, assign specific module (Module) to process the request, and then returns the processing result. If the request is a file, then Raspkate will return the contents of the file, if it is a RESTful request, Raspkate uses already registered RESTful API controller to handle the request and returns the results in JSON format.

Open source address

Raspkate project is open source code base address is: https://github.com/daxnet/raspkate , the license agreement used is GPL2.0, (because the components associated with the raspberry pie they use a third-party GPL2.0 authorized by the open source library), and therefore, can not use this item in a commercial environment.

Scenarios

You can use Raspkate in the following scenarios:

  1. Implement the built-in Web server (Self-Hosting) in your own applications
  2. The Raspkate hosted in Windows Service provided RESTful service outside
  3. The Raspkate hosted in Windows Service, the outward providing static Web pages requested service
  4. The Raspkate as Raspberry Pi is a small independent Web server, providing control Raspberry Pi GPIO Web user interface

Overall structure

The figure below shows the overall architecture design Raspkate project:

ArchitectureOverview

可以看到,在Raspkate核心部分就是一个HTTP Listener组件,并且由Module和Configuration支撑Raspkate完成HTTP请求处理。每个模块可以包含多个HTTP Handler,这在定义模块的时候可以对多个HTTP Handler进行注册。

Raspkate配置信息(也就是上图中的Configuration)非常简单,下图就是这个配置信息的类型视图,在RaspkateConfiguration对象上,设定了HttpListener所使用的Prefix,而Modules部分则指定了Raspkate扫描可用模块的路径,IsRelative指定模块路径是否是相对的。

image_thumb8

以下是一个标准的Raspkate配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
<? xml  version="1.0" encoding="utf-8" ?>
< configuration >
   < configSections >
     < section  name="raspkateConfiguration" type="Raspkate.Config.RaspkateConfiguration, Raspkate"/>
   </ configSections >
 
   < raspkateConfiguration  xmlns="urn:Raspkate.Config" prefix="http://127.0.0.1:9023/">
     < modules >
       < add  path="modules"/>
     </ modules >
   </ raspkateConfiguration >
 
</ configuration >

代码使用

首先,使用Git客户端将代码库克隆到本地:

然后,在装有MSBuild的Windows机器上,执行build.bat命令:

  • build.bat All - 这将编译所有模块,并将模块的二进制文件复制到modules目录下
  • build.bat Minimal - 这仅编译核心模块(不包含类似树莓派支持等扩展模块)

如果是在装有Mono的Linux机器上,则相应地执行build.sh即可。

在编译完成之后,进入bin目录,执行RaspkateService.exe即可(注意:如果是在树莓派中执行,并且希望加载树莓派支持模块,则需要通过 sudo ./RaspkateService.exe 命令执行,以获得root权限)。在成功启动后,你应该看到类似下面的画面:

image_thumb1

这时,打开浏览器,在浏览器中输入http://127.0.0.1:9023,您应该可以看到类似以下的画面:

image_thumb5

这表示您已经成功运行Raspkate服务,此页面显示了运行服务器相关的信息。注意:如果你希望你的Raspkate服务能够在同网络的其它机器访问,请在执行RaspkateService.exe之前,将RaspkateService.exe.config文件中的Prefix设置修改为:http://+:9023/。但这样做可能会牵涉到用户访问控制的问题,如果将Prefix改为该值后,出现Raspkate无法启动的问题,请先在Command Line执行:

1
netsh http add urlacl url="http://+:9023/" user=everyone

演示

这里有几张屏幕截图,展示了Raspkate提供的相关功能。

获取并显示树莓派信息

以下屏幕截取自我的手机浏览器,分别显示了我的树莓派系统信息、树莓派主板信息以及树莓派中所有引脚的名称、类型和电平值列表。

Screenshot_2016-03-20-20-23-10_com.a[1]    Screenshot_2016-03-20-20-23-57_com.a    Screenshot_2016-03-20-20-24-22_com.a[2]

使用HTML5+jQuery控制发光二极管的点亮和熄灭状态

前端是一个寄宿在Raspkate上的一个HTML5+jQuery的单页面应用,后端是运行Raspkate的树莓派。

image_thumb9    image_thumb10

 
 

出处:https://www.cnblogs.com/Leo_wl/p/5319481.html

Raspkate - 基于.NET的可运行于树莓派的轻量型Web服务器

 

最近在业余时间玩玩树莓派,刚开始的时候在树莓派里写一些基于wiringPi库的C语言程序来控制树莓派的GPIO引脚,从而控制LED发光二极管的闪烁,后来觉得,是不是可以使用HTML5+jQuery等流行的前端技术做一个简单的Web站点,让树莓派搭载这个站点,通过手机或者平板电脑来控制树莓派。经过一番调研,考虑如下:

  • 使用Apache或者Nginx搭建一个Web服务器
    • 优点:服务器实现相对成熟
    • 缺点:偏重,而且与树莓派的交互需要涉及Service调用,而实现RESTful服务对于Apache或者Nginx需要额外的组件支持,要付出一定的学习成本
  • 使用Python+Flesk
    • 优点:技术相对成熟,也有很多成功应用案例
    • 缺点:对于我来说需要额外学习Python语言

基于这样的前提,鉴于目前Mono在Linux下的出色表现,想想还是自己使用.NET开发一个轻量型的Web服务器吧,第一期版本让它既支持静态文件的访问服务,也支持RESTful API的调用,在RESTful API中可以暴露访问树莓派GPIO引脚的接口,供HTML5+jQuery的单页面应用调用,于是也就实现了在任意设备上通过浏览器来控制树莓派的目的。

Raspkate项目

Raspkate项目的名字,我想就是两个方面:Rasp,它来源于树莓派的英文名字Raspberry Pi,有表示“小、轻量”的意义,而Kate则寓意能够充分发挥开发者的想象,对其进行扩展和定制。Raspkate就是这样一款基于.NET Framework、由C#开发的轻量型Web服务器,它的核心部分是一个HttpListener对象,通过多线程模型将HTTP请求异步委托给HttpListener执行。当一个HTTP请求被接受的时候,Raspkate会根据请求的格式或类型,分配特定的模块(Module)对请求进行处理,然后将处理结果返回。如果请求的是一个文件,那么Raspkate会返回文件的内容,如果是一个RESTful请求,则Raspkate会使用已经注册的RESTful API控制器来处理这个请求,并将计算结果以JSON格式返回。

开源地址

Raspkate项目是开源的,代码库地址是:https://github.com/daxnet/raspkate,所使用的许可协议是GPL2.0,(因为其所使用的与树莓派相关的组件是第三方的由GPL2.0授权的开源库),因此,不能在商业环境中使用此项目。

应用场景

您可以在以下场景中使用Raspkate:

  1. 在自己的应用程序中实现内建的Web服务器(Self-Hosting)
  2. 将Raspkate寄宿在Windows Service中,向外提供RESTful服务
  3. 将Raspkate寄宿在Windows Service中,向外提供静态Web页面请求服务
  4. 将Raspkate作为树莓派中的一个独立的小型Web服务器,提供控制树莓派GPIO的Web用户界面

整体架构

下图展示了Raspkate项目的整体架构设计:

ArchitectureOverview

可以看到,在Raspkate核心部分就是一个HTTP Listener组件,并且由Module和Configuration支撑Raspkate完成HTTP请求处理。每个模块可以包含多个HTTP Handler,这在定义模块的时候可以对多个HTTP Handler进行注册。

Raspkate配置信息(也就是上图中的Configuration)非常简单,下图就是这个配置信息的类型视图,在RaspkateConfiguration对象上,设定了HttpListener所使用的Prefix,而Modules部分则指定了Raspkate扫描可用模块的路径,IsRelative指定模块路径是否是相对的。

image_thumb8

以下是一个标准的Raspkate配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
<? xml  version="1.0" encoding="utf-8" ?>
< configuration >
   < configSections >
     < section  name="raspkateConfiguration" type="Raspkate.Config.RaspkateConfiguration, Raspkate"/>
   </ configSections >
 
   < raspkateConfiguration  xmlns="urn:Raspkate.Config" prefix="http://127.0.0.1:9023/">
     < modules >
       < add  path="modules"/>
     </ modules >
   </ raspkateConfiguration >
 
</ configuration >

代码使用

首先,使用Git客户端将代码库克隆到本地:

然后,在装有MSBuild的Windows机器上,执行build.bat命令:

  • build.bat All - 这将编译所有模块,并将模块的二进制文件复制到modules目录下
  • build.bat Minimal - 这仅编译核心模块(不包含类似树莓派支持等扩展模块)

如果是在装有Mono的Linux机器上,则相应地执行build.sh即可。

在编译完成之后,进入bin目录,执行RaspkateService.exe即可(注意:如果是在树莓派中执行,并且希望加载树莓派支持模块,则需要通过 sudo ./RaspkateService.exe 命令执行,以获得root权限)。在成功启动后,你应该看到类似下面的画面:

image_thumb1

这时,打开浏览器,在浏览器中输入http://127.0.0.1:9023,您应该可以看到类似以下的画面:

image_thumb5

这表示您已经成功运行Raspkate服务,此页面显示了运行服务器相关的信息。注意:如果你希望你的Raspkate服务能够在同网络的其它机器访问,请在执行RaspkateService.exe之前,将RaspkateService.exe.config文件中的Prefix设置修改为:http://+:9023/。但这样做可能会牵涉到用户访问控制的问题,如果将Prefix改为该值后,出现Raspkate无法启动的问题,请先在Command Line执行:

1
netsh http add urlacl url="http://+:9023/" user=everyone

演示

这里有几张屏幕截图,展示了Raspkate提供的相关功能。

获取并显示树莓派信息

以下屏幕截取自我的手机浏览器,分别显示了我的树莓派系统信息、树莓派主板信息以及树莓派中所有引脚的名称、类型和电平值列表。

Screenshot_2016-03-20-20-23-10_com.a[1]    Screenshot_2016-03-20-20-23-57_com.a    Screenshot_2016-03-20-20-24-22_com.a[2]

使用HTML5+jQuery控制发光二极管的点亮和熄灭状态

前端是一个寄宿在Raspkate上的一个HTML5+jQuery的单页面应用,后端是运行Raspkate的树莓派。

image_thumb9    image_thumb10

 
 

最近在业余时间玩玩树莓派,刚开始的时候在树莓派里写一些基于wiringPi库的C语言程序来控制树莓派的GPIO引脚,从而控制LED发光二极管的闪烁,后来觉得,是不是可以使用HTML5+jQuery等流行的前端技术做一个简单的Web站点,让树莓派搭载这个站点,通过手机或者平板电脑来控制树莓派。经过一番调研,考虑如下:

  • 使用Apache或者Nginx搭建一个Web服务器
    • 优点:服务器实现相对成熟
    • 缺点:偏重,而且与树莓派的交互需要涉及Service调用,而实现RESTful服务对于Apache或者Nginx需要额外的组件支持,要付出一定的学习成本
  • 使用Python+Flesk
    • 优点:技术相对成熟,也有很多成功应用案例
    • 缺点:对于我来说需要额外学习Python语言

基于这样的前提,鉴于目前Mono在Linux下的出色表现,想想还是自己使用.NET开发一个轻量型的Web服务器吧,第一期版本让它既支持静态文件的访问服务,也支持RESTful API的调用,在RESTful API中可以暴露访问树莓派GPIO引脚的接口,供HTML5+jQuery的单页面应用调用,于是也就实现了在任意设备上通过浏览器来控制树莓派的目的。

Raspkate项目

Raspkate项目的名字,我想就是两个方面:Rasp,它来源于树莓派的英文名字Raspberry Pi,有表示“小、轻量”的意义,而Kate则寓意能够充分发挥开发者的想象,对其进行扩展和定制。Raspkate就是这样一款基于.NET Framework、由C#开发的轻量型Web服务器,它的核心部分是一个HttpListener对象,通过多线程模型将HTTP请求异步委托给HttpListener执行。当一个HTTP请求被接受的时候,Raspkate会根据请求的格式或类型,分配特定的模块(Module)对请求进行处理,然后将处理结果返回。如果请求的是一个文件,那么Raspkate会返回文件的内容,如果是一个RESTful请求,则Raspkate会使用已经注册的RESTful API控制器来处理这个请求,并将计算结果以JSON格式返回。

开源地址

Raspkate项目是开源的,代码库地址是:https://github.com/daxnet/raspkate,所使用的许可协议是GPL2.0,(因为其所使用的与树莓派相关的组件是第三方的由GPL2.0授权的开源库),因此,不能在商业环境中使用此项目。

应用场景

您可以在以下场景中使用Raspkate:

  1. 在自己的应用程序中实现内建的Web服务器(Self-Hosting)
  2. 将Raspkate寄宿在Windows Service中,向外提供RESTful服务
  3. 将Raspkate寄宿在Windows Service中,向外提供静态Web页面请求服务
  4. 将Raspkate作为树莓派中的一个独立的小型Web服务器,提供控制树莓派GPIO的Web用户界面

整体架构

下图展示了Raspkate项目的整体架构设计:

ArchitectureOverview

可以看到,在Raspkate核心部分就是一个HTTP Listener组件,并且由Module和Configuration支撑Raspkate完成HTTP请求处理。每个模块可以包含多个HTTP Handler,这在定义模块的时候可以对多个HTTP Handler进行注册。

Raspkate配置信息(也就是上图中的Configuration)非常简单,下图就是这个配置信息的类型视图,在RaspkateConfiguration对象上,设定了HttpListener所使用的Prefix,而Modules部分则指定了Raspkate扫描可用模块的路径,IsRelative指定模块路径是否是相对的。

image_thumb8

以下是一个标准的Raspkate配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
<? xml  version="1.0" encoding="utf-8" ?>
< configuration >
   < configSections >
     < section  name="raspkateConfiguration" type="Raspkate.Config.RaspkateConfiguration, Raspkate"/>
   </ configSections >
 
   < raspkateConfiguration  xmlns="urn:Raspkate.Config" prefix="http://127.0.0.1:9023/">
     < modules >
       < add  path="modules"/>
     </ modules >
   </ raspkateConfiguration >
 
</ configuration >

代码使用

首先,使用Git客户端将代码库克隆到本地:

然后,在装有MSBuild的Windows机器上,执行build.bat命令:

  • build.bat All - 这将编译所有模块,并将模块的二进制文件复制到modules目录下
  • build.bat Minimal - 这仅编译核心模块(不包含类似树莓派支持等扩展模块)

如果是在装有Mono的Linux机器上,则相应地执行build.sh即可。

After compilation is complete, go to the bin directory, you can execute RaspkateService.exe ( Note: If you are performing in raspberry pie, and want to load module support Raspberry Pi, you need to execute commands through sudo ./RaspkateService.exe to get root privileges ). After a successful start, you should see something like the following picture:

image_thumb1

Then, open the browser, enter in your browser http://127.0.0.1:9023 , you should see a screen similar to the following:

image_thumb5

This means you have successfully run Raspkate service, this page shows the information related to the operation of the server. Note: If you want your Raspkate services to other machines in the same network access, before performing RaspkateService.exe, will set the Prefix modify RaspkateService.exe.config file is: HTTP: // +: 9023 / . But doing so may involve problems of user access control, if the value of the Prefix instead, there is a problem Raspkate not start, please run in Command Line:

1
netsh http add urlacl url="http://+:9023/" user=everyone

Show

Here are a few screen shots, showing the correlation function Raspkate provided.

Obtain and display information about Raspberry Pi

The following screen capture self mobile browser, show my Raspberry Pi system information, mainboard information, and raspberry pie raspberry pie all pins name, type and power level value list.

Screenshot_2016-03-20-20-23-10_com.a[1]    Screenshot_2016-03-20-20-23-57_com.a    Screenshot_2016-03-20-20-24-22_com.a[2]

Use HTML5 + jQuery control turning on and off the light emitting diode status

A front-end is hosted on Raspkate a HTML5 + jQuery single page application, and the backend is running Raspkate raspberry pie.

image_thumb9    image_thumb10

Guess you like

Origin www.cnblogs.com/mq0036/p/11140947.html