2016年8月8日 星期一

[mqtt]在 XP 上安裝 mosquitto

[mqtt]在 XP 上安裝 mosquitto

在 XP 上安裝 mosquitto,與在 windows 7 以上不一樣。

在 XP 上,需要使用標有 cygwin 的安裝檔。如果直接使用標注 win32 的安裝檔來裝,在執行時會出現 mosquitto.exe 不是正確的 win32 應用程式的錯誤。
下載 mosquitto-1.4.9-install-cygwin.exe 之後,安裝。一樣的,需要依說明下載 Win32OpenSSL_Light-1_0_2h.exe 與 pthreads-w32-2-9-1-release.zip 然後把需要的 dll 放到 mosquitto.exe 旁邊。
再來則必須要安裝 cygwin,到 cygwin 官網下載 setup-x86.exe,然後執行。只需要安裝基本 (base) 就好了,我們只要是複製幾個檔案。安裝好之後,就點 mosquitto.exe,它會告知少什麼 dll,然後就去 cygwin 安裝的地方 copy 過來。我記得是以下幾個:
* cygcrypto-1.0.0.dll
* cyggcc_s-1.dll
* cygssl-1.0.0.dll
* cygwin1.dll
* cygz.dll
這樣,mosquitto.exe 就正常可用。如果希望它變成一個服務,那最簡單是再執行一次安裝檔,選擇服務選項。如此就會在開機的時候自動啟動。
但是,這個安裝檔裡的 mosquitto_pub.exe 與 mosquitto_sub.exe 仍出現不是正確的 win32 應用程式的錯誤。我想也許是打包錯了,報了 issue 結果負責人說他沒有 XP,所以要我們自己 compile 試試。其實在他回覆之前,我就下載舊的安裝檔來試,裝在另一個目錄,中間有很多不同的錯誤,像是找不到 entry point 的、getTickCount64 不在 KERNEL32.DLL 的。最後終於在 mosquitto-1.2-install-cygwin.exe 找到一個說缺 msvcr100.dll 的。
msvcr100.dll 我找到有兩個,一個在 C:\windows\system32,一個則在 anaconda\Library\bin 裡面。結果是後面這個放到 mosquitto_sub/pub 旁邊才成功。但是 mosquitto-1.2-install-cygwin.exe 裡面的 mosquitto.exe 會出現 無法找到程序輸入點 inet_ntop (在動態連結函式庫 WS2_32.dll) 的錯誤。所以就安裝兩版來解決問題吧 XD
目前只有用到最簡單的連結,其他功能沒有用到(例如 ssl、帳號密碼),所以不知道是不是全功能完整無蟲。可以確定的是 服務正常、pub/sub 正常。私用應該沒問題。

[超譯]Node.js 加 MQTT 入門

https://blog.risingstack.com/getting-started-with-nodejs-and-mqtt/

Node.js 加 MQTT 入門

