Explore latest trends in technology, internet, and more on Sarab Tech Blog!

USB Communication with IoT Devices

Tuesday, June 18, 2019 11:14 AM

Hello,

        IoT devices are taking over the world by being always connected and extremely functional to serve as a solution for variety of technical requirements. I’ve been experimenting with them for a couple of months and found them technically intriguing and worth great value. However, if you’re experimenting with an IoT device or wish to communicate with the same using a regular desktop-pc or laptop running any modern OS, there will be a need to communicate with the device after deployment to debug issues or collect sensors data.

This communication with the IoT Device usually happens through the famous and extremely versatile Universal Serial Bus (USB) communication protocol found in modern electronics hardware. In order to use an IoT device to communicate with a pc, one can simply connect the same via regular UART/USB interface cable and use following code (written in C#) to communicate with the device:

Code Snippet:

[DllImport("user32.dll")]    //Required for the Serial Communication

static void Main(string[] args)

{

 Initialize();

}


private static void Initialize()

{

SerialPort mySerialPort = new SerialPort("COM3"); //Port Selection

           mySerialPort.BaudRate = 115200; //Set as per setting on Device

           mySerialPort.Parity = Parity.None;

           mySerialPort.StopBits = StopBits.One;

           mySerialPort.DataBits = 8;

           mySerialPort.Handshake = Handshake.None;

           mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

           mySerialPort.Open();

           Console.WriteLine("Awaiting for the data...");

           Console.ReadKey();

           mySerialPort.Close();

}

private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)

        {

            try

            {

              SerialPort sp = (SerialPort)sender;

              string indata = sp.ReadLine();

              Console.WriteLine(indata); //Print received information

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message.ToString());

            }

}



Business Rule Engine (BRE) in C#

Monday, June 17, 2019 11:03 AM

