Last active
June 6, 2017 02:02
-
-
Save pxsta/5422dc0e1341f6f85456 to your computer and use it in GitHub Desktop.
Arduinoで扉の開閉監視する
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
void setup() { | |
Serial.begin(9600) ; | |
pinMode(13, OUTPUT); | |
pinMode(2, INPUT_PULLUP); | |
} | |
unsigned long prev=0; | |
void loop() { | |
int x; | |
unsigned long current = millis(); | |
if(abs(current-prev)>1000){ | |
prev=current; | |
x = digitalRead(2); | |
if (x == 0){ | |
digitalWrite(13, 1); | |
} | |
else{ | |
digitalWrite(13, 0); | |
} | |
Serial.println(x); | |
Serial.flush(); | |
} | |
} |
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
source "https://rubygems.org" | |
gem 'washbullet' | |
gem 'serialport' |
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
require 'bundler' | |
Bundler.require | |
serial_port = '/dev/ttyACM1' | |
serial_bps = 9600 | |
prev_value = 0 | |
client = Washbullet::Client.new(ENV['PUSHBULLET_API_KEY']) | |
sp = SerialPort.new(serial_port,serial_bps) | |
sp.read_timeout = 0 | |
while line = sp.readline | |
line = line.strip | |
t = Time.now | |
if line.to_i == 1 and line.to_i != prev_value then | |
t = Time.now | |
#平日の10-19時ならPushbulletで通知する | |
if t.wday.between?(1,5) and t.hour.between?(10,19) then | |
client.push_note(nil,'Room door sensor','open') | |
end | |
end | |
prev_value = line.to_i | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment