In this article, I will tell you how to use Scikit-learn with Python scripts for IoT applications by using Node.js. First, to know more, you have to read my previous articles about IoT with Node.js. Let’s look at these articles briefly:
- Node.js in IoT Part-1
- Node.js in IoT Part-2
My first article is an introduction for IoT developers who want to use johnnyfive.io in their projects. The second one is a simple experimental project to show NoSQL+Node.js+Arduino usage together.
Now in the third article, I am going to tell you how to use Redis (NoSQL db)+ASP.NET Web API + Node.js + johnnyfive.io + Python + Arduino. I believe that it is a kind of industry 4.0 article to understand the scope of industry 4.0. that I explained previously.
What is Redis?
As defined by Wikipedia,
"Redis is an in-memory database open-source software project implementing a networked, in-memory key-value store with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, hyperlog logs, bitmaps and spatial indexes. The project is mainly developed by Salvatore Sanfilippo and is currently sponsored by Redis Labs.
History
The name Redis means Remote Dictionary Server. Salvatore Sanfilippo, the original developer of Redis, was hired by VMware in March, 2010. In May, 2013, Redis was sponsored by Pivotal Software (a VMware spin-off).[9] In June 2015, development became sponsored by Redis Labs.According to monthly rankings by DB-Engines.com, Redis is often ranked the most popular key-value database.Redis has also been ranked the NoSQL database in user satisfaction and market presence based on user reviews, the most popular NoSQL database in containers, and the NoSQL database among Top 50 Developer Tools & Services.
Supported languages
Many languages have Redis bindings, including: ActionScript, C, C++, C#, Chicken Scheme, Clojure, Common Lisp, D, Dart, Erlang, Go, Haskell, Haxe, Io, Java, JavaScript (Node.js), Julia, Lua, Objective-C, OCaml, Perl, PHP, Pure Data, Python, R, Racket, Ruby, Rust, Scala, Smalltalk and Tcl."
Simple usage
In C#
- [HttpGet]
- public HttpResponseMessage GetCalculatedVal(int val1, int val2) {
- using(var redis = new RedisClient("localhost")) {
- redis.StartPipe();
- redis.StartPipeTransaction();
- redis.Set("val1", val1);
- redis.Set("val2", val2);
- object[] result = redis.EndPipe();
- }
In the above code, you can see that setting a value inside of a key is simple in Redis. You can get the value of “val1” by using the "get" keyword using the same method.
In Python
- import redis
- r_server = redis.Redis('localhost')
-
- r_server.set('test_key', 'test_value')
-
- print 'previous set key ' + r_server.get('test_key')
Like in C#, you can use Redis inside the Python environment. It also is simple.
Why do we need Redis?
We use Redis because Redis can be a bridge between Node.js IoT applications that have fired Arduino com ports and Python over ASP.NET WebAPI. Also, you can use zeroMQ, RabbitMQ to make pipelines. Redis can be used as a message broker too.
Steps
We will prepare our breadboard for measuring the distance using HC-S04, which is a kind of module for proximity measurement. You have to follow the below instructions.
PingFirmata must be loaded onto Arduino-compatible boards to enable this component. PingFirmata DOES NOT APPLY TO NON-ARDUINO PLATFORMS.
The most straightforward method for enabling this module is to use the Ping I2C Backpack. If you still want/need to use a custom firmata, then the easiest way to get the firmata onto your board is to use interchange. Follow the directions mentioned below.
npm install nodebots-interchange
With your Arduino plugged in, issue the following instruction, using the serial port your arduino is plugged into. (COM3, /dev/ttyUSB0 etc).
interchange install hc-sr04 -a uno -p <port> --firmata
The latest firmware will be downloaded and installed on your board.
The final alternative is to copy and paste the source into the Arduino IDE and click the Upload button.
The above picture is simple sketching of HC-SR04 usage with Arduino Uno. We will have to add two LEDs to show the Python Machine Learning algorithm how to decide which LED will blink. We use ASP.NET Web API to send distance values to Python ML algorithms.
After that, you can see in the below image there is a handmade drawing description to teach you my logic of calling Python over Node.js.
Python File
You can easily see that we use Redis and SciKitlearn together.
- Features means your accumulated data of situations; they include your previous experiences.
- Labels are a kind of result set; these are the descriptions of features.
- You have to use DecisionTree algorithms and you have to bind features and labels before predicting method.
Let’s look at the Node.js side.
- var five = require("johnny-five");
- var unirest = require('unirest');
- var board = new five.Board();
- board.on("ready", function() {
- var ledgreen = new five.Led(3);
- var ledred = new five.Led(5);
- var cm = 0;
- var result = 0;
- this.repl.inject({
- led: ledgreen,
- ledred
- });
- var proximity = new five.Proximity({
- controller: "HCSR04",
- pin: "7"
- });
- proximity.on("data", function() {
- cm = this.cm;
- });
- proximity.on("change", function() {
- unirest.get('http://localhost:56842/api/CallPythonFuncForIOT/?val=' + cm).end(function(response) {
- console.log(response.body);
- result = response.body;
- });
- if (parseInt(result) == 1) {
- console.log("RED BLINK!")
- console.log('\x1b[36m%s\x1b[31m', 'cm:' + cm)
- ledred.blink();
- ledgreen.stop();
- } else {
- console.log("GREEN BLINK!")
- console.log('\x1b[36m%s\x1b[32m', 'cm:' + cm)
- ledgreen.blink();
- ledred.stop();
- }
- });
- });
If you look at calling the Web API RESTful service, there are two kinds of result call-back.
- proximity.on("change", function()
- {
- unirest.get('http://localhost:56842/api/CallPythonFuncForIOT/?val=' + cm)
- if (parseInt(result) == 1) {
-
- }
- lse {
-
- }
RESTful service will set parameters inside of the Redis DB key-value pair. Then, it will call the Python file to run the Machine Learning decision tree algorithm by using SciKitlearn.
- import redis
- from sklearn import tree
- #features
- features = [[25],[24],[23],[22],[21],[20],[19],[18],[17],[16],[15],[14],[13],[12],[11],[10],[9]]
- #labels = ["green","red","green","red". . . . . ]
- labels = [0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]
- clf = tree.DecisionTreeClassifier()
- clf = clf.fit(features,labels)
- r = redis.StrictRedis(host='localhost', port=6379, db=0)
- val = r.get('valueIOT')
- print (clf.predict([[val]]))
In this article, I tried to show you how to use Node.js by using Arduino and how to call ASP.NET WebAPI RESTful service. Also, you learned how can we call Python file from C# codes. We used a HC-SR04 measurement to send distance between sensor and obstacle, and then decided which LED will blink by using Python ML decision tree algorithms.
References
- https://redis.io/
- https://en.wikipedia.org/wiki/Redis