2017年3月22日 星期三

2017 又再 IoT: NodeJs + Mosquitto + MongoDB 簡單測試

好喇, 已經裝左 mosquitto, MongoDB 同埋 nodejs, 家陣仲係唔知發生乜事.
不過, 先做個簡單測試, 睇下 work 唔 work 先.

1) mosquitto - 可以用 chrome apps: MQTTLENS, 又或者 mqtt_spy

2) mongoDB - 可以起左 server 後 執行 mongo.exe 直接連上 MongoDB

3) 裝先行到  node 或者 npm 都應該得掛.
    再唔係可以試下呢個 script, 用 node example.js 去行, 佢會起左個 web server.
    用 browser 開 localhost:3000 應該見到句 Hello Work 就係得了.

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => { 
  console.log(`Server running at http://${hostname}:${port}/`);
});
 4) 最後, 更係要試下 3 件野係咪夾得埋黎用, 如果唔係, 裝黎托咩.
     可以做個簡單既測試:

  • 用 nodejs 起個 process
  • 睇住 mosquitto 既 message
  • 收到想要既 message 就儲入 MongoDB
    是旦搵個 folder, 安裝 nodejs 定所需既野.  今次只係要 mqtt 同 mongdb
    比如開個新 folder, E:\Node\Tester, 然後走去 E:\Node\Tester 行呢句野:
npm install mqtt mongdb
    佢就會裝左 mqtt 同 mongdb 既 package 去 E:\Node\Tester\node_modules
    之後就開個 text file 叫 example.js copy 呢堆野入去:

consoleLog('MQTT MongoDB Tester Started');
var mqtt=require('mqtt');
var mongodb=require('mongodb'); 
var mongodbClient=mongodb.MongoClient; 
var mongodbURI='mongodb://localhost/data';
var deviceRoot='mqtt/demo/';
var collection,client; 
 
mongodbClient.connect(mongodbURI, setupCollection);
 
var connInfo = {
  host: 'localhost',
  port: 1883,
  clientId: 'guest169'
}
 
function strNow() {
  return (new Date()).toISOString().replace('T',' ').replace('Z','');
}

function consoleLog(msg) {
  console.log(strNow() + ' : ' + msg);
}

function setupCollection(err,db) { 
  if(err) throw err;
  collection=db.collection('mqtt_demo');
  client=mqtt.connect(connInfo)
  client.subscribe(deviceRoot + '#')
  client.on('message', insertEvent);
}

function insertEvent(topic,payload) { 
  var now = strNow();
  var key=topic.replace(deviceRoot,'');
  console.log('%s : [%s] - %s', now, key, payload);
  collection.update( 
    { _id:key },
    { $push: { events: { event: { value:payload, when:new Date() } } } },
    { upsert:true },
    function(err,docs) {
      if(err) { consoleLog('#### Insert fail'); }
    }
  )
}

    然後行 node example.js 就可以了.
    之後, 用  mqtt_spy 或者  MQTTLENS 去 publish, 個 topic 如果開頭係  "mqtt/demo/" 就會接收, 然後放入 MongoDB.  只要走去 MongoDB, use data, 再 show collections, 應該會見到 mqtt_demo 既, 然後再db.mqtt_demo.find(), 就會見到收左乜野了.




2017年3月21日 星期二

2017 又再 IoT: node.js, npm

明明講緊 IoT, 關個 node.js 乜事.

原因為小弟懶, 發覺好多免費 package 好似幾好用 (例如 Node-RED), 不過就要先裝 node.js 用 npm 去裝.

無計, 老人家唔想做大多野, 有免費野就唔好浪費, 裝就裝喇.

  1. 下載軟件:  https://nodejs.org/en/download/
    原來在 https://nodejs.org/en/  已經有 v7.7.3 可以試, 但 download page 係  v6.10.0 LTS.
  2. 安裝軟件:  Windows 軟件, 就咁  click 個 msi 等佢裝.
    裝完就會去左:   C:\Program Files\nodejs
  3. 裝完之後, 佢會自動加左入 path, 可以直接在 command prompt 行 node 同 npm
  4. 執行 node <filename> 就可以用黎行 nodejs 既 script 了.  例如下面呢個:
    執行之後會起左個 web server, 用 browser 去 http://localhost:3000 會出句 Hello World.
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => { 
  console.log(`Server running at http://${hostname}:${port}/`);
});

  1. 跟 nodejs 6.10.0 黎既 npm 係 4.4.1 版本, 可以用黎裝你需要既 modules.
    安裝既方法:  npm install <module list>
  2. 比如話裝個 mqtt, 就可以做個好簡單既 mqtt monitor 去睇住 mosquitto 收左D 乜

