顯示具有 mosquitto 標籤的文章。 顯示所有文章
顯示具有 mosquitto 標籤的文章。 顯示所有文章

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 正常。私用應該沒問題。

2013年12月8日 星期日

[mosquitto]在 windows command 下,要傳送雙引號。

這應該算是 windows command prompt 的小技巧,只是我到現在要用 mosquitto 的 mosquitto_pub.exe 才遇到。要把雙引號送進批次檔或是執行檔送出雙引號,其跳脫符號就是雙引號。

因為每次執行 mosquitto_pub.exe 都要到安裝的目錄下,而且,topic 跟 host 每次都要手打,很想偷懶。

於是在順手的地方,例如 C:\User\user,放了一個叫做 send.bat 的批次檔。內容如下:

"C:\Program Files (x86)\mosquitto\mosquitto_pub.exe" -t test -h 192.168.1.111 -m %1

然後,只要我在 C:\User\user 底下執行

send test

就會得到

test

若是執行

send test test

卻只會得到

send test

若要得到 test test,就要執行

send "test test"

接下來,我想要送一個 json 物件,{"id":"ricky","msg":"hello world!"},若直接執行

send {"id":"ricky","msg":"hello world!"}

,會變成

{id:ricky

我們看一下實際執行的指令是:

C:\User\user>"C:\Program Files (x86)\mosquitto\mosquitto_pub.exe" -t test -h 192.168.1.111 -m {"id":"ricky"

原因是,參數有先傳進批次檔,雙引號算是控制符號之一,真的送出去的字串裡,雙引號都被拿掉了。

所以不論是執行檔或是批次檔,要傳送 json 這種東西,雙引號的部份要用雙引號跳脫,所以就長成這樣:

send.bat "{""id"":""ricky"",""msg"":""this is test""}"

跟 Visual Basic (VB) 一樣,是用雙引號來跳脫。我還試了很久的反斜線…,兩邊沒有要統一一下嗎?