Hello,

            I’ve been doing quite some research for an apt technical solution (in C#) for implementing automated business rules on large datasets, whether stored locally or in the cloud, which allows dynamic loading of rules, facts, and target datasets. So I came across various implementations, concepts, ideas, and models boasting this functionality with certain pros and cons of each implementation; however I couldn’t find anything simple enough until I decided to build one myself.

 First task was to understand the architectural requirements for such a solution and then proceed towards building the structural dynamism using code. For such a system to be built, I resorted towards creating a foundational database structure which allows CRUD operations for creating / storing business rules, then moving towards loading rules in-memory and on-demand within the context of supplied datasets; and ultimately executing those rules on the supplied dataset for results. Results produced predictively enough had a requirement to further notify or take an action if a rule was passed or failed during execution. Here one important concept to implement at this stage was to ensure the principle of recursion commonly used in coding. Recursion principle ensures firing of rule engine again when a condition is matched regardless of the program flow. This ends up creating a very dynamic rules execution plan / engine / workflow which works quite magically ‘as-and-when or if’ the data becomes available for an in-memory object. I see some application of this in real-time systems such as IoT devices or services. Using C# language’s latest features allowed me to have above code implementation as neat and readable as possible for any future modifications.

 

Truly interesting stuff!

Insufficient Disk-space in MS SQL Database!

Sunday, March 3, 2019 2:21 AM

Hello,

 One needs to be always cautious about the physical size of the back-end database to prevent certain scenarios which can cause the front-end application to malfunction due to age old problem of running out of storage space!

 Recently I had used an IoT Device to stream data collected from few deployed sensors and save all that into a back-end MS SQL Database which over a period of time ran out of the storage space on the server. Yeah! Imagine how many rows were dumped into the database system to cause this scenario. Anyways, to remedy the  situation following SQL Script  was extremely useful:

 

use "YourDatabaseName"

exec sp_spaceused;

 

USE YourDatabaseName

GO

 

SELECT

  t.Name                                       AS TableName,

  s.Name                                       AS SchemaName,

  p.Rows                                       AS RowCounts,

  SUM(a.total_pages) * 8                       AS TotalSpaceKB,

  SUM(a.used_pages) * 8                        AS UsedSpaceKB,

  (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB

FROM

  sys.tables t

  INNER JOIN sys.indexes i ON t.object_id = i.object_id

  INNER JOIN sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id

  INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id

  LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id

WHERE

  t.Name NOT LIKE 'dt%'

  AND t.is_ms_shipped = 0

  AND i.object_id > 255

GROUP BY

  t.Name, s.Name, p.Rows

ORDER BY

  t.Name;

GO

IoT-based HTTP Webserver (DIY)

Monday, February 18, 2019 9:43 AM

Hello Everyone!

 In this post I am going to talk about an Internet of Things (IoT) project which demonstrates the HTTP web-server functionality (just like regular web-servers which run on powerful desktops) on way less powerful hardware. I chose NodeMCU board based on ESP8266 microchip which integrates GPIO, PWM, IIC, 1-Wire and ADC all in one board! It has USB-TTL included with plug & play and has around 10 GPIOs where every GPIO can be PWM or I2C... Well, without getting too nerdy about it, let me explain what I did with it.

 Any IoT device should be designed with a Service-oriented Architecture (SoA) to offer its core functionality to the end-users. If you look around any smart device usually comes with some way of communicating through either via Bluetooth or WiFi, so I was thinking maybe to use NodeMCU to act as a HTTP webserver since it comes with inbuilt WiFi, and let a service designed for the end-users to trigger an event on the device itself. In my case, i decided to use a small inbuilt LED which is conveniently placed on the NodeMCU board itself which comes prebuilt with the board.

 Thereafter it took me a couple of hours to put together and test code using the Arduino IDE and used serial monitor tool to review the internet communication activity on the NodeMCU, which to less of my surprise it just worked!  

 The code enables persistence for auto-connection to the given WiFi Router using pre-supplied login credentials for obtaining an Internet Protocol (IP) address. This means that the moment there is power, the IoT device would auto-connect or keep trying unless it does that! Having an IP address and the whole system listening on port 80 means any third-party device or system can connect to it and exchange information or data. In my case, I just used my laptop’s web-browser to connect to the NodeMCU’s IP address and be presented with following screen:

  

Turning on the LED on board was simply a matter of a click on the button!

 

Thank you for reading and I hope you’ll find this post useful and in case if you have any questions then please feel free to drop a comment below!


Blog News Update – February 2019

Monday, February 18, 2019 4:04 AM

Hello Everyone,

 Just a brief announcement regarding the new and upcoming content on the blog. Last year or so was spent on Internet of Things (IoT) related areas where I wanted to experiment more on Internet of Things (IoT) and mixing and merging it with Blockchain technology.

 Blockchain technology had kept me interested for a while before I decided to build one application using it. The purpose of the application was to just have a decentralized chat server for the blockchain network consisting of nodes. Basic idea was simply to implement software application for the nodes participating in network and keep the ChatLedger database file updated with chat transactions happening between various nodes/clients. Following shows the network nodes communicating with each other and discovering new clients/nodes joining or leaving the network:

For some reason, I got obsessed with IoT and it struck me really badly that I wanted to somehow combine it with the blockchain technology as it made lots of sense for these technologies to work together. At one point of time, I decided that I need to start teaching a course about IoT which I am currently teaching at the University to third year undergrad students since January 2019. So far it has been a great experience tinkering with IoT, Sensors, and Electronics and trying to build an IoT device with a purpose.

 I thought it would be a good idea if I start posting about some of the advanced projects I am working on and make this blog little bit more useful for my students among others.


State of Artificial Intelligence Today

Monday, February 18, 2019 2:23 AM

Hello Everyone!

 In this post, my desire is to pen down few thoughts on elusive artificial intelligence to describe current state and somewhat ponder over big claims made by tech companies competing in this market space. If you look around seriously in tech startup scene, most of the new upcoming tech product companies are ‘claiming’ to have figured out the AI puzzle and worked out-kinks to implement the technology.

 AI-driven systems must have an ability to process large amounts of knowledge. I mention 'knowledge' and not 'information' for a reason in this statement because having knowledge of something enables someone to follow the shortest logical path to attaining success. On the other side having just information about something merely enables awareness at best. A system built using AI must have or be capable to extract and differentiate knowledge from ‘information’ where information must be seen as a fundamental requirement for the AI system to start work.

 Here I would like to also point out two variations of knowledge which needs to harvested on-the-go. I classify it as dynamic knowledge vs. static knowledge. Dynamic knowledge is derived, extracted, and analyzed in real-time from raw data as soon as the system goes live and information is processed for further action. Static knowledge on the other hand is, as on date, the hardcoded knowledge as static information which doesn’t change often. For extracting actionable static knowledge, machine learning techniques have been developed to produce actionable insights from vast amounts of raw data. But we know as humans, that static knowledge won't serve us in every situation we're facing. Imagine as humans if we experiment this static knowledge in a social scenario as a rule of "One should be nice to other people" and apply this rule to all interactions, then even in situations where "One should not be nice" e.g. someone is trying to kill you, you can't be nice then. This means that the knowledge is subjective to the situation. Rules of engagement with other people change.

 Clearly, the emphasis needs to be on being able to produce dynamic knowledge from information gathered. There is a downside to all this and that is the well-known Utility Problem. For an AI system to be smart, same system needs to be designed with AI architecture capable of learning relevant knowledge from raw data or plain information without the nasty consequence of slowdown in overall functionality using clever optimization-related algorithms.

 Thank you for reading and I hope you’ll find this post useful and in case if you wish to add any additional point to the post, then please leave a comment below.