Quantcast
Channel: CodeSection,代码区,数据库(综合) - CodeSec
Viewing all articles
Browse latest Browse all 6262

Machine Learning In IoT By Node.js

$
0
0

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 by 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 kind of introduction for IoT developers who want to use johnnyfive.io in their own 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 earlier.

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

InC#

[HttpGet] publicHttpResponseMessageGetCalculatedVal(intval1,intval2){ using(varredis=newRedisClient("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 value of “val1” by using "get" keyword by the same method.

In Python

importredis r_server=redis.Redis('localhost')#thislinecreatesanewRedisobjectand #connectstoourredisserver r_server.set('test_key','test_value')#withthecreatedredisobjectwecan #submitsrediscommandsasitsmethods print'previoussetkey'+r_server.get('test_key')#theprevioussetkeyisfetched

Like in C#, you can use Redis inside 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 will have fired Arduino com ports and Python over ASP.NET WebAPI. Also, you can prefer zeroMQ, RabbitMQ to make pipeline. Redis can be used as message broker too.

Steps

We will prepare our breadboard for measuring the distance using HC-S04 that 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 to 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, where in 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 upon your board.

The final alternative is to copy and paste the source into the Arduino IDE and click the Upload button.


Machine Learning In IoT By Node.js

The above picture is simple sketching of HC-SR04 usage with Arduino Uno. We will have to add 2 LEDs to show 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.


Machine Learning In IoT By Node.js

After that, you can see that in the below image, there is a handmade drawing description to teach you my logic of calling Python over Node.js


Machine Learning In IoT By Node.js

Python File


Machine Learning In IoT By Node.js

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 sets; these are description of features. You have to use DecisionTree algorithms and you have to bind features and labels before predicting method.

Let’s look at Node.js side.

varfive=require("johnny-five"); varunirest=require('unirest'); varboard=newfive.Board(); board.on("ready",function(){ varledgreen=newfive.Led(3); varledred=newfive.Led(5); varcm=0; varresult=0; this.repl.inject({ led:ledgreen, ledred }); varproximity=newfive.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("REDBLINK!") console.log('\x1b[36m%s\x1b[31m','cm:'+cm) ledred.blink(); ledgreen.stop(); }else{ console.log("GREENBLINK!") console.log('\x1b[36m%s\x1b[32m','cm:'+cm) ledgreen.blink(); ledred.stop(); } }); });

If you look at calling Web API RESTful service, there is two kinds of result call-back.

proximity.on("change",function() { unirest.get('http://localhost:56842/api/CallPythonFuncForIOT/?val='+cm) if(parseInt(result)==1){ //RED } lse{ //GREEN }

RESTful service will set parameters inside of the Redis DB key-value pair. And then, it will call Python file to run the Machine Learning decision tree algorithm by using SciKitlearn.

importredis fromsklearnimporttree

Viewing all articles
Browse latest Browse all 6262

Trending Articles