consoleLog('MQTT Monitor Started');
var mqtt=require('mqtt');
var data;
var connInfo = {
  host: 'localhost',
  port: 1883,
  clientId: 'mqtt_receiver'
}

client=mqtt.connect(connInfo);
client.subscribe('#');
client.on('message', receiveMessageEvent);

function strNow() {
  return (new Date()).toISOString().replace('T',' ').replace('Z','');
}

function consoleLog(msg) {
  console.log(strNow() + ' : ' + msg);
}

function receiveMessageEvent(topic, payload) {
  console.log('%s : [%s] - %s', strNow(), topic, payload);
}

  1. 之後好多野我都仲研究緊

2017 又再 IoT: Database - MongoDB

今天玩大佢, 唔用人地既 service, 自己黎料.  咁就更係要有個 DB 喇.

近期吹 BigData, NoSQL 之類既野, MongoDB 又吹到好似好勁.  反正唔駛錢就試下喇.

基本要求, 暫時只係話 XP 唔 support, 之後既都得, 今次先用 Windows 7 x64 試試.

1) 下載軟件:  https://www.mongodb.com/download-center#community
    唔知有幾多分別, 先玩住隻 Community Server, 64bit with SSL Support

2) 安裝軟件: Windows 軟件最簡單, 直接 click 個 msi 就得.
    Setup type 選 [Complete] 裝完會去左
    C:\Program Files\MongoDB\Server\{version}
   (注意: 行 msi 就裝 3.4.2, {version} 會用 3.4 而唔係 3.4.2, 自已執生.)

3) 起 Server... 仲研究緊, 好似去到邊個 folder, 下面開定個 data\db, 就咁行 mongod.exe 比埋個 dbpath 佢就得, 當然要 full path 去行喇. 例如:

"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --dbpath E:\my.Files\IoT\MongoDB\data

(注意, 條 path 係指到去 data, 唔駛指落 db, 呢個仲唔係好知點解.)

起到既話, 佢會出句 "waiting for connecitons on port xxxx". 咁就行緊喇.




4) 識起最緊要識得收, shtudown 就開令一個 command prompt, 行呢句:

"C:\Program Files\MongoDB\Server\3.4\bin\mongo.exe" admin --eval "db.shutdownServer()"

2017年3月1日 星期三

2017 又再 IoT: MQTT Broker - mosquitto

pat pat 痕就更係要攪下 伺服 至得.
今次就決定試下唔用人地既 broker, 自己黎料, 攪得幾多得幾多.

初步諗住用 mosquitto, 其實我都唔知係乜, 一隻蚊, 做得D乜.



For installation on a new machine, don't trust the readme file that you just need the 3 DLLs........

Dependencies:

libeay32.dll ssleay32.dll from OpenSSL:
Installed version:  Win32 OpenSSL v1.0.2k Light   (note: the new version does not contain these dll files)
Installed location: C:\OpenSSL-Win32  (the required dll files can be found here also)
In fact, it is not only need these two files, so don't just copy the files from other machine, you need to install the OpenSSL-Win32 as there has some other files required also.
               
pthreadVC2.dll from pthreads
Installed version: pthreads-w32-2-9-1-release.zip
Unzip the file, and the dll can be found in Pre-built.2\dll\x64 & Pre-built.2\dll\x86.
It must use the one in Pre-built.2\dll\x86.

So, all required DLL can be found as below,
DLL file
Source
Date modified
Size
libeay32.dll
OpenSSL
2017-01-26 07:25:34
1.20 MB (1,265,664 bytes)
ssleay32.dll
OpenSSL
2017-01-26 07:25:42
268 KB (274.944 bytes)
pthreadVC2.dll
pthreads
2012-05-27 01:36:06
54.5 KBB (55,808 bytes)


The above 3 DLLs has been mentioned in mosquitto's website, and the readme file after installation.  You may proceed the installation if you are a developer.  But you will hit the error if you just install on a new PC, because it has not mentioned that, Microsoft Visual C++ Runtime is also required.

Download the latest version from: https://www.microsoft.com/en-us/download/details.aspx?id=53587

