Skip to content

Instantly share code, notes, and snippets.

@pxsta
Last active June 6, 2017 02:02
Show Gist options
  • Save pxsta/5422dc0e1341f6f85456 to your computer and use it in GitHub Desktop.
Save pxsta/5422dc0e1341f6f85456 to your computer and use it in GitHub Desktop.
Arduinoで扉の開閉監視する
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();
}
}
source "https://rubygems.org"
gem 'washbullet'
gem 'serialport'
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