diff options
author | Joe Robinson <joe@lc8n.com> | 2019-02-25 20:07:42 +0000 |
---|---|---|
committer | Joe Robinson <joe@lc8n.com> | 2019-02-25 20:07:42 +0000 |
commit | 943f992dc370237509f0b73dd9ccc99b2fecdf2f (patch) | |
tree | d719f24e55e865907b8fc0f409f9ddcb64f067d6 | |
parent | 6fc78ed9cc97510782a916c02e8810e2754569a7 (diff) | |
parent | c1eafa32f4d832b270d56c0f9c29c59caec99aea (diff) |
Merge branch 'kitchen_sink' into 'master'
Kitchen sink
See merge request wjoe/scarecrow!1
-rw-r--r-- | scarecrow.rb | 98 | ||||
-rw-r--r-- | views/index.erb | 7 |
2 files changed, 91 insertions, 14 deletions
diff --git a/scarecrow.rb b/scarecrow.rb index 2fbc2ab..bd5509f 100644 --- a/scarecrow.rb +++ b/scarecrow.rb @@ -30,13 +30,21 @@ class Humidity property :value, Float end +class Moisture + include DataMapper::Resource + property :id, Serial + property :device, String + property :timestamp, DateTime + property :value, Float +end + def init DataMapper.auto_migrate! DataMapper.auto_upgrade! # sleep 10 Thread.new do MQTT::Client.connect('192.168.0.59') do |c| - c.subscribe('+/temperature', '+/humidity') + c.subscribe('+/temperature', '+/humidity', '+/moisture') loop do while !c.queue_empty? do topic, message = c.get @@ -57,6 +65,8 @@ def handle_msg(topic, msg) handle_humidity_msg(device_id, msg) elsif type == "temperature" handle_temp_message(device_id, msg) + elsif type == "moisture" + handle_moisture_message(device_id, msg) end end @@ -74,16 +84,64 @@ def handle_temp_message(device_id, msg) end end +def handle_moisture_message(device_id, msg) + moisture = Moisture.create(timestamp: DateTime.now, device: device_id, value: msg) + moisture.errors.each do |error| + puts error + end +end + +def get_value(type, device_id) + entry = nil + value = 0.0 + if type == :temperature + entry = Temperature.last(device: device_id) + elsif type == :humidity + entry = Humidity.last(device: device_id) + elsif type == :moisture + entry = Moisture.last(device: device_id) + end + + if entry != nil + value = entry[:value] + end + + return value +end + +def get_time(type, device_id) + entry = nil + value = Time.at(0) + if type == :temperature + entry = Temperature.last(device: device_id) + elsif type == :humidity + entry = Humidity.last(device: device_id) + elsif type == :moisture + entry = Moisture.last(device: device_id) + end + + if entry != nil + value = entry[:timestamp] + end + + return value +end + get '/' do # send_file File.join(settings.public_folder, 'index.html') - @temp_prop = Temperature.last(device: "cricket0")[:value] - @humidity_prop = Humidity.last(device: "cricket0")[:value] - @temp_air = Temperature.last(device: "mercury")[:value] - @humidity_air = Humidity.last(device: "mercury")[:value] - time_prop = Temperature.last(device: "mercury")[:timestamp] - time_air = Temperature.last(device: "cricket0")[:timestamp] + @temp_prop = get_value(:temperature, "cricket0") + @humidity_prop = get_value(:humidity, "cricket0") + @temp_air = get_value(:temperature, "mercury") + @humidity_air = get_value(:humidity, "mercury") + @temp_kitchen = get_value(:temperature, "cricket1") + @humidity_kitchen = get_value(:humidity, "cricket1") + @moisture_kitchen = (1000-(get_value(:moisture, "cricket1")))/10 + time_prop = get_time(:temperature, "cricket0") + time_air = get_time(:temperature, "mercury") + time_kitchen = get_time(:temperature, "cricket1") @time_formatted_prop = time_prop.strftime("%A, %d %b %Y %l:%M %p") @time_formatted_air = time_air.strftime("%A, %d %b %Y %l:%M %p") + @time_formatted_kitchen = time_kitchen.strftime("%A, %d %b %Y %l:%M %p") erb :index # return "#{temp_val}°C and #{humidity_val}% humidity at #{time_formatted}" end @@ -91,6 +149,7 @@ end get '/temperature/:device_id' do device_id = "mercury" if (params[:device_id] == "air" || params[:device_id] == "kitchen") device_id = "cricket0" if (params[:device_id] == "propagator") + device_id = "cricket1" if (params[:device_id] == "kitchen") device_id ||= params[:device_id] temp = Temperature.last(device: device_id)[:value] return "#{temp}" @@ -99,20 +158,35 @@ end get '/humidity/:device_id' do device_id = "mercury" if (device_id == "air" || device_id == "basement") device_id = "cricket0" if (device_id == "propagator") + device_id = "cricket1" if (device_id == "kitchen") humidity = Humidity.last(device: device_id)[:value] "#{humidity}" end +get '/moisture/:device_id' do + device_id = "mercury" if (device_id == "air" || device_id == "basement") + device_id = "cricket0" if (device_id == "propagator") + device_id = "cricket1" if (device_id == "kitchen") + moisture = Moisture.last(device: device_id)[:value] + "#{moisture}" +end + get '/metrics' do content_type :text - temp_prop = Temperature.last(device: "cricket0")[:value] - humidity_prop = Humidity.last(device: "cricket0")[:value] - temp_air = Temperature.last(device: "mercury")[:value] - humidity_air = Humidity.last(device: "mercury")[:value] + temp_prop = get_value(:temperature, "cricket0") + humidity_prop = get_value(:humidity, "cricket0") + temp_air = get_value(:temperature, "mercury") + humidity_air = get_value(:humidity, "mercury") + temp_kitchen = get_value(:temperature, "cricket1") + humidity_kitchen = get_value(:humidity, "cricket1") + moisture_kitchen = (1000-(get_value(:moisture, "cricket1")))/10 "humidity_basement #{humidity_air} humidity_propagator #{humidity_prop} +humidity_kitchen #{humidity_kitchen} temperature_basement #{temp_air} -temperature_propagator #{temp_prop}" +temperature_propagator #{temp_prop} +temperature_kitchen #{temp_kitchen} +moisture_kitchen #{moisture_kitchen}" end init diff --git a/views/index.erb b/views/index.erb index f22328b..c366141 100644 --- a/views/index.erb +++ b/views/index.erb @@ -8,12 +8,15 @@ <h1>thebluegarden</h1> <p>Basement: <%= @temp_air %>°C and <%= @humidity_air %>% humidity at <%= @time_formatted_air %></p> - <p>Propagator: <%= @temp_prop %>°C and <%= @humidity_prop %>% humidity at <%= @time_formatted_prop %></p> + <p>Propagator (Chillis): <%= @temp_prop %>°C and <%= @humidity_prop %>% humidity at <%= @time_formatted_prop %></p> + <p>Kitchen (Peas & Spinach): <%= @temp_kitchen %>°C, <%= @humidity_kitchen %>% humidity and <%= @moisture_kitchen %>% moisture at <%= @time_formatted_kitchen %></p> <p> <img src="photos/current.png"> </p> <p> - <img src="photos/previous.png"> + <video width="1280" height="720" controls> + <source src="videos/timelapse.mp4" type="video/mp4"> + </video> </p> </body> </html> |