* Must install vc_redist.x86.exe even you are running a 64bit windows.

Before installation of mosquito, copy the above three files to C:\Widnows\System32
Then execute the  mosquitto-1.4.11-install-win32.exe  (Check the “Service” to install)
Installed location: C:\Program Files (x86)\mosquitto
New service added:  Mosquitto Broker
Try to start the service (it should be installed as Automatic, which will be started after restart windows).

Test the mosquito installation by using Chrome application: MQTTLENS
Subscribe for topic: # (i.e. all topics)
Publish any message; it should be shown under Subscriptions.

Other testing tools:





2017 又再 IoT: 裝備篇 - ESP8266 + Usart-GPU

首先, 諗下今次點玩.

(1) MCU
第一件事, 搵左 硬件先,  D1 好似唔係咁 stable, 唔知點解好容易出  wdt reset.

買左塊 NodeMcu Lua WIFI V3 , 強國野, 佢叫乜就乜, V3 就 V3 喇.


官網 簡單到無人有, 想 check 下 pin assignment 都唔得.
自己上網搵下每個版本既資料, V3 最大好處, 係有 USB 直出既 5V pin.





其他舊版本, 可以參考一下:

V2





V0.9/V1





注意: 家陣 I2C 既 Wire 庫, 係唔支援 Slave mode 既, 只可以做 master.
所以, 如果有兩塊 nodeMCU, 係唔可以用 I2C 通訊既.

不過, 因為 IO 唔係咁多,  又搵唔到靚仔又易睇既 I2C 彩屏, 準備用 Usart-GPU 做 display.
但係 Usart-GPU 果D要用 5V 電....諗諗下, 可能用番 D1, 又或者買塊 MeWos D1 R2 V2.1.


用番 D1, 佢 D 針腳真難估, D1 R1 同 D1 R2 又唔同哂, 真係要小心D試清楚至開始.
唔知點解, TX1 總係怪怪地.

注意:
- 如果要用 Interrupt 既話, ESP8266 只係可以用  FALLING, RISING 同 CHANGE.
  千萬唔好用 LOW 或者 HIGH, 會無反應的.


ESP8266 既針腳, 玩玩下, 直接用粒 chip 駁電仲好玩:




(2) Display
呢樣其實唔係必然既, 24小時行既野, 邊得閒睇住.  不過, 一旦出事, 無個顯示就唔知發生乜事.
最後都係決定加個 display.  上面都講左, 為左省 IO 線, 諗住用 Usart-GPU 既 display, 仲痕痕地想試下用 touch mon.   諗住自己做埋省電功能, 5分鐘無人掂就關 mon, 再 touch 佢就開番.


不過...可能會捉蟲, 萬一死左, 個 mon 又 off 左....touch 佢都唔會開番, 又係無用.  唉..心大心細.




2017 又再 IoT

三年前第一次聽見 IoT, 好似幾好玩, 就用 塊 Arduino Mega2560 加幾個 sensor, 再用 yeelink 做左個測試系統:



(終極版係多左 D 設備, 不過好似無再影相, 就用張第一代既相算了, 反正都唔係介紹佢.)

玩左一年左右, 都唔知有乜好玩, 諗唔到乜野新意, 就自動摺埋左.
收集左一年既數據, 唔知有乜用.

事隔一年多, 世界變左好多.  之前仲係用 W5100 插線既網路, 果時 ESP8266 仲係用串口連接 Arduino 既網絡卡, 今時今日, 因為 ESP8266 仲快過 Arduino, 已經獨立了.  加上有人 port 左套 Arduino 落去 (或者叫左 Arduino IDE 支援埋 獨立既 ESP8266), 要做無線網路都好易....係超易至真.

背脊對落既地方有少少痕, 之前入左塊 D1 都無認真試過, 攞番出黎玩.
發覺已經可以直接裝 driver, 而且簡單易用, 又心思思想試下.
再上 io.adafruit.com  測試下, 又幾方便喎....就 pat pat 痕再玩番下.

雖然 , 今時今日, 已經通街都係免費 broken, 有哂靚仔 UI.
但係既然三年前都玩過, 再玩番都係無新意, 今次再痕 D, 自己起埋個 server 玩.
希望....唔會衰喇.

其實自己都唔知有乜選擇, 要裝幾多至夠, 不過, 一向貪玩既性格, 玩左先算.
老人家玩野, 玩得幾多日就幾多日,.....