這篇貼文由 Charlie Key 提供,他是 Structure 的 CEO 與 Co-Founder。Structure 是一個 IoT 的平台,讓你能輕鬆建立相連的經驗與解決方案。Charlie 已經用 Node.js 於工作幾年,現在用它來為 IoT 的世界充能。
Javascript 的世界持續地開發新彊界,像 Node.js 的技術可讓伺服端快速擴展,而現在達到 IoT 的世界。Node.js 現在可在許多嵌入式裝置內,像是 Intel Edison。與嵌入式裝置的溝通一向都可行的,但使用 Node.js 與 MQTT 這類協定讓溝通前所未有的簡單。
在這貼文,我們會看一下如何利用兩個技術(Node.js 與 MQTT)來傳送訊息,建立一個簡易的車庫開門應用程式。這只是此類通訊的其中的一個可能的應用。
MQTT 本身是個非常簡單的 publish / subscribe (出版/訂閱)協定。它讓你在一個主題上送訊息(你可以想像那些是頻道),經由一個中央管理的 message broker。整個協定故意非常輕量。這會讓它能輕易地在嵌入式裝置上執行。幾乎所有的微處理器都有函式庫可用讓它能收送 MQTT 的訊息。以下可以看到 MQTT 溝通的基本概念。
這裡一張架構圖
現在,想像一下我們要打造一個遠端控制的車庫開門系統,使用 MQTT。第一件事我們需要計畫車庫門與遠端遙控器要傳送什麼訊息。為了要讓這範例簡單,我們只打算能夠開門與關門就好。真實的架構圖會長成這樣:
又一張架構圖
門會有幾個狀態,已開、已關、開門中、關門中。真的門也許會有其他狀態,如 暫停。但我們今天暫不考慮。
我們的應用程式會分開兩個檔案,一個是給車庫用另一個給控制器用。我會在每個程式的上頭標名檔名。首先,我們會需要用 npm 安裝 mqtt 函式庫,然後設定我們要用的 broker。現在有很多開放的 broker 可用於測試,我會使用 broker.hivemq.com。再次強調,這只是測試用,不要在正式產品還用這個。以下是兩個檔案一開始都要的程式碼:
// contoller.js and garage.js
const mqtt = require('mqtt')  
const client = mqtt.connect('mqtt://broker.hivemq.com') 
接下來,我們要加一些程式碼來連上 broker。一定連上,我們會建立一個主題(頻道),這在車庫門連上時的溝通用的。在門這邊,是 publish(出版)訊息到這個主題,而控制器這邊則是subscribe(訂閱)。同樣,在這個時間點,我們會加一個區域變數,追蹤車庫門現在的狀態。你會發現我們的主題加了前綴 “garage/”,這是為了組織目的的簡化,你也可以隨自己喜歡來命名。
// garage.js
const mqtt = require('mqtt')  
const client = mqtt.connect('mqtt://broker.hivemq.com')

/**
* The state of the garage, defaults to closed
* Possible states : closed, opening, open, closing
*/

var state = 'closed'

client.on('connect', () => {  
  // Inform controllers that garage is connected
  client.publish('garage/connected', 'true')
})
在控制器端,我們不只是要訂閱這主題,我們也需要加上訊息接聽者,對訊息出版時採取動作。一但訊息收到,我們會使用一個變數,檢查變數的值且追蹤是否門還連在系統上。
// controller.js
const mqtt = require('mqtt')  
const client = mqtt.connect('mqtt://broker.hivemq.com')

var garageState = ''  
var connected = false

client.on('connect', () => {  
  client.subscribe('garage/connected')
})

client.on('message', (topic, message) => {  
  if(topic === 'garage/connected') {
    connected = (message.toString() === 'true');
  }
})
目前為止,門與控制器只知道,門是否連在系統上,我們還不能採取什麼動作。為了要讓控憲器知道門發生什麼事,我們再加上一個函式,送出現在門的狀態,函式長這樣:
// added to end of garage.js
function sendStateUpdate () {  
  console.log('sending state %s', state)
  client.publish('garage/state', state)
}
要使用這個函式,我們會加在車庫連上的呼叫裡:
// updated garage.js connect
client.on('connect', () => {  
  // Inform controllers that garage is connected
  client.publish('garage/connected', 'true')
  sendStateUpdate()
})
現在車庫門可以更新,告訴每個人它現在的狀態。現在控制器需要更新自己的門狀態的變數。然而在這個時間點,先更新訊息處理函式,對應不同的主題呼叫不同的函式。這會增加一點程式的結構性。整個更新完如下:
// updated controller.js
const mqtt = require('mqtt')  
const client = mqtt.connect('mqtt://broker.hivemq.com')

var garageState = ''  
var connected = false

client.on('connect', () => {  
  client.subscribe('garage/connected')
  client.subscribe('garage/state')
})

client.on('message', (topic, message) => {  
  switch (topic) {
    case 'garage/connected':
      return handleGarageConnected(message)
    case 'garage/state':
      return handleGarageState(message)
  }
  console.log('No handler for topic %s', topic)
})

function handleGarageConnected (message) {  
  console.log('garage connected status %s', message)
  connected = (message.toString() === 'true')
}

