Skip to content

Commit

Permalink
Merge branch 'co2_widget' (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
YtvwlD committed Dec 15, 2023
2 parents 4758049 + a7b0530 commit 43ff34b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/dashboards/chaosdorf.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
.row
%div{"data-id" => "downlink-traffic", "data-max" => "1000000000", "data-min" => "0", "data-title" => "Downlink Traffic", "data-view" => "Meter", "data-suffix" => "bit/s"}
%div{"data-id" => "uplink-traffic", "data-max" => "50000000", "data-min" => "0", "data-title" => "Uplink Traffic", "data-view" => "Meter", "data-suffix" => "bit/s"}
%div{"data-id" => "power-total", "data-title" => "Power", "data-view" => "Graph", "data-suffix" => "W" }
%div{"data-id" => "lounge-co2", "data-title" => "CO2", "data-view" => "Graph", "data-suffix" => "ppm" }
%div{"data-id" => "music", "data-header" => "now playing", "data-view" => "Music"}
.row
%div{"data-id" => "efa", "data-title" => "EFA: Kruppstr.", "data-view" => "Efa"}
Expand Down
26 changes: 15 additions & 11 deletions src/jobs/mqtt.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'mqtt'
require 'json'

power_series = Array.new(20).fill(0)
co2_series = Array.new(180).fill(0)
music_status = {}
last_music_update = Time.now
last_dorfstatus = "unknown"
Expand All @@ -16,30 +18,32 @@
client.connect()
puts "mqtt: connected to #{client.host} as #{client.client_id}."

client.subscribe('sensors/flukso/power/sum/30s_average') # power
client.subscribe('sensor/esp8266_64760E/data') # hackcenter co2 sensor
client.subscribe('space/dorfstatus')
client.subscribe('space/bell') # door bell
client.subscribe('music/#') # music

client.get do |topic,message|
message = message.force_encoding('utf-8')
if topic == 'sensors/flukso/power/sum/30s_average'
if topic == 'sensor/esp8266_64760E/data'
##################################
# P O W E R #
# C O 2 #
##################################
value = Float(message).round
power_series[0] = value
power_series.rotate!
# Hackcenter CO2
data = JSON.parse(message)
value = Float(data["co2_ppm"])
co2_series[0] = value
co2_series.rotate!
case value
when 0..1500
when 0...1000
status = "normal"
when 1500..3000
when 1000...2000
status = "danger"
else
status = "warning"
end
data = power_series.map.with_index{ |n,i| {"x" => -i, "y" => n} }
send_event('power-total', { points: data, status: status, moreinfo: "total power consumption" })
data = co2_series.map.with_index { |n,i| {"x" => -i, "y" => n} }
send_event('lounge-co2', {points: data, status: status})
elsif topic == 'space/dorfstatus'
##################################
# D O R F #
Expand All @@ -64,7 +68,7 @@
SCHEDULER.in '1s' do
send_event('music', music_status.clone)
end
end
end
end
end
client.disconnect() # TODO: does this make sense?
Expand Down

0 comments on commit 43ff34b

Please sign in to comment.