Created
February 10, 2013 21:33
-
-
Save diegocouto/4751136 to your computer and use it in GitHub Desktop.
An experiment using Ruby and an Arduino to notify the user about unseen email messages.
This file contains hidden or 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
const int led = 3; | |
boolean active = false; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(led, OUTPUT); | |
} | |
void loop() { | |
if (Serial.available() > 0) { | |
if( Serial.read() == '1' && !active ) { | |
fade(led, 0); | |
active = true; | |
} | |
else if( active ) { | |
fade(led, 255); | |
active = false; | |
} | |
} | |
} | |
// If brightness is more than 'half' of 255, is fadeOut. | |
void fade(int pin, int startBrightness) { | |
int fadeAmount = 5; | |
int brightness = startBrightness; | |
if ( brightness > 125 ) | |
fadeAmount = -5; | |
while( brightness >= 0 && brightness <=255 ) { | |
analogWrite(pin, brightness); | |
brightness = brightness + fadeAmount; | |
delay(25); | |
} | |
} |
This file contains hidden or 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
class EmailAccount | |
require 'net/imap' | |
def initialize(user, password) | |
@imap = Net::IMAP.new('imap.gmail.com', 993, true) | |
@imap.login(user, password) | |
end | |
def newMail? | |
new = 0 | |
new = 1 if @imap.status("inbox", ['UNSEEN'])['UNSEEN'] > 0 | |
return new | |
end | |
end | |
class SerialWriter | |
require 'serialport' | |
def initialize | |
portStr = "/dev/ttyACM0" # Probably you'll have to change this. :/ | |
baudRate = 9600 | |
dataBits = 8 | |
stopBits = 1 | |
parity = SerialPort::NONE | |
@port = SerialPort.new(portStr, baudRate, dataBits, stopBits, parity) | |
end | |
def write(content) | |
@port.write(content) | |
end | |
end | |
class Notifier | |
def initialize | |
@writer = SerialWriter.new() | |
end | |
def setMail(user, password) | |
@email = EmailAccount.new(user, password) | |
end | |
def run | |
while true do | |
@writer.write(@email.newMail?) | |
sleep(600) | |
end | |
end | |
end | |
# Usage ------------------------------------------------------- | |
notifier = Notifier.new() | |
notifier.setMail('[email protected]', 'yourBeautifulAndSecretPassword') | |
notifier.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment