Skip to content

Instantly share code, notes, and snippets.

@fortyfivan
Last active September 13, 2015 20:11
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 fortyfivan/af0638684aa723d7ec5e to your computer and use it in GitHub Desktop.
Save fortyfivan/af0638684aa723d7ec5e to your computer and use it in GitHub Desktop.
# 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