IGate 除了將無線電世界與互聯網接軌,傳送位置 (location) 及路徑 (route) 資訊外,還可以傳遞偵測數據 (telemetry data) 及天氣等資訊。在外國,「火腿操作員」在戶外架設氣象站監測天氣,並且將數據利用 APRS 透過大氣電波及互聯網發送給其他用家,無論使用業餘無線電器材,或利用手機或電腦瀏覽 aprs.fi 網站,也可以知道最新天氣情況。
【相關文章:當 IGate 戀上 SDR 】
利用 IGate 傳送氣象資訊,首先需要架設一個氣象站 (weather station),用來量度氣溫、相對濕度、氣壓、風向、風速、及雨量記錄等天氣數據。在 Sparkfun 網站一台氣象站(包括風向計、風速計及雨量計)售價只需 80 美元,但需要配合 Arduino 或 Particle Photon 等單晶片微控制器 (microcontroller) 及相關氣象擴充板(包含溫度計、濕度計、及氣壓計在內)一齊使用,才能收集所有氣象數據。同一款氣象站淘寶網上亦有發售,最平 350 人仔就有交易。但尋找地方架設氣象站相當困難。若然沒有地方,亦可選擇使用香港天文台所提供的氣象數據,絕對適合作測試用途。
▲(左)氣象站(右)Sparkfun Photon 氣象擴充板
除了氣象站,亦需要 IGate 配合,將收集得來的氣象數據經 APRS 傳送。雖然現時所選用的 IGate 軟件 Dire Wolf 沒有直接提供相關功能,但其 hackable(容易被破解)特性只需透過編寫程式就能發放天氣及其它不同訊息。最新 1.3 版本在 CBEACON 自定信標設定中設有 infocmd 指令,能定時運行指定程式,並將程式所提供的氣象數據整合到 APRS 數據包 (packet) 內的資訊字段 (information field) 中傳送出去。
CBEACON sendto=IG delay=0:10 every=10:00 infocmd="weather.pl"
至於格式方面,根據 APRS 協議參考書 (protocol reference) ,可以利用以下格式建立無位置資訊氣象報告 (positionless weather report) 將氣象數據整合:
註 :灰色部分屬非必要性資訊,可以無需提供
當中氣象數據部分 (positionless weather data) 的格式如下:
註 :灰色部分屬非必要性資訊,可以無需提供
天氣數據
單位
字節數量
c
風向
度數
4
s
風速
英里每小時
4
g
陣風
英里每小時
4
t
溫度
華氏度
4
r
過去一小時雨量
百分之一英寸
4
p
過去廿四小時雨量
百分之一英寸
4
P
今日雨量
百分之一英寸
4
h
濕度
百分比
3
b
氣壓
百帕斯卡
6
程式需要按照格式要求將數據轉換成所需單位及字節數量,假設現時氣溫攝氏 28 度(華氏 82 度)、相對濕度百分之 60、氣壓 1006.3 百帕斯卡、吹南風(180 度)每小時 8 公里(大約每小時 5 英里)、及陣風每小時 10 公里(大約每小時 6 英里),程式就需要以以下格式顯示數據:
_ 08090820c 180s 005g 006t 082h 60b 10063
要將香港天文台天氣資訊整合到 IGate,需要透過編寫程式從天文台網站下載最新數據。天文台網站提供純文字網頁,方便軟件開發人員 (software developer) 讀取天氣資訊。其中「香港分區天氣」網頁提供各區風向、風速、陣風、溫度、濕度、及氣壓等數據,每十分鐘自動更新。為方便於 LinuxMint 上編寫及運行,及方便將來將整套系統遷移到 Raspberry Pi 上運作,選擇了 Perl 作為程式語言。
開啟任何 text editor 開始編寫程式。首先將日期及時分轉換到所需要的兩位字節格式:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
# Variables for temperature, humidity, wind direction, speed, gust and pressure
my $temperature;
my $humidity;
my $winddirection;
my $windspeed;
my $windgust;
my $pressure;
# RegEx match count
my $count;
# Convert month into 2 digit format
my $month = sprintf("%02d", ((localtime)[4] + 1) % 100);
# Convert day into 2 digit format
my $day = sprintf("%02d", (localtime)[3] % 100);
# Convert hour into 2 digit format
my $hour = sprintf("%02d", (localtime)[2] % 100);
# Convert minute into 2 digit format
my $minute = sprintf("%02d", (localtime)[1] % 100);
View the code on Gist .
然後到天文台網站下載「香港分區天氣」網頁,讀取網頁內相關數據,並同時轉換所需單位及字節格式:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# URL of Regional Weather in Hong Kong (updates every 10 minutes)
my $html = get("http://data.weather.gov.hk/wxinfo/ts/text_readings_e.htm");
# Extract temperature from HTML (3 bytes)
$count = () = $html =~ m{Shau Kei Wan[ ]+([\d\.?]+)};
if ($count > 0) {
$temperature = sprintf("%03d", ($1 * 9/5 + 32) % 1000);
} else {
$temperature = "...";
}
# Extract humidity (2 bytes)
$count = () = $html =~ m{HK Observatory[ ]+[\S]+[ ]+([\d]+)};
if ($count > 0) {
$humidity = sprintf("%02d", $1 % 100);
} else {
$humidity = "..";
}
# Extract wind direction (3 bytes)
my $pattern = "(km/hour)";
my $offset = index($html, $pattern) + length($pattern) + 1;
$html = substr($html, $offset);
$count = () = $html =~ m{Kai Tak[ ]+([^\s]+)};
if ($count > 0) {
if (lc $1 eq "north") { $winddirection = "000"; }
elsif (lc $1 eq "northeast") { $winddirection = "045"; }
elsif (lc $1 eq "east") { $winddirection = "090"; }
elsif (lc $1 eq "southeast") { $winddirection = "135"; }
elsif (lc $1 eq "south") { $winddirection = "180"; }
elsif (lc $1 eq "southwest") { $winddirection = "225"; }
elsif (lc $1 eq "west") { $winddirection = "270"; }
elsif (lc $1 eq "northwest") { $winddirection = "315"; }
elsif (lc $1 eq "variable") { $winddirection = "..."; }
else { # When wind direction is either "Calm" or "N/A"
$winddirection = "...";
$windspeed = "...";
$windgust = "...";
}
} else {
$winddirection = "...";
$windspeed = "...";
$windgust = "...";
}
if ((!defined $windspeed) || (!defined $windgust)){
# Extract wind speed (3 bytes)
$count = () = $html =~ m{Kai Tak[ ]+[\S]+[ ]+([\d]+)};
if ($count > 0) {
$windspeed = sprintf("%03d", $1 * 0.621371192 % 1000);
} else {
$windspeed = "...";
}
# Extract wind gust (3 bytes)
$count = () = $html =~ m{Kai Tak[ ]+[\S]+[ ]+[\S]+[ ]+([\d]+)};
if ($count > 0) {
$windgust = sprintf("%03d", $1 * 0.621371192 % 1000);
} else {
$windgust = "...";
}
}
# Extract pressure (5 bytes)
$pattern = "(hPa)";
$offset = index($html, $pattern) + length($pattern) + 1;
$html = substr($html, $offset);
$count = () = $html =~ m{HK Observatory[ ]+([\d\.?]+)};
if ($count > 0) {
$pressure = sprintf("%05d", $1 * 10 % 100000);
} else {
$pressure = ".....";
}
View the code on Gist .
完成所有單位轉換及將數據格式化後,現在可以按照無位置資訊氣象報告格式將數據排列並且顯示出來:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Output weather in the formation _MMDDHHMMcCCCsSSSgGGGtTTThHHbBBBBB
print "_" . $month . $day . $hour . $minute . "c" . $winddirection . "s" . $windspeed . "g" . $windgust . "t" . $temperature . "h" . $humidity . "b" . $pressure;
View the code on Gist .
編寫程式部分大致完成,先將檔案命名為 wx.pl 並儲存到 /opt/direwolf-1.3/telemetry-toolkit 文件夾內,然後開啟 Terminal 執行以下指令,給予 wx.pl 執行權限:
$ cd /opt/direwolf-1.3/telemetry-toolkit
$ chmod +x wx.pl
現在可以執行 wx.pl 測試程式:
$ ./wx.pl
當接下 Enter 鍵後,程式會於下一行顯示整合後的氣象數據。完成後會立即跳回 shell,所以數據尾部會緊貼 shell prompt,完全正常。
完成測試後,用 text editor 開啟 direwolf.conf 設定檔案並加入以下設定:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CBEACON sendto=IG delay=0:31 every=10:00 infocmd="/opt/direwolf-1.3/telemetry-toolkit/wx.pl"
View the code on Gist .
以上 CBEACON 設定代表 Dire Wolf 啟動 31 秒後自動運行 wx.pl 程式,並將 wx.pl 從天文台下載得來的氣象數據轉送到 APRS-IS 去,每 10 分鐘重複動作一次。當完成更新設定後,所有準備工作已大致完成,現在可以重新啟動 Dire Wolf,令新設定即時生效。
▲ Dire Wolf 成功執行 wx.pl 並將數據傳送到 APRS-IS
▲ 如果傳送成功,aprs.fi 網站會將氣象數據顯示出來
按下 show weather charts ,會顯示詳盡天氣圖表,可選擇顯示過去 24 小時、48 小時、一星期、一個月、或一年天氣情況。
雖然很想架設一個屬於自己的個人氣象站,但尋找地方非常困難,暫時只好將 IGate 連接天文台網站讀取氣象數據。使用天文台網站提供的氣象數據需要到網上辦理申請手續 才可以複製、或再發放香港天文台網站所載資料。除此之外,現時 IGate 所使用的 RX-only SDR 方案只可接收無線電訊號,不能發射,氣象數據只能透過 IGate 經互聯網傳送到 APRS-IS 伺服器,不能以大氣電波傳送給業餘無線電愛好者。如果附近 IGate 設有 digipeater 功能,或許有機會將從 APRS-IS 接收到的氣象數據經無線電再轉發,但此功能需要特別設定才能運作,而且其它 IGate 無責任及必要提供此服務。世事無捷徑,所以都是靠自己,一步一步研究及改善無線電器材及技術吧。
73 de VR2ZDX
參考:
發表意見