#!/usr/bin/ruby
# -*- coding: utf-8 -*-
# お天気時計 for Rasberry PI 2014/08/23 Ushio KOHAGI
# - tenki.jpの天気時計ブログパーツからxmlデータを取得
# - 現在時刻から1~12時間後までで雨の予報の時刻のLEDを点灯する
# - LEDはシリアルパラレル変換で制御(下記ページを参考)
# http://qiita.com/kasei-san/items/4c51ec6bdfac1c04bf56
# - xmlデータ抜粋
# 1晴れ
# 2曇り
# 3強雨
# 4弱雨
require 'rexml/document'
require 'open-uri'
require 'pi_piper'
# 市町村コードで天気予報データを取得(21202:大垣市)
url = "http://api.tenki.jp/api/blog/parts/point_clock/forecast_map_point_21202.xml"
doc = REXML::Document.new(open(url))
now = Time.now + 9*60*60 # JST(+9h)
# シリアルパラレル変換にGPIO 23,24,25pinを使用
$pins = {
:ser => PiPiper::Pin.new(:pin => 25, :direction => :out), # serial data
:sck => PiPiper::Pin.new(:pin => 24, :direction => :out), # shift register clock
:rck => PiPiper::Pin.new(:pin => 23, :direction => :out) # latch clock
}
def shift(key)
$pins[key.to_sym].on
$pins[key.to_sym].off
end
def reset
$pins.values.each{|pin| pin.off}
end
def send_bits(data)
printf "0x%04x\n",data
0.upto(15) do |byte|
if ((1 << byte) & data) == 0
$pins[:ser].off
else
$pins[:ser].on
end
shift(:sck)
end
shift(:rck)
end
puts now
bits = 0
for i in 1..11 do
t = now + 60*60 * i
str = t.strftime("%Y-%m-%d %H:00:00")
id = doc.elements['//forecast[public_datetime="'+str+'"]/weather/id']
h = t.hour % 12
if id then
wid = id.text.to_i
print h, "\t", wid, "\n"
# 強雨(3)または弱雨(4)ならLED点灯
if (wid == 3 || wid == 4) then
bits |= 1 << (15-h)
end
end
end
puts ""
reset
send_bits(bits)