Skip to content

Instantly share code, notes, and snippets.

@clicube
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clicube/fdbf6171e93d8a1eb5d0 to your computer and use it in GitHub Desktop.
Save clicube/fdbf6171e93d8a1eb5d0 to your computer and use it in GitHub Desktop.
#! /usr/bin/ruby
# coding: utf-8
IP_ADDRESS = "192.168.111.102"
LASTSEEN_FILE = "/tmp/lastseen.txt"
OUTPUT_FILE = "/tmp/house.html"
require 'serialport'
OUTPUT_TEMP = <<EOS
<html lang="ja">
<head>
<meta charset="utf-8">
<title>house.cubik.jp</title>
</head>
<body>
$$OUTPUT$$
</body>
</html>
EOS
if File.exist? LASTSEEN_FILE
lastseen = Time.at(File.open(LASTSEEN_FILE){|f| f.read.to_i })
else
lastseen = Time.at(0)
end
result = system "/bin/ping #{IP_ADDRESS} -c 1 -w 1"
if result
File.open(LASTSEEN_FILE,"w"){|f| f.puts Time.now.to_i }
else
if Time.now - lastseen < 60 * 30
result = true
end
end
result_str = ""
#result_str += "<p>"
#result_str += result ? "いる" : "いない"
#result_str += "</p>"
# ここから温度取得
sp = SerialPort.new('/dev/ttyAMA0', 9600, 8, 1, 0)
sp.read_timeout = 5000
sp.puts
sp.puts "temp_read"
puts sp.gets
puts sp.gets
temp = sp.gets
puts temp
hum = sp.gets
puts hum
sp.close
temp = (temp.slice(5,3).to_i.to_f)/10
hum = (hum.slice(5,3).to_i.to_f)/10
result_str += "<p>室温: #{temp}</p>"
result_str += "<p>湿度: #{hum}</p>"
output = OUTPUT_TEMP.gsub("$$OUTPUT$$",result_str)
File.open(OUTPUT_FILE,"w"){|f| f.puts output }
system "scp #{OUTPUT_FILE} cubik:html/house/index.html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment