This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For local Docker vendor dependencies | |
require_relative 'bundle/bundler/setup' | |
require "iron_mq" | |
require "iron_worker" | |
require "yaml" | |
require "json" | |
require "forecast_io" | |
# Set some constants | |
DENSITY = 1.23 # Air density | |
BETZ = 0.59 # Max power output | |
PI = 3.14 # PI | |
DIAMETER = 50 # Windmill blade size | |
# Area is needed to calculate power | |
def calc_swept_area(blade) | |
return PI * ((blade / 2) * (blade / 2)) | |
end | |
# Calculate power generation | |
def calc_power(area, speed) | |
return BETZ * area * DENSITY * (speed * speed * speed) | |
end | |
# Load config and payload | |
cfg = YAML.load_file('windmill.config.yml') | |
lat = IronWorker.payload['lat'] | |
lon = IronWorker.payload['lon'] | |
# Fire up a fake windmill | |
puts "Windmill started at location: #{lat} #{lon}\n" | |
# Hit Forecast API with coordinates | |
ForecastIO.api_key = "#{cfg['forecast']['key']}" | |
forecast = ForecastIO.forecast(lat, lon) | |
# Current weather conditions | |
humidity = forecast.currently.humidity | |
temp = forecast.currently.temperature | |
pressure = forecast.currently.pressure | |
speed = forecast.currently.windSpeed | |
bearing = forecast.currently.windBearing | |
puts "Temperature: #{temp} Fahrenheit\n" | |
puts "Humidity: #{humidity}\n" | |
puts "Pressure: #{pressure} Millibars\n" | |
puts "Wind Speed: #{speed} mph\n" | |
puts "Wind Bearing: #{bearing} degrees\n" | |
# Run energy calculations | |
area = calc_swept_area(DIAMETER) | |
power = calc_power(area, speed) | |
puts "Windmill current power generation: #{power} Watts\n" | |
# Format data and place in queue | |
ironmq = IronMQ::Client.new | |
queue = ironmq.queue("windmill_calc") | |
hash = { | |
:lat => lat, | |
:lon => lon, | |
:humidity => humidity, | |
:temp => temp, | |
:pressure => pressure, | |
:speed => speed, | |
:bearing => bearing, | |
:power => power | |
} | |
queue.post(JSON.generate(hash)) | |
puts "Posted data to queue\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment