著者:すずきひろのぶ
前回は、温湿センサーモジュールAM2320 をRaspberryPi 3 ( 以下 RPi3)に接続しI2C ( アイ・スクエアード・シー)からデータを取得しデータベースに登録していくまでのプログラムを作成しました。今回は、データベースをアクセスし表示するWeb アプリケーションを作成します。
記事本文掲載のシェルスクリプトマガジンvol.51は以下リンク先でご購入できます。
1 2 3 4 5 6 7 8 9 10 |
-- HUTEMP table creation. -- For Shell Script Mag. -- (C) 2017 Hironobu SUZUKI / GPLv3 or later. -- CREATE TABLE HUMTEMP ( ID INTEGER PRIMARY KEY AUTOINCREMENT, DTIME DATETIME, HUM INTEGER, TEMP INTEGER ); |
1 2 3 4 5 6 7 8 9 10 11 12 |
# # Webrick Test # For Shell Script Mag. # (C) 2017 Hironobu SUZUKI / GPLv3 or Later # require 'webrick' servlet = WEBrick::HTTPServer.new({ :DocumentRoot => './', :Port => 8080}) servlet.mount('/hello', WEBrick::HTTPServlet::CGIHandler, 'hello.rb') servlet.mount('/', WEBrick::HTTPServlet::FileHandler, 'index.html') trap("INT"){ servlet.shutdown } servlet.start |
Hello World
1 |
1 2 3 4 5 6 |
#!/usr/bin/ruby require 'cgi' cgi = CGI.new cgi.out(type: 'text/html', charset: 'UTF-8') do " |
hello again
1 2 |
" end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#!/usr/bin/ruby require 'sqlite3' require 'cgi' cgi = CGI.new print cgi.header({"charset" => "UTF-8", "status" => "OK"}) print "\r\n" print "\r\n" maxid=0 numberofentry=10 db = SQLite3::Database.new 'humtemp.db' db.execute('select MAX(ID) from HUMTEMP') do |row| maxid=row[0].to_i end targetid=maxid - numberofentry db.execute('select * from HUMTEMP where id > ' + targetid.to_s) do |row| hum = row[2].to_i temp = row[3].to_i print " |
“,row[1],” “, temp/10.0, ” “,hum/10.0,”
\r\n”
end
db.close
print “\n
1 |
\r\n" |
1 2 3 4 5 6 7 8 9 10 |
# # webrick -- webrick deamon # (C) 2017 Hironobu SUZUKI / GPLv3 or Later. # require 'webrick' server = WEBrick::HTTPServer.new({ DocumentRoot: './', Port: 8080, }) server.mount('/', WEBrick::HTTPServlet::CGIHandler, './temphum.rb') trap("INT"){server.shutdown} server.start |
以下はvol.50掲載コードのおさらいです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# # am2320.rb -- read data from AM2320 and save information into sqlite3 db. # (C) 2017 Hironobu SUZUKI / GPLv3 or Later. # require 'rubygems' require 'i2c' require 'date' require 'digest/crc16_modbus' require 'sqlite3' device = I2C.create("/dev/i2c-1") address = 0x5c loop do sleep(0.05) s=nil begin s = device.read(address, 8, "\x03\x00\x04") rescue next end func_code, ret_len, hum_h, hum_l, temp_h, temp_l, crc_l, crc_h = s.bytes.to_a crc16a=Digest::CRC16Modbus.checksum(s[0,6]) crc16b=(crc_h * 256) + crc_l if crc16a != crc16b puts "CRC error" next end db = SQLite3::Database.new("humtemp.db") hum_orig = (hum_h *256 + hum_l) temp_orig = (temp_h *256 + temp_l) puts Time.now puts hum_orig/10.0 puts temp_orig/10.0 db.transaction do sql = "INSERT INTO humtemp(DTIME,HUM,TEMP) VALUES(datetime('now'), ?, ?)" db.execute(sql, hum_orig, temp_orig) end db.close end |
以下は本誌に公開されていないお楽しみコードです。どうぞご利用ください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script><script type="text/javascript"> google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ['Date', 'Temp', 'Hum'], ['2004', 1000, 400], ['2005', 1170, 460], ['2006', 660, 1120], ['2007', 1030, 540] ]); var options = { title: 'Company Performance', curveType: 'function', legend: { position: 'bottom' } }; var chart = new google.visualization.LineChart(document.getElementById('curve_chart')); chart.draw(data, options); } </script> |
1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#!/usr/bin/ruby # # temphum-googlechart : temphum with Google Chart # For Shell Script Mag. # (C) 2017 Hironobu SUZUKI / GPLv3 or Later. # require 'sqlite3' require 'cgi' cgi = CGI.new print cgi.header({"charset" => "UTF-8", "status" => "OK"}) str=<<"EOT" <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script><script type="text/javascript"> google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ['Samples', 'Temperature', 'Humidity', 'Discomfort'], EOT print str ###### maxid=0 db = SQLite3::Database.new 'humtemp.db' db.execute('select MAX(ID) from HUMTEMP') do |row| maxid=row[0].to_i end targetid=maxid-30 indexnum=1 db.execute('select * from HUMTEMP where id > ' + targetid.to_s) do |row| if indexnum != 1 print ",\n" end hum = (row[2].to_i)/10.0 temp = (row[3].to_i)/10.0 disco = 0.81*temp + 0.01*hum*(0.99*temp -14.3) + 46.3 # print "</p> <p> </p> <P>",row[1]," ", temp/10.0, " ",hum/10.0,"</P> <p> </p> <p>\r\n" # ['2004', 1000, 400], printf "[%d,%f,%f,%f]", indexnum, temp, hum, disco indexnum +=1 end db.close ###### str=<<"EOF" ]); var options = { title: 'Temperature, Humidity, and Discomfort Index', curveType: 'function', legend: { position: 'bottom' } }; var chart = new google.visualization.LineChart(document.getElementById('curve_chart')); chart.draw(data, options); } </script> EOF print str ###### str=<<'EOT' |
1 2 |
EOT print str |