function handleGarageState (message) {  
  garageState = message
  console.log('garage state update to %s', message)
}
在這裡,我們的控制器可以跟上車庫門的狀態與連線狀態。現在可以加一些功能來控制我們的門。第一件事是讓車庫開始接聽一些訊息,告訴它開或關。
// updated garage.js connect call
client.on('connect', () => {  
  client.subscribe('garage/open')
  client.subscribe('garage/close')

  // Inform controllers that garage is connected
  client.publish('garage/connected', 'true')
  sendStateUpdate()
})
我們現在需要在車庫門這裡加個訊息接聽者:
// added to garage.js
client.on('message', (topic, message) => {  
  console.log('received message %s %s', topic, message)
})
在控制器這裡,我們也會加上傳送開門或關門訊息的能力。這有兩個簡單的函式。在一個真實的應用程式中,這會由外部輸入來呼叫(像是 web 應用程式,手機 app…等)。在這個範例中,我們會用個計時器來呼叫,只是測試這個系統而已。新增的程式碼如下:
// added to controller.js
function openGarageDoor () {  
  // can only open door if we're connected to mqtt and door isn't already open
  if (connected && garageState !== 'open') {
    // Ask the door to open
    client.publish('garage/open', 'true')
  }
}

function closeGarageDoor () {  
  // can only close door if we're connected to mqtt and door isn't already closed
  if (connected && garageState !== 'closed') {
    // Ask the door to close
    client.publish('garage/close', 'true')
  }
}

//--- For Demo Purposes Only ----//

// simulate opening garage door
setTimeout(() => {  
  console.log('open door')
  openGarageDoor()
}, 5000)

// simulate closing garage door
setTimeout(() => {  
  console.log('close door')
  closeGarageDoor()
}, 20000)
以上的程式碼包含開與關的功能。它們確認車庫已經連上系統而且不在已要求的狀態中。我們的控制器最後版本的程式碼如下:
// controller.js
const mqtt = require('mqtt')  
const client = mqtt.connect('mqtt://broker.hivemq.com')

var garageState = ''  
var connected = false

client.on('connect', () => {  
  client.subscribe('garage/connected')
  client.subscribe('garage/state')
})

client.on('message', (topic, message) => {  
  switch (topic) {
    case 'garage/connected':
      return handleGarageConnected(message)
    case 'garage/state':
      return handleGarageState(message)
  }
  console.log('No handler for topic %s', topic)
})

function handleGarageConnected (message) {  
  console.log('garage connected status %s', message)
  connected = (message.toString() === 'true')
}

function handleGarageState (message) {  
  garageState = message
  console.log('garage state update to %s', message)
}

function openGarageDoor () {  
  // can only open door if we're connected to mqtt and door isn't already open
  if (connected && garageState !== 'open') {
    // Ask the door to open
    client.publish('garage/open', 'true')
  }
}

function closeGarageDoor () {  
  // can only close door if we're connected to mqtt and door isn't already closed
  if (connected && garageState !== 'closed') {
    // Ask the door to close
    client.publish('garage/close', 'true')
  }
}

// --- For Demo Purposes Only ----//

// simulate opening garage door
setTimeout(() => {  
  console.log('open door')
  openGarageDoor()
}, 5000)

// simulate closing garage door
setTimeout(() => {  
  console.log('close door')
  closeGarageDoor()
}, 20000)
現在,車庫門必須對應這些訊息做反應。再一次,我們使用 switch 引導不同的主題。一但訊息被收到,門會試著處理它且確認它能到那狀態才動作。然後它會進入轉移狀態(開門中、關門中),送出更新訊息,最後到達持續狀態(已開、已關)。為了測試的目的,最後一個部份是用計時器來完成。在真實情況中,系統應該等待硬體訊號通知它已完成。
// updated garage.js message handler
client.on('message', (topic, message) => {  
  console.log('received message %s %s', topic, message)
  switch (topic) {
    case 'garage/open':
      return handleOpenRequest(message)
    case 'garage/close':
      return handleCloseRequest(message)
  }
})
開門與關門的處理函式可以加到檔案的最後。
// added to garage.js
function handleOpenRequest (message) {  
  if (state !== 'open' && state !== 'opening') {
    console.log('opening garage door')
    state = 'opening'
    sendStateUpdate()

    // simulate door open after 5 seconds (would be listening to hardware)
    setTimeout(() => {
      state = 'open'
      sendStateUpdate()
    }, 5000)
  }
}

function handleCloseRequest (message) {  
  if (state !== 'closed' && state !== 'closing') {
    state = 'closing'
    sendStateUpdate()

    // simulate door closed after 5 seconds (would be listening to hardware)
    setTimeout(() => {
      state = 'closed'
      sendStateUpdate()
    }, 5000)
  }
}
有了這些函式,我們現在有個完整功能的車庫系統。為了測試你可以開啟控制器程式然後再開車庫門程式。控制器會在開啟後 5 秒送開門指令,20 秒送關門指令。
最後我要建議的事是讓我們的車庫門更新自己的連線狀態,當我們的程式因為任何原因被關掉的時候。這個離開的乾淨程式碼是依照 stackoverflow answer 建議,然後改用 mqtt 訊息傳送。這可在放在車庫檔案的最後。所有的東西組合起來就得到最後的車庫檔案。
// garage.js
const mqtt = require('mqtt')  
const client = mqtt.connect('mqtt://broker.hivemq.com')

/**
 * The state of the garage, defaults to closed
 * Possible states : closed, opening, open, closing
 */
var state = 'closed'

client.on('connect', () => {  
  client.subscribe('garage/open')
  client.subscribe('garage/close')

  // Inform controllers that garage is connected
  client.publish('garage/connected', 'true')
  sendStateUpdate()
})

client.on('message', (topic, message) => {  
  console.log('received message %s %s', topic, message)
  switch (topic) {
    case 'garage/open':
      return handleOpenRequest(message)
    case 'garage/close':
      return handleCloseRequest(message)
  }
})

function sendStateUpdate () {  
  console.log('sending state %s', state)
  client.publish('garage/state', state)
}

function handleOpenRequest (message) {  
  if (state !== 'open' && state !== 'opening') {
    console.log('opening garage door')
    state = 'opening'
    sendStateUpdate()

    // simulate door open after 5 seconds (would be listening to hardware)
    setTimeout(() => {
      state = 'open'
      sendStateUpdate()
    }, 5000)
  }
}

function handleCloseRequest (message) {  
  if (state !== 'closed' && state !== 'closing') {
    state = 'closing'
    sendStateUpdate()

    // simulate door closed after 5 seconds (would be listening to hardware)
    setTimeout(() => {
      state = 'closed'
      sendStateUpdate()
    }, 5000)
  }
}

/**
 * Want to notify controller that garage is disconnected before shutting down
 */
function handleAppExit (options, err) {  
  if (err) {
    console.log(err.stack)
  }

  if (options.cleanup) {
    client.publish('garage/connected', 'false')
  }

  if (options.exit) {
    process.exit()
  }
}

/**
 * Handle the different ways an application can shutdown
 */
process.on('exit', handleAppExit.bind(null, {  
  cleanup: true
}))
process.on('SIGINT', handleAppExit.bind(null, {  
  exit: true
}))
process.on('uncaughtException', handleAppExit.bind(null, {  
  exit: true
}))
這了那些,我們完成了我們的車庫門控制器。我希望你能挑戰下一級。一些修改與一個 Intel Edison 會讓你建立一個完整的遠端車庫開門系統。此範例完整的原始碼也會放在 Github。
這只是一個開始。還有一些 MQTT 的新選項與能力,包含使用 SSL、使用者/密碼 認證來增加安全性。
如果你喜歡這篇貼文且想知道 Node.js 能到什麼程度,這有個超讚會議會來到:Node Community Convention。將會有許多偉大的演講,主題包含 IoT、系統放大……等等。

作者
Gabor Nagy
在 Marketing 是個全端。在 web 開發正在從零到英雄的路上。

2016年7月28日 星期四

[超譯] 核心版控演義

linux 核心的版本控制管理系統,曾經有經歷過一段動盪,linus 自己曾經在郵件列表裡回過一段文章,提出自己的看法。郵件標題是 「Kernel SCM saga..」
原文連結:
http://lkml.iu.edu/hypermail/linux/kernel/0504.0/1540.html

