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:
- Arduino UNO scrap (the piece of code runs inside the aduino board to read sensor data and push data to port)
- C# code to read the data from port
- 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)
int ldr_read = 0;
const int DELAY = 1000;
const int LDR_PIN = 3;
const int BAUD96H = 9600;
void setup() {
Serial.begin(BAUD96H);
}
void loop() {
ldr_read = analogRead(LDR_PIN);
Serial.println(ldr_read);
delay(DELAY);
}
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.
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:
string message = _serialPort.ReadLine();
FeedValue = Convert.ToInt16(message);
3. SignalR Implementation
<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.
LightFeedStitching objectStitching;
protected void Application_Start(object sender, EventArgs e)
{
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<LightFeedHub>();
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