未加星标

Machine Learning In IoT By Node.js

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 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

本文数据库(综合)相关术语:系统安全软件

分页:12
转载请注明
本文标题:Machine Learning In IoT By Node.js
本站链接:http://www.codesec.net/view/561407.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(17)