好,有很多人已經發現(有些人已經知道好幾個星期),我們正努力解決這一兩個月與 BK [譯註:BitKeeper] 之間的衝突。但,我們沒有成功,所以,核心組正在尋找替代方案。
(顯然這訊息會到 slashdot,所以應該 每個人都知道 )
我選了 BK 就讓能衝突消失的期待,似乎沒有成功 ( “你沒看見這幾G又幾G 的火熖嗎?”)。所以,在某種意義上是無法避免的。但我確信我曾希望能有合理的開源替代能選擇,為此,我們將必須忙和一陣子。
題外話,別怪 BitMover,就算這是一個常見的反應。Larry 真的是想把事情做好,但事情到了我不想要的地步,想要把兩塊湊在一起需要的膠水越多越好。
我們已經使用 BK 三年了,事實上,現在最大的問題是有許多人用過最好的工具之後,變得非常挑剔。包含我自己,我們都因為 BitKeeper 的幫助,在 merging 我的 tree,或把他們的 tree 傳給我的時候變得比較輕鬆。
當然,也許更大多數的人只是把 BK 當成更好(也更快)的「匿名 CVS」的客端。眼前的問題是我花了許多時間試圖找到最好的協作方案。
注意,BitKeeper 不是消失了。只是現在最真切的事情是,我決定不再使用 BK,主要因為我需要找到替代,而不是「讓東西持續正常」。我決定硬著頭皮,看看沒有 BK 的生活會是怎樣。到目前為止,是個黑白的世界。
不要過度解釋,我會用個星期閉關,(想做是 linus 只是去放假的正常事件),我也正在找人負責維護 BK tree,至少是能夠傳給我(單獨)補丁,我會自己 想辦法 merge。
所謂「單獨補丁 individual patches」是個關鍵字。題外話,BK 一個做得很好的事,許多人就算沒用 BK 也喜歡的,就是它對於改變的視圖很棒。這個不會消失。
事實上,BK 讓我們做事的方法有了根本的改變。像是 changeset 的視圖、讓我能相信子維護者能做更大的事、不用按順序上補丁…等。所以三年的 BK 使用不是浪費。我相信這會讓我們用更好的方式做事。我會尋找讓這些事情繼續的方法。
所以,我只是要說,我個人與 BK 相處愉快,與 Larry 也是。合作沒有成功讓核心開發有了巨大的困難。一個暫時性的問題,要產出在 BK 允許的範圍內使用的工具,這一點我們會繼續努力。
讓火開始燒吧。
Linus
PS. 不要再跟我說 subversion。如果你一定要,去研究 “monotone”,那看來是最可行的。但不要再煩開發者讓他們無法把事情做完。他們已經要煩惱我的問題了。

[閒聊]超簡單回答 - 什麼是 github?

github 是一個程式碼代管網站,它讓你可以放程式碼供大家下載。由於它使用 git 版本控制程式,所以名字 github 裡面有個 git。

git 是由 linus 開發出來的(第一版)。是為了因應分散式開發的特性而另外開發的。在他開發的時候,同時間也有其他的分散式開的版本控制系統已經出現,BitKeeper,或是 Mercurial。Mercurial 開發時間只有晚 git 一些些而已。但是 linus 覺得都不合他的意,所以就自幹一套,然後把 linux kernel 的版本控制系統換成了 git。

另外一提,Mercurial,俗稱 hg,這一套版本控制系統是用 python 寫的,Mozilla 與 python 都將程式碼轉移到 hg 的系統之中。

我想,如果有人問我要選哪一個?因為現在使用 git 的人多,也許學用 git 會比較好。至於我自己個人,已經用了 hg,現在還不想隨便換。

參考:
https://zh.wikipedia.org/wiki/Git
https://git-scm.com/book/zh-tw/v1/%E9%96%8B%E5%A7%8B-%E9%97%9C%E6%96%BC%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6
http://lkml.iu.edu/hypermail/linux/kernel/0504.0/1540.html

2016年7月27日 星期三

[閒聊] arduino 也算是個語言?

