Skip to content

Instantly share code, notes, and snippets.

@mimosz
Created March 13, 2013 09:18
Show Gist options
  • Save mimosz/5150484 to your computer and use it in GitHub Desktop.
Save mimosz/5150484 to your computer and use it in GitHub Desktop.
raspberry pi
# -*- encoding: utf-8 -*-
require 'wiringpi'
class Pi
def initialize(pins)
@pins = pins
@io = WiringPi::GPIO.new
@io.wiringPiMode(WPI_MODE_GPIO)
# 初始化,状态
@pins.each do |pin|
@io.mode(pin, OUTPUT)
end
end
# 全开
def all_on
@pins.each do |pin|
on_by_pin(pin)
end
end
# 全关
def all_off
@pins.each do |pin|
off_by_pin(pin)
end
end
# 重启
def reset_by_num(num, time=5)
pin = get_pin(num)
if pin
reset_by_pin(pin, time)
else
nil
end
end
# 重启
def reset_by_pin(pin, time=5)
stat = stat_by_pin(pin)
if stat == 1
stat = off_by_pin(pin)
sleep(time)
end
if stat == 0
stat = on_by_pin(pin)
end
return stat
end
# 状态
def stat_by_num(num)
pin = get_pin(num)
if pin
stat_by_pin(pin)
else
nil
end
end
# 关
def off_by_num(num)
pin = get_pin(num)
if pin
off_by_pin(pin)
else
nil
end
end
# 开
def on_by_num(num)
pin = get_pin(num)
if pin
on_by_pin(pin)
else
nil
end
end
# 状态
def stat_by_pin(pin)
@io.read(pin)
end
# 关
def off_by_pin(pin, stat=nil)
stat ||= stat_by_pin(pin)
@io.write(pin, LOW) if stat == 1
return 0
end
# 开
def on_by_pin(pin, stat=nil)
stat ||= stat_by_pin(pin)
@io.write(pin, HIGH) if stat == 0
return 1
end
private
def get_pin(num)
@pins[num]
end
end
gpio = Pi.new([ 23, 24, 25, 22 ])
gpio.all_on
gpio.all_off
gpio.on_by_num(0)
gpio.off_by_pin(23)
gpio.on_by_pin(22)
gpio.off_by_num(3)
gpio.on_by_num(1)
gpio.reset_by_num(1)
gpio.reset_by_pin(25)
gpio.all_off
# -*- encoding: utf-8 -*-
require 'nestful'
class Weather
def initialize(ip=nil)
@ip = ip
@geo = nil
@weather = nil
set_ip if ip.nil?
set_geo
set_weather
end
def ip
@ip
end
def geo
@geo
end
def weather
@weather
end
private
def set_ip
ip = get_ip
@ip = ip if ip.index('.') == 3
end
def set_geo
location = geo_by_ip(@ip)
@geo = { ip: @ip, region: location['state'], city: location['city'], lat: location['latitude'], long: location['longitude'], woeid: location['woeid'] } if location
end
def set_weather
if @geo
forecast = weather_by_woeid(@geo[:woeid])
if forecast
@weather = {}
# 大气指数
atmosphere = forecast['atmosphere']
@weather[:humidity] = atmosphere['humidity'] # 湿度
@weather[:visibility] = atmosphere['visibility'] # 能见度
@weather[:pressure] = atmosphere['pressure'] # 大气压
# 风
wind = forecast['wind']
@weather[:direction] = wind['direction'] # 风向
@weather[:speed] = wind['speed'] # 风速
# 切换至当前环境
forecast = forecast['item']
# 当前气温
current = forecast['condition']
@weather[:current] = current['text']
@weather[:temp] = current['temp']
# 全天气温
full_day = forecast['forecast'].first
@weather[:day] = full_day['text']
@weather[:high] = full_day['high']
@weather[:low] = full_day['low']
end
end
end
def get_ip
ip = Nestful.get 'icanhazip.com'
ip.strip if ip
end
def geo_by_ip(ip)
url = 'http://where.yahooapis.com/geocode'
json = Nestful.get(url, format: :json, params: { location: ip, flags: 'J', gflags: 'R' })
json['ResultSet']['Results'].first if json
end
def weather_by_woeid(woeid)
url = 'http://weather.yahooapis.com/forecastrss'
xml = Nestful.get(url, format: :xml, params: { w: woeid, u: 'c' })
xml['channel'] if xml
end
end
wf = Weather.new
puts wf.geo
puts wf.weather
wf = Weather.new('219.145.61.94')
puts wf.geo
puts wf.weather
# -*- encoding: utf-8 -*-
class Wifi
def initialize(wlan)
@wlan = wlan
@root_path = '/home/pi/'
@list_file = @root_path + 'wifi.list'
@stat_file = @root_path + 'wifi.status'
@bak_file = @root_path + 'wpa_supplicant.conf'
@conf_file = '/etc/wpa_supplicant/wpa_supplicant.conf'
end
# 扫描,wifi
def wifi_scan
wifi_list = {}
if scan_cmd
is_auth = false
File.readlines(@list_file).each do |line|
arr = line.strip.split(':')
case arr[0]
when 'Encryption key'
is_auth = arr[1] == 'on'
when 'ESSID'
key = arr[1].gsub("\"",'')
wifi_list[key] = is_auth
end
end
end
return wifi_list
end
# wifi状态
def wifi_stat
if stat_cmd
stat = File.readlines(@stat_file).first.strip.split('=')[1]
case stat
when 'SCANNING'
return false
else
return true
end
end
end
# 连接新AP
def wifi_conn(ssid, passwd)
puts "备份,成功" if back_cmd # 备份
file = File.open(@conf_file, 'w')
file.puts <<-EOF
ctrl_interface=/var/run/wpa_supplicant
network={
ssid=\"#{ssid}\"
psk=\"#{passwd}\"
}
EOF
file.close
if wifi_try
puts "新配置,生效"
puts "备份删除,成功" if remove_cmd # 删除备份
return true
else
puts "新配置,失败"
if restore_cmd # 恢复
puts "恢复备份,成功"
end
return wifi_try
end
end
private
# 尝试连接
def wifi_try
down_cmd
conn_cmd
return wifi_stat
end
def restore_cmd
system("mv -f #{@bak_file} #{@conf_file}")
end
def remove_cmd
system("rm -rf #{@bak_file}")
end
def back_cmd
system("cp -f #{@conf_file} #{@bak_file}")
end
def conn_cmd
system("wpa_supplicant -B -Dwext -i #{@wlan} -c " + @conf_file)
end
def down_cmd
system("wpa_cli -i #{@wlan} terminate")
end
def scan_cmd
system("iwlist #{@wlan} scanning | egrep 'Encryption|ESSID'>" + @list_file)
end
def stat_cmd
system("wpa_cli -i #{@wlan} status | egrep 'wpa_state'>#{@stat_file}")
end
end
wlan0 = Wifi.new('wlan0')
puts wlan0.wifi_scan
puts wlan0.wifi_stat
# puts wlan0.wifi_conn('Howl', '11111111')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment