65.9K
CodeProject 正在变化。 阅读更多。
Home

IoT 实战 - SignalR

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (3投票s)

2016年3月13日

CPOL

2分钟阅读

viewsIcon

28150

downloadIcon

2

我正在使用 Arduino 板作为物联网设备,演示如何使用 SignalR 通过互联网将传感器数据中继到多个客户端。

引言

我们正在模拟一个案例研究,将“环境光馈送”分发给各个客户端。客户端可能会在许多决策制定控制系统中使用此馈送,例如根据从传感器接收到的光馈送来设置环境光强度。

Using the Code

我假设阅读本文的人对使用 Arduino、SignalR 和基本编程技能有一定的了解。

该项目有三个部分

  1. Arduino UNO 代码片段(在 Arduino 板内运行的代码,用于读取传感器数据并将数据推送到端口)
  2. C# 代码,用于从端口读取数据
  3. SignalR 实现,用于将数据馈送发送到连接的客户端

您可以将实现分离,以便可以拥有此解决方案的更分布式架构。例如,您可以在单独的服务中拥有端口读取器,通过 SignalR 发送数据。SignalR 服务器将向客户端发送通知。但在这种情况下,我将所有内容都放在一个解决方案中,以专注于感兴趣的点。

1. 让我们看一下 Arduino 代码片段(完整源代码)

                                       /* LDR -> light-dependent resistor*/
int ldr_read      = 0;                 /* variable to read from LDR sensor*/
const int DELAY   = 1000;              /* constant delay in feed frequency*/
const int LDR_PIN = 3;                 /* LDR pin reference */
const int BAUD96H = 9600;

void setup() {
  Serial.begin(BAUD96H);               /* setting the port baud rate*/
}

void loop() {                          /* starting main function - runs forever*/
  ldr_read = analogRead(LDR_PIN);      /* reading LDR data*/
  Serial.println(ldr_read);            /* writing LDR data to port*/
  delay(DELAY);                        /* one second wait*/
}

//

在上面的代码片段中,我假设 LDR 插入到引脚 3。我还在处理过程中延迟了 1000 毫秒。

2. 在 LightFeedReader 类中读取端口数据。让我们看看这个类的有趣部分。

Public 属性仅在属性更改时引发事件。只是为了减少开销,它可能对系统的其余部分产生影响。

public int FeedValue 
{ 
       .....

       .....

    set
    {
       if(value != _feedValue)
       {
           _feedValue = value;
           if (OnLightFeedChanged != null) OnLightFeedChanged(this, new LightFeedEventArgs(_feedValue));
       }
    }
}

并且端口在单独的线程中读取

string message = _serialPort.ReadLine();
FeedValue = Convert.ToInt16(message);

3. SignalR 实现

   <script>
        $(function ()
        {
            var hub = $.connection.LightFeed,
                $LightFeedValue = $("#LightFeedValue");
                hub.client.LightFeedChanged = function (lightFeedValue)
                {
                    $LightFeedValue.text(lightFeedValue);
                };
                $.connection.hub.start().done(function () {  });
        });
    </script>

客户端 hub 连接到服务器,并且从 SignalR 服务器调用 function(lightFeedValue) 来获取更新的值。<h1 id="LightFeedValue">0<h1> <h1>.Text 在函数中更新。

为了方便单一实现,我还编写了一个名为“LightFeedStitching”的类。这让我的生活更轻松,可以关联和控制端口读取线程,并使用事件模型调用 SignalR 通知。在分布式设计的情况下,它应该是一个单独的单元,但无论如何都需要进行拼接。

LightFeedStitching objectStitching;
protected void Application_Start(object sender, EventArgs e)
{
    // get the hub from the globalHost 
    IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<LightFeedHub>();     

    // send the message to all clients, make sure that the method is camelCase.
    objectStitching = new LightFeedStitching(hubContext);
    objectStitching.StartTheSystem();
}

关注点

整个世界都对物联网充满热情,我意识到它实现起来是多么容易和有趣吗?而且,最新技术使其成为可能。可以通过多种方式实现,比这更好,比这更有效。需要思考。

继续努力,敬请期待,我的朋友们——物联网和大数据正在迅速发展。

历史

  • 第一版
© . All rights reserved.