IEEE 2016 程式排行榜 裡把 arduino 算是一個語言,引起一些討論。
我個人是可以理解為什麼會列為另一種語言。
也許它被認為”足夠”降低開發門檻成功給一般人使用,而且其生態系也大到一個程度。它的前處理,著實也做了很多事,從使用者的角度,根本看不到 C/C++ 要的 ..h, .c or .cpp。在簡化的程度上,已經讓只會寫 arduino 的人,一定不會用 C/C++ 的環境寫程式。
IEEE 官網也很多人質疑,說 arduino 不過是個開發環境,應該要算在 C 上面。但是就如同 C 與 C++ 一樣會有論戰,為什麼 C 要跟 C++ 算一起?或反過來說,C 與 C++ 要算一種語言嗎?如果算一種語言,為什麼要寫 C/C++ 而不乾脆說 C? 如果算兩種語言,但是算人頭的時候又要一起算呢?
曾經在閱讀 cython 的時候,看到他們寫的 「The Cython language is a superset of the Python language that additionally supports calling C functions and declaring C types on variables and class attributes.」他們做的事情,就是讓使用者可以用一般的 python 寫程式,也可以用 cython 新增加的語法指定與 c 合作的宣告與功能。他們也是先經過自己的「前處理器」,把程式轉換過。他們自稱是 cython language。而且還是 superset 。我曾經想了很久。後來又看到 TypeScript,CoffeeScript。如果這些努力可以說是新增了一個新語言,那麼,arduino 被稱做一個語言,我是可以理解的。
我自己的臉書備份

2016年6月15日 星期三

[windows] usb碟在 windows 與 mac 之間插來插去然後就不能用了。

為了交換資料,用 mac 把一個 usb碟做成 FAT32,剛開始還正常,幾次之後發現,中文字都亂碼了。再用個幾次連容量都不對。回到 windows 看裡面有兩個分割區,比較大的那個 windows 就看不見,想用 windows 來清掉分割區又點不到那個指令,卡住了怎辦?這時候 diskpart 是救星。

有很大的資料要交換,例如影片,用網路來傳還是比較慢,家裡的 wifi 就是沒辦法每秒 25 或 50 MB 來傳。所以一個 FAT32 的 usb碟一直是我的方式。但是這次就遇到檔名亂碼的問題,隨後發生分割區看不到的問題。從 windows 這邊來看,用 mac 來割 FAT32 會弄出兩個主分割區,讓 windows 搞不懂。要救回這個 usb碟的方法,應該是用 windows 自己來切分割區了。打開磁碟管理,對著分割區按下右鍵,清除分割區的選項居然是灰色不能選,這下無解了。至少這條路不能走。難道 windows 真的無招了嗎?
還好,這問題不是只有我發生,外國人也發生,解決方法就是 diskpart.exe 這個內建程式。
我只需要做到清除分割,建立分割區還是用原來的圖形格式化工具。
步驟:
(1) 進到命令列
(2) 輸入 diskpart 之後按 <enter>
(3) 輸入 list disk,看清楚第幾個是你的 usb碟,沒看清楚會非常悲劇,一定要算對。然後用 select disk n 選到你的 usb碟,n 是列表上第幾個磁碟。選完之後,再用 detail disk 確認一下。
(4) 接下來輸入 clean 指令,清除整個分割區。
(5) 離開程式,輸入 exit。
接下來的建立分割區,就照一般的格式化做法就行了。

參考:
http://superuser.com/questions/274687/how-do-i-format-a-usb-drive-on-a-pc-that-was-formatted-on-a-mac

[python] try except raise,為什麼會需要 raise ?

在 python 裡,對於有可能錯誤的地方,就是用 try 去包住它,然後針對可能的錯誤去處理它。像是:

try:
    f = open('x','r')
except IOError:
    raise
except Exception:
    print 'other error'
    raise

有天某個人問我,為什麼,既然已經是抓到問題了,為什麼還要 raise,把它丟出去呢?也許有很多理由,但我有兩個簡單的理由:(1)我想直接讓程式被停掉,(2)我想讓執行我寫的程式的程式也知道有問題,讓它自己決定該怎麼辦。這在寫程式給別的程式用的時候很重要。

在 raise exception 的時候,我以前是寫成以下這樣:
try:
    f = open('x','r')
except Exception, ex:
    print 'other error'
    raise ex

這樣在文句上好像很正常,但是就會出現困擾的地方,就是 traceback 所指的 exception 行號是在 raise 的地上,而不是真正發生的地方。

IOError                                   Traceback (most recent call last)
 in ()
      3 except Exception, ex:
      4     print 'other error'
----> 5     raise ex

IOError: [Errno 2] No such file or directory: 'x'
後來才發現,應該直接 raise 後面不加參數,才會如下指到對的行數去。

IOError                                   Traceback (most recent call last)
 in ()
      1 try:
----> 2     f = open('x','r')
      3 except IOError:
      4     raise
      5 except Exception:

IOError: [Errno 2] No such file or directory: 'x'