Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT / Arduino

IoT in Action with SignalR

2.00/5 (3 votes)
13 Mar 2016CPOL2 min read 27.5K   2  
I am using Arduino board as IoT device, demonstrating how we can relay sensor data over the internet to many clients using signalR

Introduction

We are simulating a case study where the "Ambiance Light Feed" will be distributed to various clients. Clients may use this feed in many decision making control systems. For example setting ambiance light intensity based on the light feed received from sensor.

Using the Code

I am assuming that people reading this article have a brief idea about using Arduino, signalR and basic programming skills.

This project has three parts:

  1. Arduino UNO scrap (the piece of code runs inside the aduino board to read sensor data and push data to port)
  2. C# code to read the data from port
  3. SignalR implementation to send data feed to connected clients

You can separate the implementation to be able to have more distributed architecture of this solution. For example, you can have the port reader in a separate service sending data across to signalR. signalR server will send the notification to the clients. But in this case, I just have everything in one solution to focus on the point of interest.

1. Let's Have a Look at Arduino Scrap (Complete Source)

C++
                                       /* 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*/
}

//

In the above scrap, I am assuming that the LDR is plugged at PIN number 3. I am also delaying the process by 1000 milliseconds.

2. Reading Port Data in LightFeedReader Class. Let us See the Interesting Part of this Class.

Public property raises an event only if the property changes. Just to reduce the overhead, it might have on rest of the system.

C#
public int FeedValue 
{ 
       .....

       .....

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

And the port is read in a separate thread:

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

3. SignalR Implementation

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

The client side hub connects to the server and function(lightFeedValue) is called from the signalR server with the updated value. The <h1 id="LightFeedValue">0<h1> <h1>.Text is updated in the function.

I have also written a class called "LightFeedStitching" just for the sake of single implementation. It made my life easy to associate and control the port reading thread and invoke signalR notifications using event model. It should be a separate unit in case of distributed design, but the stitching has to be there anyway.

C#
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();
}

Points of Interest

The whole world is buzzing with IoT, and I realize how easy and interesting implementation it can be? Moreover, latest technology made it available. It is possible to implement in multiple ways, better than this, more efficient than this. One needs to think.

Keep working and stay tuned, my friends - IoT and big data is taking over very fast.

History

  • First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)